pm100

pm100

\r\n as a grapheme is it 1 or 2?

I am assuming this is a Windows thing. Regex seems to treat \r\n as 2 characters but String treats them as one

trying to strip whitespace off the front of a string

iex(17)> regex = ~r/^\s+/
~r/^\s+/
iex(18)> junk = "\r\n\r\nabc"
"\r\n\r\nabc"
iex(19)> Regex.run(regex, junk, return: :index)
[{0, 4}]
iex(20)> String.slice(junk,  4..-1//1)
"c"
iex(21)> String.graphemes(juk)
["\r\n", "\r\n", "a", "b", "c"]
iex(22)>

Am I reading this correctly (absolute elixir noob). What the correct way to fix it?

Marked As Solved

sabiwara

sabiwara

Elixir Core Team

As the Regex docs are mentioning, :index “returns byte index and match length”, not the grapheme index.

This works:

iex> binary_slice("\r\n\r\nabc", 4..-1//1)
"abc"

Bytes != codepoints != graphemes.

iex> junk = "\r\n\r\nabc é"
"\r\n\r\nabc é"
iex> for <<byte <- junk>>, do: <<byte>>
["\r", "\n", "\r", "\n", "a", "b", "c", " ", <<195>>, <<169>>]
iex> String.codepoints(junk)
["\r", "\n", "\r", "\n", "a", "b", "c", " ", "é"]
iex> String.graphemes(junk)
["\r\n", "\r\n", "a", "b", "c", " ", "é"]

The String documentation explains this topic well.

Also Liked

sabiwara

sabiwara

Elixir Core Team

String is basically a set of helper functions for UTF8 binaries

Exactly, it is the same underlying structure (binaries containing valid UTF8) but what changes is if you chose to process it as bytes, codepoints (UTF-8 characters) or graphemes (groups of codepoints which will be displayed as a single character to the human reader).

However this does not change a thing that only String sees \r\n as a single character.

This depends of the function. Many functions of the String module are explicitly working with graphemes, such as String.length, String.at, String.first
String.codepoints, String.next_codepoints, String.to_charlist, ::utf8 in bitstring patterns all work with codepoints.

It’s a serious gotcha that 2 UTF8 characters are seen as 1

Graphemes and codepoints are arguably a gotcha in any language, so I’m not disagreeing, but I wouldn’t pin it on the String library :sweat_smile:

LostKobrakai

LostKobrakai

No. Graphemes are a group construct. Sometimes a grapheme is just a single codepoint. Sometimes a grapheme consists of multiple codepoints – depending on how those codepoints follow up on each other.

iex(1)> str = "🏴‍☠️"
"🏴\u200D☠️"
iex(2)> String.graphemes(str)
["🏴\u200D☠️"]
iex(3)> String.codepoints(str)
["🏴", "\u200D", "☠", "️"]
iex(4)> str = "🏴"
"🏴"
iex(5)> String.graphemes(str)
["🏴"]
iex(6)> String.codepoints(str)
["🏴"]

And if you forget about “the computer in the back” \r\n and \n look the exact same to someone in notepad. It’s just us programmers still dealing with the fact that all this came from a typewrighter once. To everyone else it’s a newline no matter what.

adamu

adamu

FYI flags are multiple codepoints too.

They are the country code offset by 127397.

iex> for <<cp::utf8 <- "UN">>, do: <<(cp +  127397)::utf8>>, into: ""
"🇺🇳"

Where Next?

Popular in Questions Top

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement