Handle final sigma unicode exception

Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
This commit is contained in:
Ben Olive
2017-10-30 18:45:16 +01:00
committed by José Valim
parent 46dd155fc4
commit 6f40fae504
2 changed files with 24 additions and 4 deletions
+5
View File
@@ -159,6 +159,11 @@ defmodule StringTest do
assert String.downcase("áüÈß") == "áüèß"
end
test "downcase/1 with greek final sigma" do
assert String.downcase("ΣΣ") == "σς"
assert String.downcase("ΣΣ ΣΣ") == "σς σς"
end
test "capitalize/1" do
assert String.capitalize("") == ""
assert String.capitalize("abc") == "Abc"
+19 -4
View File
@@ -11,12 +11,12 @@ to_binary = fn
|> IO.iodata_to_binary()
end
{codes, non_breakable, decompositions, combining_classes} =
Enum.reduce(File.stream!(data_path), {[], [], %{}, %{}}, fn line, {cacc, wacc, dacc, kacc} ->
{codes, letters, non_breakable, decompositions, combining_classes} =
Enum.reduce(File.stream!(data_path), {[], [], [], %{}, %{}}, fn line, {cacc, lacc, wacc, dacc, kacc} ->
[
codepoint,
_name,
_category,
category,
class,
_bidi,
decomposition,
@@ -40,6 +40,12 @@ end
cacc
end
lacc =
case category do
"L" <> _ -> [to_binary.(codepoint) | lacc]
_ -> lacc
end
wacc =
case decomposition do
"<noBreak>" <> _ -> [to_binary.(codepoint) | wacc]
@@ -67,7 +73,7 @@ end
{n, ""} -> Map.put(kacc, String.to_integer(codepoint, 16), n)
end
{cacc, wacc, dacc, kacc}
{cacc, lacc, wacc, dacc, kacc}
end)
defmodule String.Casing do
@@ -86,6 +92,15 @@ defmodule String.Casing do
def downcase(string) when is_binary(string), do: downcase(string, "")
for codepoint <- letters do
defp downcase(<<0x03A3::utf8, unquote(codepoint), rest::bits>>, acc) do
downcase(<<unquote(codepoint), rest::bits>>, <<acc::binary, 0x03C3::utf8>>)
end
end
defp downcase(<<0x03A3::utf8, rest::bits>>, acc) do
downcase(rest, <<acc::binary, 0x03C2::utf8>>)
end
for {codepoint, _upper, lower, _title} <- codes, lower && lower != codepoint do
defp downcase(<<unquote(codepoint), rest::bits>>, acc) do
downcase(rest, acc <> unquote(lower))