Packages
localize
0.3.0
1.0.0-rc.4
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.41.3
0.41.2
0.41.1
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.0
0.34.0
0.33.0
0.32.0
0.31.0
0.30.1
0.30.0
retired
0.29.0
0.28.0
0.27.0
0.26.0
0.25.0
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.19.0
0.18.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.3.0
0.2.0
0.1.0
0.1.0-alpha.1
Localization (parsing, formatting) of numbers, dates/time/calendar, units of measure, messages and lists. Includes localized collation.
Current section
Files
Jump to
Current section
Files
lib/localize/collation/fast_latin.ex
defmodule Localize.Collation.FastLatin do
@moduledoc """
Fast lookup table for Basic Latin and Latin Extended-A codepoints.
Provides O(1) collation element lookup for codepoints U+0000..U+017F
by pre-computing a tuple indexed by codepoint value. This bypasses
the full contraction-checking path in `Localize.Collation.Table` for the
most commonly encountered characters.
Codepoints that are contraction starters (e.g., `L` and `l` for Catalan
l·l) or combining marks (CCC > 0) are excluded from the fast table and
fall back to the normal lookup path.
"""
@table_name {:localize, :collation_fast_latin}
@latin_limit 0x0180
@doc """
Build the fast Latin lookup table from the loaded collation table.
Reads the collation table and contraction starters from `:persistent_term`,
and constructs a tuple of `@latin_limit` entries. Each entry is either a
list of collation element tuples (for direct lookup) or `nil` (indicating
the codepoint must use the full lookup path).
Called automatically during table loading.
### Returns
* `:ok`.
"""
@spec build() :: :ok
def build do
table = :persistent_term.get({:localize, :collation_table})
contractions = :persistent_term.get({:localize, :collation_contractions})
entries =
for cp <- 0..(@latin_limit - 1) do
cond do
Map.has_key?(contractions, cp) ->
nil
combining_mark?(cp) ->
nil
true ->
Map.get(table, cp)
end
end
tuple = List.to_tuple(entries)
:persistent_term.put(@table_name, tuple)
:ok
end
@doc """
Look up collation elements for a Latin codepoint.
### Arguments
* `cp` - an integer codepoint less than `0x0180`.
### Returns
* A list of collation element tuples - the codepoint has a direct mapping.
* `nil` - the codepoint is a contraction starter, combining mark, or unmapped;
use the full lookup path.
"""
@spec lookup(non_neg_integer()) :: [Localize.Collation.Element.t()] | nil
def lookup(cp) when cp < @latin_limit do
elem(table(), cp)
end
defp table do
case :persistent_term.get(@table_name, :not_loaded) do
:not_loaded ->
# The fast-latin table is normally built when
# `Localize.Collation.Table` loads. If it is missing — for
# example because the collation table was loaded directly
# into `:persistent_term` without running `build/0`, or
# because the fast-latin term was erased — rebuild it by
# routing through `Table.ensure_loaded/0`, which runs the
# full load path and invokes `build/0`.
Localize.Collation.Table.ensure_loaded()
:persistent_term.get(@table_name)
tuple ->
tuple
end
end
defp combining_mark?(cp) do
Localize.Collation.Unicode.combining_class(cp) > 0
end
end