mruoss

mruoss

Is there a way to escape escape characters i.e. turn `"\u2003"` => `"\\u2003"`?

Hey, does anybody know if it is possible to escape escape characters like \r or \u2003? Given a string containing such characters, I’d like to turn them into an escaped form as follows:

iex> Magic.escape("\r\u2003")
"\\r\\u2003"

First Post!

dimitarvp

dimitarvp

The only thing that I am aware of after 5 minutes of searching is Regex.escape/1.

And it only covers part of these: Escape characters.

Most Liked

Eiji

Eiji

All you need to do is to define a list of characters you want to escape and the rest is a simple reduce:

defmodule Example do
  @characters_to_escape [?b]

  def sample(string) when is_binary(string) do
    for <<char::utf8 <- string>>, reduce: "" do
      acc -> acc <> maybe_escape(char)
    end
  end

  defp maybe_escape(char) when char in @characters_to_escape do
    char
    |> Integer.to_string(16)
    |> String.pad_leading(4, "0")
    |> then(&"\\u" <> &1)
  end

  defp maybe_escape(char), do: <<char::utf8>>
end

iex> Example.sample("a b c")
"a \\u0062 c"
Eiji

Eiji

Enum.reject(0x00..0x1F, & &1 in '\n\t')
https://github.com/rrrene/elixir-style-guide

mruoss

mruoss

Thank you all for the input and the sparring. @Eiji I guess I was hoping for some well-defined distinction / function to tell “printable” from other unicode chars. But I learned that what needs to be escaped depends very much on the use case. Looking at Jason, there is the :escape option which can be one of 4 possible values, each one escaping more or fewer chars.

I think the problem is that what you are asking for doesn’t make sense.

@adamu Yes, that too, probably! :smiley:

Inspired by Jason I ended up with something like the following which works for my case for now (probably needs refinement):

defmodule NotSoMuchMagic do
  @escape_chars '\b\f\r\v\"\\'
  @escape_char_maping Enum.zip('\b\f\r\v\"\\', 'bfrv"\\')
  @unicode_char_mapping 0x00..0x1F |> Enum.reject(&Kernel.in(&1, '\n\t'))

  for {src, dst} <- @escape_char_maping do
    defp encode_char(unquote(src)), do: [?\\, unquote(dst)]
  end

  for uchar <- @unicode_char_mapping do
    unicode_sequence = List.to_string(:io_lib.format("\\u~4.16.0B", [uchar]))
    defp encode_char(unquote(uchar)), do: unquote(unicode_sequence)
  end

  defp encode_char(char), do: char

  def encode(binary) do
    iodata = for (<< char::utf8 <- binary >>), do: encode_char(char)
    IO.iodata_to_binary(iodata)
  end
end
iex> NotSoMuchMagic.encode("\r \t \v \0 \u0028 \u0012")
"\\r \t \\v \\u0000 ( \\u0012"

Last Post!

mruoss

mruoss

Yes, that’s almost exactly what I’m doing in the PR linked above. However, there are some things to consider. Let’s take char \u0085. It has an escape character in YAML (\N). But it is also in the c-printable range. So while theoretically it can be left alone, I preferred to encode it as \N because I think it is editor/reader friendlier. Would you agree? So my order is…

  1. Is \n or \t or? Leave it alone
  2. Has c escape? Convert to that.
  3. In printable range? Leave it alone.
  4. Escape with \u

Printing to console:

iex> IO.puts("a\u0085b")
a
b
:ok

Decode YAML (escape char inside double quotes) and printing to console:

iex> ~s("a\\Nb") |> YamlElixir.read_from_string!() |> IO.puts
a
b
:ok

Decode YAML (actual character) and printing to console:

iex> "a\u0085b" |> YamlElixir.read_from_string!() |> IO.puts
a
b
:ok

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement