Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3text-utils.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3text-utils.dtx
4 %
5 % Copyright (C) 2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 % \title{^^A
34 % The \pkg{l3text-utils} module\\ Text processing (support utilities)^^A
35 % }
36 %
37 % \author{^^A
38 % The \LaTeX{} Project\thanks
39 % {^^A
40 % E-mail:
41 % \href{mailto:[email protected]}
42 % {[email protected]}^^A
43 % }^^A
44 % }
45 %
46 % \date{Released 2026-05-15}
47 %
48 % \maketitle
49 %
50 % \begin{documentation}
51 %
52 % \end{documentation}
53 %
54 % \begin{implementation}
55 %
56 % \section{\pkg{l3text-utils} implementation}
57 %
58 % \begin{macrocode}
59 %<*code>
60 % \end{macrocode}
61 %
62 % \subsection{Parsing BCP~47 strings}
63 %
64 % \begin{macrocode}
65 %<@@=text_bcp>
66 % \end{macrocode}
67 %
68 % For a reference implementation in JavaScript, see
69 % \url{https://github.com/wooorm/bcp-47/}. This gives clear details of the
70 % overall algorithm needed.
71 %
72 % \begin{variable}{\c_@@_normal_prop}
73 % There are a small number of non-standard tags which are grandfathered into
74 % the current standard. Here, we set up a mapping to the equivalent standard
75 % version, which allows us to avoid complexity. The commented lines have
76 % no equivalent I can track down at the moment! The \texttt{prop} is made
77 % into the linked form as this gains efficiency in the lookup.
78 % \begin{macrocode}
79 \prop_const_from_keyval:Nn \c_@@_normal_prop
80 {
81 en-gb-oed = en-gb-oxendict ,
82 i-ami = ami ,
83 i-bnn = bnn ,
84 % i-default = ,
85 % i-enochian = ,
86 i-hak = hak ,
87 i-klingon = tlh ,
88 i-lux = lb ,
89 % i-mingo = ,
90 i-navajo = nv ,
91 i-pwn = pwn ,
92 i-tao = tao ,
93 i-tay = tay ,
94 i-tsu = tsu ,
95 sgn-be-fr = sfb ,
96 sgn-be-nl = vgt ,
97 sgn-ch-de = sgg ,
98 art-lojban = jbo ,
99 % cel-gaulish = ,
100 no-bok = nb ,
101 no-nyn = nn ,
102 zh-guoyu = cmn ,
103 zh-hakka = hak ,
104 % zh-min = ,
105 zh-min-nan = nan ,
106 zh-xiang = hsn
107 }
108 \prop_make_linked:N \c_@@_normal_prop
109 % \end{macrocode}
110 % \end{variable}
111 %
112 % \begin{macro}[rEXP]{\text_bcp_parse:n}
113 % \begin{macro}[rEXP]{\@@_parse_auxi:n}
114 % \begin{macro}[rEXP]{\@@_parse_auxii:nn}
115 % \begin{macro}[rEXP]{\@@_parse_auxiii:n}
116 % \begin{macro}[rEXP]{\@@_parse_auxiv:w}
117 % \begin{macro}[rEXP]{\@@_parse_auxv:w}
118 % \begin{macro}[rEXP]{\@@_parse_auxvi:n}
119 % \begin{macro}[rEXP]{\@@_parse_auxvii:NNNN}
120 % \begin{macro}[rEXP]{\@@_parse_extlang:n}
121 % \begin{macro}[rEXP]{\@@_parse_extlang:nw}
122 % \begin{macro}[rEXP]{\@@_parse_extlang:nn}
123 % \begin{macro}[rEXP]{\@@_parse_script:n}
124 % \begin{macro}[rEXP]{\@@_parse_script:w}
125 % \begin{macro}[rEXP]{\@@_parse_region:n}
126 % \begin{macro}[rEXP]{\@@_parse_region:w}
127 % \begin{macro}[rEXP]{\@@_parse_variant_chk:n}
128 % \begin{macro}[rEXP]{\@@_parse_variant_chk:NNNN}
129 % \begin{macro}[rEXP]{\@@_parse_variant:n}
130 % \begin{macro}[rEXP]{\@@_parse_variant:nw}
131 % \begin{macro}[rEXP]{\@@_parse_variant:nn}
132 % \begin{macro}[rEXP]{\@@_parse_variant_chk:nn}
133 % \begin{macro}[rEXP]{\@@_parse_variant_chk:NNNNn}
134 % \begin{macro}[rEXP]{\@@_parse_variant_end:n}
135 % \begin{macro}[rEXP]{\@@_parse_ext:n}
136 % \begin{macro}[rEXP]{\@@_parse_ext:nN}
137 % \begin{macro}[rEXP]{\@@_parse_ext:nNnw}
138 % \begin{macro}[rEXP]{\@@_parse_private:nw}
139 % \begin{macro}[rEXP]{\@@_parse_count:n}
140 % \begin{macro}[rEXP]{\@@_parse_count_auxi:w}
141 % \begin{macro}[rEXP]{\@@_parse_count_auxii:w}
142 % \begin{macro}[rEXP]{\@@_parse_count_auxiii:w}
143 % \begin{macro}[rEXP]{\@@_parse_count_auxiv:N}
144 % Before we get to the business end of the parse, we need to deal with the
145 % special cases: entirely blank input or one of the non-standard inputs
146 % above. We also want to deal with a string not a token list, but do that
147 % once any replacement is sorted.
148 % \begin{macrocode}
149 \cs_new:Npn \text_bcp_parse:n #1
150 {
151 \tl_if_blank:nTF {#1}
152 { \msg_expandable_error:nn { text } { bcp-blank } }
153 { \exp_args:Ne \@@_parse_auxi:n { \str_casefold:n {#1} } }
154 }
155 \cs_new:Npn \@@_parse_auxi:n #1
156 {
157 \exp_args:Ne \@@_parse_auxii:nn
158 { \prop_item:Nn \c_@@_normal_prop {#1} }
159 {#1}
160 }
161 \cs_new:Npn \@@_parse_auxii:nn #1#2
162 {
163 \tl_if_blank:nTF {#1}
164 { \@@_parse_auxiii:n {#2} }
165 { \@@_parse_auxiii:n {#1} }
166 }
167 % \end{macrocode}
168 % The main loop is set up to allow us to iterate over each block, separated
169 % by a |-| token. The first block \emph{must} be the language, which can be
170 % either two or three characters: this is an easy test. There doesn't have
171 % to be any other input, so things could well stop here. If not, we need
172 % to know how many characters are in the next block to proceed.
173 % \begin{macrocode}
174 \cs_new:Npn \@@_parse_auxiii:n #1
175 {
176 \@@_parse_auxiv:w #1 - \q_recursion_tail - \q_recursion_stop
177 }
178 \cs_new:Npn \@@_parse_auxiv:w #1 -
179 {
180 \int_compare:nTF { 1 < \@@_parse_count:n {#1} < 4 }
181 {
182 {#1}
183 \@@_parse_auxv:w
184 }
185 { \msg_expandable_error:nn { text } { bcp-invalid-lang } }
186 }
187 % \end{macrocode}
188 % We will see versions of this several times. We know that there are a number
189 % of valid subtag types at this point, differentiated by their length. (A
190 % length of zero is never valid, but we do not special case it in the
191 % counting code as it's quite unlikely.) There's therefore a split to choose
192 % the appropriate subtag parser.
193 % \begin{macrocode}
194 \cs_new:Npn \@@_parse_auxv:w #1 -
195 {
196 \quark_if_recursion_tail_stop_do:nn {#1}
197 { { } { } { } { } { } { } }
198 \int_case:nnF { \@@_parse_count:n {#1} }
199 {
200 { 1 } { { } { } { } { } \@@_parse_ext:n }
201 { 2 } { { } { } \@@_parse_region:n }
202 { 3 } { \@@_parse_extlang:n }
203 { 4 } { { } \@@_parse_auxvi:n }
204 { 5 } { { } { } { } \@@_parse_variant:n }
205 { 6 } { { } { } { } \@@_parse_variant:n }
206 { 7 } { { } { } { } \@@_parse_variant:n }
207 { 8 } { { } { } { } \@@_parse_variant:n }
208 }
209 {
210 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
211 \use_none_delimit_by_q_recursion_stop:w
212 }
213 {#1}
214 }
215 % \end{macrocode}
216 % There is one case that cannot be determined purely on block length.
217 % Both variants and scripts can be made up of four characters, but for
218 % variants the first character has to be a digit.
219 % \begin{macrocode}
220 \cs_new:Npn \@@_parse_auxvi:n #1
221 { \@@_parse_auxvii:NNNN #1 }
222 \cs_new:Npn \@@_parse_auxvii:NNNN #1#2#3#4
223 {
224 \bool_lazy_or:nnTF
225 { \int_compare_p:nNn {`#1} < { `0 } }
226 { \int_compare_p:nNn {`#1} > { `9 } }
227 { \@@_parse_script:n }
228 { { } { } \@@_parse_variant:n }
229 {#1#2#3#4}
230 }
231 % \end{macrocode}
232 % The first block allowed after the language is \enquote{extended language},
233 % which can have up to three entries of three characters.
234 % \begin{macrocode}
235 \cs_new:Npn \@@_parse_extlang:n #1
236 { \@@_parse_extlang:nw { {#1} } }
237 \cs_new:Npn \@@_parse_extlang:nw #1#2 -
238 {
239 \quark_if_recursion_tail_stop_do:nn {#2}
240 { {#1} { } { } { } { } { } }
241 \int_case:nnF { \@@_parse_count:n {#2} }
242 {
243 { 1 } { { {#1} } { } { } { } \@@_parse_ext:n }
244 { 2 } { { {#1} } { } \@@_parse_region:n }
245 { 3 } { \@@_parse_extlang:nn {#1} }
246 { 4 } { { {#1} } \@@_parse_auxvi:n }
247 { 5 } { { {#1} } { } { } \@@_parse_variant:n }
248 { 6 } { { {#1} } { } { } \@@_parse_variant:n }
249 { 7 } { { {#1} } { } { } \@@_parse_variant:n }
250 { 8 } { { {#1} } { } { } \@@_parse_variant:n }
251 }
252 {
253 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
254 \use_none_delimit_by_q_recursion_stop:w
255 }
256 {#2}
257 }
258 \cs_new:Npn \@@_parse_extlang:nn #1#2
259 {
260 \int_compare:nNnTF { \tl_count:n {#1} } = 3
261 {
262 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
263 \use_none_delimit_by_q_recursion_stop:w
264 }
265 { \@@_parse_extlang:nw { #1 {#2} } }
266 }
267 % \end{macrocode}
268 % The next valid block is a script: a single entry so not a lot to do.
269 % \begin{macrocode}
270 \cs_new:Npn \@@_parse_script:n #1
271 {
272 {#1}
273 \@@_parse_script:w
274 }
275 \cs_new:Npn \@@_parse_script:w #1 -
276 {
277 \quark_if_recursion_tail_stop_do:nn {#1}
278 { { } { } { } { } }
279 \int_case:nnF { \@@_parse_count:n {#1} }
280 {
281 { 1 } { { } { } \@@_parse_ext:n }
282 { 2 } { \@@_parse_region:n }
283 { 4 } { { } \@@_parse_variant_chk:n }
284 { 5 } { { } \@@_parse_variant:n }
285 { 6 } { { } \@@_parse_variant:n }
286 { 7 } { { } \@@_parse_variant:n }
287 { 8 } { { } \@@_parse_variant:n }
288 }
289 {
290 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
291 \use_none_delimit_by_q_recursion_stop:w
292 }
293 {#1}
294 }
295 % \end{macrocode}
296 % Much the same story for the region: a single block with simply fewer
297 % possible blocks after it.
298 % \begin{macrocode}
299 \cs_new:Npn \@@_parse_region:n #1
300 {
301 {#1}
302 \@@_parse_region:w
303 }
304 \cs_new:Npn \@@_parse_region:w #1 -
305 {
306 \quark_if_recursion_tail_stop_do:nn {#1}
307 { { } { } { } }
308 \int_case:nnF { \@@_parse_count:n {#1} }
309 {
310 { 1 } { { } \@@_parse_ext:n }
311 { 4 } { \@@_parse_variant_chk:n }
312 { 5 } { \@@_parse_variant:n }
313 { 6 } { \@@_parse_variant:n }
314 { 7 } { \@@_parse_variant:n }
315 { 8 } { \@@_parse_variant:n }
316 }
317 {
318 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
319 \use_none_delimit_by_q_recursion_stop:w
320 }
321 {#1}
322 }
323 % \end{macrocode}
324 % The same idea about a four-character block as we've already seen: to be a
325 % valid variant, it has to start with a digit. Unlike the earlier version, at
326 % this stage a script is not allowed, so anything except a leading digit is
327 % an error.
328 % \begin{macrocode}
329 \cs_new:Npn \@@_parse_variant_chk:n #1
330 { \@@_parse_variant_chk:NNNN #1 }
331 \cs_new:Npn \@@_parse_variant_chk:NNNN #1#2#3#4
332 {
333 \bool_lazy_or:nnTF
334 { \int_compare_p:nNn {`#1} < { `0 } }
335 { \int_compare_p:nNn {`#1} > { `9 } }
336 {
337 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
338 \use_none_delimit_by_q_recursion_stop:w
339 }
340 { \@@_parse_variant:n }
341 {#1#2#3#4}
342 }
343 % \end{macrocode}
344 % Variants form an open-ended list so a loop is required to handle this.
345 % At each step, the length of the next block (if present) needs to checked:
346 % if it's a valid variant, keep collecting, otherwise it's a extension or
347 % an error.
348 % \begin{macrocode}
349 \cs_new:Npn \@@_parse_variant:n #1
350 { \@@_parse_variant:nw { {#1} } }
351 \cs_new:Npn \@@_parse_variant:nw #1#2 -
352 {
353 \quark_if_recursion_tail_stop_do:nn {#2}
354 { {#1} { } { } }
355 \int_case:nnF { \@@_parse_count:n {#2} }
356 {
357 { 1 } { \@@_parse_variant_end:nn }
358 { 4 } { \@@_parse_variant_chk:nn }
359 { 5 } { \@@_parse_variant:nn }
360 { 6 } { \@@_parse_variant:nn }
361 { 7 } { \@@_parse_variant:nn }
362 { 8 } { \@@_parse_variant:nn }
363 }
364 {
365 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
366 \use_none_delimit_by_q_recursion_stop:w
367 }
368 {#1} {#2}
369 }
370 \cs_new:Npn \@@_parse_variant:nn #1#2
371 { \@@_parse_variant:nw { #2 {#1} } }
372 \cs_new:Npn \@@_parse_variant_chk:nn #1#2
373 { \@@_parse_variant_chk:NNNNn #1 {#2} }
374 \cs_new:Npn \@@_parse_variant_chk:NNNNn #1#2#3#4
375 {
376 \bool_lazy_or:nnTF
377 { \int_compare_p:nNn {`#1} < { `0 } }
378 { \int_compare_p:nNn {`#1} > { `9 } }
379 {
380 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
381 \use_none_delimit_by_q_recursion_stop:w
382 }
383 { \@@_parse_variant:nn }
384 {#1#2#3#4} {#2}
385 }
386 \cs_new:Npn \@@_parse_variant_end:nn #1#2
387 {
388 {#2}
389 \@@_parse_ext:n {#1}
390 }
391 % \end{macrocode}
392 % There are only three possible valid one-letter blocks: the extensions
393 % |t| and |u|, and the private use marker |x|. All of these then allow an
394 % open-ended set of subtags, the only restriction being these cannot be
395 % one-letter other than after |x|. So we need to collect up quite a bit
396 % of information whilst allowing for the fact that only one |u| or |t| should
397 % occur.
398 % \begin{macrocode}
399 \cs_new:Npn \@@_parse_ext:n #1
400 {
401 \str_if_eq:nnTF {#1} { x }
402 {
403 { }
404 \@@_parse_private:nw { }
405 }
406 { \@@_parse_ext:nN { } #1 }
407 }
408 % \end{macrocode}
409 % Test for a valid letter, then start collecting up or switch to the private
410 % use area. Each extension can only be given once, and the comparison needs
411 % to be case-insensitive, so there is a little work to do.
412 % \begin{macrocode}
413 \cs_new:Npn \@@_parse_ext:nN #1#2
414 {
415 \bool_lazy_or:nnTF
416 { \str_if_eq_p:nn {#2} { t } }
417 { \str_if_eq_p:nn {#2} { u } }
418 {
419 \bool_lazy_or:nnTF
420 { \tl_if_head_eq_charcode_p:nN {#1} #2 }
421 { \int_compare_p:nNn { \tl_count:n {#1} } > 2 }
422 {
423 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
424 \use_none_delimit_by_q_recursion_stop:w
425 }
426 { \@@_parse_ext:nNnw {#1} #2 { } }
427 }
428 {
429 \str_if_eq:nnTF {#2} { x }
430 {
431 {#1}
432 \@@_parse_private:nw { }
433 }
434 {
435 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
436 \use_none_delimit_by_q_recursion_stop:w
437 }
438 }
439 }
440 % \end{macrocode}
441 % The loop for extensions: largely just collection with a test in case we
442 % find another extension or private use marker.
443 % \begin{macrocode}
444 \cs_new:Npn \@@_parse_ext:nNnw #1#2#3#4 -
445 {
446 \quark_if_recursion_tail_stop_do:nn {#4}
447 {
448 \str_if_empty:nTF {#3}
449 { \msg_expandable_error:nn { text } { bcp-invalid-subtag } }
450 { { #1 #2 {#3} } { } }
451 }
452 \int_compare:nTF { 1 < \@@_parse_count:n {#4} < 9 }
453 { \@@_parse_ext:nNnw {#1} #2 { #3 {#4} } }
454 {
455 \int_compare:nNnTF { \@@_parse_count:n {#4} } = 1
456 {
457 \str_if_empty:nTF {#3}
458 {
459 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
460 \use_none_delimit_by_q_recursion_stop:w
461 }
462 {
463 \use:e
464 {
465 \exp_not:n { \@@_parse_ext:nN { #1 #2 {#3} } }
466 \str_casefold:n {#4}
467 }
468 }
469 }
470 {
471 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
472 \use_none_delimit_by_q_recursion_stop:w
473 }
474 }
475 }
476 % \end{macrocode}
477 % Private use area: all bets are off! The only tests here is we need at least
478 % one subtag, and they all need to be shorter than nine characters.
479 % \begin{macrocode}
480 \cs_new:Npn \@@_parse_private:nw #1#2 -
481 {
482 \quark_if_recursion_tail_stop_do:nn {#2}
483 {
484 \str_if_empty:nTF {#1}
485 { \msg_expandable_error:nn { text } { bcp-invalid-subtag } }
486 { {#1} }
487 }
488 \int_compare:nTF { 0 < \@@_parse_count:n {#2} < 9 }
489 { \@@_parse_private:nw { #1 {#2} } }
490 {
491 \msg_expandable_error:nn { text } { bcp-invalid-subtag }
492 \use_none_delimit_by_q_recursion_stop:w
493 }
494 }
495 % \end{macrocode}
496 % As BCP~47 is largely specified in terms of number of characters, there is
497 % a need to count up repeatedly. Whilst \cs{tl_count:n} is reasonably fast,
498 % the predictable nature of the input here means we can use a slightly
499 % more focussed approach. We know that input can never be more than
500 % 8~characters, so can test for that, then get the character number by
501 % a simple expansion. This saves around half the tracing lines for typical
502 % input lengths.
503 % \begin{macrocode}
504 \cs_new:Npn \@@_parse_count:n #1
505 {
506 \@@_parse_count_auxi:w #1
507 \q_nil \q_nil \q_nil \q_nil \q_nil \q_nil \q_nil \q_nil \q_nil
508 \q_stop {#1}
509 }
510 \cs_new:Npn \@@_parse_count_auxi:w #1#2#3#4#5#6#7#8#9
511 {
512 \quark_if_nil:NTF #9
513 { \@@_parse_count_auxii:w }
514 { \msg_expandable_error:nn { text } { bcp-invalid-lang } }
515 }
516 \cs_new:Npn \@@_parse_count_auxii:w #1 \q_stop #2
517 { \@@_parse_count_auxiii:w #2 876543210 \q_stop }
518 \cs_new:Npn \@@_parse_count_auxiii:w #1#2#3#4#5#6#7#8#9
519 { \@@_parse_count_auxiv:N #9 }
520 \cs_new:Npn \@@_parse_count_auxiv:N #1#2 \q_stop {#1}
521 % \end{macrocode}
522 % \end{macro}
523 % \end{macro}
524 % \end{macro}
525 % \end{macro}
526 % \end{macro}
527 % \end{macro}
528 % \end{macro}
529 % \end{macro}
530 % \end{macro}
531 % \end{macro}
532 % \end{macro}
533 % \end{macro}
534 % \end{macro}
535 % \end{macro}
536 % \end{macro}
537 % \end{macro}
538 % \end{macro}
539 % \end{macro}
540 % \end{macro}
541 % \end{macro}
542 % \end{macro}
543 % \end{macro}
544 % \end{macro}
545 % \end{macro}
546 % \end{macro}
547 % \end{macro}
548 % \end{macro}
549 % \end{macro}
550 % \end{macro}
551 % \end{macro}
552 % \end{macro}
553 % \end{macro}
554 %
555 % \begin{macrocode}
556 \msg_new:nnn { text } { bcp-blank }
557 { Empty~input~for~BCP~47~decoding. }
558 \msg_new:nnn { text } { bcp-invalid-lang }
559 { Invalid~language~in~BCP~input. }
560 \msg_new:nnn { text } { bcp-invalid-subtag }
561 { Invalid~subtag~in~BCP~input. }
562 % \end{macrocode}
563 %
564 % \begin{macrocode}
565 %</code>
566 % \end{macrocode}
567 %
568 % \end{implementation}
569 %
570 % \PrintIndex