kip
String transliteration - best strategy?
One of my projects involves transliterating digits from one number system to another. For example:
iex> roman_digits = "0123456789"
iex> arab_digits = "٠١٢٣٤٥٦٧٨٩"
iex> transliterate("123", from: roman_digits, to: arab_digits)
"١٢٣"
What is the fastest transliteration strategy? Currently I generate a set of functions that translate each code point from one number system to another and then join them. Is there a faster/better method? Would using map lookups be faster? These would all be “small maps” (< 32 entries) since the set of digits is defined to be 0..9, +, -, \s, ., E
Most Liked
Schultzer
Take a look at https://github.com/Schultzer/unidekode/blob/master/lib/unidekode.ex I do something similar.
dimitarvp
I am very late here but kept this thread open since this interested me.
This is what I came up with (~4.82 μs per function call with the parameter "01xyz234abc567def890!"):
defmodule Translit do
def western_to_eastern(string, acc \\ "")
def western_to_eastern(<<>>, acc), do: acc
~c[0123456789]
|> Enum.zip(~c[٠١٢٣٤٥٦٧٨٩])
|> Enum.each(fn {western, eastern} ->
def western_to_eastern(<<unquote(western)::utf8, rest::binary>>, acc), do: western_to_eastern(rest, acc <> unquote(to_string([eastern])))
end)
def western_to_eastern(<<any::utf8, rest::binary>>, acc), do: western_to_eastern(rest, acc <> to_string([any]))
end
Why does unquote work without a quote block though, I have no idea. ![]()
OvermindDL1
def’s are implicit quote blocks is why. ![]()







