/
l3str-convert.dtx
3515 lines (3515 loc) · 108 KB
/
l3str-convert.dtx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% \iffalse meta-comment
%
%% File: l3str-convert.dtx Copyright (C) 2013-2018 The LaTeX3 Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version. The latest version
% of this license is in the file
%
% https://www.latex-project.org/lppl.txt
%
% This file is part of the "l3experimental bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
% https://github.com/latex3/latex3
%
% for those people who are interested.
%
%<*driver|package>
\RequirePackage{expl3}
%</driver|package>
%<*driver>
\documentclass[full]{l3doc}
\usepackage{amsmath}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
%
% \title{^^A
% The \textsf{l3str-convert} package: string encoding conversions^^A
% }
%
% \author{^^A
% The \LaTeX3 Project\thanks
% {^^A
% E-mail:
% \href{mailto:latex-team@latex-project.org}
% {latex-team@latex-project.org}^^A
% }^^A
% }
%
% \date{Released 2018-10-17}
%
% \maketitle
%
% \newcommand{\hexnum}[1]{\text{\texttt{\char`\"}#1}}
% \begin{documentation}
%
% \section{Encoding and escaping schemes}
%
% Traditionally, string encodings only specify how strings of characters
% should be stored as bytes. However, the resulting lists of bytes are
% often to be used in contexts where only a restricted subset of bytes
% are permitted (\emph{e.g.}, \textsc{pdf} string objects,
% \textsc{url}s). Hence, storing a string of characters is done in two
% steps.
% \begin{itemize}
% \item The code points (\enquote{character codes}) are expressed as
% bytes following a given \enquote{encoding}. This can be
% \textsc{utf-16}, \textsc{iso 8859-1}, \emph{etc.} See
% Table~\ref{tab:encodings} for a list of supported
% encodings.\footnote{Encodings and escapings will be added as they
% are requested.}
% \item Bytes are translated to \TeX{} tokens through a given
% \enquote{escaping}. Those are defined for the most part by the
% \texttt{pdf} file format. See Table~\ref{tab:escapings} for a
% list of escaping methods supported.\footnotemark
% \end{itemize}
%
% \begin{table}\centering
% \caption{\label{tab:encodings}Supported encodings.
% Non-alphanumeric characters are ignored,
% and capital letters are lower-cased
% before searching for the encoding in this list.}
% \begin{tabular}{cc}
% \toprule
% \meta{Encoding} & description \\
% \midrule
% \texttt{utf8} & \textsc{utf-8} \\
% \texttt{utf16} & \textsc{utf-16}, with byte-order mark \\
% \texttt{utf16be} & \textsc{utf-16}, big-endian \\
% \texttt{utf16le} & \textsc{utf-16}, little-endian \\
% \texttt{utf32} & \textsc{utf-32}, with byte-order mark \\
% \texttt{utf32be} & \textsc{utf-32}, big-endian \\
% \texttt{utf32le} & \textsc{utf-32}, little-endian \\
% \midrule
% \texttt{iso88591}, \texttt{latin1} & \textsc{iso 8859-1} \\
% \texttt{iso88592}, \texttt{latin2} & \textsc{iso 8859-2} \\
% \texttt{iso88593}, \texttt{latin3} & \textsc{iso 8859-3} \\
% \texttt{iso88594}, \texttt{latin4} & \textsc{iso 8859-4} \\
% \texttt{iso88595} & \textsc{iso 8859-5} \\
% \texttt{iso88596} & \textsc{iso 8859-6} \\
% \texttt{iso88597} & \textsc{iso 8859-7} \\
% \texttt{iso88598} & \textsc{iso 8859-8} \\
% \texttt{iso88599}, \texttt{latin5} & \textsc{iso 8859-9} \\
% \texttt{iso885910}, \texttt{latin6} & \textsc{iso 8859-10} \\
% \texttt{iso885911} & \textsc{iso 8859-11} \\
% \texttt{iso885913}, \texttt{latin7} & \textsc{iso 8859-13} \\
% \texttt{iso885914}, \texttt{latin8} & \textsc{iso 8859-14} \\
% \texttt{iso885915}, \texttt{latin9} & \textsc{iso 8859-15} \\
% \texttt{iso885916}, \texttt{latin10} & \textsc{iso 8859-16} \\
% \midrule
% \texttt{clist} & comma-list of integers \\
% \meta{empty} & native (Unicode) string \\
% \bottomrule
% \end{tabular}
% \end{table}
%
% \begin{table}\centering
% \caption{\label{tab:escapings}Supported escapings.
% Non-alphanumeric characters are ignored,
% and capital letters are lower-cased
% before searching for the escaping in this list.}
% \begin{tabular}{cc}
% \toprule
% \meta{Escaping} & description \\
% \midrule
% \texttt{bytes}, or empty
% & arbitrary bytes \\
% \texttt{hex}, \texttt{hexadecimal}
% & byte $=$ two hexadecimal digits \\
% \texttt{name}
% & see \tn{pdfescapename} \\
% \texttt{string}
% & see \tn{pdfescapestring} \\
% \texttt{url}
% & encoding used in \textsc{url}s \\
% \bottomrule
% \end{tabular}
% \end{table}
%
% \section{Conversion functions}
%
% \begin{function}{\str_set_convert:Nnnn, \str_gset_convert:Nnnn}
% \begin{syntax}
% \cs{str_set_convert:Nnnn} \meta{str~var} \Arg{string} \Arg{name~1} \Arg{name~2}
% \end{syntax}
% This function converts the \meta{string} from the encoding given by
% \meta{name~1} to the encoding given by \meta{name~2}, and stores the
% result in the \meta{str~var}. Each \meta{name} can have the form
% \meta{encoding} or \meta{encoding}\texttt{/}\meta{escaping}, where
% the possible values of \meta{encoding} and \meta{escaping} are given
% in Tables~\ref{tab:encodings} and~\ref{tab:escapings}, respectively.
% The default escaping is to input and output bytes directly. The
% special case of an empty \meta{name} indicates the use of
% \enquote{native} strings, 8-bit for \pdfTeX{}, and Unicode strings
% for the other two engines.
%
% For example,
% \begin{verbatim}
% \str_set_convert:Nnnn \l_foo_str { Hello! } { } { utf16/hex }
% \end{verbatim}
% results in the variable \cs{l_foo_str} holding the string
% \texttt{FEFF00480065006C006C006F0021}. This is obtained by
% converting each character in the (native) string \texttt{Hello!} to
% the \textsc{utf-16} encoding, and expressing each byte as a pair of
% hexadecimal digits. Note the presence of a (big-endian) byte order
% mark \hexnum{FEFF}, which can be avoided by specifying the encoding
% \texttt{utf16be/hex}.
%
% An error is raised if the \meta{string} is not valid according to
% the \meta{escaping~1} and \meta{encoding~1}, or if it cannot be
% reencoded in the \meta{encoding~2} and \meta{escaping~2} (for
% instance, if a character does not exist in the \meta{encoding~2}).
% Erroneous input is replaced by the Unicode replacement character
% \hexnum{FFFD}, and characters which cannot be reencoded are replaced
% by either the replacement character \hexnum{FFFD} if it exists in
% the \meta{encoding~2}, or an encoding-specific replacement
% character, or the question mark character.
% \end{function}
%
% \begin{function}[TF]{\str_set_convert:Nnnn, \str_gset_convert:Nnnn}
% \begin{syntax}
% \cs{str_set_convert:NnnnTF} \meta{str~var} \Arg{string} \Arg{name~1} \Arg{name~2} \Arg{true code} \Arg{false code}
% \end{syntax}
% As \cs{str_set_convert:Nnnn}, converts the \meta{string} from the
% encoding given by \meta{name~1} to the encoding given by
% \meta{name~2}, and assigns the result to \meta{str~var}. Contrarily
% to \cs{str_set_convert:Nnnn}, the conditional variant does not raise
% errors in case the \meta{string} is not valid according to the
% \meta{name~1} encoding, or cannot be expressed in the \meta{name~2}
% encoding. Instead, the \meta{false code} is performed.
% \end{function}
%
% \section{Possibilities, and things to do}
%
% Encoding/escaping-related tasks.
% \begin{itemize}
% \item In \XeTeX{}/\LuaTeX{}, would it be better to use the
% |^^^^....| approach to build a string from a given list of
% character codes? Namely, within a group, assign |0-9a-f| and all
% characters we want to category ``other'', then assign~|^| the
% category superscript, and use \tn{scantokens}.
% \item Change \cs{str_set_convert:Nnnn} to expand its last two
% arguments.
% \item Describe the internal format in the code comments. Refuse code
% points in $[\hexnum{D800}, \hexnum{DFFF}]$ in the internal
% representation?
% \item Add documentation about each encoding and escaping method, and
% add examples.
% \item The \texttt{hex} unescaping should raise an error for
% odd-token count strings.
% \item Decide what bytes should be escaped in the \texttt{url}
% escaping. Perhaps |!'()*-./0123456789_| are safe, and all other
% characters should be escaped?
% \item Automate generation of 8-bit mapping files.
% \item Change the framework for 8-bit encodings: for decoding from
% 8-bit to Unicode, use $256$ integer registers; for encoding, use a
% tree-box.
% \item More encodings (see Heiko's \pkg{stringenc}). CESU?
% \item More escapings: \textsc{ascii85}, shell escapes, lua escapes,
% \emph{etc.}?
% \end{itemize}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3str} implementation}
%
% \begin{macrocode}
%<*initex|package>
% \end{macrocode}
%
% \begin{macrocode}
%<@@=str>
% \end{macrocode}
%
% \begin{macrocode}
\ProvidesExplPackage{l3str-convert}{2018-10-17}{}
{L3 Experimental string encoding conversions}
% \end{macrocode}
%
% \subsection{Helpers}
%
% \subsubsection{A function unrelated to strings}
%
% \begin{macro}[EXP]{\use_ii_i:nn}
% A function used to swap its arguments.
% \begin{macrocode}
\cs_if_exist:NF \use_ii_i:nn
{ \cs_new:Npn \use_ii_i:nn #1#2 { #2 #1 } }
% \end{macrocode}
% \end{macro}
%
% \subsubsection{Variables and constants}
%
% \begin{macro}{\@@_tmp:w}
% \begin{variable}{\l_@@_internal_int}
% \begin{variable}{\l_@@_internal_tl}
% Internal scratch space for some functions.
% \begin{macrocode}
\cs_new_protected:Npn \@@_tmp:w { }
\tl_new:N \l_@@_internal_tl
\int_new:N \l_@@_internal_int
% \end{macrocode}
% \end{variable}
% \end{variable}
% \end{macro}
%
% \begin{variable}{\g_@@_result_tl}
% The \cs{g_@@_result_tl} variable is used to hold the result of
% various internal string operations (mostly conversions) which are
% typically performed in a group. The variable is global so that it
% remains defined outside the group, to be assigned to a user-provided
% variable.
% \begin{macrocode}
\tl_new:N \g_@@_result_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_@@_replacement_char_int}
% When converting, invalid bytes are replaced by the Unicode
% replacement character \hexnum{FFFD}.
% \begin{macrocode}
\int_const:Nn \c_@@_replacement_char_int { "FFFD }
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\c_@@_max_byte_int}
% The maximal byte number.
% \begin{macrocode}
\int_const:Nn \c_@@_max_byte_int { 255 }
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_alias_prop}
% To avoid needing one file per encoding/escaping alias, we keep track
% of those in a property list.
% \begin{macrocode}
\prop_new:N \g_@@_alias_prop
\prop_gput:Nnn \g_@@_alias_prop { latin1 } { iso88591 }
\prop_gput:Nnn \g_@@_alias_prop { latin2 } { iso88592 }
\prop_gput:Nnn \g_@@_alias_prop { latin3 } { iso88593 }
\prop_gput:Nnn \g_@@_alias_prop { latin4 } { iso88594 }
\prop_gput:Nnn \g_@@_alias_prop { latin5 } { iso88599 }
\prop_gput:Nnn \g_@@_alias_prop { latin6 } { iso885910 }
\prop_gput:Nnn \g_@@_alias_prop { latin7 } { iso885913 }
\prop_gput:Nnn \g_@@_alias_prop { latin8 } { iso885914 }
\prop_gput:Nnn \g_@@_alias_prop { latin9 } { iso885915 }
\prop_gput:Nnn \g_@@_alias_prop { latin10 } { iso885916 }
\prop_gput:Nnn \g_@@_alias_prop { utf16le } { utf16 }
\prop_gput:Nnn \g_@@_alias_prop { utf16be } { utf16 }
\prop_gput:Nnn \g_@@_alias_prop { utf32le } { utf32 }
\prop_gput:Nnn \g_@@_alias_prop { utf32be } { utf32 }
\prop_gput:Nnn \g_@@_alias_prop { hexadecimal } { hex }
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\g_@@_error_bool}
% In conversion functions with a built-in conditional, errors are not
% reported directly to the user, but the information is collected in
% this boolean, used at the end to decide on which branch of the
% conditional to take.
% \begin{macrocode}
\bool_new:N \g_@@_error_bool
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{str_byte, str_error}
% Conversions from one \meta{encoding}/\meta{escaping} pair to another
% are done within \texttt{x}-expanding assignments. Errors are
% signalled by raising the relevant flag.
% \begin{macrocode}
\flag_new:n { str_byte }
\flag_new:n { str_error }
% \end{macrocode}
% \end{variable}
%
% \subsection{String conditionals}
%
% \begin{macro}[EXP]{\@@_if_contains_char:NNT, \@@_if_contains_char:NNTF}
% \begin{macro}[EXP]{\@@_if_contains_char:nNTF}
% \begin{macro}[EXP]{\@@_if_contains_char_aux:NN}
% \begin{macro}[EXP]{\@@_if_contains_char_true:}
% \begin{syntax}
% \cs{@@_if_contains_char:nNTF} \Arg{token list} \meta{char}
% \end{syntax}
% Expects the \meta{token list} to be an \meta{other string}: the
% caller is responsible for ensuring that no (too-)special catcodes
% remain. Spaces with catcode $10$ are ignored.
% Loop over the characters of the string, comparing character codes.
% The loop is broken if character codes match. Otherwise we return
% \enquote{false}.
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_contains_char:NN #1#2 { T , TF }
{
\exp_after:wN \@@_if_contains_char_aux:NN \exp_after:wN #2
#1 { \prg_break:n { ? \fi: } }
\prg_break_point:
\prg_return_false:
}
\prg_new_conditional:Npnn \@@_if_contains_char:nN #1#2 { TF }
{
\@@_if_contains_char_aux:NN #2 #1 { \prg_break:n { ? \fi: } }
\prg_break_point:
\prg_return_false:
}
\cs_new:Npn \@@_if_contains_char_aux:NN #1#2
{
\if_charcode:w #1 #2
\exp_after:wN \@@_if_contains_char_true:
\fi:
\@@_if_contains_char_aux:NN #1
}
\cs_new:Npn \@@_if_contains_char_true:
{ \prg_break:n { \prg_return_true: \use_none:n } }
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[rEXP]{\@@_octal_use:NTF}
% \begin{syntax}
% \cs{@@_octal_use:NTF} \meta{token} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{token} is an octal digit, it is left in the input
% stream, \emph{followed} by the \meta{true code}. Otherwise, the
% \meta{false code} is left in the input stream.
% \begin{texnote}
% This function will fail if the escape character is an octal
% digit. We are thus careful to set the escape character to a known
% value before using it.
% \end{texnote}
% \TeX{} dutifully detects octal digits for us: if |#1| is an octal
% digit, then the right-hand side of the comparison is |'1#1|, greater
% than $1$. Otherwise, the right-hand side stops as |'1|, and the
% conditional takes the \texttt{false} branch.
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_octal_use:N #1 { TF }
{
\if_int_compare:w 1 < '1 \token_to_str:N #1 \exp_stop_f:
#1 \prg_return_true:
\else:
\prg_return_false:
\fi:
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[rEXP]{\@@_hexadecimal_use:NTF}
% \TeX{} detects uppercase hexadecimal digits for us (see
% \cs{@@_octal_use:NTF}), but not the lowercase letters, which we
% need to detect and replace by their uppercase counterpart.
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_hexadecimal_use:N #1 { TF }
{
\if_int_compare:w 1 < "1 \token_to_str:N #1 \exp_stop_f:
#1 \prg_return_true:
\else:
\if_case:w \int_eval:n { \exp_after:wN ` \token_to_str:N #1 - `a }
A
\or: B
\or: C
\or: D
\or: E
\or: F
\else:
\prg_return_false:
\exp_after:wN \use_none:n
\fi:
\prg_return_true:
\fi:
}
% \end{macrocode}
% \end{macro}
%
% \subsection{Conversions}
%
% \subsubsection{Producing one byte or character}
%
% \begin{variable}{\c_@@_byte_0_tl, \c_@@_byte_1_tl, \c_@@_byte_255_tl}
% \begin{variable}{\c_@@_byte_-1_tl}
% For each integer $N$ in the range $[0,255]$, we create a constant
% token list which holds three character tokens with category code
% other: the character with character code $N$, followed by the
% representation of $N$ as two hexadecimal digits. The value $-1$ is
% given a default token list which ensures that later functions give
% an empty result for the input $-1$.
% \begin{macrocode}
\group_begin:
\tl_set:Nx \l_@@_internal_tl { \tl_to_str:n { 0123456789ABCDEF } }
\tl_map_inline:Nn \l_@@_internal_tl
{
\tl_map_inline:Nn \l_@@_internal_tl
{
\tl_const:cx { c_@@_byte_ \int_eval:n {"#1##1} _tl }
{ \char_generate:nn { "#1##1 } { 12 } #1 ##1 }
}
}
\group_end:
\tl_const:cn { c_@@_byte_-1_tl } { { } \use_none:n { } }
% \end{macrocode}
% \end{variable}
% \end{variable}
%
% \begin{macro}[EXP]{\@@_output_byte:n}
% \begin{macro}[EXP]{\@@_output_byte:w}
% \begin{macro}[EXP]{\@@_output_hexadecimal:n}
% \begin{macro}[EXP]{\@@_output_end:}
% Those functions must be used carefully: feeding them a value outside
% the range $[-1,255]$ will attempt to use the undefined token list
% variable \cs{c_@@_byte_\meta{number}_tl}. Assuming that the
% argument is in the right range, we expand the corresponding token
% list, and pick either the byte (first token) or the hexadecimal
% representations (second and third tokens). The value $-1$ produces
% an empty result in both cases.
% \begin{macrocode}
\cs_new:Npn \@@_output_byte:n #1
{ \@@_output_byte:w #1 \@@_output_end: }
\cs_new:Npn \@@_output_byte:w
{
\exp_after:wN \exp_after:wN
\exp_after:wN \use_i:nnn
\cs:w c_@@_byte_ \int_eval:w
}
\cs_new:Npn \@@_output_hexadecimal:n #1
{
\exp_after:wN \exp_after:wN
\exp_after:wN \use_none:n
\cs:w c_@@_byte_ \int_eval:n {#1} _tl \cs_end:
}
\cs_new:Npn \@@_output_end:
{ \scan_stop: _tl \cs_end: }
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[rEXP]{\@@_output_byte_pair_be:n}
% \begin{macro}[rEXP]{\@@_output_byte_pair_le:n}
% \begin{macro}[rEXP]{\@@_output_byte_pair:nnN}
% Convert a number in the range $[0,65535]$ to a pair of bytes, either
% big-endian or little-endian.
% \begin{macrocode}
\cs_new:Npn \@@_output_byte_pair_be:n #1
{
\exp_args:Nf \@@_output_byte_pair:nnN
{ \int_div_truncate:nn { #1 } { "100 } } {#1} \use:nn
}
\cs_new:Npn \@@_output_byte_pair_le:n #1
{
\exp_args:Nf \@@_output_byte_pair:nnN
{ \int_div_truncate:nn { #1 } { "100 } } {#1} \use_ii_i:nn
}
\cs_new:Npn \@@_output_byte_pair:nnN #1#2#3
{
#3
{ \@@_output_byte:n { #1 } }
{ \@@_output_byte:n { #2 - #1 * "100 } }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsubsection{Mapping functions for conversions}
%
% \begin{macro}{\@@_convert_gmap:N}
% \begin{macro}[rEXP]{\@@_convert_gmap_loop:NN}
% This maps the function |#1| over all characters in
% \cs{g_@@_result_tl}, which should be a byte string in most cases,
% sometimes a native string.
% \begin{macrocode}
\cs_new_protected:Npn \@@_convert_gmap:N #1
{
\tl_gset:Nx \g_@@_result_tl
{
\exp_after:wN \@@_convert_gmap_loop:NN
\exp_after:wN #1
\g_@@_result_tl { ? \prg_break: }
\prg_break_point:
}
}
\cs_new:Npn \@@_convert_gmap_loop:NN #1#2
{
\use_none:n #2
#1#2
\@@_convert_gmap_loop:NN #1
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_convert_gmap_internal:N}
% \begin{macro}[rEXP]{\@@_convert_gmap_internal_loop:Nw}
% This maps the function |#1| over all character codes in
% \cs{g_@@_result_tl}, which must be in the internal representation.
% \begin{macrocode}
\cs_new_protected:Npn \@@_convert_gmap_internal:N #1
{
\tl_gset:Nx \g_@@_result_tl
{
\exp_after:wN \@@_convert_gmap_internal_loop:Nww
\exp_after:wN #1
\g_@@_result_tl \s__tl \q_stop \prg_break: \s__tl
\prg_break_point:
}
}
\cs_new:Npn \@@_convert_gmap_internal_loop:Nww #1 #2 \s__tl #3 \s__tl
{
\use_none_delimit_by_q_stop:w #3 \q_stop
#1 {#3}
\@@_convert_gmap_internal_loop:Nww #1
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Error-reporting during conversion}
%
% \begin{macro}{\@@_if_flag_error:nnx}
% \begin{macro}{\@@_if_flag_no_error:nnx}
% When converting using the function \cs{str_set_convert:Nnnn}, errors
% should be reported to the user after each step in the
% conversion. Errors are signalled by raising some flag (typically
% \texttt{@@_error}), so here we test that flag: if it is raised,
% give the user an error, otherwise remove the arguments. On the other
% hand, in the conditional functions \cs{str_set_convert:NnnnTF},
% errors should be suppressed. This is done by changing
% \cs{@@_if_flag_error:nnx} into \cs{@@_if_flag_no_error:nnx}
% locally.
% \begin{macrocode}
\cs_new_protected:Npn \@@_if_flag_error:nnx #1
{
\flag_if_raised:nTF {#1}
{ \__kernel_msg_error:nnx { str } }
{ \use_none:nn }
}
\cs_new_protected:Npn \@@_if_flag_no_error:nnx #1#2#3
{ \flag_if_raised:nT {#1} { \bool_gset_true:N \g_@@_error_bool } }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[rEXP]{\@@_if_flag_times:nT}
% At the end of each conversion step, we raise all relevant errors as
% one error message, built on the fly. The height of each flag
% indicates how many times a given error was encountered. This
% function prints |#2| followed by the number of occurrences of an
% error if it occurred, nothing otherwise.
% \begin{macrocode}
\cs_new:Npn \@@_if_flag_times:nT #1#2
{ \flag_if_raised:nT {#1} { #2~(x \flag_height:n {#1} ) } }
% \end{macrocode}
% \end{macro}
%
% \subsubsection{Framework for conversions}
%
% Most functions in this module expect to be working with
% \enquote{native} strings. Strings can also be stored as bytes, in one
% of many encodings, for instance \textsc{utf8}. The bytes themselves
% can be expressed in various ways in terms of \TeX{} tokens, for
% instance as pairs of hexadecimal digits. The questions of going from
% arbitrary Unicode code points to bytes, and from bytes to tokens are
% mostly independent.
%
% Conversions are done in four steps:
% \begin{itemize}
% \item \enquote{unescape} produces a string of bytes;
% \item \enquote{decode} takes in a string of bytes, and converts it
% to a list of Unicode characters in an internal representation,
% with items of the form
% \begin{quote}
% \meta{bytes} \cs{s__tl} \meta{Unicode code point} \cs{s__tl}
% \end{quote}
% where we have collected the \meta{bytes} which combined to form
% this particular Unicode character, and the \meta{Unicode code
% point} is in the range $[0,\hexnum{10FFFF}]$.
% \item \enquote{encode} encodes the internal list of code points as a
% byte string in the new encoding;
% \item \enquote{escape} escapes bytes as requested.
% \end{itemize}
% The process is modified in case one of the encoding is empty (or the
% conversion function has been set equal to the empty encoding because
% it was not found): then the unescape or escape step is ignored, and
% the decode or encode steps work on tokens instead of bytes. Otherwise,
% each step must ensure that it passes a correct byte string or internal
% string to the next step.
%
% \begin{macro}{\str_set_convert:Nnnn, \str_gset_convert:Nnnn}
% \begin{macro}[TF]{\str_set_convert:Nnnn, \str_gset_convert:Nnnn}
% \begin{macro}{\@@_convert:nNNnnn}
% The input string is stored in \cs{g_@@_result_tl}, then we:
% unescape and decode; encode and escape; exit the group and store the
% result in the user's variable. The various conversion functions all
% act on \cs{g_@@_result_tl}. Errors are silenced for the conditional
% functions by redefining \cs{@@_if_flag_error:nnx} locally.
% \begin{macrocode}
\cs_new_protected:Npn \str_set_convert:Nnnn
{ \@@_convert:nNNnnn { } \tl_set_eq:NN }
\cs_new_protected:Npn \str_gset_convert:Nnnn
{ \@@_convert:nNNnnn { } \tl_gset_eq:NN }
\prg_new_protected_conditional:Npnn
\str_set_convert:Nnnn #1#2#3#4 { T , F , TF }
{
\bool_gset_false:N \g_@@_error_bool
\@@_convert:nNNnnn
{ \cs_set_eq:NN \@@_if_flag_error:nnx \@@_if_flag_no_error:nnx }
\tl_set_eq:NN #1 {#2} {#3} {#4}
\bool_if:NTF \g_@@_error_bool \prg_return_false: \prg_return_true:
}
\prg_new_protected_conditional:Npnn
\str_gset_convert:Nnnn #1#2#3#4 { T , F , TF }
{
\bool_gset_false:N \g_@@_error_bool
\@@_convert:nNNnnn
{ \cs_set_eq:NN \@@_if_flag_error:nnx \@@_if_flag_no_error:nnx }
\tl_gset_eq:NN #1 {#2} {#3} {#4}
\bool_if:NTF \g_@@_error_bool \prg_return_false: \prg_return_true:
}
\cs_new_protected:Npn \@@_convert:nNNnnn #1#2#3#4#5#6
{
\group_begin:
#1
\tl_gset:Nx \g_@@_result_tl { \__kernel_str_to_other_fast:n {#4} }
\exp_after:wN \@@_convert:wwwnn
\tl_to_str:n {#5} /// \q_stop
{ decode } { unescape }
\prg_do_nothing:
\@@_convert_decode_:
\exp_after:wN \@@_convert:wwwnn
\tl_to_str:n {#6} /// \q_stop
{ encode } { escape }
\use_ii_i:nn
\@@_convert_encode_:
\group_end:
#2 #3 \g_@@_result_tl
}
% \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_convert:wwwnn}
% \begin{macro}{\@@_convert:NNnNN}
% The task of \cs{@@_convert:wwwnn} is to split
% \meta{encoding}/\meta{escaping} pairs into their components, |#1|
% and |#2|. Calls to \cs{@@_convert:nnn} ensure that the
% corresponding conversion functions are defined. The third auxiliary
% does the main work.
% \begin{itemize}
% \item |#1| is the encoding conversion function;
% \item |#2| is the escaping function;
% \item |#3| is the escaping name for use in an error message;
% \item |#4| is \cs{prg_do_nothing:} for unescaping/decoding, and
% \cs{use_ii_i:nn} for encoding/escaping;
% \item |#5| is the default encoding function (either
% \enquote{decode} or \enquote{encode}), for which there should be
% no escaping.
% \end{itemize}
% Let us ignore the native encoding for a second. In the
% unescaping/decoding phase, we want to do |#2#1| in this order, and
% in the encoding/escaping phase, the order should be reversed:
% |#4#2#1| does exactly that. If one of the encodings is the default
% (native), then the escaping should be ignored, with an error if any
% was given, and only the encoding, |#1|, should be performed.
% \begin{macrocode}
\cs_new_protected:Npn \@@_convert:wwwnn
#1 / #2 // #3 \q_stop #4#5
{
\@@_convert:nnn {enc} {#4} {#1}
\@@_convert:nnn {esc} {#5} {#2}
\exp_args:Ncc \@@_convert:NNnNN
{ @@_convert_#4_#1: } { @@_convert_#5_#2: } {#2}
}
\cs_new_protected:Npn \@@_convert:NNnNN #1#2#3#4#5
{
\if_meaning:w #1 #5
\tl_if_empty:nF {#3}
{ \__kernel_msg_error:nnx { str } { native-escaping } {#3} }
#1
\else:
#4 #2 #1
\fi:
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_convert:nnn}
% \begin{macro}{\@@_convert:nnnn}
% The arguments of \cs{@@_convert:nnn} are: \texttt{enc} or
% \texttt{esc}, used to build filenames, the type of the conversion
% (unescape, decode, encode, escape), and the encoding or escaping
% name. If the function is already defined, no need to do anything.
% Otherwise, filter out all non-alphanumerics in the name, and
% lowercase it. Feed that, and the same three arguments, to
% \cs{@@_convert:nnnn}. The task is then to make sure that the
% conversion function |#3_#1| corresponding to the type |#3| and
% filtered name |#1| is defined, then set our initial conversion
% function |#3_#4| equal to that.
%
% How do we get the |#3_#1| conversion to be defined if it isn't?
% Two main cases.
%
% First, if |#1| is a key in \cs{g_@@_alias_prop}, then the value
% \cs{l_@@_internal_tl} tells us what file to load. Loading is
% skipped if the file was already read, \emph{i.e.}, if the conversion
% command based on \cs{l_@@_internal_tl} already exists. Otherwise,
% try to load the file; if that fails, there is an error, use the
% default empty name instead.
%
% Second, |#1| may be absent from the property list. The
% \cs{cs_if_exist:cF} test is automatically false, and we search for a
% file defining the encoding or escaping |#1| (this should allow
% third-party \texttt{.def} files). If the file is not found, there is
% an error, use the default empty name instead.
%
% In all cases, the conversion based on \cs{l_@@_internal_tl} is
% defined, so we can set the |#3_#1| function equal to that. In some
% cases (\emph{e.g.}, \texttt{utf16be}), the |#3_#1| function is
% actually defined within the file we just loaded, and it is different
% from the \cs{l_@@_internal_tl}-based function: we mustn't clobber
% that different definition.
% \begin{macrocode}
\cs_new_protected:Npn \@@_convert:nnn #1#2#3
{
\cs_if_exist:cF { @@_convert_#2_#3: }
{
\exp_args:Nx \@@_convert:nnnn
{ \@@_convert_lowercase_alphanum:n {#3} }
{#1} {#2} {#3}
}
}
\cs_new_protected:Npn \@@_convert:nnnn #1#2#3#4
{
\cs_if_exist:cF { @@_convert_#3_#1: }
{
\prop_get:NnNF \g_@@_alias_prop {#1} \l_@@_internal_tl
{ \tl_set:Nn \l_@@_internal_tl {#1} }
\cs_if_exist:cF { @@_convert_#3_ \l_@@_internal_tl : }
{
\file_if_exist:nTF { l3str-#2- \l_@@_internal_tl .def }
{
\group_begin:
\@@_load_catcodes:
\file_input:n { l3str-#2- \l_@@_internal_tl .def }
\group_end:
}
{
\tl_clear:N \l_@@_internal_tl
\__kernel_msg_error:nnxx { str } { unknown-#2 } {#4} {#1}
}
}
\cs_if_exist:cF { @@_convert_#3_#1: }
{
\cs_gset_eq:cc { @@_convert_#3_#1: }
{ @@_convert_#3_ \l_@@_internal_tl : }
}
}
\cs_gset_eq:cc { @@_convert_#3_#4: } { @@_convert_#3_#1: }
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[rEXP]{\@@_convert_lowercase_alphanum:n}
% \begin{macro}[rEXP]{\@@_convert_lowercase_alphanum_loop:N}
% This function keeps only letters and digits, with upper case letters
% converted to lower case.
% \begin{macrocode}
\cs_new:Npn \@@_convert_lowercase_alphanum:n #1
{
\exp_after:wN \@@_convert_lowercase_alphanum_loop:N
\tl_to_str:n {#1} { ? \prg_break: }
\prg_break_point:
}
\cs_new:Npn \@@_convert_lowercase_alphanum_loop:N #1
{
\use_none:n #1
\if_int_compare:w `#1 > `Z \exp_stop_f:
\if_int_compare:w `#1 > `z \exp_stop_f: \else:
\if_int_compare:w `#1 < `a \exp_stop_f: \else:
#1
\fi:
\fi:
\else:
\if_int_compare:w `#1 < `A \exp_stop_f:
\if_int_compare:w 1 < 1#1 \exp_stop_f:
#1
\fi:
\else:
\@@_output_byte:n { `#1 + `a - `A }
\fi:
\fi:
\@@_convert_lowercase_alphanum_loop:N
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_load_catcodes:}
% Since encoding files may be loaded at arbitrary places in a \TeX{}
% document, including within verbatim mode, we set the catcodes of all
% characters appearing in any encoding definition file.
% \begin{macrocode}
\cs_new_protected:Npn \@@_load_catcodes:
{
\char_set_catcode_escape:N \\
\char_set_catcode_group_begin:N \{
\char_set_catcode_group_end:N \}
\char_set_catcode_math_toggle:N \$
\char_set_catcode_alignment:N \&
\char_set_catcode_parameter:N \#
\char_set_catcode_math_superscript:N \^
\char_set_catcode_ignore:N \ %
\char_set_catcode_space:N \~
\tl_map_function:nN { abcdefghijklmnopqrstuvwxyz_:ABCDEFILNPSTUX }
\char_set_catcode_letter:N
\tl_map_function:nN { 0123456789"'?*+-.(),`!/<>[];= }
\char_set_catcode_other:N
\char_set_catcode_comment:N \%
\int_set:Nn \tex_endlinechar:D {32}
}
% \end{macrocode}
% \end{macro}
%
% \subsubsection{Byte unescape and escape}
%
% Strings of bytes may need to be stored in auxiliary files in safe
% \enquote{escaping} formats. Each such escaping is only loaded as
% needed. By default, on input any non-byte is filtered out, while the
% output simply consists in letting bytes through.
%
% \begin{macro}[rEXP]{\@@_filter_bytes:n}
% \begin{macro}[rEXP]{\@@_filter_bytes_aux:N}
% In the case of 8-bit engines, every character is a byte. For
% Unicode-aware engines, test the character code; non-bytes cause us
% to raise the flag \texttt{str_byte}. Spaces have already been given
% the correct category code when this function is called.
% \begin{macrocode}
\bool_lazy_any:nTF
{
\sys_if_engine_luatex_p:
\sys_if_engine_xetex_p:
}
{
\cs_new:Npn \@@_filter_bytes:n #1
{
\@@_filter_bytes_aux:N #1
{ ? \prg_break: }
\prg_break_point:
}
\cs_new:Npn \@@_filter_bytes_aux:N #1
{
\use_none:n #1
\if_int_compare:w `#1 < 256 \exp_stop_f:
#1
\else:
\flag_raise:n { str_byte }
\fi:
\@@_filter_bytes_aux:N
}
}
{ \cs_new_eq:NN \@@_filter_bytes:n \use:n }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_convert_unescape_:}
% \begin{macro}{\@@_convert_unescape_bytes:}
% The simplest unescaping method removes non-bytes from
% \cs{g_@@_result_tl}.
% \begin{macrocode}
\bool_lazy_any:nTF
{
\sys_if_engine_luatex_p:
\sys_if_engine_xetex_p:
}
{
\cs_new_protected:Npn \@@_convert_unescape_:
{
\flag_clear:n { str_byte }
\tl_gset:Nx \g_@@_result_tl
{ \exp_args:No \@@_filter_bytes:n \g_@@_result_tl }
\@@_if_flag_error:nnx { str_byte } { non-byte } { bytes }
}
}
{ \cs_new_protected:Npn \@@_convert_unescape_: { } }
\cs_new_eq:NN \@@_convert_unescape_bytes: \@@_convert_unescape_:
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_convert_escape_:}
% \begin{macro}{\@@_convert_escape_bytes:}
% The simplest form of escape leaves the bytes from the previous step
% of the conversion unchanged.
% \begin{macrocode}
\cs_new_protected:Npn \@@_convert_escape_: { }
\cs_new_eq:NN \@@_convert_escape_bytes: \@@_convert_escape_:
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \subsubsection{Native strings}
%
% \begin{macro}{\@@_convert_decode_:}
% \begin{macro}[rEXP]{\@@_decode_native_char:N}
% Convert each character to its character code, one at a time.
% \begin{macrocode}
\cs_new_protected:Npn \@@_convert_decode_:
{ \@@_convert_gmap:N \@@_decode_native_char:N }
\cs_new:Npn \@@_decode_native_char:N #1
{ #1 \s__tl \int_value:w `#1 \s__tl }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_convert_encode_:}
% \begin{macro}[EXP]{\@@_encode_native_char:n}
% The conversion from an internal string to native character tokens
% basically maps \cs{char_generate:nn} through the code-points, but in
% non-Unicode-aware engines we use a fall-back character |?| rather
% than nothing when given a character code outside $[0,255]$. We
% detect the presence of bad characters using a flag and only produce
% a single error after the \texttt{x}-expanding assignment.
% \begin{macrocode}
\bool_lazy_any:nTF
{
\sys_if_engine_luatex_p:
\sys_if_engine_xetex_p:
}
{
\cs_new_protected:Npn \@@_convert_encode_:
{ \@@_convert_gmap_internal:N \@@_encode_native_char:n }
\cs_new:Npn \@@_encode_native_char:n #1
{ \char_generate:nn {#1} {12} }
}
{