fireproofsocks

fireproofsocks

How to detect if a given character (grapheme) is whitespace?

While working on an Elixir parser (of Handlebars syntax), I came across the need to determine whether or not a given character (a grapheme). I know that there are the various String functions (like String.trim_leading), but I’m hoping to be able to write a guard clause on a function along the lines of this:

defp custom_trim(<<h :: binary - size(1)>> <> tail) when h not in [" ", "\t", "\n"], do: # ...

But I don’t know how to specify all the other whitespace characters. Is there a function that I could leverage to tell me whether a given character represents whitespace? If I use a regular expression pattern like ~r/\s/ would that match all whitespace? If it did I could do something like this:

String.split(my_str, ~r/\s/, parts: 2)

Thanks!

Most Liked

NobbZ

NobbZ

As you are splitting of a single byte only, you already covered most available whitespace. In that range there is only \v and \r missing AFAIR…

A better approach that you can extend to work with all whitespace is this one:

def f(<<h :: utf8, tail::binary>> when h not in ~c[ \t\n\r\v…], do…

You can find a list of all characters that are marked as whitespace by the unicode standard in wikipedia:

kip

kip

ex_cldr Core Team

As of Unicode 12.1 the following are categorised as whitespace (a list of code point ranges in this representation):

iex> Cldr.Unicode.Category.categories[:Zs] 
[
  {32, 32},
  {160, 160},
  {5760, 5760},
  {8192, 8202},
  {8239, 8239},
  {8287, 8287},
  {12288, 12288}
]

I have a lib that defines some guards to help with this sort of thing but specifically for whitespace you can:

when codepoint == 32 or codepoint == 160 or codepoint == 5760 or codepoint in 8192..8202 or codepoint == 8239 or codepoint == 8287 or codepoint == 12288

Or in a regex you can match on Unicode character categories:

iex> Regex.match? ~r/\p{Zs}/u, "   "
true
NobbZ

NobbZ

"\u00a0" as a string, or ~c[\u00a0] as charlist (with singlequote syntax as well).

fireproofsocks

fireproofsocks

Thanks! What’s your lib? It sounds relevant.

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

We're in Beta

About us Mission Statement