Add encoding functionality

This commit is contained in:
Mikko Ahlroth 2013-08-23 07:15:04 +03:00
parent 35dbc8e731
commit 9df067c1a2

View file

@ -2076,10 +2076,26 @@ defmodule ExCoder do
def encode(str) do
str
def encode(str) when is_binary str do
String.codepoints(str) |> encode
end
def encode([]) do
""
end
def encode([<< codepoint :: utf8 >> | rest]) when codepoint >= 32 and codepoint <= 126 do
<< codepoint :: utf8 >> <> encode rest
end
def encode([<< codepoint :: utf8 >> | rest]) do
"&#x" <> integer_to_binary(codepoint, 16) <> ";" <> encode rest
end
defp strip_numeric(str) do
String.split(str, ";")
end