diff --git a/lib/excoder.ex b/lib/excoder.ex index e7d1c33..8b91659 100644 --- a/lib/excoder.ex +++ b/lib/excoder.ex @@ -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