Debian Codegraph @

texmf-dist/tex/luatex/lua-uni-algos/lua-uni-case.lua

raw
1 -- lua-uni-case.lua
2 -- Copyright 2020--2025 Marcel Krüger
3 --
4 -- This work may be distributed and/or modified under the
5 -- conditions of the LaTeX Project Public License, either version 1.3
6 -- of this license or (at your option) any later version.
7 -- The latest version of this license is in
8 -- http://www.latex-project.org/lppl.txt
9 -- and version 1.3 or later is part of all distributions of LaTeX
10 -- version 2005/12/01 or later.
11 --
12 -- This work has the LPPL maintenance status `maintained'.
13 --
14 -- The Current Maintainer of this work is Marcel Krüger
15
16 local unpack = table.unpack
17 local move = table.move
18 local codes = utf8.codes
19 local utf8char = utf8.char
20
21 local empty = {}
22 local result = {}
23
24 local casefold, casefold_lookup do
25 local p = require'lua-uni-parse'
26 local l = lpeg or require'lpeg'
27
28 local data = p.parse_file('CaseFolding', l.Cf(
29 l.Ct(l.Cg(l.Ct'', 'C') * l.Cg(l.Ct'', 'F') * l.Cg(l.Ct'', 'S') * l.Cg(l.Ct'', 'T'))
30 * (l.Cg(p.fields(p.codepoint, l.C(1), l.Ct(p.codepoint * (' ' * p.codepoint)^0), true)) + p.eol)^0
31 * -1
32 , function(t, base, class, mapping)
33 t[class][base] = mapping
34 return t
35 end))
36 local C, F, S, T = data.C, data.F, data.S, data.T
37 data = nil
38
39 function casefold_lookup(c, full, special)
40 return (special and T[c]) or C[c] or (full and F or S)[c]
41 end
42 function casefold(s, full, special)
43 local first = special and T or empty
44 local second = C
45 local third = full and F or S
46 local result = result
47 for i = #result, 1, -1 do result[i] = nil end
48 local i = 1
49 for _, c in codes(s) do
50 local datum = first[c] or second[c] or third[c]
51 if datum then
52 local l = #datum
53 move(datum, 1, l, i, result)
54 i = i + l
55 else
56 result[i] = c
57 i = i + 1
58 end
59 end
60 return utf8char(unpack(result))
61 end
62 end
63
64 return {
65 casefold = casefold,
66 casefold_lookup = casefold_lookup,
67 }