Current section
Files
Jump to
Current section
Files
lib/elixir/string.ex
defmodule AntlUtils.Elixir.String do
@alphabets %{
62 => "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
64 => "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
}
@spec to_integer(binary, 62 | 64) :: integer
def to_integer(string, base) when is_binary(string) and base in [62, 64] do
String.codepoints(string)
|> Enum.reduce(0, fn letter, acc ->
acc * base + (:binary.match(Map.get(@alphabets, base), letter) |> elem(0))
end)
end
end