Skip to content

Commit 6ecfbc5

Browse files
author
José Valim
committed
Optimize String.downcase/1
Thanks to @michalmuskala for feedback and review.
1 parent f61abff commit 6ecfbc5

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

lib/elixir/unicode/properties.ex

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,23 @@ defmodule String.Casing do
105105

106106
# Downcase
107107

108-
def downcase(<<0x03A3::utf8, codepoint::utf8, rest::bits>>, acc, :greek) do
108+
@conditional_downcase [
109+
sigma = <<0x03A3::utf8>>
110+
]
111+
112+
def downcase(<<unquote(sigma), rest::bits>>, acc, mode) do
109113
downcased =
110-
case letter?(codepoint) do
114+
case mode == :greek and starts_with_letter?(rest) do
111115
true -> 0x03C3
112116
false -> 0x03C2
113117
end
114118

115-
downcase(<<codepoint::utf8, rest::bits>>, <<acc::binary, downcased::utf8>>, :greek)
116-
end
117-
118-
def downcase(<<0x03A3::utf8, rest::bits>>, acc, mode) do
119-
downcase(rest, <<acc::binary, 0x03C2::utf8>>, mode)
119+
downcase(rest, <<acc::binary, downcased::utf8>>, mode)
120120
end
121121

122-
for {codepoint, _upper, lower, _title} <- codes, lower && lower != codepoint do
122+
for {codepoint, _upper, lower, _title} <- codes,
123+
lower && lower != codepoint,
124+
codepoint not in @conditional_downcase do
123125
def downcase(<<unquote(codepoint), rest::bits>>, acc, mode) do
124126
downcase(rest, acc <> unquote(lower), mode)
125127
end
@@ -133,6 +135,14 @@ defmodule String.Casing do
133135

134136
# Sigma handling
135137

138+
defp starts_with_letter?(<<codepoint::utf8, _::bits>>) do
139+
letter?(codepoint)
140+
end
141+
142+
defp starts_with_letter?(_) do
143+
false
144+
end
145+
136146
for {first, last} <- rangify.(letters) do
137147
if first == last do
138148
defp letter?(unquote(first)), do: true

0 commit comments

Comments
 (0)