Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3tl.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3tl.dtx
4 %
5 % Copyright (C) 1990-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{l3tl} module\\ Token lists^^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 % \TeX{} works with tokens, and \LaTeX3 therefore provides a number of
53 % functions to deal with lists of tokens. Token lists may be present
54 % directly in the argument to a function:
55 % \begin{verbatim}
56 % \foo:n { a collection of \tokens }
57 % \end{verbatim}
58 % or may be stored in a so-called \enquote{tl~var} (\meta{tl~var}), which
59 % have the suffix \texttt{tl}: a token list variable can also be used as
60 % the argument to a function, for example
61 % \begin{verbatim}
62 % \foo:N \l_some_tl
63 % \end{verbatim}
64 % In both cases, functions are available to test and manipulate the lists
65 % of tokens, and these have the module prefix \texttt{tl}.
66 % In many cases, functions which can be applied to token list variables
67 % are paired with similar functions for application to explicit lists
68 % of tokens: the two \enquote{views} of a token list are therefore collected
69 % together here.
70 %
71 % A token list (explicit, or stored in a variable) can be seen either
72 % as a list of \enquote{items},
73 % or a list of \enquote{tokens}. An item is whatever \cs{use:n} would
74 % grab as its argument: a single non-space token or a brace group,
75 % with optional leading explicit space characters (each item is thus
76 % itself a token list). A token is either a normal \texttt{N} argument,
77 % or \verb*| |, |{|, or |}| (assuming normal \TeX{} category codes).
78 % Thus for example
79 % \begin{verbatim}
80 % { Hello } ~ world
81 % \end{verbatim}
82 % contains six items (\texttt{Hello}, \texttt{w}, \texttt{o}, \texttt{r},
83 % \texttt{l} and \texttt{d}), but thirteen tokens (|{|, \texttt{H}, \texttt{e},
84 % \texttt{l}, \texttt{l}, \texttt{o}, |}|, \verb*| |, \texttt{w}, \texttt{o},
85 % \texttt{r}, \texttt{l} and \texttt{d}).
86 % Functions which act on items are often faster than their analogue acting
87 % directly on tokens.
88 %
89 % \section{Creating and initializing token list variables}
90 %
91 % \begin{function}{\tl_new:N, \tl_new:c}
92 % \begin{syntax}
93 % \cs{tl_new:N} \meta{tl~var}
94 % \end{syntax}
95 % Creates a new \meta{tl~var} or raises an error if the
96 % name is already taken. The declaration is global. The
97 % \meta{tl~var} is initially empty.
98 % \end{function}
99 %
100 % \begin{function}
101 % {
102 % \tl_const:Nn, \tl_const:NV, \tl_const:Ne,
103 % \tl_const:cn, \tl_const:cV, \tl_const:ce
104 % }
105 % \begin{syntax}
106 % \cs{tl_const:Nn} \meta{tl~var} \Arg{tokens}
107 % \end{syntax}
108 % Creates a new constant \meta{tl~var} or raises an error
109 % if the name is already taken. The value of the
110 % \meta{tl~var} is set globally to the \meta{tokens}.
111 % \end{function}
112 %
113 % \begin{function}{\tl_clear:N, \tl_clear:c, \tl_gclear:N, \tl_gclear:c}
114 % \begin{syntax}
115 % \cs{tl_clear:N} \meta{tl~var}
116 % \end{syntax}
117 % Clears all entries from the \meta{tl~var}.
118 % \end{function}
119 %
120 % \begin{function}
121 % {\tl_clear_new:N, \tl_clear_new:c, \tl_gclear_new:N, \tl_gclear_new:c}
122 % \begin{syntax}
123 % \cs{tl_clear_new:N} \meta{tl~var}
124 % \end{syntax}
125 % Ensures that the \meta{tl~var} exists globally by applying
126 % \cs{tl_new:N} if necessary, then applies \cs[index=tl_clear:N]{tl_(g)clear:N} to leave
127 % the \meta{tl~var} empty.
128 % \end{function}
129 %
130 % \begin{function}
131 % {
132 % \tl_set_eq:NN, \tl_set_eq:cN, \tl_set_eq:Nc, \tl_set_eq:cc,
133 % \tl_gset_eq:NN, \tl_gset_eq:cN, \tl_gset_eq:Nc, \tl_gset_eq:cc
134 % }
135 % \begin{syntax}
136 % \cs{tl_set_eq:NN} \meta{tl~var_1} \meta{tl~var_2}
137 % \end{syntax}
138 % Sets the content of \meta{tl~var_1} equal to that of
139 % \meta{tl~var_2}.
140 % \end{function}
141 %
142 % \begin{function}[added = 2012-05-18]
143 % {
144 % \tl_concat:NNN, \tl_concat:ccc,
145 % \tl_gconcat:NNN, \tl_gconcat:ccc
146 % }
147 % \begin{syntax}
148 % \cs{tl_concat:NNN} \meta{tl~var_1} \meta{tl~var_2} \meta{tl~var_3}
149 % \end{syntax}
150 % Concatenates the content of \meta{tl~var_2} and \meta{tl~var_3}
151 % together and saves the result in \meta{tl~var_1}. The \meta{tl~var_2}
152 % is placed at the left side of the new token list.
153 % \end{function}
154 %
155 % \begin{function}[EXP, pTF, added=2012-03-03]{\tl_if_exist:N, \tl_if_exist:c}
156 % \begin{syntax}
157 % \cs{tl_if_exist_p:N} \meta{tl~var}
158 % \cs{tl_if_exist:NTF} \meta{tl~var} \Arg{true code} \Arg{false code}
159 % \end{syntax}
160 % Tests whether the \meta{tl~var} is currently defined. This does not
161 % check that the \meta{tl~var} really is a token list variable.
162 % \end{function}
163 %
164 % \section{Adding data to token list variables}
165 %
166 % \begin{function}
167 % {
168 % \tl_set:Nn, \tl_set:NV, \tl_set:Nv, \tl_set:No, \tl_set:Ne, \tl_set:Nf,
169 % \tl_set:cn, \tl_set:cV, \tl_set:cv, \tl_set:co, \tl_set:ce, \tl_set:cf,
170 % \tl_gset:Nn, \tl_gset:NV, \tl_gset:Nv,
171 % \tl_gset:No, \tl_gset:Ne, \tl_gset:Nf,
172 % \tl_gset:cn, \tl_gset:cV, \tl_gset:cv,
173 % \tl_gset:co, \tl_gset:ce, \tl_gset:cf
174 % }
175 % \begin{syntax}
176 % \cs{tl_set:Nn} \meta{tl~var} \Arg{tokens}
177 % \end{syntax}
178 % Sets \meta{tl~var} to contain \meta{tokens},
179 % removing any previous content from the variable.
180 % \end{function}
181 %
182 % \begin{function}
183 % {
184 % \tl_put_left:Nn, \tl_put_left:NV, \tl_put_left:Nv, \tl_put_left:Ne,
185 % \tl_put_left:No,
186 % \tl_put_left:cn, \tl_put_left:cV, \tl_put_left:cv, \tl_put_left:ce,
187 % \tl_put_left:co,
188 % \tl_gput_left:Nn, \tl_gput_left:NV, \tl_gput_left:Nv, \tl_gput_left:Ne,
189 % \tl_gput_left:No,
190 % \tl_gput_left:cn, \tl_gput_left:cV, \tl_gput_left:cv, \tl_gput_left:ce,
191 % \tl_gput_left:co
192 % }
193 % \begin{syntax}
194 % \cs{tl_put_left:Nn} \meta{tl~var} \Arg{tokens}
195 % \end{syntax}
196 % Appends \meta{tokens} to the left side of the current content of
197 % \meta{tl~var}.
198 % \end{function}
199 %
200 % \begin{function}
201 % {
202 % \tl_put_right:Nn, \tl_put_right:NV, \tl_put_right:Nv, \tl_put_right:Ne,
203 % \tl_put_right:No,
204 % \tl_put_right:cn, \tl_put_right:cV, \tl_put_right:cv, \tl_put_right:ce,
205 % \tl_put_right:co,
206 % \tl_gput_right:Nn, \tl_gput_right:NV, \tl_gput_right:Nv, \tl_gput_right:Ne,
207 % \tl_gput_right:No,
208 % \tl_gput_right:cn, \tl_gput_right:cV, \tl_gput_right:cv, \tl_gput_right:ce,
209 % \tl_gput_right:co
210 % }
211 % \begin{syntax}
212 % \cs{tl_put_right:Nn} \meta{tl~var} \Arg{tokens}
213 % \end{syntax}
214 % Appends \meta{tokens} to the right side of the current content of
215 % \meta{tl~var}.
216 % \end{function}
217 %
218 % \section{Token list conditionals}
219 %
220 % \begin{function}[EXP,pTF, updated = 2019-09-04]
221 % {\tl_if_blank:n, \tl_if_blank:e, \tl_if_blank:V, \tl_if_blank:o}
222 % \begin{syntax}
223 % \cs{tl_if_blank_p:n} \Arg{token list}
224 % \cs{tl_if_blank:nTF} \Arg{token list} \Arg{true code} \Arg{false code}
225 % \end{syntax}
226 % Tests if the \meta{token list} consists only of blank spaces
227 % (i.e., contains no item). The test is \texttt{true} if
228 % \meta{token list} is zero or more explicit space characters
229 % (explicit tokens with character code~$32$ and category code~$10$),
230 % and is \texttt{false} otherwise.
231 % \end{function}
232 %
233 % \begin{function}[EXP,pTF]{\tl_if_empty:N, \tl_if_empty:c}
234 % \begin{syntax}
235 % \cs{tl_if_empty_p:N} \meta{tl~var}
236 % \cs{tl_if_empty:NTF} \meta{tl~var} \Arg{true code} \Arg{false code}
237 % \end{syntax}
238 % Tests if the \meta{tl~var} is entirely empty
239 % (i.e., contains no tokens at all).
240 % \end{function}
241 %
242 % \begin{function}[added = 2012-05-24, updated = 2012-06-05, EXP,pTF]
243 % {\tl_if_empty:n, \tl_if_empty:V, \tl_if_empty:o, \tl_if_empty:e}
244 % \begin{syntax}
245 % \cs{tl_if_empty_p:n} \Arg{token list}
246 % \cs{tl_if_empty:nTF} \Arg{token list} \Arg{true code} \Arg{false code}
247 % \end{syntax}
248 % Tests if the \meta{token list} is entirely empty
249 % (i.e., contains no tokens at all).
250 % \end{function}
251 %
252 % \begin{function}[EXP,pTF]
253 % {\tl_if_eq:NN, \tl_if_eq:Nc, \tl_if_eq:cN, \tl_if_eq:cc}
254 % \begin{syntax}
255 % \cs{tl_if_eq_p:NN} \meta{tl~var_1} \meta{tl~var_2}
256 % \cs{tl_if_eq:NNTF} \meta{tl~var_1} \meta{tl~var_2} \Arg{true code} \Arg{false code}
257 % \end{syntax}
258 % Compares the content of \meta{tl~var_1} and \meta{tl~var_2} and
259 % is logically \texttt{true} if the two contain the same list of
260 % tokens (i.e., identical in both the list of characters they
261 % contain and the category codes of those characters). Thus for example
262 % \begin{verbatim}
263 % \tl_set:Nn \l_tmpa_tl { abc }
264 % \tl_set:Ne \l_tmpb_tl { \tl_to_str:n { abc } }
265 % \tl_if_eq:NNTF \l_tmpa_tl \l_tmpb_tl { true } { false }
266 % \end{verbatim}
267 % yields \texttt{false}.
268 % See also \cs{str_if_eq:nnTF} for a comparison that ignores category codes.
269 % \end{function}
270 %
271 % \begin{function}[TF, added = 2020-07-14]{\tl_if_eq:Nn, \tl_if_eq:cn}
272 % \begin{syntax}
273 % \cs{tl_if_eq:NnTF} \meta{tl~var_1} \Arg{token list_2} \Arg{true code} \Arg{false code}
274 % \end{syntax}
275 % Tests if the \meta{tl~var_1} and the \meta{token
276 % list_2} contain the same list of tokens, both in respect of
277 % character codes and category codes. This conditional is not
278 % expandable: see \cs{tl_if_eq:NNTF} for an expandable version when
279 % both token lists are stored in variables, or \cs{str_if_eq:nnTF} if
280 % category codes are not important.
281 % \end{function}
282 %
283 % \begin{function}[TF]
284 % {
285 % \tl_if_eq:nn, \tl_if_eq:nV, \tl_if_eq:ne, \tl_if_eq:Vn, \tl_if_eq:en,
286 % \tl_if_eq:ee
287 % }
288 % \begin{syntax}
289 % \cs{tl_if_eq:nnTF} \Arg{token list_1} \Arg{token list_2} \Arg{true code} \Arg{false code}
290 % \end{syntax}
291 % Tests if \meta{token list_1} and \meta{token list_2} contain the
292 % same list of tokens, both in respect of character codes and category
293 % codes. This conditional is not expandable: see \cs{tl_if_eq:NNTF}
294 % for an expandable version when token lists are stored in variables,
295 % or \cs{str_if_eq:nnTF} if category codes are not important.
296 % \end{function}
297 %
298 % \begin{function}[TF]
299 % {
300 % \tl_if_in:Nn, \tl_if_in:NV, \tl_if_in:No,
301 % \tl_if_in:cn, \tl_if_in:cV, \tl_if_in:co
302 % }
303 % \begin{syntax}
304 % \cs{tl_if_in:NnTF} \meta{tl~var} \Arg{token list} \Arg{true code} \Arg{false code}
305 % \end{syntax}
306 % Tests if the \meta{token list} is found in the content of the
307 % \meta{tl~var}. The \meta{token list} cannot contain
308 % the tokens |{|, |}| or |#|
309 % (more precisely, explicit character tokens with category code $1$
310 % (begin-group) or $2$ (end-group), and tokens with category code $6$).
311 % \end{function}
312 %
313 % \begin{function}[TF]
314 % {
315 % \tl_if_in:nn, \tl_if_in:Vn, \tl_if_in:VV, \tl_if_in:on, \tl_if_in:oo,
316 % \tl_if_in:nV, \tl_if_in:no
317 % }
318 % \begin{syntax}
319 % \cs{tl_if_in:nnTF} \Arg{token list_1} \Arg{token list_2} \Arg{true code} \Arg{false code}
320 % \end{syntax}
321 % Tests if \meta{token list_2} is found inside \meta{token list_1}.
322 % The \meta{token list_2} cannot contain the tokens |{|, |}| or |#|
323 % (more precisely, explicit character tokens with category code $1$
324 % (begin-group) or $2$ (end-group), and tokens with category code $6$).
325 % The search does \emph{not} enter brace (category code $1$/$2$) groups.
326 % \end{function}
327 %
328 % \begin{function}[added = 2017-11-14, EXP,pTF]{\tl_if_novalue:n}
329 % \begin{syntax}
330 % \cs{tl_if_novalue_p:n} \Arg{token list}
331 % \cs{tl_if_novalue:nTF} \Arg{token list} \Arg{true code} \Arg{false code}
332 % \end{syntax}
333 % Tests if the \meta{token list} and the special \cs{c_novalue_tl} marker
334 % contain the same list of tokens, both in respect of character codes and
335 % category codes. This means that
336 % \cs{exp_args:No} \cs{tl_if_novalue:nTF} \texttt\{ \cs{c_novalue_tl} \texttt\} is
337 % logically \texttt{true} but \cs{tl_if_novalue:nTF} \texttt\{ \cs{c_novalue_tl} \texttt\}
338 % is logically \texttt{false}.
339 % This function is intended to allow construction
340 % of flexible document interface structures in which missing optional
341 % arguments are detected.
342 % \end{function}
343 %
344 % \begin{function}[updated = 2011-08-13, EXP,pTF]
345 % {\tl_if_single:N, \tl_if_single:c}
346 % \begin{syntax}
347 % \cs{tl_if_single_p:N} \meta{tl~var}
348 % \cs{tl_if_single:NTF} \meta{tl~var} \Arg{true code} \Arg{false code}
349 % \end{syntax}
350 % Tests if the content of the \meta{tl~var} consists of a single \meta{item},
351 % i.e., is a single normal token (neither an explicit space
352 % character nor a begin-group character) or a single brace group,
353 % surrounded by optional spaces on both sides. In other words, such a
354 % token list has token count $1$ according to \cs{tl_count:N}.
355 % \end{function}
356 %
357 % \begin{function}[updated = 2011-08-13, EXP,pTF]{\tl_if_single:n}
358 % \begin{syntax}
359 % \cs{tl_if_single_p:n} \Arg{token list}
360 % \cs{tl_if_single:nTF} \Arg{token list} \Arg{true code} \Arg{false code}
361 % \end{syntax}
362 % Tests if the \meta{token list} has exactly one \meta{item}, i.e., is
363 % a single normal token (neither an explicit space character nor a
364 % begin-group character) or a single brace group, surrounded by
365 % optional spaces on both sides. In other words, such a token list has
366 % token count $1$ according to \cs{tl_count:n}.
367 % \end{function}
368 %
369 % \begin{function}[EXP,pTF]{\tl_if_single_token:n}
370 % \begin{syntax}
371 % \cs{tl_if_single_token_p:n} \Arg{token list}
372 % \cs{tl_if_single_token:nTF} \Arg{token list} \Arg{true code} \Arg{false code}
373 % \end{syntax}
374 % Tests if the token list consists of exactly one token, i.e., is
375 % either a single space character or a single normal token.
376 % Token groups (|{|\ldots|}|) are not single tokens.
377 % \end{function}
378 %
379 % \begin{function}[TF, added = 2024-12-08]
380 % {
381 % \tl_if_regex_match:nn, \tl_if_regex_match:Vn,
382 % \tl_if_regex_match:nN, \tl_if_regex_match:VN,
383 % }
384 % \begin{syntax}
385 % \cs{tl_if_regex_match:nnTF} \Arg{token list} \Arg{regex} \Arg{true code} \Arg{false code}
386 % \cs{tl_if_regex_match:nNTF} \Arg{token list} \meta{regex~var} \Arg{true code} \Arg{false code}
387 % \end{syntax}
388 % Tests whether the \meta{regular expression} matches any part
389 % of the \meta{token list}. For instance,
390 % \begin{verbatim}
391 % \tl_if_regex_match:nnTF { abecdcx } { b [cde]* } { TRUE } { FALSE }
392 % \tl_if_regex_match:nnTF { example } { [b-dq-w] } { TRUE } { FALSE }
393 % \end{verbatim}
394 % leaves \texttt{TRUE} then \texttt{FALSE} in the input stream.
395 % Theses are alternative names for \cs{regex_if_match:nnTF} and friends,
396 % with arguments re-ordered for \meta{token list} testing;
397 % see \pkg{l3regex} chapter for more details of the \meta{regex}
398 % format.
399 % \end{function}
400 %
401 % \subsection{Testing the first token}
402 %
403 % \begin{function}[updated = 2012-07-09, EXP, pTF]
404 % {
405 % \tl_if_head_eq_catcode:nN, \tl_if_head_eq_catcode:VN,
406 % \tl_if_head_eq_catcode:eN, \tl_if_head_eq_catcode:oN
407 % }
408 % \begin{syntax}
409 % \cs{tl_if_head_eq_catcode_p:nN} \Arg{token list} \meta{test token}
410 % \cs{tl_if_head_eq_catcode:nNTF} \Arg{token list} \meta{test token}
411 % ~~\Arg{true code} \Arg{false code}
412 % \end{syntax}
413 % Tests if the first \meta{token} in the \meta{token list} has the
414 % same category code as the \meta{test token}. In the case where the
415 % \meta{token list} is empty, the test is always \texttt{false}.
416 % \end{function}
417 %
418 % \begin{function}[updated = 2012-07-09, EXP, pTF]
419 % {
420 % \tl_if_head_eq_charcode:nN, \tl_if_head_eq_charcode:VN,
421 % \tl_if_head_eq_charcode:eN, \tl_if_head_eq_charcode:fN
422 % }
423 % \begin{syntax}
424 % \cs{tl_if_head_eq_charcode_p:nN} \Arg{token list} \meta{test token}
425 % \cs{tl_if_head_eq_charcode:nNTF} \Arg{token list} \meta{test token}
426 % ~~\Arg{true code} \Arg{false code}
427 % \end{syntax}
428 % Tests if the first \meta{token} in the \meta{token list} has the
429 % same character code as the \meta{test token}. In the case where the
430 % \meta{token list} is empty, the test is always \texttt{false}.
431 % \end{function}
432 %
433 % \begin{function}[updated = 2012-07-09, EXP, pTF]
434 % {
435 % \tl_if_head_eq_meaning:nN, \tl_if_head_eq_meaning:VN,
436 % \tl_if_head_eq_meaning:eN
437 % }
438 % \begin{syntax}
439 % \cs{tl_if_head_eq_meaning_p:nN} \Arg{token list} \meta{test token}
440 % \cs{tl_if_head_eq_meaning:nNTF} \Arg{token list} \meta{test token}
441 % ~~\Arg{true code} \Arg{false code}
442 % \end{syntax}
443 % Tests if the first \meta{token} in the \meta{token list} has the
444 % same meaning as the \meta{test token}. In the case where
445 % \meta{token list} is empty, the test is always \texttt{false}.
446 % \end{function}
447 %
448 % \begin{function}[added = 2012-07-08, EXP, pTF]{\tl_if_head_is_group:n}
449 % \begin{syntax}
450 % \cs{tl_if_head_is_group_p:n} \Arg{token list}
451 % \cs{tl_if_head_is_group:nTF} \Arg{token list} \Arg{true code} \Arg{false code}
452 % \end{syntax}
453 % Tests if the first \meta{token} in the \meta{token list}
454 % is an explicit begin-group character (with category code~$1$
455 % and any character code), in other words, if the \meta{token list}
456 % starts with a brace group. In particular, the test is \texttt{false}
457 % if the \meta{token list} starts with an implicit token such as
458 % \cs{c_group_begin_token}, or if it is empty.
459 % This function is useful to implement actions on token lists on
460 % a token by token basis.
461 % \end{function}
462 %
463 % \begin{function}[added = 2012-07-08, EXP, pTF]{\tl_if_head_is_N_type:n}
464 % \begin{syntax}
465 % \cs{tl_if_head_is_N_type_p:n} \Arg{token list}
466 % \cs{tl_if_head_is_N_type:nTF} \Arg{token list} \Arg{true code} \Arg{false code}
467 % \end{syntax}
468 % Tests if the first \meta{token} in the \meta{token list}
469 % is a normal \texttt{N}-type argument. In other words,
470 % it is neither an explicit space character
471 % (explicit token with character code~$32$ and category code~$10$)
472 % nor an explicit begin-group character
473 % (with category code~1 and any character code). An empty
474 % argument yields \texttt{false}, as it does not have a normal
475 % first token.
476 % This function is useful to implement actions on token lists on
477 % a token by token basis.
478 % \end{function}
479 %
480 % \begin{function}[updated = 2012-07-08, EXP, pTF]{\tl_if_head_is_space:n}
481 % \begin{syntax}
482 % \cs{tl_if_head_is_space_p:n} \Arg{token list}
483 % \cs{tl_if_head_is_space:nTF} \Arg{token list} \Arg{true code} \Arg{false code}
484 % \end{syntax}
485 % Tests if the first \meta{token} in the \meta{token list}
486 % is an explicit space character
487 % (explicit token with character code~$32$ and category code~$10$).
488 % In particular, the test is \texttt{false} if the \meta{token list}
489 % starts with an implicit token such as \cs{c_space_token}, or if it
490 % is empty.
491 % This function is useful to implement actions on token lists on
492 % a token by token basis.
493 % \end{function}
494 %
495 % \section{Working with token lists as a whole}
496 %
497 % \subsection{Using token lists}
498 %
499 % \begin{function}[EXP]
500 % {\tl_to_str:n, \tl_to_str:o, \tl_to_str:V, \tl_to_str:v, \tl_to_str:e}
501 % \begin{syntax}
502 % \cs{tl_to_str:n} \Arg{token list}
503 % \end{syntax}
504 % Converts the \meta{token list} to a \meta{string}, leaving the
505 % resulting character tokens in the input stream. A \meta{string}
506 % is a series of tokens with category code $12$ (other) with the exception
507 % of spaces, which retain category code $10$ (space).
508 % The base function requires only a single expansion.
509 % Its argument \emph{must} be braced.
510 % \begin{texnote}
511 % This is the \eTeX{} primitive \tn{detokenize}.
512 % Converting a \meta{token list} to a \meta{string} yields a
513 % concatenation of the string representations of every token in the
514 % \meta{token list}.
515 % The string representation of a control sequence is
516 % \begin{itemize}
517 % \item an escape character, whose character code is given by the
518 % internal parameter \tn{escapechar}, absent if the
519 % \tn{escapechar} is negative or greater than the largest
520 % character code;
521 % \item the control sequence name, as defined by \cs{cs_to_str:N};
522 % \item a space, unless the control sequence name is a single
523 % character whose category at the time of expansion of
524 % \cs{tl_to_str:n} is not \enquote{letter}.
525 % \end{itemize}
526 % The string representation of an explicit character token is that
527 % character, doubled in the case of (explicit) macro parameter
528 % characters (normally |#|).
529 % In particular, the string representation of a token list may
530 % depend on the category codes in effect when it is evaluated, and
531 % the value of the \tn{escapechar}: for instance |\tl_to_str:n {\a}|
532 % normally produces the three character \enquote{backslash},
533 % \enquote{lower-case a}, \enquote{space}, but it may also produce a
534 % single \enquote{lower-case a} if the escape character is negative
535 % and \texttt{a} is currently not a letter.
536 % \end{texnote}
537 % \end{function}
538 %
539 % \begin{function}[EXP]{\tl_to_str:N, \tl_to_str:c}
540 % \begin{syntax}
541 % \cs{tl_to_str:N} \meta{tl~var}
542 % \end{syntax}
543 % Converts the content of the \meta{tl~var} into a series of characters
544 % with category code $12$ (other) with the exception of spaces, which
545 % retain category code $10$ (space). This \meta{string} is then left
546 % in the input stream. For low-level details, see the notes given for
547 % \cs{tl_to_str:n}.
548 % \end{function}
549 %
550 % \begin{function}[EXP]{\tl_use:N, \tl_use:c}
551 % \begin{syntax}
552 % \cs{tl_use:N} \meta{tl~var}
553 % \end{syntax}
554 % Recovers the content of a \meta{tl~var} and places it
555 % directly in the input stream. An error is raised if the variable
556 % does not exist or if it is invalid. Note that it is possible to use
557 % a \meta{tl~var} directly without an accessor function.
558 % \end{function}
559 %
560 % \subsection{Counting and reversing token lists}
561 %
562 % \begin{function}[added = 2012-05-13, EXP]
563 % {\tl_count:n, \tl_count:V, \tl_count:v, \tl_count:e, \tl_count:o}
564 % \begin{syntax}
565 % \cs{tl_count:n} \Arg{token list}
566 % \end{syntax}
567 % Counts the number of \meta{items} in the \meta{token list} and leaves this
568 % information in the input stream. Unbraced tokens count as one
569 % element as do each token group (|{|\ldots|}|). This process
570 % ignores any unprotected spaces within the \meta{token list}. See also
571 % \cs{tl_count:N}. This function requires three expansions,
572 % giving an \meta{integer denotation}.
573 % \end{function}
574 %
575 % \begin{function}[added = 2012-05-13, EXP]{\tl_count:N, \tl_count:c}
576 % \begin{syntax}
577 % \cs{tl_count:N} \meta{tl~var}
578 % \end{syntax}
579 % Counts the number of \meta{items} in the \meta{tl~var}
580 % and leaves this information in the input stream. Unbraced tokens
581 % count as one element as do each token group (|{|\ldots|}|). This
582 % process ignores any unprotected spaces within the \meta{tl~var}.
583 % See also \cs{tl_count:n}. This function requires three expansions,
584 % giving an \meta{integer denotation}.
585 % \end{function}
586 %
587 % \begin{function}[EXP, added = 2019-02-25]{\tl_count_tokens:n}
588 % \begin{syntax}
589 % \cs{tl_count_tokens:n} \Arg{token list}
590 % \end{syntax}
591 % Counts the number of \TeX{} tokens in the \meta{token list} and leaves
592 % this information in the input stream. Every token, including spaces and
593 % braces, contributes one to the total; thus for instance, the token count of
594 % |a~{bc}| is $6$.
595 % \end{function}
596 %
597 % \begin{function}[updated = 2012-01-08, EXP]
598 % {\tl_reverse:n, \tl_reverse:V, \tl_reverse:o, \tl_reverse:f, \tl_reverse:e}
599 % \begin{syntax}
600 % \cs{tl_reverse:n} \Arg{token list}
601 % \end{syntax}
602 % Reverses the order of the \meta{items} in the \meta{token list},
603 % so that \meta{item_1}\meta{item_2}\meta{item_3} \ldots \meta{item_n}
604 % becomes \meta{item_n}\ldots \meta{item_3}\meta{item_2}\meta{item_1}.
605 % This process preserves unprotected space within the
606 % \meta{token list}. Tokens are not reversed within braced token
607 % groups, which keep their outer set of braces.
608 % In situations where performance is important,
609 % consider \cs{tl_reverse_items:n}.
610 % See also \cs{tl_reverse:N}.
611 % \begin{texnote}
612 % The result is returned within \tn{unexpanded}, which means that the token
613 % list does not expand further when appearing in an \texttt{e}-type
614 % or \texttt{x}-type argument expansion.
615 % \end{texnote}
616 % \end{function}
617 %
618 % \begin{function}[updated = 2012-01-08]
619 % {\tl_reverse:N, \tl_reverse:c, \tl_greverse:N, \tl_greverse:c}
620 % \begin{syntax}
621 % \cs{tl_reverse:N} \meta{tl~var}
622 % \end{syntax}
623 % Sets the \meta{tl~var} to contain the result of reversing
624 % the order of its \meta{items}, so
625 % that \meta{item_1}\meta{item_2}\meta{item_3} \ldots \meta{item_n}
626 % becomes \meta{item_n}\ldots \meta{item_3}\meta{item_2}\meta{item_1}.
627 % This process preserves unprotected spaces within the
628 % \meta{tl~var}. Braced token groups are copied without
629 % reversing the order of tokens, but keep the outer set of braces.
630 % This is equivalent to a combination of an assignment and
631 % \cs{tl_reverse:V}. See also \cs{tl_reverse_items:n} for improved
632 % performance.
633 % \end{function}
634 %
635 % \begin{function}[added = 2012-01-08, EXP]{\tl_reverse_items:n}
636 % \begin{syntax}
637 % \cs{tl_reverse_items:n} \Arg{token list}
638 % \end{syntax}
639 % Reverses the order of the \meta{items} in the \meta{token list},
640 % so that \meta{item_1}\meta{item_2}\meta{item_3} \ldots \meta{item_n}
641 % becomes \Arg{item_n} \ldots{} \Arg{item_3}\Arg{item_2}\Arg{item_1}.
642 % This process removes any unprotected space within the
643 % \meta{token list}. Braced token groups are copied without
644 % reversing the order of tokens, and keep the outer set of braces.
645 % Items which are initially not braced are copied with braces in
646 % the result. In cases where preserving spaces is important,
647 % consider the slower function \cs{tl_reverse:n}.
648 % \begin{texnote}
649 % The result is returned within \tn{unexpanded}, which means that the token
650 % list does not expand further when appearing in an \texttt{e}-type
651 % or \texttt{x}-type argument expansion.
652 % \end{texnote}
653 % \end{function}
654 %
655 % \begin{function}[added = 2011-07-09, updated = 2012-06-25, EXP]
656 % {
657 % \tl_trim_spaces:n, \tl_trim_spaces:V, \tl_trim_spaces:v,
658 % \tl_trim_spaces:e, \tl_trim_spaces:o,
659 % }
660 % \begin{syntax}
661 % \cs{tl_trim_spaces:n} \Arg{token list}
662 % \end{syntax}
663 % Removes any leading and trailing explicit space characters
664 % (explicit tokens with character code~$32$ and category code~$10$)
665 % from the \meta{token list} and leaves the result in the input
666 % stream.
667 % \begin{texnote}
668 % The result is returned within \tn{unexpanded}, which means that the token
669 % list does not expand further when appearing in an \texttt{e}-type
670 % or \texttt{x}-type argument expansion.
671 % \end{texnote}
672 % \end{function}
673 %
674 % \begin{function}[added = 2025-02-02, EXP]
675 % {
676 % \tl_trim_left_spaces:n, \tl_trim_left_spaces:V, \tl_trim_left_spaces:v,
677 % \tl_trim_left_spaces:e, \tl_trim_left_spaces:o,
678 % \tl_trim_right_spaces:n, \tl_trim_right_spaces:V, \tl_trim_right_spaces:v,
679 % \tl_trim_right_spaces:e, \tl_trim_right_spaces:o
680 % }
681 % \begin{syntax}
682 % \cs{tl_trim_left_spaces:n} \Arg{token list}
683 % \end{syntax}
684 % Analogue of \cs{tl_trim_spaces:n} which removes any leading \emph{or}
685 % trailing explicit space characters
686 % (explicit tokens with character code~$32$ and category code~$10$)
687 % from the \meta{token list} and leaves the result in the input
688 % stream.
689 % \begin{texnote}
690 % The result is returned within \tn{unexpanded}, which means that the token
691 % list does not expand further when appearing in an \texttt{e}-type
692 % or \texttt{x}-type argument expansion.
693 % \end{texnote}
694 % \end{function}
695 %
696 % \begin{function}[added = 2018-04-12, EXP]
697 % {\tl_trim_spaces_apply:nN, \tl_trim_spaces_apply:oN}
698 % \begin{syntax}
699 % \cs{tl_trim_spaces_apply:nN} \Arg{token list} \meta{function}
700 % \end{syntax}
701 % Removes any leading and trailing explicit space characters (explicit
702 % tokens with character code~$32$ and category code~$10$) from the
703 % \meta{token list} and passes the result to the \meta{function} as an
704 % \texttt{n}-type argument.
705 % \end{function}
706 %
707 % \begin{function}[added = 2025-02-02, EXP]
708 % {
709 % \tl_trim_left_spaces_apply:nN, \tl_trim_left_spaces_apply:oN,
710 % \tl_trim_right_spaces_apply:nN, \tl_trim_right_spaces_apply:oN
711 % }
712 % \begin{syntax}
713 % \cs{tl_trim_left_spaces_apply:nN} \Arg{token list} \meta{function}
714 % \end{syntax}
715 % Analogue of \cs{tl_trim_spaces_apply:nN} which removes any leading
716 % \emph{or} trailing explicit space characters (explicit
717 % tokens with character code~$32$ and category code~$10$) from the
718 % \meta{token list} and passes the result to the \meta{function} as an
719 % \texttt{n}-type argument.
720 % \end{function}
721 %
722 % \begin{function}[added = 2011-07-09]
723 % {
724 % \tl_trim_spaces:N, \tl_trim_spaces:c,
725 % \tl_gtrim_spaces:N, \tl_gtrim_spaces:c
726 % }
727 % \begin{syntax}
728 % \cs{tl_trim_spaces:N} \meta{tl~var}
729 % \end{syntax}
730 % Sets the \meta{tl~var} to contain the result of removing any leading
731 % and trailing explicit space characters (explicit tokens with
732 % character code~$32$ and category code~$10$) from its contents.
733 % \end{function}
734 %
735 % \begin{function}[added = 2025-02-02]
736 % {
737 % \tl_trim_left_spaces:N, \tl_trim_left_spaces:c,
738 % \tl_trim_right_spaces:N, \tl_trim_right_spaces:c,
739 % \tl_gtrim_left_spaces:N, \tl_gtrim_left_spaces:c,
740 % \tl_gtrim_right_spaces:N, \tl_gtrim_right_spaces:c
741 % }
742 % \begin{syntax}
743 % \cs{tl_trim_left_spaces:N} \meta{tl~var}
744 % \end{syntax}
745 % Analogue of \cs{tl_trim_spaces:N} which sets the \meta{tl~var} to
746 % contain the result of removing any leading
747 % \emph{or} trailing explicit space characters (explicit tokens with
748 % character code~$32$ and category code~$10$) from its contents.
749 % \end{function}
750 %
751 % \subsection{Viewing token lists}
752 %
753 % \begin{function}[updated = 2021-04-29]{\tl_show:N, \tl_show:c}
754 % \begin{syntax}
755 % \cs{tl_show:N} \meta{tl~var}
756 % \end{syntax}
757 % Displays the content of the \meta{tl~var} on the terminal.
758 % \begin{texnote}
759 % This is similar to the \TeX{} primitive \tn{show}, wrapped to a
760 % fixed number of characters per line.
761 % \end{texnote}
762 % \end{function}
763 %
764 % \begin{function}[updated = 2015-08-07]{\tl_show:n, \tl_show:e}
765 % \begin{syntax}
766 % \cs{tl_show:n} \Arg{token list}
767 % \end{syntax}
768 % Displays the \meta{token list} on the terminal.
769 % \begin{texnote}
770 % This is similar to the \eTeX{} primitive \tn{showtokens}, wrapped
771 % to a fixed number of characters per line.
772 % \end{texnote}
773 % \end{function}
774 %
775 % \begin{function}[added = 2014-08-22, updated = 2021-04-29]{\tl_log:N, \tl_log:c}
776 % \begin{syntax}
777 % \cs{tl_log:N} \meta{tl~var}
778 % \end{syntax}
779 % Writes the content of the \meta{tl~var} in the log file. See also
780 % \cs{tl_show:N} which displays the result in the terminal.
781 % \end{function}
782 %
783 % \begin{function}[added = 2014-08-22, updated = 2015-08-07]
784 % {\tl_log:n, \tl_log:e, \tl_log:x}
785 % \begin{syntax}
786 % \cs{tl_log:n} \Arg{token list}
787 % \end{syntax}
788 % Writes the \meta{token list} in the log file. See also
789 % \cs{tl_show:n} which displays the result in the terminal.
790 % \end{function}
791 %
792 % \section{Manipulating items in token lists}
793 %
794 % \subsection{Mapping over token lists}
795 %
796 % All mappings are done at the current group level, i.e., any
797 % local assignments made by the \meta{function} or \meta{code} discussed
798 % below remain in effect after the loop.
799 %
800 % \begin{function}[updated = 2012-06-29, rEXP]
801 % {\tl_map_function:NN, \tl_map_function:cN}
802 % \begin{syntax}
803 % \cs{tl_map_function:NN} \meta{tl~var} \meta{function}
804 % \end{syntax}
805 % Applies \meta{function} to every \meta{item} in the \meta{tl~var}.
806 % The \meta{function} receives one argument for each iteration.
807 % This may be a number of tokens if the \meta{item} was stored within
808 % braces. Hence the \meta{function} should anticipate receiving
809 % \texttt{n}-type arguments. See also \cs{tl_map_function:nN}.
810 % \end{function}
811 %
812 % \begin{function}[updated = 2012-06-29, rEXP]
813 % {\tl_map_function:nN, \tl_map_function:eN}
814 % \begin{syntax}
815 % \cs{tl_map_function:nN} \Arg{token list} \meta{function}
816 % \end{syntax}
817 % Applies \meta{function} to every \meta{item} in the \meta{token list},
818 % The \meta{function} receives one argument for each iteration.
819 % This may be a number of tokens if the \meta{item} was stored within
820 % braces. Hence the \meta{function} should anticipate receiving
821 % \texttt{n}-type arguments. See also \cs{tl_map_function:NN}.
822 % \end{function}
823 %
824 % \begin{function}[updated = 2012-06-29]
825 % {\tl_map_inline:Nn, \tl_map_inline:cn}
826 % \begin{syntax}
827 % \cs{tl_map_inline:Nn} \meta{tl~var} \Arg{inline function}
828 % \end{syntax}
829 % Applies the \meta{inline function} to every \meta{item} stored within the
830 % \meta{tl~var}. The \meta{inline function} should consist of code which
831 % receives the \meta{item} as |#1|. See also \cs{tl_map_function:NN}.
832 % \end{function}
833 %
834 % \begin{function}[updated = 2012-06-29]{\tl_map_inline:nn}
835 % \begin{syntax}
836 % \cs{tl_map_inline:nn} \Arg{token list} \Arg{inline function}
837 % \end{syntax}
838 % Applies the \meta{inline function} to every \meta{item} stored within the
839 % \meta{token list}. The \meta{inline function} should consist of code which
840 % receives the \meta{item} as |#1|. See also \cs{tl_map_function:nN}.
841 % \end{function}
842 %
843 % \begin{function}[rEXP, added = 2019-09-02]
844 % {\tl_map_tokens:Nn, \tl_map_tokens:cn, \tl_map_tokens:nn}
845 % \begin{syntax}
846 % \cs{tl_map_tokens:Nn} \meta{tl~var} \Arg{code}
847 % \cs{tl_map_tokens:nn} \Arg{token list} \Arg{code}
848 % \end{syntax}
849 % Analogue of \cs{tl_map_function:NN} which maps several tokens
850 % instead of a single function. The \meta{code} receives each \meta{item} in
851 % the \meta{tl~var} or in the \meta{token list} as a trailing brace group. For
852 % instance,
853 % \begin{verbatim}
854 % \tl_map_tokens:Nn \l_my_tl { \prg_replicate:nn { 2 } }
855 % \end{verbatim}
856 % expands to twice each \meta{item} in the \meta{tl~var}: for each \meta{item} in
857 % \cs[no-index]{l_my_tl} the function \cs{prg_replicate:nn} receives |2| and
858 % \meta{item} as its two arguments. The function
859 % \cs{tl_map_inline:Nn} is typically faster but is not expandable.
860 % \end{function}
861 %
862 % \begin{function}[updated = 2012-06-29]
863 % {\tl_map_variable:NNn, \tl_map_variable:cNn}
864 % \begin{syntax}
865 % \cs{tl_map_variable:NNn} \meta{tl~var} \meta{variable} \Arg{code}
866 % \end{syntax}
867 % Stores each \meta{item} of the \meta{tl~var} in turn in the (token
868 % list) \meta{variable} and applies the \meta{code}. The \meta{code}
869 % will usually make use of the \meta{variable}, but this is not
870 % enforced. The assignments to the \meta{variable} are local. Its
871 % value after the loop is the last \meta{item} in the \meta{tl~var},
872 % or its original value if the \meta{tl~var} is blank. See also
873 % \cs{tl_map_inline:Nn}.
874 % \end{function}
875 %
876 % \begin{function}[updated = 2012-06-29]{\tl_map_variable:nNn}
877 % \begin{syntax}
878 % \cs{tl_map_variable:nNn} \Arg{token list} \meta{variable} \Arg{code}
879 % \end{syntax}
880 % Stores each \meta{item} of the \meta{token list} in turn in the
881 % (token list) \meta{variable} and applies the \meta{code}. The
882 % \meta{code} will usually make use of the \meta{variable}, but this
883 % is not enforced. The assignments to the \meta{variable} are local.
884 % Its value after the loop is the last \meta{item} in the
885 % \meta{tl~var}, or its original value if the \meta{tl~var} is blank.
886 % See also \cs{tl_map_inline:nn}.
887 % \end{function}
888 %
889 % \begin{function}[updated = 2012-06-29, rEXP]{\tl_map_break:}
890 % \begin{syntax}
891 % \cs{tl_map_break:}
892 % \end{syntax}
893 % Used to terminate a \cs[no-index]{tl_map_\ldots} function before all
894 % entries in the \meta{token list} have been processed. This
895 % normally takes place within a conditional statement, for example
896 % \begin{verbatim}
897 % \tl_map_inline:Nn \l_my_tl
898 % {
899 % \str_if_eq:nnT { #1 } { bingo } { \tl_map_break: }
900 % % Do something useful
901 % }
902 % \end{verbatim}
903 % See also \cs{tl_map_break:n}.
904 % Use outside of a \cs[no-index]{tl_map_\ldots} scenario leads to low
905 % level \TeX{} errors.
906 % \begin{texnote}
907 % When the mapping is broken, additional tokens may be inserted
908 % before further items are taken
909 % from the input stream. This depends on the design of the mapping
910 % function.
911 % \end{texnote}
912 % \end{function}
913 %
914 % \begin{function}[updated = 2012-06-29, rEXP]{\tl_map_break:n}
915 % \begin{syntax}
916 % \cs{tl_map_break:n} \Arg{code}
917 % \end{syntax}
918 % Used to terminate a \cs[no-index]{tl_map_\ldots} function before all
919 % entries in the \meta{token list} have been processed, inserting
920 % the \meta{code} after the mapping has ended. This
921 % normally takes place within a conditional statement, for example
922 % \begin{verbatim}
923 % \tl_map_inline:Nn \l_my_tl
924 % {
925 % \str_if_eq:nnT { #1 } { bingo }
926 % { \tl_map_break:n { <code> } }
927 % % Do something useful
928 % }
929 % \end{verbatim}
930 % Use outside of a \cs[no-index]{tl_map_\ldots} scenario leads to low
931 % level \TeX{} errors.
932 % \begin{texnote}
933 % When the mapping is broken, additional tokens may be inserted
934 % before the \meta{code} is
935 % inserted into the input stream.
936 % This depends on the design of the mapping function.
937 % \end{texnote}
938 % \end{function}
939 %
940 % \subsection{Head and tail of token lists}
941 %
942 % Functions which deal with either only the very first item (balanced
943 % text or single normal token) in a token list, or the remaining tokens.
944 %
945 % \begin{function}[updated = 2012-09-09, EXP]
946 % {\tl_head:N, \tl_head:n, \tl_head:V, \tl_head:v, \tl_head:f, \tl_head:e}
947 % \begin{syntax}
948 % \cs{tl_head:n} \Arg{token list}
949 % \end{syntax}
950 % Leaves in the input stream the first \meta{item} in the
951 % \meta{token list}, discarding the rest of the \meta{token list}.
952 % All leading explicit space characters
953 % (explicit tokens with character code~$32$ and category code~$10$)
954 % are discarded; for example
955 % \begin{verbatim}
956 % \tl_head:n { abc }
957 % \end{verbatim}
958 % and
959 % \begin{verbatim}
960 % \tl_head:n { ~ abc }
961 % \end{verbatim}
962 % both leave |a| in the input stream. If the \enquote{head} is a
963 % brace group, rather than a single token, the braces are removed, and
964 % so
965 % \begin{verbatim}
966 % \tl_head:n { ~ { ~ ab } c }
967 % \end{verbatim}
968 % yields \verb*| ab|.
969 % A blank \meta{token list} (see \cs{tl_if_blank:nTF}) results in
970 % \cs{tl_head:n} leaving nothing in the input stream.
971 % \begin{texnote}
972 % The result is returned within \cs{exp_not:n}, which means that the token
973 % list does not expand further when appearing in an \texttt{e}-type
974 % or \texttt{x}-type argument expansion.
975 % \end{texnote}
976 % \end{function}
977 %
978 % \begin{function}[EXP]{\tl_head:w}
979 % \begin{syntax}
980 % \cs{tl_head:w} \meta{token list} | { } | \cs{q_stop}
981 % \end{syntax}
982 % Leaves in the input stream the first \meta{item} in the
983 % \meta{token list}, discarding the rest of the \meta{token list}.
984 % All leading explicit space characters
985 % (explicit tokens with character code~$32$ and category code~$10$)
986 % are discarded.
987 % A blank \meta{token list} (which consists only of space characters)
988 % results in a low-level \TeX{} error, which may be avoided by the
989 % inclusion of an empty group in the input (as shown), without the need
990 % for an explicit test. Alternatively, \cs{tl_if_blank:nF} may be used to
991 % avoid using the function with a \enquote{blank} argument.
992 % This function requires only a single expansion, and thus is suitable for
993 % use within an \texttt{o}-type expansion. In general, \cs{tl_head:n} should
994 % be preferred if the number of expansions is not critical.
995 % \end{function}
996 %
997 % \begin{function}[updated = 2012-09-01, EXP]
998 % {\tl_tail:N, \tl_tail:n, \tl_tail:V, \tl_tail:v, \tl_tail:f, \tl_tail:e}
999 % \begin{syntax}
1000 % \cs{tl_tail:n} \Arg{token list}
1001 % \end{syntax}
1002 % Discards all leading explicit space characters
1003 % (explicit tokens with character code~$32$ and category code~$10$)
1004 % and the first \meta{item} in the \meta{token list}, and leaves the
1005 % remaining tokens in the input stream. Thus for example
1006 % \begin{verbatim}
1007 % \tl_tail:n { a ~ {bc} d }
1008 % \end{verbatim}
1009 % and
1010 % \begin{verbatim}
1011 % \tl_tail:n { ~ a ~ {bc} d }
1012 % \end{verbatim}
1013 % both leave \verb*| {bc}d| in the input stream. A blank
1014 % \meta{token list} (see \cs{tl_if_blank:nTF}) results
1015 % in \cs{tl_tail:n} leaving nothing in the input stream.
1016 % \begin{texnote}
1017 % The result is returned within \cs{exp_not:n}, which means that the
1018 % token list does not expand further when appearing in an \texttt{e}-type
1019 % or \texttt{x}-type argument expansion.
1020 % \end{texnote}
1021 % \end{function}
1022 %
1023 % If you wish to handle token lists where the first token may be a space, and
1024 % this needs to be treated as the head/tail, this can be accomplished using
1025 % \cs{tl_if_head_is_space:nTF}, for example
1026 % \begin{verbatim}
1027 % \exp_last_unbraced:NNo
1028 % \cs_new:Npn \__mypkg_gobble_space:w \c_space_tl { }
1029 % \cs_new:Npn \mypkg_tl_head_keep_space:n #1
1030 % {
1031 % \tl_if_head_is_space:nTF {#1}
1032 % { ~ }
1033 % { \tl_head:n {#1} }
1034 % }
1035 % \cs_new:Npn \mypkg_tl_tail_keep_space:n #1
1036 % {
1037 % \tl_if_head_is_space:nTF {#1}
1038 % { \exp_not:o { \__mypkg_gobble_space:w #1 } }
1039 % { \tl_tail:n {#1} }
1040 % }
1041 % \end{verbatim}
1042 %
1043 % \subsection{Items and ranges in token lists}
1044 %
1045 % \begin{function}[added = 2014-07-17, EXP]
1046 % {\tl_item:nn, \tl_item:Nn, \tl_item:cn}
1047 % \begin{syntax}
1048 % \cs{tl_item:nn} \Arg{token list} \Arg{integer expression}
1049 % \end{syntax}
1050 % Indexing items in the \meta{token list} from~$1$ on the left, this
1051 % function evaluates the \meta{integer expression} and leaves the
1052 % appropriate item from the \meta{token list} in the input stream.
1053 % If the \meta{integer expression} is negative, indexing occurs from
1054 % the right of the token list, starting at $-1$ for the right-most item.
1055 % If the index is out of bounds, then the function expands to nothing.
1056 % \begin{texnote}
1057 % The result is returned within the \tn{unexpanded}
1058 % primitive (\cs{exp_not:n}), which means that the \meta{item}
1059 % does not expand further when appearing in an \texttt{e}-type
1060 % or \texttt{x}-type argument expansion.
1061 % \end{texnote}
1062 % \end{function}
1063 %
1064 % \begin{function}[EXP, added = 2016-12-06]
1065 % {\tl_rand_item:N, \tl_rand_item:c, \tl_rand_item:n}
1066 % \begin{syntax}
1067 % \cs{tl_rand_item:N} \meta{tl~var}
1068 % \cs{tl_rand_item:n} \Arg{token list}
1069 % \end{syntax}
1070 % Selects a pseudo-random item of the \meta{token list}. If the
1071 % \meta{token list} is blank, the result is empty.
1072 % \begin{texnote}
1073 % The result is returned within the \tn{unexpanded}
1074 % primitive (\cs{exp_not:n}), which means that the \meta{item}
1075 % does not expand further when appearing in an \texttt{e}-type
1076 % or \texttt{x}-type argument expansion.
1077 % \end{texnote}
1078 % \end{function}
1079 %
1080 % \begin{function}[EXP, added = 2017-02-17, updated = 2017-07-15]
1081 % {\tl_range:Nnn, \tl_range:nnn}
1082 % \begin{syntax}
1083 % \cs{tl_range:Nnn} \meta{tl~var} \Arg{start index} \Arg{end index}
1084 % \cs{tl_range:nnn} \Arg{token list} \Arg{start index} \Arg{end index}
1085 % \end{syntax}
1086 % Leaves in the input stream the items from the \meta{start index} to the
1087 % \meta{end index} inclusive. Spaces and braces are preserved between
1088 % the items returned (but never at either end of the list).
1089 % Here \meta{start index} and \meta{end index} should be \meta{integer expressions}.
1090 % For describing in detail the functions' behavior, let $m$ and $n$ be the start
1091 % and end index respectively. If either is $0$, the result is empty. A positive
1092 % index means `start counting from the left end', and a negative index means
1093 % `from the right end'. Let $l$ be the count of the token list.
1094 %
1095 % The \emph{actual start point} is determined as $M=m$ if~$m>0$ and as $M=l+m+1$
1096 % if~$m<0$. Similarly the \emph{actual end point} is $N=n$ if~$n>0$ and $N=l+n+1$
1097 % if~$n<0$. If $M>N$, the result is empty. Otherwise it consists of all items from
1098 % position $M$ to position $N$ inclusive; for the purpose of this rule, we can
1099 % imagine that the token list extends at infinity on either side, with void items
1100 % at positions $s$ for $s\le0$ or $s>l$.
1101 %
1102 % Spaces in between items in the actual range are preserved. Spaces at either end
1103 % of the token list will be removed anyway (think to the token list being passed to
1104 % |\tl_trim_spaces:n| to begin with.
1105 %
1106 % Thus, with $l=7$ as in the examples below, all of the following are equivalent
1107 % and result in the whole token list
1108 % \begin{verbatim}
1109 % \tl_range:nnn { abcd~{e{}}fg } { 1 } { 7 }
1110 % \tl_range:nnn { abcd~{e{}}fg } { 1 } { 12 }
1111 % \tl_range:nnn { abcd~{e{}}fg } { -7 } { 7 }
1112 % \tl_range:nnn { abcd~{e{}}fg } { -12 } { 7 }
1113 % \end{verbatim}
1114 % Here are some more interesting examples. The calls
1115 % \begin{verbatim}
1116 % \iow_term:e { \tl_range:nnn { abcd{e{}}fg } { 2 } { 5 } }
1117 % \iow_term:e { \tl_range:nnn { abcd{e{}}fg } { 2 } { -3 } }
1118 % \iow_term:e { \tl_range:nnn { abcd{e{}}fg } { -6 } { 5 } }
1119 % \iow_term:e { \tl_range:nnn { abcd{e{}}fg } { -6 } { -3 } }
1120 % \end{verbatim}
1121 % are all equivalent and will print |bcd{e{}}| on the terminal; similarly
1122 % \begin{verbatim}
1123 % \iow_term:e { \tl_range:nnn { abcd~{e{}}fg } { 2 } { 5 } }
1124 % \iow_term:e { \tl_range:nnn { abcd~{e{}}fg } { 2 } { -3 } }
1125 % \iow_term:e { \tl_range:nnn { abcd~{e{}}fg } { -6 } { 5 } }
1126 % \iow_term:e { \tl_range:nnn { abcd~{e{}}fg } { -6 } { -3 } }
1127 % \end{verbatim}
1128 % are all equivalent and will print |bcd {e{}}| on the
1129 % terminal (note the space in the middle). To the contrary,
1130 % \begin{verbatim}
1131 % \tl_range:nnn { abcd~{e{}}f } { 2 } { 4 }
1132 % \end{verbatim}
1133 % will discard the space after `d'.
1134 %
1135 % If we want to get the items from, say, the third to the last in a token
1136 % list |<tl>|, the call
1137 % is |\tl_range:nnn { <tl> } { 3 } { -1 }|. Similarly, for discarding
1138 % the last item, we can do |\tl_range:nnn { <tl> } { 1 } { -2 }|.
1139 %
1140 %^^A The behavior of \cs{tl_range:Nnn} is exactly the same, acting on the
1141 %^^A contents of the tl variable.
1142 %
1143 % \begin{texnote}
1144 % The result is returned within the \tn{unexpanded}
1145 % primitive (\cs{exp_not:n}), which means that the \meta{item}
1146 % does not expand further when appearing in an \texttt{e}-type
1147 % or \texttt{x}-type argument expansion.
1148 % \end{texnote}
1149 % \end{function}
1150 %
1151 % \subsection{Formatting token lists}
1152 %
1153 % \begin{function}[EXP, added = 2025-06-09]{\tl_format:Nn, \tl_format:cn, \tl_format:nn}
1154 % \begin{syntax}
1155 % \cs{tl_format:Nn} \meta{tl~var} \Arg{format specification}
1156 % \cs{tl_format:nn} \Arg{token list} \Arg{format specification}
1157 % \end{syntax}
1158 % Converts the \meta{tl~var} or \meta{token list} to a string according to the
1159 % \meta{format specification}. The \meta{style}, if present, must
1160 % be~|s|. If \meta{precision} is given, all characters of the string
1161 % representation of the \meta{token list} beyond the first
1162 % \meta{precision} characters are discarded. The details of the
1163 % \meta{format specification} are described in
1164 % Section~\ref{sec:l3str-format:spec}.
1165 % \end{function}
1166 %
1167 % \subsection{Sorting token lists}
1168 %
1169 % \begin{function}[added = 2017-02-06]
1170 % {\tl_sort:Nn, \tl_sort:cn, \tl_gsort:Nn, \tl_gsort:cn}
1171 % \begin{syntax}
1172 % \cs{tl_sort:Nn} \meta{tl var} \Arg{comparison code}
1173 % \end{syntax}
1174 % Sorts the items in the \meta{tl var} according to the
1175 % \meta{comparison code}, and assigns the result to
1176 % \meta{tl var}. The details of sorting comparison are
1177 % described in Section~\ref{sec:l3sort:mech}.
1178 % \end{function}
1179 %
1180 % \begin{function}[added = 2017-02-06, EXP]{\tl_sort:nN}
1181 % \begin{syntax}
1182 % \cs{tl_sort:nN} \Arg{token list} \meta{conditional}
1183 % \end{syntax}
1184 % Sorts the items in the \meta{token list}, using the
1185 % \meta{conditional} to compare items, and leaves the result in the
1186 % input stream. The \meta{conditional} should have signature |:nnTF|,
1187 % and return \texttt{true} if the two items being compared should be
1188 % left in the same order, and \texttt{false} if the items should be
1189 % swapped. The details of sorting comparison are
1190 % described in Section~\ref{sec:l3sort:mech}.
1191 % \begin{texnote}
1192 % The result is returned within \cs{exp_not:n}, which means that the
1193 % token list does not expand further when appearing in an
1194 % \texttt{e}-type or \texttt{x}-type argument expansion.
1195 % \end{texnote}
1196 % \end{function}
1197 %
1198 % \section{Manipulating tokens in token lists}
1199 %
1200 % \subsection{Replacing tokens}
1201 %
1202 % Within token lists, replacement takes place at the top level: there is
1203 % no recursion into brace groups (more precisely, within a group defined by
1204 % a category code $1$/$2$ pair).
1205 %
1206 % \begin{function}[updated = 2011-08-11]
1207 % {
1208 % \tl_replace_once:Nnn, \tl_replace_once:NVn, \tl_replace_once:NnV,
1209 % \tl_replace_once:Nen, \tl_replace_once:Nne, \tl_replace_once:Nee,
1210 % \tl_replace_once:cnn, \tl_replace_once:cVn, \tl_replace_once:cnV,
1211 % \tl_replace_once:cen, \tl_replace_once:cne, \tl_replace_once:cee,
1212 % \tl_greplace_once:Nnn, \tl_greplace_once:NVn, \tl_greplace_once:NnV,
1213 % \tl_greplace_once:Nen, \tl_greplace_once:Nne, \tl_greplace_once:Nee,
1214 % \tl_greplace_once:cnn, \tl_greplace_once:cVn, \tl_greplace_once:cnV,
1215 % \tl_greplace_once:cen, \tl_greplace_once:cne, \tl_greplace_once:cee
1216 % }
1217 % \begin{syntax}
1218 % \cs{tl_replace_once:Nnn} \meta{tl~var} \Arg{old tokens} \Arg{new tokens}
1219 % \end{syntax}
1220 % Replaces the first (leftmost) occurrence of \meta{old tokens} in the
1221 % \meta{tl~var} with \meta{new tokens}. \meta{Old tokens}
1222 % cannot contain |{|, |}| or |#|
1223 % (more precisely, explicit character tokens with category code $1$
1224 % (begin-group) or $2$ (end-group), and tokens with category code $6$).
1225 % \end{function}
1226 %
1227 % \begin{function}[updated = 2011-08-11]
1228 % {
1229 % \tl_replace_all:Nnn, \tl_replace_all:NVn, \tl_replace_all:NnV,
1230 % \tl_replace_all:Nen, \tl_replace_all:Nne, \tl_replace_all:Nee,
1231 % \tl_replace_all:cnn, \tl_replace_all:cVn, \tl_replace_all:cnV,
1232 % \tl_replace_all:cen, \tl_replace_all:cne, \tl_replace_all:cee,
1233 % \tl_greplace_all:Nnn, \tl_greplace_all:NVn, \tl_greplace_all:NnV,
1234 % \tl_greplace_all:Nen, \tl_greplace_all:Nne, \tl_greplace_all:Nee,
1235 % \tl_greplace_all:cnn, \tl_greplace_all:cVn, \tl_greplace_all:cnV,
1236 % \tl_greplace_all:cen, \tl_greplace_all:cne, \tl_greplace_all:cee
1237 % }
1238 % \begin{syntax}
1239 % \cs{tl_replace_all:Nnn} \meta{tl~var} \Arg{old tokens} \Arg{new tokens}
1240 % \end{syntax}
1241 % Replaces all occurrences of \meta{old tokens} in the
1242 % \meta{tl~var} with \meta{new tokens}. \meta{Old tokens}
1243 % cannot contain |{|, |}| or |#|
1244 % (more precisely, explicit character tokens with category code $1$
1245 % (begin-group) or $2$ (end-group), and tokens with category code $6$).
1246 % As this function
1247 % operates from left to right, the pattern \meta{old tokens}
1248 % may remain after the replacement (see \cs{tl_remove_all:Nn}
1249 % for an example).
1250 % \end{function}
1251 %
1252 % \begin{function}[added = 2024-12-08]
1253 % {
1254 % \tl_regex_replace_once:Nnn, \tl_regex_replace_once:cnn,
1255 % \tl_regex_replace_once:NNn, \tl_regex_replace_once:cNn,
1256 % \tl_regex_greplace_once:Nnn, \tl_regex_greplace_once:cnn,
1257 % \tl_regex_greplace_once:NNn, \tl_regex_greplace_once:cNn
1258 % }
1259 % \begin{syntax}
1260 % \cs{tl_regex_replace_once:Nnn} \meta{tl~var} \Arg{regex} \Arg{replacement}
1261 % \cs{tl_regex_replace_once:NNn} \meta{tl~var} \meta{regex~var} \Arg{replacement}
1262 % \end{syntax}
1263 % Searches for the \meta{regular expression} in the contents of the
1264 % \meta{tl~var} and replaces the first match with the
1265 % \meta{replacement}. In the \meta{replacement},
1266 % |\0| represents the full match, |\1| represents the contents of the
1267 % first capturing group, |\2| of the second, etc.
1268 % Theses are alternative names for \cs{regex_replace_once:nnN} and friends,
1269 % with arguments re-ordered for \meta{tl~var} setting;
1270 % See \pkg{l3regex} chapter for more details of the \meta{regex}
1271 % format.
1272 % \end{function}
1273 %
1274 % \begin{function}[added = 2024-12-08]
1275 % {
1276 % \tl_regex_replace_all:Nnn, \tl_regex_replace_all:cnn,
1277 % \tl_regex_replace_all:NNn, \tl_regex_replace_all:cNn,
1278 % \tl_regex_greplace_all:Nnn, \tl_regex_greplace_all:cnn,
1279 % \tl_regex_greplace_all:NNn, \tl_regex_greplace_all:cNn
1280 % }
1281 % \begin{syntax}
1282 % \cs{tl_regex_replace_all:Nnn} \meta{tl~var} \Arg{regex} \Arg{replacement}
1283 % \cs{tl_regex_replace_all:NNn} \meta{tl~var} \meta{regex~var} \Arg{replacement}
1284 % \end{syntax}
1285 % Replaces all occurrences of the \meta{regular expression} in the
1286 % contents of the \meta{tl~var}
1287 % by the \meta{replacement}, where |\0| represents
1288 % the full match, |\1| represent the contents of the first capturing
1289 % group, |\2| of the second, etc. Every match is treated
1290 % independently, and matches cannot overlap.
1291 % Theses are alternative names for \cs{regex_replace_all:nnN} and friends,
1292 % with arguments re-ordered for \meta{tl~var} setting;
1293 % see \pkg{l3regex} chapter for more details of the \meta{regex}
1294 % format.
1295 % \end{function}
1296 %
1297 % \begin{function}[updated = 2011-08-11]
1298 % {
1299 % \tl_remove_once:Nn, \tl_remove_once:NV, \tl_remove_once:Ne,
1300 % \tl_remove_once:cn, \tl_remove_once:cV, \tl_remove_once:ce,
1301 % \tl_gremove_once:Nn, \tl_gremove_once:NV, \tl_gremove_once:Ne,
1302 % \tl_gremove_once:cn, \tl_gremove_once:cV, \tl_gremove_once:ce
1303 % }
1304 % \begin{syntax}
1305 % \cs{tl_remove_once:Nn} \meta{tl~var} \Arg{tokens}
1306 % \end{syntax}
1307 % Removes the first (leftmost) occurrence of \meta{tokens} from the
1308 % \meta{tl~var}. The \meta{tokens} cannot contain |{|, |}| or |#|
1309 % (more precisely, explicit character tokens with category code $1$
1310 % (begin-group) or $2$ (end-group), and tokens with category code $6$).
1311 % \end{function}
1312 %
1313 % \begin{function}[updated = 2011-08-11]
1314 % {
1315 % \tl_remove_all:Nn, \tl_remove_all:NV, \tl_remove_all:Ne,
1316 % \tl_remove_all:cn, \tl_remove_all:cV, \tl_remove_all:ce,
1317 % \tl_gremove_all:Nn, \tl_gremove_all:NV, \tl_gremove_all:Ne,
1318 % \tl_gremove_all:cn, \tl_gremove_all:cV, \tl_gremove_all:ce,
1319 % }
1320 % \begin{syntax}
1321 % \cs{tl_remove_all:Nn} \meta{tl~var} \Arg{tokens}
1322 % \end{syntax}
1323 % Removes all occurrences of \meta{tokens} from the
1324 % \meta{tl~var}. The \meta{tokens} cannot contain |{|, |}| or |#|
1325 % (more precisely, explicit character tokens with category code $1$
1326 % (begin-group) or $2$ (end-group), and tokens with category code $6$).
1327 % As this function
1328 % operates from left to right, the pattern \meta{tokens}
1329 % may remain after the removal, for instance,
1330 % \begin{quote}
1331 % \cs{tl_set:Nn} \cs{l_tmpa_tl} |{abbccd}|
1332 % \cs{tl_remove_all:Nn} \cs{l_tmpa_tl} |{bc}|
1333 % \end{quote}
1334 % results in \cs{l_tmpa_tl} containing \texttt{abcd}.
1335 % \end{function}
1336 %
1337 % \subsection{Reassigning category codes}
1338 %
1339 % These functions allow the rescanning of tokens: re-apply \TeX{}'s
1340 % tokenization process to apply category codes different from those
1341 % in force when the tokens were absorbed. Whilst this functionality is
1342 % supported, it is often preferable to find alternative approaches
1343 % to achieving outcomes rather than rescanning tokens (for example
1344 % construction of token lists token-by-token with intervening category
1345 % code changes or using \cs{char_generate:nn}).
1346 %
1347 % \begin{function}[updated = 2015-08-11]
1348 % {
1349 % \tl_set_rescan:Nnn, \tl_set_rescan:NnV, \tl_set_rescan:Nne,
1350 % \tl_set_rescan:Nno,
1351 % \tl_set_rescan:cnn, \tl_set_rescan:cnV, \tl_set_rescan:cne,
1352 % \tl_set_rescan:cno,
1353 % \tl_gset_rescan:Nnn, \tl_gset_rescan:NnV, \tl_gset_rescan:Nne,
1354 % \tl_gset_rescan:Nno,
1355 % \tl_gset_rescan:cnn, \tl_gset_rescan:cnV, \tl_gset_rescan:cne,
1356 % \tl_gset_rescan:cno,
1357 % }
1358 % \begin{syntax}
1359 % \cs{tl_set_rescan:Nnn} \meta{tl~var} \Arg{setup} \Arg{tokens}
1360 % \end{syntax}
1361 % Sets \meta{tl~var} to contain \meta{tokens}, applying the category
1362 % code r\'{e}gime specified in the \meta{setup} before carrying out
1363 % the assignment. (Category codes applied to tokens not explicitly covered
1364 % by the \meta{setup} are those in force at the point of use of
1365 % \cs{tl_set_rescan:Nnn}.)
1366 % This allows the \meta{tl~var} to contain material
1367 % with category codes other than those that apply when \meta{tokens}
1368 % are absorbed. The \meta{setup} is run within a group and may
1369 % contain any valid input, although only changes in category codes,
1370 % such as uses of \cs{cctab_select:N},
1371 % are relevant. See also \cs{tl_rescan:nn}.
1372 % \begin{texnote}
1373 % The \meta{tokens} are first turned into a string (using
1374 % \cs{tl_to_str:n}). If the string contains one or more characters
1375 % with character code \tn{newlinechar} (set equal to
1376 % \tn{endlinechar} unless that is equal to $32$, before the user
1377 % \meta{setup}), then it is split into lines at these characters,
1378 % then read as if reading multiple lines from a file, ignoring
1379 % spaces (catcode $10$) at the beginning and spaces and tabs
1380 % (character code $32$ or $9$) at the end of every line.
1381 % Otherwise, spaces (and tabs) are retained at both ends of the
1382 % single-line string, as if it appeared in the middle of a line
1383 % read from a file.
1384 % \end{texnote}
1385 % \end{function}
1386 %
1387 % \begin{function}[updated = 2015-08-11]{\tl_rescan:nn, \tl_rescan:nV}
1388 % \begin{syntax}
1389 % \cs{tl_rescan:nn} \Arg{setup} \Arg{tokens}
1390 % \end{syntax}
1391 % Rescans \meta{tokens} applying the category code r\'{e}gime
1392 % specified in the \meta{setup}, and leaves the resulting tokens in
1393 % the input stream. (Category codes applied to tokens not explicitly covered
1394 % by the \meta{setup} are those in force at the point of use of
1395 % \cs{tl_rescan:nn}.)
1396 % The \meta{setup} is run within a group and may
1397 % contain any valid input, although only changes in category codes,
1398 % such as uses of \cs{cctab_select:N},
1399 % are relevant. See also \cs{tl_set_rescan:Nnn}, which is more
1400 % robust than using \cs{tl_set:Nn} in the \meta{tokens} argument of
1401 % \cs{tl_rescan:nn}.
1402 % \begin{texnote}
1403 % The \meta{tokens} are first turned into a string (using
1404 % \cs{tl_to_str:n}). If the string contains one or more characters
1405 % with character code \tn{newlinechar} (set equal to
1406 % \tn{endlinechar} unless that is equal to $32$, before the user
1407 % \meta{setup}), then it is split into lines at these characters,
1408 % then read as if reading multiple lines from a file, ignoring
1409 % spaces (catcode $10$) at the beginning and spaces and tabs
1410 % (character code $32$ or $9$) at the end of every line.
1411 % Otherwise, spaces (and tabs) are retained at both ends of the
1412 % single-line string, as if it appeared in the middle of a line
1413 % read from a file.
1414 %
1415 % Contrarily to the \tn{scantokens} \eTeX{} primitive, \cs{tl_rescan:nn}
1416 % tokenizes the whole string in the same category code régime rather
1417 % than one token at a time, so that directives such as \tn{verb}
1418 % that rely on changing category codes will not function properly.
1419 % \end{texnote}
1420 % \end{function}
1421 %
1422 % \begin{function}[added = 2025-07-08]{\tl_retokenize:n, \tl_retokenize:V}
1423 % \begin{syntax}
1424 % \cs{tl_retokenize:n} \Arg{tokens}
1425 % \end{syntax}
1426 % Retokenizes the \meta{tokens} by applying the current category code
1427 % régime. In contrast to \cs{tl_rescan:nn}, this function executes the
1428 % \meta{tokens} as the category codes are reassigned to the tokens. As such, any
1429 % category code changes within the \meta{tokens} \emph{will} apply to
1430 % later content. The \emph{tokens} are always treated as ending with
1431 % a space. Within the \meta{tokens}, the character |^^J| should be used
1432 % to represent line breaks.
1433 % \begin{texnote}
1434 % This is a thin wrapper around the \tn{scantokens} primitive. In
1435 % addition to the primitive behavior, it detokenizes the
1436 % input and resets the value of \tn{endlinechar} to~$13$ (i.e.~|^^M|)
1437 % and \tn{newlinechar} to $10$ (i.e.~|^^J|).
1438 % \end{texnote}
1439 % \end{function}
1440 %
1441 % \section{Constant token lists}
1442 %
1443 % \begin{variable}{\c_empty_tl}
1444 % Constant that is always empty.
1445 % \end{variable}
1446 %
1447 % \begin{variable}[added = 2017-11-14]{\c_novalue_tl}
1448 % A marker for the absence of an argument. This constant |tl| can safely
1449 % be typeset (\emph{cf.}~\cs{q_nil}), with the result being |-NoValue-|.
1450 % It is important to note that \cs{c_novalue_tl} is constructed such that it
1451 % will \emph{not} match the simple text input |-NoValue-|, i.e.
1452 % that
1453 % \begin{verbatim}
1454 % \tl_if_eq:NnTF \c_novalue_tl { -NoValue- }
1455 % \end{verbatim}
1456 % is logically \texttt{false}. The \cs{c_novalue_tl} marker is intended for
1457 % use in creating document-level interfaces, where it serves as an indicator
1458 % that an (optional) argument was omitted. In particular, it is distinct
1459 % from a simple empty |tl|.
1460 % \end{variable}
1461 %
1462 % \begin{variable}{\c_space_tl}
1463 % An explicit space character contained in a token list (compare this with
1464 % \cs{c_space_token}). For use where an explicit space is required.
1465 % \end{variable}
1466 %
1467 % \section{Scratch token lists}
1468 %
1469 % \begin{variable}{\l_tmpa_tl, \l_tmpb_tl}
1470 % Scratch token lists for local assignment. These are never used by
1471 % the kernel code, and so are safe for use with any \LaTeX3-defined
1472 % function. However, they may be overwritten by other non-kernel
1473 % code and so should only be used for short-term storage.
1474 % \end{variable}
1475 %
1476 % \begin{variable}{\g_tmpa_tl, \g_tmpb_tl}
1477 % Scratch token lists for global assignment. These are never used by
1478 % the kernel code, and so are safe for use with any \LaTeX3-defined
1479 % function. However, they may be overwritten by other non-kernel
1480 % code and so should only be used for short-term storage.
1481 % \end{variable}
1482 %
1483 % \end{documentation}
1484 %
1485 % \begin{implementation}
1486 %
1487 % \section{\pkg{l3tl} implementation}
1488 %
1489 % \begin{macrocode}
1490 %<*code>
1491 % \end{macrocode}
1492 %
1493 % \begin{macrocode}
1494 %<@@=tl>
1495 % \end{macrocode}
1496 %
1497 % A token list variable is a \TeX{} macro that holds tokens. By using the
1498 % \eTeX{} primitive \tn{unexpanded} inside a \TeX{} \tn{edef} it is
1499 % possible to store any tokens, including |#|, in this way.
1500 %
1501 % \subsection{Functions}
1502 %
1503 % \begin{macro}{\__kernel_tl_set:Nx,\__kernel_tl_gset:Nx}
1504 % These two are supplied to get better performance for macros which would
1505 % otherwise use \cs{tl_set:Ne} or \cs{tl_gset:Ne} internally.
1506 % \begin{macrocode}
1507 \cs_new_eq:NN \__kernel_tl_set:Nx \cs_set_nopar:Npe
1508 \cs_new_eq:NN \__kernel_tl_gset:Nx \cs_gset_nopar:Npe
1509 % \end{macrocode}
1510 % \end{macro}
1511 %
1512 % \begin{macro}{\tl_new:N, \tl_new:c}
1513 % Creating new token list variables is a case of checking for an
1514 % existing definition and doing the definition.
1515 % \begin{macrocode}
1516 \cs_new_protected:Npn \tl_new:N #1
1517 {
1518 \__kernel_chk_if_free_cs:N #1
1519 \cs_gset_eq:NN #1 \c_empty_tl
1520 }
1521 \cs_generate_variant:Nn \tl_new:N { c }
1522 % \end{macrocode}
1523 % \end{macro}
1524 %
1525 % \begin{macro}
1526 % {
1527 % \tl_const:Nn, \tl_const:NV, \tl_const:Ne, \tl_const:Nx,
1528 % \tl_const:cn, \tl_const:cV, \tl_const:ce, \tl_const:cx
1529 % }
1530 % Constants are also easy to generate. They use \cs{cs_gset_nopar:Npe} instead
1531 % of \cs{__kernel_tl_gset:Nx} so that the correct scope checking for |c|,
1532 % instead of for |g|, is applied when
1533 % \cs{debug_on:n} |{ check-declarations }| is used.
1534 % Constant assignment functions are patched specially in \pkg{l3debug} to
1535 % apply such checks.
1536 % \begin{macrocode}
1537 \cs_new_protected:Npn \tl_const:Nn #1#2
1538 {
1539 \__kernel_chk_if_free_cs:N #1
1540 \cs_gset_nopar:Npe #1 { \__kernel_exp_not:w {#2} }
1541 }
1542 \cs_generate_variant:Nn \tl_const:Nn { NV , Ne , c , cV , ce }
1543 \cs_generate_variant:Nn \tl_const:Nn { Nx , cx }
1544 % \end{macrocode}
1545 % \end{macro}
1546 %
1547 % \begin{macro}{\tl_clear:N, \tl_clear:c}
1548 % \begin{macro}{\tl_gclear:N, \tl_gclear:c}
1549 % Clearing a token list variable means setting it to an empty value.
1550 % Error checking is sorted out by the parent function.
1551 % \begin{macrocode}
1552 \cs_new_protected:Npn \tl_clear:N #1
1553 { \tex_let:D #1 = ~ \c_empty_tl }
1554 \cs_new_protected:Npn \tl_gclear:N #1
1555 { \tex_global:D \tex_let:D #1 ~ \c_empty_tl }
1556 \cs_generate_variant:Nn \tl_clear:N { c }
1557 \cs_generate_variant:Nn \tl_gclear:N { c }
1558 % \end{macrocode}
1559 % \end{macro}
1560 % \end{macro}
1561 %
1562 % \begin{macro}{\tl_clear_new:N, \tl_clear_new:c}
1563 % \begin{macro}{\tl_gclear_new:N, \tl_gclear_new:c}
1564 % Clearing a token list variable means setting it to an empty value.
1565 % Error checking is sorted out by the parent function.
1566 % \begin{macrocode}
1567 \cs_new_protected:Npn \tl_clear_new:N #1
1568 { \tl_if_exist:NTF #1 { \tl_clear:N #1 } { \tl_new:N #1 } }
1569 \cs_new_protected:Npn \tl_gclear_new:N #1
1570 { \tl_if_exist:NTF #1 { \tl_gclear:N #1 } { \tl_new:N #1 } }
1571 \cs_generate_variant:Nn \tl_clear_new:N { c }
1572 \cs_generate_variant:Nn \tl_gclear_new:N { c }
1573 % \end{macrocode}
1574 % \end{macro}
1575 % \end{macro}
1576 %
1577 % \begin{macro}{\tl_set_eq:NN, \tl_set_eq:Nc, \tl_set_eq:cN, \tl_set_eq:cc}
1578 % \begin{macro}{\tl_gset_eq:NN, \tl_gset_eq:Nc, \tl_gset_eq:cN, \tl_gset_eq:cc}
1579 % For setting token list variables equal to each other. To allow for
1580 % patching, the arguments have to be explicit. In addition this
1581 % ensures that a braced second argument will not cause problems.
1582 % \begin{macrocode}
1583 \cs_new_protected:Npn \tl_set_eq:NN #1#2
1584 { \tex_let:D #1 = ~ #2 }
1585 \cs_new_protected:Npn \tl_gset_eq:NN #1#2
1586 { \tex_global:D \tex_let:D #1 = ~ #2 }
1587 \cs_generate_variant:Nn \tl_set_eq:NN { cN, Nc, cc }
1588 \cs_generate_variant:Nn \tl_gset_eq:NN { cN, Nc, cc }
1589 % \end{macrocode}
1590 % \end{macro}
1591 % \end{macro}
1592 %
1593 % \begin{macro}{\tl_concat:NNN, \tl_concat:ccc}
1594 % \begin{macro}{\tl_gconcat:NNN, \tl_gconcat:ccc}
1595 % Concatenating token lists is easy. When checking is turned on, all
1596 % three arguments must be checked: a token list |#2| or |#3| equal to
1597 % \cs{scan_stop:} would lead to problems later on.
1598 % \begin{macrocode}
1599 \cs_new_protected:Npn \tl_concat:NNN #1#2#3
1600 {
1601 \__kernel_tl_set:Nx #1
1602 {
1603 \__kernel_exp_not:w \exp_after:wN {#2}
1604 \__kernel_exp_not:w \exp_after:wN {#3}
1605 }
1606 }
1607 \cs_new_protected:Npn \tl_gconcat:NNN #1#2#3
1608 {
1609 \__kernel_tl_gset:Nx #1
1610 {
1611 \__kernel_exp_not:w \exp_after:wN {#2}
1612 \__kernel_exp_not:w \exp_after:wN {#3}
1613 }
1614 }
1615 \cs_generate_variant:Nn \tl_concat:NNN { ccc }
1616 \cs_generate_variant:Nn \tl_gconcat:NNN { ccc }
1617 % \end{macrocode}
1618 % \end{macro}
1619 % \end{macro}
1620 %
1621 % \begin{macro}[pTF]{\tl_if_exist:N, \tl_if_exist:c}
1622 % Copies of the \texttt{cs} functions defined in \pkg{l3basics}.
1623 % \begin{macrocode}
1624 \prg_new_eq_conditional:NNn \tl_if_exist:N \cs_if_exist:N { TF , T , F , p }
1625 \prg_new_eq_conditional:NNn \tl_if_exist:c \cs_if_exist:c { TF , T , F , p }
1626 % \end{macrocode}
1627 % \end{macro}
1628 %
1629 % \subsection{Constant token lists}
1630 %
1631 % \begin{variable}{\c_empty_tl}
1632 % Never full. We need to define that constant before using \cs{tl_new:N}.
1633 % \begin{macrocode}
1634 \tl_const:Nn \c_empty_tl { }
1635 % \end{macrocode}
1636 % \end{variable}
1637 %
1638 % \begin{variable}{\c_novalue_tl}
1639 % A special marker: as we don't have |\char_generate:nn| yet, has to be
1640 % created the old-fashioned way.
1641 % \begin{macrocode}
1642 \group_begin:
1643 \tex_catcode:D `- = 11 ~
1644 \tl_const:Ne \c_novalue_tl { - NoValue \token_to_str:N - }
1645 \group_end:
1646 % \end{macrocode}
1647 % \end{variable}
1648 %
1649 % \begin{variable}{\c_space_tl}
1650 % A space as a token list (as opposed to as a character).
1651 % \begin{macrocode}
1652 \tl_const:Nn \c_space_tl { ~ }
1653 % \end{macrocode}
1654 % \end{variable}
1655 %
1656 % \subsection{Adding to token list variables}
1657 %
1658 % \begin{macro}
1659 % {
1660 % \tl_set:Nn, \tl_set:NV, \tl_set:Nv, \tl_set:No, \tl_set:Ne, \tl_set:Nf, \tl_set:Nx,
1661 % \tl_set:cn, \tl_set:cV, \tl_set:cv, \tl_set:co, \tl_set:ce, \tl_set:cf, \tl_set:cx
1662 % }
1663 % \begin{macro}
1664 % {
1665 % \tl_gset:Nn, \tl_gset:NV, \tl_gset:Nv,
1666 % \tl_gset:No, \tl_gset:Ne, \tl_gset:Nf, \tl_gset:Nx,
1667 % \tl_gset:cn, \tl_gset:cV, \tl_gset:cv,
1668 % \tl_gset:co, \tl_gset:ce, \tl_gset:cf, \tl_gset:cx
1669 % }
1670 % By using \cs{exp_not:n} token list variables can contain |#| tokens,
1671 % which makes the token list registers provided by \TeX{}
1672 % more or less redundant. The \cs{tl_set:No} version is done
1673 % by hand as it is used quite a lot.
1674 % \begin{macrocode}
1675 \cs_new_protected:Npn \tl_set:Nn #1#2
1676 { \__kernel_tl_set:Nx #1 { \__kernel_exp_not:w {#2} } }
1677 \cs_new_protected:Npn \tl_set:No #1#2
1678 { \__kernel_tl_set:Nx #1 { \__kernel_exp_not:w \exp_after:wN {#2} } }
1679 \cs_new_protected:Npn \tl_gset:Nn #1#2
1680 { \__kernel_tl_gset:Nx #1 { \__kernel_exp_not:w {#2} } }
1681 \cs_new_protected:Npn \tl_gset:No #1#2
1682 { \__kernel_tl_gset:Nx #1 { \__kernel_exp_not:w \exp_after:wN {#2} } }
1683 \cs_generate_variant:Nn \tl_set:Nn { NV , Nv , Ne , Nf }
1684 \cs_generate_variant:Nn \tl_set:Nn { c, cV , cv , ce , cf }
1685 \cs_generate_variant:Nn \tl_set:No { c }
1686 \cs_generate_variant:Nn \tl_set:Nn { Nx , cx }
1687 \cs_generate_variant:Nn \tl_gset:Nn { NV , Nv , Ne , Nf }
1688 \cs_generate_variant:Nn \tl_gset:Nn { c, cV , cv , ce , cf }
1689 \cs_generate_variant:Nn \tl_gset:No { c }
1690 \cs_generate_variant:Nn \tl_gset:Nn { Nx , cx }
1691 % \end{macrocode}
1692 % \end{macro}
1693 % \end{macro}
1694 %
1695 % \begin{macro}
1696 % {
1697 % \tl_put_left:Nn, \tl_put_left:NV, \tl_put_left:Nv, \tl_put_left:Ne,
1698 % \tl_put_left:No, \tl_put_left:Nx,
1699 % \tl_put_left:cn, \tl_put_left:cV, \tl_put_left:cv, \tl_put_left:ce,
1700 % \tl_put_left:co, \tl_put_left:cx
1701 % }
1702 % \begin{macro}
1703 % {
1704 % \tl_gput_left:Nn, \tl_gput_left:NV, \tl_gput_left:Nv, \tl_gput_left:Ne,
1705 % \tl_gput_left:No, \tl_gput_left:Nx,
1706 % \tl_gput_left:cn, \tl_gput_left:cV, \tl_gput_left:cv, \tl_gput_left:ce,
1707 % \tl_gput_left:co, \tl_gput_left:cx
1708 % }
1709 % Adding to the left is done directly to gain a little performance.
1710 % \begin{macrocode}
1711 \cs_new_protected:Npn \tl_put_left:Nn #1#2
1712 {
1713 \__kernel_tl_set:Nx #1
1714 { \__kernel_exp_not:w {#2} \__kernel_exp_not:w \exp_after:wN {#1} }
1715 }
1716 \cs_new_protected:Npn \tl_put_left:NV #1#2
1717 {
1718 \__kernel_tl_set:Nx #1
1719 { \exp_not:V #2 \__kernel_exp_not:w \exp_after:wN {#1} }
1720 }
1721 \cs_new_protected:Npn \tl_put_left:Nv #1#2
1722 {
1723 \__kernel_tl_set:Nx #1
1724 { \exp_not:v {#2} \__kernel_exp_not:w \exp_after:wN {#1} }
1725 }
1726 \cs_new_protected:Npn \tl_put_left:Ne #1#2
1727 {
1728 \__kernel_tl_set:Nx #1
1729 {
1730 \__kernel_exp_not:w \tex_expanded:D { {#2} }
1731 \__kernel_exp_not:w \exp_after:wN {#1}
1732 }
1733 }
1734 \cs_new_protected:Npn \tl_put_left:No #1#2
1735 {
1736 \__kernel_tl_set:Nx #1
1737 {
1738 \__kernel_exp_not:w \exp_after:wN {#2}
1739 \__kernel_exp_not:w \exp_after:wN {#1}
1740 }
1741 }
1742 \cs_new_protected:Npn \tl_gput_left:Nn #1#2
1743 {
1744 \__kernel_tl_gset:Nx #1
1745 { \__kernel_exp_not:w {#2} \__kernel_exp_not:w \exp_after:wN {#1} }
1746 }
1747 \cs_new_protected:Npn \tl_gput_left:NV #1#2
1748 {
1749 \__kernel_tl_gset:Nx #1
1750 { \exp_not:V #2 \__kernel_exp_not:w \exp_after:wN {#1} }
1751 }
1752 \cs_new_protected:Npn \tl_gput_left:Nv #1#2
1753 {
1754 \__kernel_tl_gset:Nx #1
1755 { \exp_not:v {#2} \__kernel_exp_not:w \exp_after:wN {#1} }
1756 }
1757 \cs_new_protected:Npn \tl_gput_left:Ne #1#2
1758 {
1759 \__kernel_tl_gset:Nx #1
1760 {
1761 \__kernel_exp_not:w \tex_expanded:D { {#2} }
1762 \__kernel_exp_not:w \exp_after:wN {#1}
1763 }
1764 }
1765 \cs_new_protected:Npn \tl_gput_left:No #1#2
1766 {
1767 \__kernel_tl_gset:Nx #1
1768 {
1769 \__kernel_exp_not:w \exp_after:wN {#2}
1770 \__kernel_exp_not:w \exp_after:wN {#1}
1771 }
1772 }
1773 \cs_generate_variant:Nn \tl_put_left:Nn { c }
1774 \cs_generate_variant:Nn \tl_put_left:NV { c }
1775 \cs_generate_variant:Nn \tl_put_left:Nv { c }
1776 \cs_generate_variant:Nn \tl_put_left:Ne { c }
1777 \cs_generate_variant:Nn \tl_put_left:No { c }
1778 \cs_generate_variant:Nn \tl_put_left:Nn { Nx, cx }
1779 \cs_generate_variant:Nn \tl_gput_left:Nn { c }
1780 \cs_generate_variant:Nn \tl_gput_left:NV { c }
1781 \cs_generate_variant:Nn \tl_gput_left:Nv { c }
1782 \cs_generate_variant:Nn \tl_gput_left:Ne { c }
1783 \cs_generate_variant:Nn \tl_gput_left:No { c }
1784 \cs_generate_variant:Nn \tl_gput_left:Nn { Nx , cx }
1785 % \end{macrocode}
1786 % \end{macro}
1787 % \end{macro}
1788 %
1789 % \begin{macro}
1790 % {
1791 % \tl_put_right:Nn, \tl_put_right:NV, \tl_put_right:Nv, \tl_put_right:Ne,
1792 % \tl_put_right:No, \tl_put_right:Nx,
1793 % \tl_put_right:cn, \tl_put_right:cV, \tl_put_right:cv, \tl_put_right:ce,
1794 % \tl_put_right:co, \tl_put_right:cx
1795 % }
1796 % \begin{macro}
1797 % {
1798 % \tl_gput_right:Nn, \tl_gput_right:NV, \tl_gput_right:Nv, \tl_gput_right:Ne,
1799 % \tl_gput_right:No, \tl_gput_right:Nx,
1800 % \tl_gput_right:cn, \tl_gput_right:cV, \tl_gput_right:cv, \tl_gput_right:ce,
1801 % \tl_gput_right:co, \tl_gput_right:cx
1802 % }
1803 % The same on the right.
1804 % \begin{macrocode}
1805 \cs_new_protected:Npn \tl_put_right:Nn #1#2
1806 { \__kernel_tl_set:Nx #1 { \__kernel_exp_not:w \exp_after:wN { #1 #2 } } }
1807 \cs_new_protected:Npn \tl_put_right:NV #1#2
1808 {
1809 \__kernel_tl_set:Nx #1
1810 { \__kernel_exp_not:w \exp_after:wN {#1} \exp_not:V #2 }
1811 }
1812 \cs_new_protected:Npn \tl_put_right:Nv #1#2
1813 {
1814 \__kernel_tl_set:Nx #1
1815 { \__kernel_exp_not:w \exp_after:wN {#1} \exp_not:v {#2} }
1816 }
1817 \cs_new_protected:Npn \tl_put_right:Ne #1#2
1818 {
1819 \__kernel_tl_set:Nx #1
1820 {
1821 \__kernel_exp_not:w \exp_after:wN {#1}
1822 \__kernel_exp_not:w \tex_expanded:D { {#2} }
1823 }
1824 }
1825 \cs_new_protected:Npn \tl_put_right:No #1#2
1826 {
1827 \__kernel_tl_set:Nx #1
1828 {
1829 \__kernel_exp_not:w \exp_after:wN {#1}
1830 \__kernel_exp_not:w \exp_after:wN {#2}
1831 }
1832 }
1833 \cs_new_protected:Npn \tl_gput_right:Nn #1#2
1834 { \__kernel_tl_gset:Nx #1 { \__kernel_exp_not:w \exp_after:wN { #1 #2 } } }
1835 \cs_new_protected:Npn \tl_gput_right:NV #1#2
1836 {
1837 \__kernel_tl_gset:Nx #1
1838 { \__kernel_exp_not:w \exp_after:wN {#1} \exp_not:V #2 }
1839 }
1840 \cs_new_protected:Npn \tl_gput_right:Nv #1#2
1841 {
1842 \__kernel_tl_gset:Nx #1
1843 { \__kernel_exp_not:w \exp_after:wN {#1} \exp_not:v {#2} }
1844 }
1845 \cs_new_protected:Npn \tl_gput_right:Ne #1#2
1846 {
1847 \__kernel_tl_gset:Nx #1
1848 {
1849 \__kernel_exp_not:w \exp_after:wN {#1}
1850 \__kernel_exp_not:w \tex_expanded:D { {#2} }
1851 }
1852 }
1853 \cs_new_protected:Npn \tl_gput_right:No #1#2
1854 {
1855 \__kernel_tl_gset:Nx #1
1856 {
1857 \__kernel_exp_not:w \exp_after:wN {#1}
1858 \__kernel_exp_not:w \exp_after:wN {#2}
1859 }
1860 }
1861 \cs_generate_variant:Nn \tl_put_right:Nn { c }
1862 \cs_generate_variant:Nn \tl_put_right:NV { c }
1863 \cs_generate_variant:Nn \tl_put_right:Nv { c }
1864 \cs_generate_variant:Nn \tl_put_right:Ne { c }
1865 \cs_generate_variant:Nn \tl_put_right:No { c }
1866 \cs_generate_variant:Nn \tl_put_right:Nn { Nx , cx }
1867 \cs_generate_variant:Nn \tl_gput_right:Nn { c }
1868 \cs_generate_variant:Nn \tl_gput_right:NV { c }
1869 \cs_generate_variant:Nn \tl_gput_right:Nv { c }
1870 \cs_generate_variant:Nn \tl_gput_right:Ne { c }
1871 \cs_generate_variant:Nn \tl_gput_right:No { c }
1872 \cs_generate_variant:Nn \tl_gput_right:Nn { Nx, cx }
1873 % \end{macrocode}
1874 % \end{macro}
1875 % \end{macro}
1876 %
1877 % \subsection{Internal quarks and quark-query functions}
1878 %
1879 % \begin{variable}{\q_@@_nil,\q_@@_mark,\q_@@_stop}
1880 % Internal quarks.
1881 % \begin{macrocode}
1882 \quark_new:N \q_@@_nil
1883 \quark_new:N \q_@@_mark
1884 \quark_new:N \q_@@_stop
1885 % \end{macrocode}
1886 % \end{variable}
1887 %
1888 % \begin{variable}{\q_@@_recursion_tail,\q_@@_recursion_stop}
1889 % Internal recursion quarks.
1890 % \begin{macrocode}
1891 \quark_new:N \q_@@_recursion_tail
1892 \quark_new:N \q_@@_recursion_stop
1893 % \end{macrocode}
1894 % \end{variable}
1895 %
1896 % \begin{macro}[EXP]{\@@_if_recursion_tail_break:nN}
1897 % \begin{macro}[pTF]{\@@_if_recursion_tail_stop:n}
1898 % Functions to query recursion quarks.
1899 % \begin{macrocode}
1900 \__kernel_quark_new_test:N \@@_if_recursion_tail_break:nN
1901 \__kernel_quark_new_conditional:Nn \@@_quark_if_nil:n { TF }
1902 % \end{macrocode}
1903 % \end{macro}
1904 % \end{macro}
1905 %
1906 % \subsection{Reassigning token list category codes}
1907 %
1908 % \begin{variable}{\c_@@_rescan_marker_tl}
1909 % The rescanning code needs a special token list containing the same
1910 % character (chosen here to be a colon) with two different category
1911 % codes: it cannot appear in the tokens being rescanned since all
1912 % colons have the same category code.
1913 % \begin{macrocode}
1914 \tl_const:Ne \c_@@_rescan_marker_tl { : \token_to_str:N : }
1915 % \end{macrocode}
1916 % \end{variable}
1917 %
1918 % \begin{macro}
1919 % {
1920 % \tl_set_rescan:Nnn, \tl_set_rescan:NnV, \tl_set_rescan:Nne,
1921 % \tl_set_rescan:Nno, \tl_set_rescan:Nnx,
1922 % \tl_set_rescan:cnn, \tl_set_rescan:cnV, \tl_set_rescan:cne,
1923 % \tl_set_rescan:cno, \tl_set_rescan:cnx,
1924 % }
1925 % \begin{macro}
1926 % {
1927 % \tl_gset_rescan:Nnn, \tl_gset_rescan:NnV, \tl_gset_rescan:Nne,
1928 % \tl_gset_rescan:Nno, \tl_gset_rescan:Nnx,
1929 % \tl_gset_rescan:cnn, \tl_gset_rescan:cnV, \tl_gset_rescan:cne,
1930 % \tl_gset_rescan:cno, \tl_gset_rescan:cnx
1931 % }
1932 % \begin{macro}{\tl_rescan:nn, \tl_rescan:nV}
1933 % \begin{macro}{\@@_rescan_aux:}
1934 % \begin{macro}{\@@_set_rescan:NNnn, \@@_set_rescan_multi:nNN}
1935 % \begin{macro}[EXP]{\@@_rescan:NNw}
1936 % In a group, after some initial setup explained below and the user
1937 % setup~|#3| (followed by \cs{scan_stop:} to be safe), there is a call
1938 % to \cs{@@_set_rescan:nNN}. This shared auxiliary defined later
1939 % distinguishes single-line and multi-line ``files''. In the simplest
1940 % case of multi-line files, it calls (with the same arguments)
1941 % \cs{@@_set_rescan_multi:nNN}, whose code is included here to help
1942 % understand the approach. This function rescans its argument |#1|,
1943 % closes the group, and performs the assignment.
1944 %
1945 % One difficulty when rescanning is that \tn{scantokens} treats the
1946 % argument as a file, and without the correct settings a \TeX{} error
1947 % occurs:
1948 % \begin{verbatim}
1949 % ! File ended while scanning definition of ...
1950 % \end{verbatim}
1951 % A related minor issue is a warning due to opening a group before the
1952 % \tn{scantokens} and closing it inside that temporary file; we avoid
1953 % that by setting \tn{tracingnesting}. The standard solution to the
1954 % ``File ended'' error is to grab the rescanned tokens as a delimited
1955 % argument of an auxiliary, here \cs{@@_rescan:NNw}, that performs the
1956 % assignment, then let \TeX{} ``execute'' the end of file marker. As
1957 % usual in delimited arguments we use \cs{prg_do_nothing:} to avoid
1958 % stripping an outer set braces: this is removed by using
1959 % \texttt{o}-expanding assignments. The delimiter cannot appear
1960 % within the rescanned token list because it contains twice the same
1961 % character, with different catcodes.
1962 %
1963 % For \cs{tl_rescan:nn} we cannot simply call \cs{@@_set_rescan:NNnn}
1964 % \cs{prg_do_nothing:} \cs{use:n} because that would leave the
1965 % end-of-file marker \emph{after} the result of rescanning. If that
1966 % rescanned result is code that looks further in the input stream for
1967 % arguments, it would break.
1968 %
1969 % For multi-line files the only subtlety is that \tn{newlinechar}
1970 % should be equal to \tn{endlinechar} because \tn{newlinechar}
1971 % characters become new lines and then become \tn{endlinechar}
1972 % characters when writing to an abstract file and reading back. This
1973 % equality is ensured by setting \tn{newlinechar} equal to
1974 % \tn{endlinechar}. Prior to this, \tn{endlinechar} is set to $-1$ if
1975 % it was $32$ (in particular true after \cs{ExplSyntaxOn}) to avoid
1976 % unreasonable line-breaks at every space for instance in error
1977 % messages triggered by the user setup. Another side effect of
1978 % reading back from the file is that spaces (catcode $10$) are ignored
1979 % at the beginning of lines, and spaces and tabs (character code $32$
1980 % and $9$) are ignored at the end of lines.
1981 %
1982 % The two \cs{if_false:} \ldots{} \cs{fi:} are there to prevent
1983 % alignment tabs to cause a change of tabular cell while rescanning.
1984 % We put the \enquote{opening} one after \cs{group_begin:} so that if
1985 % one accidentally \texttt{f}-expands \cs{tl_set_rescan:Nnn} braces
1986 % remain balanced. This is essential in \texttt{e}-type arguments
1987 % when \tn{expanded} is not available.
1988 % \begin{macrocode}
1989 \cs_new_protected:Npn \tl_rescan:nn #1#2
1990 {
1991 \tl_set_rescan:Nnn \l_@@_tmpa_tl {#1} {#2}
1992 \exp_after:wN \@@_rescan_aux:
1993 \l_@@_tmpa_tl
1994 }
1995 \cs_generate_variant:Nn \tl_rescan:nn { nV }
1996 \exp_args:NNo \cs_new_protected:Npn \@@_rescan_aux:
1997 { \tl_clear:N \l_@@_tmpa_tl }
1998 \cs_new_protected:Npn \tl_set_rescan:Nnn
1999 { \@@_set_rescan:NNnn \tl_set:No }
2000 \cs_new_protected:Npn \tl_gset_rescan:Nnn
2001 { \@@_set_rescan:NNnn \tl_gset:No }
2002 \cs_new_protected:Npn \@@_set_rescan:NNnn #1#2#3#4
2003 {
2004 \group_begin:
2005 \if_false: { \fi:
2006 \int_set_eq:NN \tex_tracingnesting:D \c_zero_int
2007 \int_compare:nNnT \tex_endlinechar:D = { 32 }
2008 { \int_set:Nn \tex_endlinechar:D { -1 } }
2009 \int_set_eq:NN \tex_newlinechar:D \tex_endlinechar:D
2010 #3 \scan_stop:
2011 \exp_args:No \@@_set_rescan:nNN { \tl_to_str:n {#4} } #1 #2
2012 \if_false: } \fi:
2013 }
2014 \cs_new_protected:Npn \@@_set_rescan_multi:nNN #1#2#3
2015 {
2016 \tex_everyeof:D \exp_after:wN { \c_@@_rescan_marker_tl }
2017 \exp_after:wN \@@_rescan:NNw
2018 \exp_after:wN #2
2019 \exp_after:wN #3
2020 \exp_after:wN \prg_do_nothing:
2021 \tex_scantokens:D {#1}
2022 }
2023 \exp_args:Nno \use:nn
2024 { \cs_new:Npn \@@_rescan:NNw #1#2#3 } \c_@@_rescan_marker_tl
2025 {
2026 \group_end:
2027 #1 #2 {#3}
2028 }
2029 \cs_generate_variant:Nn \tl_set_rescan:Nnn { NnV , Nne , c , cnV , cne }
2030 \cs_generate_variant:Nn \tl_set_rescan:Nnn { Nno , Nnx , cno , cnx }
2031 \cs_generate_variant:Nn \tl_gset_rescan:Nnn { NnV , Nne , c , cnV , cne }
2032 \cs_generate_variant:Nn \tl_gset_rescan:Nnn { Nno , Nnx , cno , cnx }
2033 % \end{macrocode}
2034 % \end{macro}
2035 % \end{macro}
2036 % \end{macro}
2037 % \end{macro}
2038 % \end{macro}
2039 % \end{macro}
2040 %
2041 % \begin{macro}{\@@_set_rescan:nNN}
2042 % \begin{macro}{\@@_set_rescan_single:nnNN, \@@_set_rescan_single_aux:nnnNN}
2043 % \begin{macro}[rEXP]{\@@_set_rescan_single_aux:w}
2044 % The function \cs{@@_set_rescan:nNN} calls \cs{@@_set_rescan_multi:nNN} or
2045 % \cs{@@_set_rescan_single:nnNN} |{ ' }| depending on whether its argument
2046 % is a single-line fragment of code/data or is made of multiple lines
2047 % by testing for the presence of a \tn{newlinechar} character. If
2048 % \tn{newlinechar} is out of range, the argument is assumed to be a
2049 % single line.
2050 %
2051 % For a single line, no \tn{endlinechar} should be added, so it is
2052 % set to $-1$, and spaces should not be removed.
2053 % Trailing spaces and tabs are a difficult matter, as \TeX{} removes
2054 % these at a very low level. The only way to preserve them is to
2055 % rescan not the argument but the argument followed by a character
2056 % with a reasonable category code. Here, $11$ (letter) and $12$ (other)
2057 % are accepted, as these are convenient, suitable for
2058 % delimiting an argument, and it is very unlikely that none of the
2059 % ASCII characters are in one of these categories. To avoid
2060 % selecting one particular character to put at the end, whose
2061 % category code may have been modified, there is a loop through
2062 % characters from |'| (ASCII $39$) to |~| (ASCII $127$). The choice
2063 % of starting point was made because this is the start of a very long
2064 % range of characters whose standard category is letter or other,
2065 % thus minimizing the number of steps needed by the loop (most often
2066 % just a single one). If no valid character is found (very rare),
2067 % fall-back on \cs{@@_set_rescan_multi:nNN}.
2068 %
2069 % Otherwise, once a valid character is found (let us use |'| in this
2070 % explanation) run some code very similar to \cs{@@_set_rescan_multi:nNN}
2071 % but with |'| added at both ends of the input. Of course, we need to
2072 % define the auxiliary \cs{@@_set_rescan_single:NNww} on the fly to remove
2073 % the additional~|'| that is just before |::| (by which we mean
2074 % \cs{c_@@_rescan_marker_tl}). Note that the argument must be
2075 % delimited by |'| with the current catcode; this is done thanks to
2076 % \cs{char_generate:nn}. Yet another issue is that the rescanned
2077 % token list may contain a comment character, in which case the |'| we
2078 % expected is not there. We fix this as follows: rather than just
2079 % |::| we set \tn{everyeof} to |::|\Arg{code1} |'::|\Arg{code2}
2080 % \cs{s_@@_stop}. The auxiliary \cs{@@_set_rescan_single:NNww} runs the
2081 % \texttt{o}-expanding assignment, expanding either \meta{code1} or
2082 % \meta{code2} before its the main argument~|#3|. In the typical case
2083 % without comment character, \meta{code1} is expanded, removing the
2084 % leading~|'|. In the rarer case with comment character, \meta{code2}
2085 % is expanded, calling \cs{@@_set_rescan_single_aux:w}, which removes the
2086 % trailing |::|\Arg{code1} and the leading~|'|.
2087 % \begin{macrocode}
2088 \cs_new_protected:Npn \@@_set_rescan:nNN #1
2089 {
2090 \int_compare:nNnTF \tex_newlinechar:D < 0
2091 { \use_ii:nn }
2092 {
2093 \exp_args:Nnf \tl_if_in:nnTF {#1}
2094 { \char_generate:nn { \tex_newlinechar:D } { 12 } }
2095 }
2096 { \@@_set_rescan_multi:nNN }
2097 {
2098 \int_set:Nn \tex_endlinechar:D { -1 }
2099 \@@_set_rescan_single:nnNN { `' }
2100 }
2101 {#1}
2102 }
2103 \cs_new_protected:Npn \@@_set_rescan_single:nnNN #1
2104 {
2105 \int_compare:nNnTF
2106 { \char_value_catcode:n {#1} / 2 } = 6
2107 {
2108 \exp_args:Nof \@@_set_rescan_single_aux:nnnNN
2109 \c_@@_rescan_marker_tl
2110 { \char_generate:nn {#1} { \char_value_catcode:n {#1} } }
2111 }
2112 {
2113 \int_compare:nNnTF {#1} < { `\~ }
2114 {
2115 \exp_args:Nf \@@_set_rescan_single:nnNN
2116 { \int_eval:n { #1 + 1 } }
2117 }
2118 { \@@_set_rescan_multi:nNN }
2119 }
2120 }
2121 \cs_new_protected:Npn \@@_set_rescan_single_aux:nnnNN #1#2#3#4#5
2122 {
2123 \tex_everyeof:D
2124 {
2125 #1 \use_none:n
2126 #2 #1 { \exp:w \@@_set_rescan_single_aux:w }
2127 \s_@@_stop
2128 }
2129 \cs_set:Npn \@@_rescan:NNw ##1##2##3 #2 #1 ##4 ##5 \s_@@_stop
2130 {
2131 \group_end:
2132 ##1 ##2 { ##4 ##3 }
2133 }
2134 \exp_after:wN \@@_rescan:NNw
2135 \exp_after:wN #4
2136 \exp_after:wN #5
2137 \tex_scantokens:D { #2 #3 #2 }
2138 }
2139 \exp_args:Nno \use:nn
2140 { \cs_new:Npn \@@_set_rescan_single_aux:w #1 }
2141 \c_@@_rescan_marker_tl #2
2142 { \use_i:nn \exp_end: #1 }
2143 % \end{macrocode}
2144 % \end{macro}
2145 % \end{macro}
2146 % \end{macro}
2147 %
2148 % \begin{macro}{\tl_set_rescan:Nnn, \tl_gset_rescan:Nnn}
2149 % \begin{macro}{\@@_set_rescan_single:nnNN}
2150 % \begin{macro}{\@@_set_rescan_single_aux:nnNN}
2151 % \begin{macro}{\@@_set_rescan_aux:w}
2152 % \begin{macro}{\@@_set_rescan_multi:nNN, \@@_set_rescan_multi_aux:nNN}
2153 % If the \tn{scantextokens} is available, the above is not all required:
2154 % in particular, in LuaMeta\TeX{} there is a statement to forget about
2155 % \tn{scantokens} so it seems sensible to use \tn{scantextokens} if possible.
2156 % We keep as much of the shared code path, but in this
2157 % case \tn{everyoef} is unused (and we do not have to worry about end-of-file
2158 % errors). Getting the documented behavior for single versus multiple line
2159 % input requires a bit of work; for the latter we need to add back in
2160 % the \tn{endlinechar} that is deliberately omitted. That is a bit awkward
2161 % as the engine really doesn't want to do this!
2162 % \begin{macrocode}
2163 \cs_if_exist:NT \tex_scantextokens:D
2164 {
2165 \cs_gset_protected:Npn \tl_set_rescan:Nnn
2166 { \@@_set_rescan:NNnn \tl_set:Nn }
2167 \cs_gset_protected:Npn \tl_gset_rescan:Nnn
2168 { \@@_set_rescan:NNnn \tl_gset:Nn }
2169 \cs_gset_protected:Npn \@@_set_rescan_single:nnNN #1
2170 {
2171 \int_compare:nNnTF
2172 { \char_value_catcode:n {#1} / 2 } = 6
2173 {
2174 \exp_args:Nf \@@_set_rescan_single_aux:nnNN
2175 { \char_generate:nn {#1} { \char_value_catcode:n {#1} } }
2176 }
2177 {
2178 \int_compare:nNnTF {#1} < { `\~ }
2179 {
2180 \exp_args:Nf \@@_set_rescan_single:nnNN
2181 { \int_eval:n { #1 + 1 } }
2182 }
2183 { \@@_set_rescan_multi:nNN }
2184 }
2185 }
2186 \cs_new_protected:Npn \@@_set_rescan_aux:w #1
2187 {
2188 \cs_new_protected:Npn \@@_set_rescan_single_aux:nnNN ##1##2##3##4
2189 {
2190 \tl_set:No \l_@@_tmpa_tl { \tex_scantextokens:D { ##1 ##2 ##1 } }
2191 \cs_set_protected:Npn \@@_set_rescan:w
2192 ####1 ##1 #1 ####2 #1 ####3 \s_@@_stop
2193 {
2194 \tl_if_blank:nTF {####3}
2195 { \@@_set_rescan_aux:w ####1 \s_@@_stop }
2196 { \tl_set:No \l_@@_tmpa_tl { \use_none:n ####1 } }
2197 }
2198 \exp_after:wN \@@_set_rescan:w \l_@@_tmpa_tl #1 ##1 #1 #1 \s_@@_stop
2199 \exp_args:NNNo \group_end:
2200 ##3 ##4 \l_@@_tmpa_tl
2201 }
2202 \cs_gset_protected:Npn \@@_set_rescan_aux:w ##1 #1 ##2 \s_@@_stop
2203 { \tl_set:No \l_@@_tmpa_tl { \use_none:n ##1 } }
2204 }
2205 \exp_args:NV \@@_set_rescan_aux:w \c_@@_rescan_marker_tl
2206 \cs_gset_protected:Npn \@@_set_rescan_multi:nNN #1#2#3
2207 {
2208 \str_if_eq:eeTF { \tl_head:e { \tl_reverse:n {#1} } }
2209 { \char_generate:nn { \tex_endlinechar:D } { 12 } }
2210 { \@@_set_rescan_multi_aux:nNN {#1} }
2211 {
2212 \exp_args:Ne \@@_set_rescan_multi_aux:nNN
2213 { #1 \char_generate:nn { \tex_endlinechar:D } { 12 } }
2214 }
2215 #2 #3
2216 }
2217 \cs_new_protected:Npn \@@_set_rescan_multi_aux:nNN #1#2#3
2218 {
2219 \tex_expanded:D
2220 {
2221 \exp_not:N \exp_args:NNNo \group_end:
2222 #2 \exp_not:N #3
2223 {
2224 \exp_not:N \tex_scantextokens:D
2225 {
2226 #1
2227 \char_generate:nn { 13 } { 12 }
2228 }
2229 }
2230 }
2231 }
2232 }
2233 % \end{macrocode}
2234 % \end{macro}
2235 % \end{macro}
2236 % \end{macro}
2237 % \end{macro}
2238 % \end{macro}
2239 %
2240 % \begin{macro}{\tl_retokenize:n, \tl_retokenize:V}
2241 % A thin wrapper from \tn{scantokens}. At present, we are told by
2242 % Hans that LuaMeta\TeX{} will retain this primitive, so we should be
2243 % OK for all engines.
2244 % \begin{macrocode}
2245 \group_begin:
2246 \tex_catcode:D `\^^M = 12 \scan_stop: %
2247 \cs_new_protected:Npn \tl_retokenize:n #1 %
2248 { %
2249 \group_begin: %
2250 \int_set:Nn \tex_newlinechar:D { `^^J } %
2251 \int_set:Nn \tex_endlinechar:D { `^^M } %
2252 \exp_after:wN \group_end: %
2253 \tex_scantokens:D \exp_after:wN %
2254 { \tl_to_str:n {#1} } %
2255 } %
2256 \group_end: %
2257 \cs_generate_variant:Nn \tl_retokenize:n { V }
2258 % \end{macrocode}
2259 % \end{macro}
2260 %
2261 % \subsection{Modifying token list variables}
2262 %
2263 % \begin{macro}
2264 % {
2265 % \tl_replace_once:Nnn, \tl_replace_once:NVn, \tl_replace_once:NnV,
2266 % \tl_replace_once:Nen, \tl_replace_once:Nne, \tl_replace_once:Nee,
2267 % \tl_replace_once:Nxn, \tl_replace_once:Nnx, \tl_replace_once:Nxx,
2268 % \tl_replace_once:cnn, \tl_replace_once:cVn, \tl_replace_once:cnV,
2269 % \tl_replace_once:cen, \tl_replace_once:cne, \tl_replace_once:cee,
2270 % \tl_replace_once:cxn, \tl_replace_once:cnx, \tl_replace_once:cxx
2271 % }
2272 % \begin{macro}
2273 % {
2274 % \tl_greplace_once:Nnn, \tl_greplace_once:NVn, \tl_greplace_once:NnV,
2275 % \tl_greplace_once:Nen, \tl_greplace_once:Nne, \tl_greplace_once:Nee,
2276 % \tl_greplace_once:Nxn, \tl_greplace_once:Nnx, \tl_greplace_once:Nxx,
2277 % \tl_greplace_once:cnn, \tl_greplace_once:cVn, \tl_greplace_once:cnV,
2278 % \tl_greplace_once:cen, \tl_greplace_once:cne, \tl_greplace_once:cee,
2279 % \tl_greplace_once:cxn, \tl_greplace_once:cnx, \tl_greplace_once:cxx
2280 % }
2281 % \begin{macro}
2282 % {
2283 % \tl_replace_all:Nnn, \tl_replace_all:NVn, \tl_replace_all:NnV,
2284 % \tl_replace_all:Nen, \tl_replace_all:Nne, \tl_replace_all:Nee,
2285 % \tl_replace_all:Nxn, \tl_replace_all:Nnx, \tl_replace_all:Nxx,
2286 % \tl_replace_all:cnn, \tl_replace_all:cVn, \tl_replace_all:cnV,
2287 % \tl_replace_all:cen, \tl_replace_all:cne, \tl_replace_all:cee,
2288 % \tl_replace_all:cxn, \tl_replace_all:cnx, \tl_replace_all:cxx
2289 % }
2290 % \begin{macro}
2291 % {
2292 % \tl_greplace_all:Nnn, \tl_greplace_all:NVn, \tl_greplace_all:NnV,
2293 % \tl_greplace_all:Nen, \tl_greplace_all:Nne, \tl_greplace_all:Nee,
2294 % \tl_greplace_all:Nxn, \tl_greplace_all:Nnx, \tl_greplace_all:Nxx,
2295 % \tl_greplace_all:cnn, \tl_greplace_all:cVn, \tl_greplace_all:cnV,
2296 % \tl_greplace_all:cen, \tl_greplace_all:cne, \tl_greplace_all:cee,
2297 % \tl_greplace_all:cxn, \tl_greplace_all:cnx, \tl_greplace_all:cxx
2298 % }
2299 % All of the \texttt{replace} functions call \cs{@@_replace:NnNNNnn}
2300 % with appropriate arguments. The first two arguments are explained
2301 % later. The next controls whether the replacement function calls
2302 % itself (\cs{@@_replace_next:w}) or stops (\cs{@@_replace_wrap:w})
2303 % after the first replacement. Next comes an \texttt{e}-type
2304 % assignment function \cs{tl_set:Ne} or \cs{tl_gset:Ne} for local or
2305 % global replacements. Finally, the three arguments \meta{tl~var}
2306 % \Arg{pattern} \Arg{replacement} provided by the user. When
2307 % describing the auxiliary functions below, we denote the contents of
2308 % the \meta{tl~var} by \meta{token list}.
2309 % \begin{macrocode}
2310 \cs_new_protected:Npn \tl_replace_once:Nnn
2311 { \@@_replace:NnNNNnn \q_@@_mark ? \@@_replace_wrap:w \__kernel_tl_set:Nx }
2312 \cs_new_protected:Npn \tl_greplace_once:Nnn
2313 { \@@_replace:NnNNNnn \q_@@_mark ? \@@_replace_wrap:w \__kernel_tl_gset:Nx }
2314 \cs_new_protected:Npn \tl_replace_all:Nnn
2315 { \@@_replace:NnNNNnn \q_@@_mark ? \@@_replace_next:w \__kernel_tl_set:Nx }
2316 \cs_new_protected:Npn \tl_greplace_all:Nnn
2317 { \@@_replace:NnNNNnn \q_@@_mark ? \@@_replace_next:w \__kernel_tl_gset:Nx }
2318 \cs_generate_variant:Nn \tl_replace_once:Nnn
2319 { NnV , Nne , NV , Ne , Nee , c , cnV , cne , cV , ce , cee }
2320 \cs_generate_variant:Nn \tl_replace_once:Nnn
2321 { Nx , Nnx , Nxx , cxn , cnx , cxx }
2322 \cs_generate_variant:Nn \tl_greplace_once:Nnn
2323 { NnV , Nne , NV , Ne , Nee , c , cnV , cne , cV , ce , cee }
2324 \cs_generate_variant:Nn \tl_greplace_once:Nnn
2325 { Nx , Nnx , Nxx , cxn , cnx , cxx }
2326 \cs_generate_variant:Nn \tl_replace_all:Nnn
2327 { NnV , Nne , NV , Ne , Nee , c , cnV , cne , cV , ce , cee }
2328 \cs_generate_variant:Nn \tl_replace_all:Nnn
2329 { Nx , Nnx , Nxx , cxn , cnx , cxx }
2330 \cs_generate_variant:Nn \tl_greplace_all:Nnn
2331 { NnV , Nne , NV , Ne , Nee , c , cnV , cne , cV , ce , cee }
2332 \cs_generate_variant:Nn \tl_greplace_all:Nnn
2333 { Nx , Nnx , Nxx , cxn , cnx , cxx }
2334 % \end{macrocode}
2335 % \end{macro}
2336 % \end{macro}
2337 % \end{macro}
2338 % \end{macro}
2339 %
2340 % \begin{macro}
2341 % {
2342 % \@@_replace:NnNNNnn,
2343 % \@@_replace_auxi:NnnNNNnn,
2344 % \@@_replace_auxii:nNNNnn,
2345 % \@@_replace_next:w,
2346 % \@@_replace_next_aux:w,
2347 % \@@_replace_wrap:w,
2348 % }
2349 % To implement the actual replacement auxiliary
2350 % \cs{@@_replace_auxii:nNNNnn} we need a \meta{delimiter} with
2351 % the following properties:
2352 % \begin{itemize}
2353 % \item all occurrences of the \meta{pattern}~|#6| in
2354 % \enquote{\meta{token list} \meta{delimiter}} belong to the
2355 % \meta{token list} and have no overlap with the \meta{delimiter},
2356 % \item the first occurrence of the \meta{delimiter} in
2357 % \enquote{\meta{token list} \meta{delimiter}} is the trailing
2358 % \meta{delimiter}.
2359 % \end{itemize}
2360 % We first find the building blocks for the \meta{delimiter}, namely
2361 % two tokens \meta{A} and~\meta{B} such that \meta{A} does not appear
2362 % in~|#6| and |#6| is not~\meta{B} (this condition is trivial if |#6|
2363 % has more than one token). Then we consider the delimiters
2364 % \enquote{\meta{A}} and \enquote{\meta{A} \meta{A}$^n$ \meta{B}
2365 % \meta{A}$^n$ \meta{B}}, for $n\geq 1$, where $\meta{A}^n$ denotes
2366 % $n$~copies of \meta{A}, and we choose as our \meta{delimiter} the
2367 % first one which is not in the \meta{token list}.
2368 %
2369 % Every delimiter in the set obeys the first condition: |#6|~does not
2370 % contain~\meta{A} hence cannot be overlapping with the \meta{token
2371 % list} and the \meta{delimiter}, and it cannot be within the
2372 % \meta{delimiter} since it would have to be in one of the two
2373 % \meta{B} hence be equal to this single token (or empty, but this is
2374 % an error case filtered separately). Given the particular form of
2375 % these delimiters, for which no prefix is also a suffix, the second
2376 % condition is actually a consequence of the weaker condition that the
2377 % \meta{delimiter} we choose does not appear in the \meta{token list}.
2378 % Additionally, the set of delimiters is such that a \meta{token list}
2379 % of $n$~tokens can contain at most $O(n^{1/2})$ of them, hence we
2380 % find a \meta{delimiter} with at most $O(n^{1/2})$ tokens in a time
2381 % at most $O(n^{3/2})$. Bear in mind that these upper bounds are
2382 % reached only in very contrived scenarios: we include the case
2383 % \enquote{\meta{A}} in the list of delimiters to try, so that the
2384 % \meta{delimiter} is simply \cs{q_@@_mark} in the most common
2385 % situation where neither the \meta{token list} nor the \meta{pattern}
2386 % contains \cs{q_@@_mark}.
2387 %
2388 % Let us now ahead, optimizing for this most common case. First, two
2389 % special cases: an empty \meta{pattern}~|#6| is an error, and if
2390 % |#1|~is absent from both the \meta{token list}~|#5| and the
2391 % \meta{pattern}~|#6| then we can use it as the \meta{delimiter}
2392 % through \cs{@@_replace_auxii:nNNNnn} |{#1}|. Otherwise, we end up
2393 % calling \cs{@@_replace:NnNNNnn} repeatedly with the first two
2394 % arguments \cs{q_@@_mark} |{?}|, |\?| |{??}|, |\??| |{???}|, and so on,
2395 % until |#6|~does not contain the control sequence~|#1|, which we take
2396 % as our~\meta{A}. The argument~|#2| only serves to collect~|?|
2397 % characters for~|#1|. Note that the order of the tests means that
2398 % the first two are done every time, which is wasteful (for instance,
2399 % we repeatedly test for the emptiness of~|#6|). However, this is
2400 % rare enough not to matter. Finally, choose~\meta{B} to be
2401 % \cs{q_@@_nil} or~\cs{q_@@_stop} such that it is not equal to~|#6|.
2402 %
2403 % The \cs{@@_replace_auxi:NnnNNNnn} auxiliary receives \Arg{A} and
2404 % |{|\meta{A}$^n$\meta{B}|}| as its arguments, initially with $n=1$.
2405 % If \enquote{\meta{A} \meta{A}$^n$\meta{B} \meta{A}$^n$\meta{B}} is
2406 % in the \meta{token list} then increase~$n$ and try again. Once it
2407 % is not anymore in the \meta{token list} we take it as our
2408 % \meta{delimiter} and pass this to the \texttt{auxii} auxiliary.
2409 % \begin{macrocode}
2410 \cs_new_protected:Npn \@@_replace:NnNNNnn #1#2#3#4#5#6#7
2411 {
2412 \tl_if_empty:nTF {#6}
2413 {
2414 \msg_error:nne { kernel } { empty-search-pattern }
2415 { \tl_to_str:n {#7} }
2416 }
2417 {
2418 \tl_if_in:onTF { #5 #6 } {#1}
2419 {
2420 \tl_if_in:nnTF {#6} {#1}
2421 { \exp_args:Nc \@@_replace:NnNNNnn {#2} {#2?} }
2422 {
2423 \@@_quark_if_nil:nTF {#6}
2424 { \@@_replace_auxi:NnnNNNnn #5 {#1} { #1 \q_@@_stop } }
2425 { \@@_replace_auxi:NnnNNNnn #5 {#1} { #1 \q_@@_nil } }
2426 }
2427 }
2428 { \@@_replace_auxii:nNNNnn {#1} }
2429 #3#4#5 {#6} {#7}
2430 }
2431 }
2432 \cs_new_protected:Npn \@@_replace_auxi:NnnNNNnn #1#2#3
2433 {
2434 \tl_if_in:NnTF #1 { #2 #3 #3 }
2435 { \@@_replace_auxi:NnnNNNnn #1 { #2 #3 } {#2} }
2436 { \@@_replace_auxii:nNNNnn { #2 #3 #3 } }
2437 }
2438 % \end{macrocode}
2439 % The auxiliary \cs{@@_replace_auxii:nNNNnn} receives the following
2440 % arguments:
2441 % \begin{quote}
2442 % \Arg{delimiter} \meta{function} \meta{assignment} \\
2443 % \meta{tl~var} \Arg{pattern} \Arg{replacement}
2444 % \end{quote}
2445 % All of its work is done between
2446 % \cs{group_align_safe_begin:} and \cs{group_align_safe_end:} to avoid
2447 % issues in alignments. It does the actual replacement within
2448 % |#3|~|#4|~|{...}|, an \texttt{e}-expanding \meta{assignment}~|#3| to
2449 % the \meta{tl~var}~|#4|. The auxiliary \cs{@@_replace_next:w} is
2450 % called, followed by the \meta{token list}, some tokens including the
2451 % \meta{delimiter}~|#1|, followed by the \meta{pattern}~|#5|.
2452 % This auxiliary finds an argument delimited by~|#5| (the presence of
2453 % a trailing~|#5| avoids runaway arguments) and calls
2454 % \cs{@@_replace_wrap:w} to test whether this |#5| is found within the
2455 % \meta{token list} or is the trailing one.
2456 %
2457 % If on the one hand it is found within the \meta{token list}, then
2458 % |##1| cannot contain the \meta{delimiter}~|#1| that we worked so
2459 % hard to obtain, thus \cs{@@_replace_wrap:w} gets~|##1| as its own
2460 % argument~|##1|, and protects it against
2461 % the \texttt{e}-expanding assignment. It also finds \cs{exp_not:n}
2462 % as~|##2| and does nothing to it, thus letting through \cs{exp_not:n}
2463 % \Arg{replacement} into the assignment. Note that
2464 % \cs{@@_replace_next:w} and \cs{@@_replace_wrap:w} are always called
2465 % followed by two empty brace groups. These are safe because no
2466 % delimiter can match them. They prevent losing braces when grabbing
2467 % delimited arguments, but require the use of \cs{exp_not:o} and
2468 % \cs{use_none:nn}, rather than simply \cs{exp_not:n}.
2469 % Afterwards, \cs{@@_replace_next:w} is called
2470 % to repeat the replacement, or \cs{@@_replace_wrap:w} if we only want
2471 % a single replacement. In this second case, |##1| is the
2472 % \meta{remaining tokens} in the \meta{token list} and |##2| is some
2473 % \meta{ending code} which ends the assignment and removes the
2474 % trailing tokens |#5| using some \cs{if_false:} |{| \cs{fi:} |}|
2475 % trickery because~|#5| may contain any delimiter.
2476 %
2477 % If on the other hand the argument~|##1| of \cs{@@_replace_next:w} is
2478 % delimited by the trailing \meta{pattern}~|#5|, then |##1| is
2479 % \enquote{\{ \} \{ \} \meta{token list} \meta{delimiter}
2480 % \Arg{ending code}}, hence \cs{@@_replace_wrap:w} finds
2481 % \enquote{\{ \} \{ \} \meta{token list}} as |##1| and the
2482 % \meta{ending code} as~|##2|. It leaves the \meta{token list} into
2483 % the assignment and unbraces the \meta{ending code} which removes
2484 % what remains (essentially the \meta{delimiter} and
2485 % \meta{replacement}).
2486 % \begin{macrocode}
2487 \cs_new_protected:Npn \@@_replace_auxii:nNNNnn #1#2#3#4#5#6
2488 {
2489 \group_align_safe_begin:
2490 \cs_set:Npn \@@_replace_wrap:w ##1 #1 ##2
2491 { \__kernel_exp_not:w \exp_after:wN { \use_none:nn ##1 } ##2 }
2492 \cs_set:Npe \@@_replace_next:w ##1 #5
2493 {
2494 \exp_not:N \@@_replace_wrap:w ##1
2495 \exp_not:n { #1 }
2496 \exp_not:n { \exp_not:n {#6} }
2497 \exp_not:n { #2 { } { } }
2498 }
2499 #3 #4
2500 {
2501 \exp_after:wN \@@_replace_next_aux:w
2502 #4
2503 #1
2504 {
2505 \if_false: { \fi: }
2506 \exp_after:wN \use_none:n \exp_after:wN { \if_false: } \fi:
2507 }
2508 #5
2509 }
2510 \group_align_safe_end:
2511 }
2512 \cs_new:Npn \@@_replace_next_aux:w { \@@_replace_next:w { } { } }
2513 \cs_new_eq:NN \@@_replace_wrap:w ?
2514 \cs_new_eq:NN \@@_replace_next:w ?
2515 % \end{macrocode}
2516 % \end{macro}
2517 %
2518 % \begin{macro}
2519 % {
2520 % \tl_regex_replace_once:Nnn, \tl_regex_replace_once:cnn,
2521 % \tl_regex_replace_once:NNn, \tl_regex_replace_once:cNn,
2522 % \tl_regex_greplace_once:Nnn, \tl_regex_greplace_once:cnn,
2523 % \tl_regex_greplace_once:NNn, \tl_regex_greplace_once:cNn
2524 % }
2525 % \begin{macro}
2526 % {
2527 % \tl_regex_replace_all:Nnn, \tl_regex_replace_all:cnn,
2528 % \tl_regex_replace_all:NNn, \tl_regex_replace_all:cNn,
2529 % \tl_regex_greplace_all:Nnn, \tl_regex_greplace_all:cnn,
2530 % \tl_regex_greplace_all:NNn, \tl_regex_greplace_all:cNn
2531 % }
2532 % Wrappers.
2533 % \begin{macrocode}
2534 \cs_new_protected:Npn \tl_regex_replace_once:Nnn #1#2#3
2535 { \regex_replace_once:nnN {#2} {#3} #1 }
2536 \cs_generate_variant:Nn \tl_regex_replace_once:Nnn { c }
2537 \cs_new_protected:Npn \tl_regex_replace_once:NNn #1#2#3
2538 { \regex_replace_once:NnN #2 {#3} #1 }
2539 \cs_generate_variant:Nn \tl_regex_replace_once:NNn { c }
2540 \cs_new_protected:Npn \tl_regex_replace_all:Nnn #1#2#3
2541 { \regex_replace_all:nnN {#2} {#3} #1 }
2542 \cs_generate_variant:Nn \tl_regex_replace_all:Nnn { c }
2543 \cs_new_protected:Npn \tl_regex_replace_all:NNn #1#2#3
2544 { \regex_replace_all:NnN #2 {#3} #1 }
2545 \cs_generate_variant:Nn \tl_regex_replace_all:NNn { c }
2546 \group_begin:
2547 \cs_set_protected:Npn \@@_tmp:w #1#2#3
2548 {
2549 \cs_new_protected:cpe { tl_regex_greplace_ #1 :N #2 n } ##1##2##3
2550 {
2551 \group_begin:
2552 \tl_set_eq:NN \exp_not:N \l_@@_tmpa_tl ##1
2553 \exp_not:c { regex_replace_ #1 :Nn #2 }
2554 #3 {##2} {##3} \exp_not:N \l_@@_tmpa_tl
2555 \tl_gset_eq:NN ##1 \exp_not:N \l_@@_tmpa_tl
2556 \group_end:
2557 }
2558 \cs_generate_variant:cn { tl_regex_greplace_ #1 :N #2 n } { c }
2559 }
2560 \@@_tmp:w { once } n { }
2561 \@@_tmp:w { once } N \use:n
2562 \@@_tmp:w { all } n { }
2563 \@@_tmp:w { all } N \use:n
2564 \group_end:
2565 % \end{macrocode}
2566 % \end{macro}
2567 % \end{macro}
2568 %
2569 % \begin{macro}
2570 % {
2571 % \tl_remove_once:Nn, \tl_remove_once:NV, \tl_remove_once:Ne,
2572 % \tl_remove_once:cn, \tl_remove_once:cV, \tl_remove_once:ce
2573 % }
2574 % \begin{macro}
2575 % {
2576 % \tl_gremove_once:Nn, \tl_gremove_once:NV,
2577 % \tl_gremove_once:cn, \tl_gremove_once:cV,
2578 % }
2579 % Removal is just a special case of replacement.
2580 % \begin{macrocode}
2581 \cs_new_protected:Npn \tl_remove_once:Nn #1#2
2582 { \tl_replace_once:Nnn #1 {#2} { } }
2583 \cs_new_protected:Npn \tl_gremove_once:Nn #1#2
2584 { \tl_greplace_once:Nnn #1 {#2} { } }
2585 \cs_generate_variant:Nn \tl_remove_once:Nn { NV , Ne , c , cV , ce }
2586 \cs_generate_variant:Nn \tl_gremove_once:Nn { NV , Ne , c , cV , ce }
2587 % \end{macrocode}
2588 % \end{macro}
2589 % \end{macro}
2590 %
2591 % \begin{macro}
2592 % {
2593 % \tl_remove_all:Nn, \tl_remove_all:NV, \tl_remove_all:Ne,
2594 % \tl_remove_all:Nx,
2595 % \tl_remove_all:cn, \tl_remove_all:cV, \tl_remove_all:ce,
2596 % \tl_remove_all:cx,
2597 % }
2598 % \begin{macro}
2599 % {
2600 % \tl_gremove_all:Nn, \tl_gremove_all:NV, \tl_gremove_all:Ne,
2601 % \tl_gremove_all:Nx,
2602 % \tl_gremove_all:cn, \tl_gremove_all:cV, \tl_gremove_all:ce,
2603 % \tl_gremove_all:cx,
2604 % }
2605 % Removal is just a special case of replacement.
2606 % \begin{macrocode}
2607 \cs_new_protected:Npn \tl_remove_all:Nn #1#2
2608 { \tl_replace_all:Nnn #1 {#2} { } }
2609 \cs_new_protected:Npn \tl_gremove_all:Nn #1#2
2610 { \tl_greplace_all:Nnn #1 {#2} { } }
2611 \cs_generate_variant:Nn \tl_remove_all:Nn { NV , Ne , c , cV , ce }
2612 \cs_generate_variant:Nn \tl_remove_all:Nn { Nx , cx }
2613 \cs_generate_variant:Nn \tl_gremove_all:Nn { NV , Ne , c , cV , ce }
2614 \cs_generate_variant:Nn \tl_gremove_all:Nn { Nx , cx }
2615 % \end{macrocode}
2616 % \end{macro}
2617 % \end{macro}
2618 %
2619 % \subsection{Token list conditionals}
2620 %
2621 % \begin{macro}[pTF]{\tl_if_empty:N, \tl_if_empty:c}
2622 % These functions check whether the token list in the argument is
2623 % empty and execute the proper code from their argument(s).
2624 % \begin{macrocode}
2625 \prg_new_conditional:Npnn \tl_if_empty:N #1 { p , T , F , TF }
2626 {
2627 \if_meaning:w #1 \c_empty_tl
2628 \prg_return_true:
2629 \else:
2630 \prg_return_false:
2631 \fi:
2632 }
2633 \prg_generate_conditional_variant:Nnn \tl_if_empty:N
2634 { c } { p , T , F , TF }
2635 % \end{macrocode}
2636 % \end{macro}
2637 %
2638 % \begin{macro}[pTF]{\tl_if_empty:n, \tl_if_empty:V, \tl_if_empty:e}
2639 % The \cs{if:w} triggers the expansion of \cs{tl_to_str:n} which converts the
2640 % argument to a string: this is empty if and only if the argument is. Then
2641 % |\if:w \scan_stop: ... \scan_stop:| is \texttt{true} if and only if the
2642 % string |...| is empty.
2643 % It could be tempting to use |\if:w \scan_stop: #1 \scan_stop:| directly.
2644 % But this fails on a token list expanding to anything starting with
2645 % \cs{scan_stop:} leaving everything that follows in the input stream.
2646 % \begin{macrocode}
2647 \prg_new_conditional:Npnn \tl_if_empty:n #1 { p , TF , T , F }
2648 {
2649 \if:w \scan_stop: \tl_to_str:n {#1} \scan_stop:
2650 \prg_return_true:
2651 \else:
2652 \prg_return_false:
2653 \fi:
2654 }
2655 \prg_generate_conditional_variant:Nnn \tl_if_empty:n
2656 { V , e } { p , TF , T , F }
2657 % \end{macrocode}
2658 % \end{macro}
2659 %
2660 % \begin{macro}[pTF,documented-as=\tl_if_empty:nTF]{\tl_if_empty:o}
2661 % \begin{macro}[EXP]{\@@_if_empty_if:o}
2662 % The auxiliary function \cs{@@_if_empty_if:o} is for use
2663 % in various token list conditionals which reduce to testing
2664 % if a given token list is empty after applying a simple function
2665 % to it.
2666 % The test for emptiness is based on \cs{tl_if_empty:nTF}, but
2667 % the expansion is hard-coded for efficiency, as this auxiliary
2668 % function is used in several places.
2669 % We don't put \cs{prg_return_true:} and so on in the definition of
2670 % the auxiliary, because that would prevent an optimization applied to
2671 % conditionals that end with this code.
2672 % Also the |\@@_if_empty_if:o| is expanded once in |\tl_if_empty:oTF| for
2673 % efficiency as well (and to reduce code doubling).
2674 % \begin{macrocode}
2675 \cs_new:Npn \@@_if_empty_if:o #1
2676 {
2677 \if:w \scan_stop: \__kernel_tl_to_str:w \exp_after:wN {#1} \scan_stop:
2678 }
2679 \exp_args:Nno \use:n
2680 { \prg_new_conditional:Npnn \tl_if_empty:o #1 { p , TF , T , F } }
2681 {
2682 \@@_if_empty_if:o {#1}
2683 \prg_return_true:
2684 \else:
2685 \prg_return_false:
2686 \fi:
2687 }
2688 % \end{macrocode}
2689 % \end{macro}
2690 % \end{macro}
2691 %
2692 % \begin{macro}[pTF]{\tl_if_blank:n, \tl_if_blank:V, \tl_if_blank:o}
2693 % \begin{macro}{\@@_if_blank_p:NNw}
2694 % \TeX{} skips spaces when reading a non-delimited arguments. Thus,
2695 % a \meta{token list} is blank if and only if \cs{use_none:n}
2696 % \meta{token list} |?| is empty after one expansion. The auxiliary
2697 % \cs{@@_if_empty_if:o} is a fast emptiness test, converting its
2698 % argument to a string (after one expansion) and using the test
2699 % \cs{if:w} \cs{scan_stop:} |...| \cs{scan_stop:}.
2700 % \begin{macrocode}
2701 \exp_args:Nno \use:n
2702 { \prg_new_conditional:Npnn \tl_if_blank:n #1 { p , T , F , TF } }
2703 {
2704 \@@_if_empty_if:o { \use_none:n #1 ? }
2705 \prg_return_true:
2706 \else:
2707 \prg_return_false:
2708 \fi:
2709 }
2710 \prg_generate_conditional_variant:Nnn \tl_if_blank:n
2711 { e , V , o } { p , T , F , TF }
2712 % \end{macrocode}
2713 % \end{macro}
2714 % \end{macro}
2715 %
2716 % \begin{macro}[pTF]{\tl_if_eq:NN, \tl_if_eq:Nc, \tl_if_eq:cN, \tl_if_eq:cc}
2717 % Returns \cs{c_true_bool} if and only if the two token list variables are
2718 % equal.
2719 % \begin{macrocode}
2720 \prg_new_eq_conditional:NNn \tl_if_eq:NN \cs_if_eq:NN { p , T , F , TF }
2721 \prg_generate_conditional_variant:Nnn \tl_if_eq:NN
2722 { Nc , c , cc } { p , TF , T , F }
2723 % \end{macrocode}
2724 % \end{macro}
2725 %
2726 % \begin{variable}{\l_@@_tmpa_tl, \l_@@_tmpb_tl}
2727 % Temporary storage.
2728 % \begin{macrocode}
2729 \tl_new:N \l_@@_tmpa_tl
2730 \tl_new:N \l_@@_tmpb_tl
2731 % \end{macrocode}
2732 % \end{variable}
2733 %
2734 % \begin{macro}[TF]{\tl_if_eq:Nn}
2735 % A simple store and compare routine.
2736 % \begin{macrocode}
2737 \prg_new_protected_conditional:Npnn \tl_if_eq:Nn #1#2 { T , F , TF }
2738 {
2739 \group_begin:
2740 \tl_set:Nn \l_@@_tmpb_tl {#2}
2741 \exp_after:wN
2742 \group_end:
2743 \if_meaning:w #1 \l_@@_tmpb_tl
2744 \prg_return_true:
2745 \else:
2746 \prg_return_false:
2747 \fi:
2748 }
2749 \prg_generate_conditional_variant:Nnn \tl_if_eq:Nn { c } { TF , T , F }
2750 % \end{macrocode}
2751 % \end{macro}
2752 %
2753 % \begin{macro}[TF]
2754 % {
2755 % \tl_if_eq:nn, \tl_if_eq:nV, \tl_if_eq:ne, \tl_if_eq:Vn, \tl_if_eq:en,
2756 % \tl_if_eq:ee,
2757 % \tl_if_eq:xn, \tl_if_eq:nx, \tl_if_eq:xx,
2758 % }
2759 % A simple store and compare routine.
2760 % \begin{macrocode}
2761 \prg_new_protected_conditional:Npnn \tl_if_eq:nn #1#2 { T , F , TF }
2762 {
2763 \group_begin:
2764 \tl_set:Nn \l_@@_tmpa_tl {#1}
2765 \tl_set:Nn \l_@@_tmpb_tl {#2}
2766 \exp_after:wN
2767 \group_end:
2768 \if_meaning:w \l_@@_tmpa_tl \l_@@_tmpb_tl
2769 \prg_return_true:
2770 \else:
2771 \prg_return_false:
2772 \fi:
2773 }
2774 \prg_generate_conditional_variant:Nnn \tl_if_eq:nn
2775 { nV , ne , nx , V, e , ee , x , xx }
2776 { TF , T , F }
2777 % \end{macrocode}
2778 % \end{macro}
2779 %
2780 % \begin{macro}[TF]
2781 % {
2782 % \tl_if_in:Nn, \tl_if_in:NV, \tl_if_in:No,
2783 % \tl_if_in:cn, \tl_if_in:cV, \tl_if_in:co
2784 % }
2785 % See \cs{tl_if_in:nnTF} for further comments. Here we simply
2786 % expand the token list variable and pass it to \cs{tl_if_in:nnTF}.
2787 % \begin{macrocode}
2788 \cs_new_protected:Npn \tl_if_in:NnT { \exp_args:No \tl_if_in:nnT }
2789 \cs_new_protected:Npn \tl_if_in:NnF { \exp_args:No \tl_if_in:nnF }
2790 \cs_new_protected:Npn \tl_if_in:NnTF { \exp_args:No \tl_if_in:nnTF }
2791 \prg_generate_conditional_variant:Nnn \tl_if_in:Nn
2792 { NV , No , c , cV , co } { T , F , TF }
2793 % \end{macrocode}
2794 % \end{macro}
2795 %
2796 % \begin{macro}[TF]
2797 % {
2798 % \tl_if_in:nn, \tl_if_in:Vn, \tl_if_in:VV, \tl_if_in:on, \tl_if_in:oo,
2799 % \tl_if_in:nV, \tl_if_in:no
2800 % }
2801 % Once more, the test relies on the emptiness test for robustness.
2802 % The function \cs{@@_tmp:w} removes tokens until the first occurrence
2803 % of |#2|. If this does not appear in |#1|, then the final |#2| is removed,
2804 % leaving an empty token list. Otherwise some tokens remain, and the
2805 % test is \texttt{false}. See \cs{tl_if_empty:nTF} for details on
2806 % the emptiness test.
2807 %
2808 % Treating correctly cases like
2809 % |\tl_if_in:nnTF {a state}{states}|, where |#1#2| contains |#2| before
2810 % the end, requires special care.
2811 % To cater for this case, we insert |{}{}| between the two token
2812 % lists. This marker may not appear in |#2| because of \TeX{} limitations
2813 % on what can delimit a parameter, hence we are safe. Using two brace
2814 % groups makes the test work also for empty arguments.
2815 % The \cs{if_false:} constructions are a faster way to do
2816 % \cs{group_align_safe_begin:} and \cs{group_align_safe_end:}.
2817 % The \cs{scan_stop:} ensures that \texttt{f}-expanding
2818 % \cs{tl_if_in:nnTF} does not lead to unbalanced braces.
2819 % \begin{macrocode}
2820 \prg_new_protected_conditional:Npnn \tl_if_in:nn #1#2 { T , F , TF }
2821 {
2822 \scan_stop:
2823 \if_false: { \fi:
2824 \cs_set:Npn \@@_tmp:w ##1 #2 { }
2825 \tl_if_empty:oTF { \@@_tmp:w #1 {} {} #2 }
2826 { \prg_return_false: } { \prg_return_true: }
2827 \if_false: } \fi:
2828 }
2829 \prg_generate_conditional_variant:Nnn \tl_if_in:nn
2830 { V , VV , o , oo , nV , no } { T , F , TF }
2831 % \end{macrocode}
2832 % \end{macro}
2833 %
2834 % \begin{macro}[pTF, EXP]{\tl_if_novalue:n}
2835 % \begin{macro}[EXP]{\@@_if_novalue:w}
2836 % Tests whether |##1| matches |-NoValue-| exactly (with suitable
2837 % catcodes): this is similar to \cs{quark_if_nil:nTF}. The first
2838 % argument of \cs{@@_if_novalue:w} is empty if and only if |##1|
2839 % starts with |-NoValue-|, while the second argument is empty if |##1|
2840 % is exactly |-NoValue-| or if it has a question mark just following
2841 % |-NoValue-|. In this second case, however, the material after the
2842 % first |?!| remains and makes the emptiness test return
2843 % \texttt{false}.
2844 % \begin{macrocode}
2845 \cs_set_protected:Npn \@@_tmp:w #1
2846 {
2847 \prg_new_conditional:Npnn \tl_if_novalue:n ##1
2848 { p , T , F , TF }
2849 {
2850 \@@_if_empty_if:o { \@@_if_novalue:w {} ##1 {} ? ! #1 ? ? ! }
2851 \prg_return_true:
2852 \else:
2853 \prg_return_false:
2854 \fi:
2855 }
2856 \cs_new:Npn \@@_if_novalue:w ##1 #1 ##2 ? ##3 ? ! { ##1 ##2 }
2857 }
2858 \exp_args:No \@@_tmp:w { \c_novalue_tl }
2859 % \end{macrocode}
2860 % \end{macro}
2861 % \end{macro}
2862 %
2863 % \begin{macro}[EXP,pTF]{\tl_if_single:N, \tl_if_single:c}
2864 % Expand the token list and feed it to \cs{tl_if_single:nTF}.
2865 % \begin{macrocode}
2866 \cs_new:Npn \tl_if_single_p:N { \exp_args:No \tl_if_single_p:n }
2867 \cs_new:Npn \tl_if_single:NT { \exp_args:No \tl_if_single:nT }
2868 \cs_new:Npn \tl_if_single:NF { \exp_args:No \tl_if_single:nF }
2869 \cs_new:Npn \tl_if_single:NTF { \exp_args:No \tl_if_single:nTF }
2870 \prg_generate_conditional_variant:Nnn \tl_if_single:N {c} { p , T , F , TF }
2871 % \end{macrocode}
2872 % \end{macro}
2873 %
2874 % \begin{macro}[EXP,pTF]{\tl_if_single:n}
2875 % \begin{macro}[EXP]{\@@_if_single:nnw}
2876 % This test is similar to \cs{tl_if_empty:nTF}. Expanding
2877 % \cs{use_none:nn} |#1| |??| once yields an empty result if |#1| is
2878 % blank, a single~|?| if |#1| has a single item, and otherwise yields
2879 % some tokens ending with |??|. Then, \cs{__kernel_tl_to_str:w} makes sure
2880 % there are no odd category codes. An earlier version would compare
2881 % the result to a single~|?| using string comparison, but the Lua call
2882 % is slow in \LuaTeX{}. Instead, \cs{@@_if_single:nnw} picks the
2883 % second token in front of it. If |#1| is empty, this token is
2884 % the trailing~|?| and the |\if:w| test yields \texttt{false}. If
2885 % |#1| has a single item, the token is~|\scan_stop:| and the |\if:w| test
2886 % yields \texttt{true}. Otherwise, it is one of the characters
2887 % resulting from \cs{tl_to_str:n}, and the |\if:w| test yields
2888 % \texttt{false}. Note that \cs{if:w} and
2889 % \cs{__kernel_tl_to_str:w} are primitives that take care of
2890 % expansion.
2891 % \begin{macrocode}
2892 \prg_new_conditional:Npnn \tl_if_single:n #1 { p , T , F , TF }
2893 {
2894 \if:w \scan_stop: \exp_after:wN \@@_if_single:nnw
2895 \__kernel_tl_to_str:w
2896 \exp_after:wN { \use_none:nn #1 ?? } \scan_stop: ? \s_@@_stop
2897 \prg_return_true:
2898 \else:
2899 \prg_return_false:
2900 \fi:
2901 }
2902 \cs_new:Npn \@@_if_single:nnw #1#2#3 \s_@@_stop {#2}
2903 % \end{macrocode}
2904 % \end{macro}
2905 % \end{macro}
2906 %
2907 % \begin{macro}[EXP,pTF]{\tl_if_single_token:n}
2908 % There are four cases: empty token list, token list starting with a
2909 % normal token, with a brace group, or with a space token. If the
2910 % token list starts with a normal token, remove it and check for
2911 % emptiness. For the next case, an empty token list is not a single
2912 % token. Finally, we have a non-empty token list starting with a
2913 % space or a brace group. Applying \texttt{f}-expansion yields an
2914 % empty result if and only if the token list is a single space.
2915 % \begin{macrocode}
2916 \prg_new_conditional:Npnn \tl_if_single_token:n #1 { p , T , F , TF }
2917 {
2918 \tl_if_head_is_N_type:nTF {#1}
2919 { \@@_if_empty_if:o { \use_none:n #1 } }
2920 {
2921 \tl_if_empty:nTF {#1}
2922 { \if_false: }
2923 { \@@_if_empty_if:o { \exp:w \exp_end_continue_f:w #1 } }
2924 }
2925 \prg_return_true:
2926 \else:
2927 \prg_return_false:
2928 \fi:
2929 }
2930 % \end{macrocode}
2931 % \end{macro}
2932 %
2933 % \begin{macro}
2934 % {
2935 % \tl_if_regex_match:nn, \tl_if_regex_match:Vn,
2936 % \tl_if_regex_match:nN, \tl_if_regex_match:VN,
2937 % }
2938 % \begin{macrocode}
2939 \prg_new_protected_conditional:Npnn \tl_if_regex_match:nn #1#2 { TF , T , F }
2940 {
2941 \regex_if_match:nnTF {#2} {#1}
2942 \prg_return_true: \prg_return_false:
2943 }
2944 \prg_generate_conditional_variant:Nnn \tl_if_regex_match:nn
2945 { V } { TF , T , F }
2946 \prg_new_protected_conditional:Npnn \tl_if_regex_match:nN #1#2 { TF , T , F }
2947 {
2948 \regex_if_match:NnTF #2 {#1}
2949 \prg_return_true: \prg_return_false:
2950 }
2951 \prg_generate_conditional_variant:Nnn \tl_if_regex_match:nN
2952 { V } { TF , T , F }
2953 % \end{macrocode}
2954 % \end{macro}
2955 %
2956 % \subsection{Mapping over token lists}
2957 %
2958 % \begin{macro}
2959 % {
2960 % \tl_map_function:nN, \tl_map_function:eN,
2961 % \tl_map_function:NN, \tl_map_function:cN,
2962 % \@@_map_function:Nnnnnnnnn, \@@_map_function_end:w,
2963 % \@@_use_none_delimit_by_s_stop:w
2964 % }
2965 % Expandable loop macro for token lists. We use the internal scan
2966 % mark \cs{s_@@_stop} (defined later), which is not allowed to show up
2967 % in the token list |#1| since it is internal to \pkg{l3tl}. This
2968 % allows us a very fast test of whether some \meta{item} is the
2969 % end-marker \cs{s_@@_stop}, namely call
2970 % \cs{@@_use_none_delimit_by_s_stop:w} \meta{item} \meta{function}
2971 % \cs{s_@@_stop}, which calls \meta{function} if the \meta{item} is
2972 % the end-marker. To speed up the loop even more, only test one out
2973 % of eight items, and once we hit one of the eight end-markers,
2974 % go more slowly through the last few items of the list using
2975 % \cs{@@_map_function_end:w}.
2976 % \begin{macrocode}
2977 \cs_new:Npn \tl_map_function:nN #1#2
2978 {
2979 \@@_map_function:Nnnnnnnnn #2 #1
2980 \s_@@_stop \s_@@_stop \s_@@_stop \s_@@_stop
2981 \s_@@_stop \s_@@_stop \s_@@_stop \s_@@_stop
2982 \prg_break_point:Nn \tl_map_break: { }
2983 }
2984 \cs_generate_variant:Nn \tl_map_function:nN { e }
2985 \cs_new:Npn \tl_map_function:NN
2986 { \exp_args:No \tl_map_function:nN }
2987 \cs_generate_variant:Nn \tl_map_function:NN { c }
2988 \cs_new:Npn \@@_map_function:Nnnnnnnnn #1#2#3#4#5#6#7#8#9
2989 {
2990 \@@_use_none_delimit_by_s_stop:w
2991 #9 \@@_map_function_end:w \s_@@_stop
2992 #1 {#2} #1 {#3} #1 {#4} #1 {#5} #1 {#6} #1 {#7} #1 {#8} #1 {#9}
2993 \@@_map_function:Nnnnnnnnn #1
2994 }
2995 \cs_new:Npn \@@_map_function_end:w \s_@@_stop #1#2
2996 {
2997 \@@_use_none_delimit_by_s_stop:w #2 \tl_map_break: \s_@@_stop
2998 #1 {#2}
2999 \@@_map_function_end:w \s_@@_stop
3000 }
3001 \cs_new:Npn \@@_use_none_delimit_by_s_stop:w #1 \s_@@_stop { }
3002 % \end{macrocode}
3003 % \end{macro}
3004 %
3005 % \begin{macro}{\tl_map_inline:nn}
3006 % \begin{macro}{\tl_map_inline:Nn, \tl_map_inline:cn}
3007 % The inline functions are straight forward by now. We use a little
3008 % trick with the counter \cs{g__kernel_prg_map_int} to make
3009 % them nestable. We can also make use of \cs{@@_map_function:Nnnnnnnnn}
3010 % from before.
3011 % \begin{macrocode}
3012 \cs_new_protected:Npn \tl_map_inline:nn #1#2
3013 {
3014 \int_gincr:N \g__kernel_prg_map_int
3015 \cs_gset_protected:cpn
3016 { @@_map_ \int_use:N \g__kernel_prg_map_int :w } ##1 {#2}
3017 \exp_args:Nc \@@_map_function:Nnnnnnnnn
3018 { @@_map_ \int_use:N \g__kernel_prg_map_int :w }
3019 #1
3020 \s_@@_stop \s_@@_stop \s_@@_stop \s_@@_stop
3021 \s_@@_stop \s_@@_stop \s_@@_stop \s_@@_stop
3022 \prg_break_point:Nn \tl_map_break:
3023 { \int_gdecr:N \g__kernel_prg_map_int }
3024 }
3025 \cs_new_protected:Npn \tl_map_inline:Nn
3026 { \exp_args:No \tl_map_inline:nn }
3027 \cs_generate_variant:Nn \tl_map_inline:Nn { c }
3028 % \end{macrocode}
3029 % \end{macro}
3030 % \end{macro}
3031 %
3032 % \begin{macro}
3033 % {
3034 % \tl_map_tokens:nn, \tl_map_tokens:Nn, \tl_map_tokens:cn,
3035 % \@@_map_tokens:nnnnnnnnn, \@@_map_tokens_end:w
3036 % }
3037 % Much like the function mapping.
3038 % \begin{macrocode}
3039 \cs_new:Npn \tl_map_tokens:nn #1#2
3040 {
3041 \@@_map_tokens:nnnnnnnnn {#2} #1
3042 \s_@@_stop \s_@@_stop \s_@@_stop \s_@@_stop
3043 \s_@@_stop \s_@@_stop \s_@@_stop \s_@@_stop
3044 \prg_break_point:Nn \tl_map_break: { }
3045 }
3046 \cs_new:Npn \tl_map_tokens:Nn
3047 { \exp_args:No \tl_map_tokens:nn }
3048 \cs_generate_variant:Nn \tl_map_tokens:Nn { c }
3049 \cs_new:Npn \@@_map_tokens:nnnnnnnnn #1#2#3#4#5#6#7#8#9
3050 {
3051 \@@_use_none_delimit_by_s_stop:w
3052 #9 \@@_map_tokens_end:w \s_@@_stop
3053 \use:n {#1} {#2} \use:n {#1} {#3} \use:n {#1} {#4} \use:n {#1} {#5}
3054 \use:n {#1} {#6} \use:n {#1} {#7} \use:n {#1} {#8} \use:n {#1} {#9}
3055 \@@_map_tokens:nnnnnnnnn {#1}
3056 }
3057 \cs_new:Npn \@@_map_tokens_end:w \s_@@_stop \use:n #1#2
3058 {
3059 \@@_use_none_delimit_by_s_stop:w #2 \tl_map_break: \s_@@_stop
3060 #1 {#2}
3061 \@@_map_tokens_end:w \s_@@_stop
3062 }
3063 % \end{macrocode}
3064 % \end{macro}
3065 %
3066 % \begin{macro}{\tl_map_variable:nNn}
3067 % \begin{macro}{\tl_map_variable:NNn, \tl_map_variable:cNn}
3068 % \begin{macro}{\@@_map_variable:Nnn}
3069 % \cs{tl_map_variable:nNn} \Arg{token list} \meta{tl~var}
3070 % \Arg{action} assigns \meta{tl~var} to each element and executes
3071 % \meta{action}. The assignment to \meta{tl~var} is done after the
3072 % quark test so that this variable does not get set to a quark.
3073 % \begin{macrocode}
3074 \cs_new_protected:Npn \tl_map_variable:nNn #1#2#3
3075 { \tl_map_tokens:nn {#1} { \@@_map_variable:Nnn #2 {#3} } }
3076 \cs_new_protected:Npn \@@_map_variable:Nnn #1#2#3
3077 { \tl_set:Nn #1 {#3} #2 }
3078 \cs_new_protected:Npn \tl_map_variable:NNn
3079 { \exp_args:No \tl_map_variable:nNn }
3080 \cs_generate_variant:Nn \tl_map_variable:NNn { c }
3081 % \end{macrocode}
3082 % \end{macro}
3083 % \end{macro}
3084 % \end{macro}
3085 %
3086 % \begin{macro}{\tl_map_break:}
3087 % \begin{macro}{\tl_map_break:n}
3088 % The break statements use the general \cs{prg_map_break:Nn}.
3089 % \begin{macrocode}
3090 \cs_new:Npn \tl_map_break:
3091 { \prg_map_break:Nn \tl_map_break: { } }
3092 \cs_new:Npn \tl_map_break:n
3093 { \prg_map_break:Nn \tl_map_break: }
3094 % \end{macrocode}
3095 % \end{macro}
3096 % \end{macro}
3097 %
3098 % \subsection{Using token lists}
3099 %
3100 % \begin{macro}{\tl_to_str:n, \tl_to_str:o, \tl_to_str:V, \tl_to_str:v, \tl_to_str:e}
3101 % Another name for a primitive: defined in \pkg{l3basics}.
3102 % \begin{macrocode}
3103 \cs_generate_variant:Nn \tl_to_str:n { o , V , v , e }
3104 % \end{macrocode}
3105 % \end{macro}
3106 %
3107 % \begin{macro}{\tl_to_str:N, \tl_to_str:c}
3108 % These functions return the replacement text of a token list as a
3109 % string.
3110 % \begin{macrocode}
3111 \cs_new:Npn \tl_to_str:N #1 { \__kernel_tl_to_str:w \exp_after:wN {#1} }
3112 \cs_generate_variant:Nn \tl_to_str:N { c }
3113 % \end{macrocode}
3114 % \end{macro}
3115 %
3116 % \begin{macro}{\tl_use:N, \tl_use:c}
3117 % Token lists which are simply not defined give a clear \TeX{}
3118 % error here. No such luck for ones equal to \cs{scan_stop:} so
3119 % instead a test is made and if there is an issue an error is forced.
3120 % \begin{macrocode}
3121 \cs_new:Npn \tl_use:N #1
3122 {
3123 \tl_if_exist:NTF #1 {#1}
3124 {
3125 \msg_expandable_error:nnn
3126 { kernel } { bad-variable } {#1}
3127 }
3128 }
3129 \cs_generate_variant:Nn \tl_use:N { c }
3130 % \end{macrocode}
3131 % \end{macro}
3132 %
3133 % \subsection{Working with the contents of token lists}
3134 %
3135 % \begin{macro}{\tl_count:n, \tl_count:V, \tl_count:v, \tl_count:e, \tl_count:o}
3136 % \begin{macro}{\tl_count:N, \tl_count:c}
3137 % \begin{macro}{\@@_count:n}
3138 % Count number of elements within a token list or token list
3139 % variable. Brace groups within the list are read as a single
3140 % element. Spaces are ignored.
3141 % \cs{@@_count:n} grabs the element and replaces it by |+1|.
3142 % The |0| ensures that it works on an empty list.
3143 % \begin{macrocode}
3144 \cs_new:Npn \tl_count:n #1
3145 {
3146 \int_eval:n
3147 { 0 \tl_map_function:nN {#1} \@@_count:n }
3148 }
3149 \cs_new:Npn \tl_count:N #1
3150 {
3151 \int_eval:n
3152 { 0 \tl_map_function:NN #1 \@@_count:n }
3153 }
3154 \cs_new:Npn \@@_count:n #1 { + 1 }
3155 \cs_generate_variant:Nn \tl_count:n { V , v , e , o }
3156 \cs_generate_variant:Nn \tl_count:N { c }
3157 % \end{macrocode}
3158 % \end{macro}
3159 % \end{macro}
3160 % \end{macro}
3161 %
3162 %
3163 % \begin{macro}[EXP]{\tl_count_tokens:n}
3164 % \begin{macro}[EXP]{\@@_act_count_normal:nN,
3165 % \@@_act_count_group:nn, \@@_act_count_space:n}
3166 % The token count is computed through an \cs{int_eval:n} construction.
3167 % Each \texttt{1+} is output to the \emph{left}, into the integer
3168 % expression, and the sum is ended by the \cs{exp_end:} inserted by
3169 % \cs{@@_act_end:wn} (which is technically implemented as \cs{c_zero_int}).
3170 % Somewhat a hack!
3171 % \begin{macrocode}
3172 \cs_new:Npn \tl_count_tokens:n #1
3173 {
3174 \int_eval:n
3175 {
3176 \@@_act:NNNn
3177 \@@_act_count_normal:N
3178 \@@_act_count_group:n
3179 \@@_act_count_space:
3180 {#1}
3181 }
3182 }
3183 \cs_new:Npn \@@_act_count_normal:N #1 { 1 + }
3184 \cs_new:Npn \@@_act_count_space: { 1 + }
3185 \cs_new:Npn \@@_act_count_group:n #1 { 2 + \tl_count_tokens:n {#1} + }
3186 % \end{macrocode}
3187 % \end{macro}
3188 % \end{macro}
3189 %
3190 % \begin{macro}{\tl_reverse_items:n}
3191 % \begin{macro}{\@@_reverse_items:nwNwn}
3192 % \begin{macro}{\@@_reverse_items:wn}
3193 % Reversal of a token list is done by taking one item at a time
3194 % and putting it after \cs{s_@@_stop}.
3195 % \begin{macrocode}
3196 \cs_new:Npn \tl_reverse_items:n #1
3197 {
3198 \@@_reverse_items:nwNwn #1 ?
3199 \s_@@_mark \@@_reverse_items:nwNwn
3200 \s_@@_mark \@@_reverse_items:wn
3201 \s_@@_stop { }
3202 }
3203 \cs_new:Npn \@@_reverse_items:nwNwn #1 #2 \s_@@_mark #3 #4 \s_@@_stop #5
3204 {
3205 #3 #2
3206 \s_@@_mark \@@_reverse_items:nwNwn
3207 \s_@@_mark \@@_reverse_items:wn
3208 \s_@@_stop { {#1} #5 }
3209 }
3210 \cs_new:Npn \@@_reverse_items:wn #1 \s_@@_stop #2
3211 { \__kernel_exp_not:w \exp_after:wN { \use_none:nn #2 } }
3212 % \end{macrocode}
3213 % \end{macro}
3214 % \end{macro}
3215 % \end{macro}
3216 %
3217 % \begin{macro}
3218 % {
3219 % \tl_trim_spaces:n, \tl_trim_spaces:V, \tl_trim_spaces:v,
3220 % \tl_trim_spaces:e, \tl_trim_spaces:o,
3221 % \tl_trim_left_spaces:n, \tl_trim_left_spaces:V, \tl_trim_left_spaces:v,
3222 % \tl_trim_left_spaces:e, \tl_trim_left_spaces:o,
3223 % \tl_trim_right_spaces:n, \tl_trim_right_spaces:V, \tl_trim_right_spaces:v,
3224 % \tl_trim_right_spaces:e, \tl_trim_right_spaces:o
3225 % }
3226 % \begin{macro}
3227 % {
3228 % \tl_trim_spaces_apply:nN, \tl_trim_spaces_apply:oN,
3229 % \tl_trim_left_spaces_apply:nN, \tl_trim_left_spaces_apply:oN,
3230 % \tl_trim_right_spaces_apply:nN, \tl_trim_right_spaces_apply:oN
3231 % }
3232 % \begin{macro}
3233 % {
3234 % \tl_trim_spaces:N, \tl_trim_spaces:c,
3235 % \tl_trim_left_spaces:N, \tl_trim_left_spaces:c,
3236 % \tl_trim_right_spaces:N, \tl_trim_right_spaces:c,
3237 % \tl_gtrim_spaces:N, \tl_gtrim_spaces:c,
3238 % \tl_gtrim_left_spaces:N, \tl_gtrim_left_spaces:c,
3239 % \tl_gtrim_right_spaces:N, \tl_gtrim_right_spaces:c
3240 % }
3241 % Trimming spaces from around the input is deferred to an internal
3242 % function whose first argument is a \meta{continuation}, which receives
3243 % as a braced argument \cs{@@_trim_mark:} \meta{trimmed token list}, and
3244 % whose second argument is the token list to trim. The control sequence
3245 % \cs{@@_trim_mark:} expands to nothing in a single expansion. In the case
3246 % at hand, we take \cs{__kernel_exp_not:w} \cs{exp_after:wN} as our
3247 % continuation, so that space trimming behaves correctly within an
3248 % \texttt{e}-type or \texttt{x}-type expansion.
3249 % \begin{macrocode}
3250 \cs_new:Npn \tl_trim_spaces:n
3251 { \@@_trim_spaces:nn { \__kernel_exp_not:w \exp_after:wN } }
3252 \cs_new:Npn \tl_trim_left_spaces:n
3253 { \@@_trim_left_spaces:nn { \__kernel_exp_not:w \exp_after:wN } }
3254 \cs_new:Npn \tl_trim_right_spaces:n
3255 { \@@_trim_right_spaces:nn { \__kernel_exp_not:w \exp_after:wN } }
3256 \cs_generate_variant:Nn \tl_trim_spaces:n { V , v , e , o }
3257 \cs_generate_variant:Nn \tl_trim_left_spaces:n { V , v , e , o }
3258 \cs_generate_variant:Nn \tl_trim_right_spaces:n { V , v , e , o }
3259 \cs_new:Npn \tl_trim_spaces_apply:nN #1#2
3260 { \@@_trim_spaces:nn { \exp_args:No #2 } { #1 } }
3261 \cs_new:Npn \tl_trim_left_spaces_apply:nN #1#2
3262 { \@@_trim_left_spaces:nn { \exp_args:No #2 } { #1 } }
3263 \cs_new:Npn \tl_trim_right_spaces_apply:nN #1#2
3264 { \@@_trim_right_spaces:nn { \exp_args:No #2 } { #1 } }
3265 \cs_generate_variant:Nn \tl_trim_spaces_apply:nN { o }
3266 \cs_generate_variant:Nn \tl_trim_left_spaces_apply:nN { o }
3267 \cs_generate_variant:Nn \tl_trim_right_spaces_apply:nN { o }
3268 \cs_new_protected:Npn \tl_trim_spaces:N #1
3269 { \__kernel_tl_set:Nx #1 { \exp_args:No \tl_trim_spaces:n {#1} } }
3270 \cs_new_protected:Npn \tl_trim_left_spaces:N #1
3271 { \__kernel_tl_set:Nx #1 { \exp_args:No \tl_trim_left_spaces:n {#1} } }
3272 \cs_new_protected:Npn \tl_trim_right_spaces:N #1
3273 { \__kernel_tl_set:Nx #1 { \exp_args:No \tl_trim_right_spaces:n {#1} } }
3274 \cs_new_protected:Npn \tl_gtrim_spaces:N #1
3275 { \__kernel_tl_gset:Nx #1 { \exp_args:No \tl_trim_spaces:n {#1} } }
3276 \cs_new_protected:Npn \tl_gtrim_left_spaces:N #1
3277 { \__kernel_tl_gset:Nx #1 { \exp_args:No \tl_trim_left_spaces:n {#1} } }
3278 \cs_new_protected:Npn \tl_gtrim_right_spaces:N #1
3279 { \__kernel_tl_gset:Nx #1 { \exp_args:No \tl_trim_right_spaces:n {#1} } }
3280 \cs_generate_variant:Nn \tl_trim_spaces:N { c }
3281 \cs_generate_variant:Nn \tl_trim_left_spaces:N { c }
3282 \cs_generate_variant:Nn \tl_trim_right_spaces:N { c }
3283 \cs_generate_variant:Nn \tl_gtrim_spaces:N { c }
3284 \cs_generate_variant:Nn \tl_gtrim_left_spaces:N { c }
3285 \cs_generate_variant:Nn \tl_gtrim_right_spaces:N { c }
3286 % \end{macrocode}
3287 % \end{macro}
3288 % \end{macro}
3289 %
3290 % \begin{macro}
3291 % {
3292 % \@@_trim_spaces:nn,
3293 % \@@_trim_left_spaces:nn, \@@_trim_right_spaces:nn
3294 % }
3295 % \begin{macro}
3296 % {
3297 % \@@_trim_spaces_auxi:w, \@@_trim_spaces_auxii:w,
3298 % \@@_trim_spaces_auxiii:w, \@@_trim_spaces_auxiv:w,
3299 % \@@_trim_spaces_auxv:w
3300 % }
3301 % \begin{macro}{\@@_trim_mark:}
3302 % Trimming spaces from around the input is done using delimited
3303 % arguments and \cs{@@_trim_mark:}, and to get spaces at odd places in the
3304 % definitions, we nest those in \cs{@@_tmp:w}, which then receives
3305 % a single space as its argument: |#1| is \verb*+ +.
3306 % Removing leading spaces is done with \cs{@@_trim_spaces_auxi:w},
3307 % which loops until \cs{@@_trim_mark:}\verb*+ + matches the end of the token
3308 % list: then |##1| is
3309 % \begin{quote}
3310 % \cs{@@_trim_mark:} \meta{left-trimmed token list} \cs{s_@@_nil}
3311 % \cs{@@_trim_mark:} \cs{@@_trim_spaces_auxi:w}
3312 % \end{quote}
3313 % and |##3| is \cs{@@_trim_spaces_auxii:w}.
3314 % This hands the relevant tokens to the
3315 % loop \cs{@@_trim_spaces_auxiii:w}, responsible for trimming
3316 % trailing spaces. The end is reached when \verb*+ +\cs{s_@@_nil}
3317 % matches the one present in the definition of \cs{tl_trim_spaces:n}.
3318 % Then \cs{@@_trim_spaces_auxiv:w} puts the token list into a group,
3319 % with a lingering \cs{@@_trim_mark:} at the start (which will expand to
3320 % nothing in one step of expansion), and feeds this to the
3321 % \meta{continuation}.
3322 %
3323 % Trimming just leading spaces (see \cs{@@_trim_left_spaces:nn}) also
3324 % starts with \cs{@@_trim_spaces_auxi:w}, but when it loops until
3325 % \cs{@@_trim_mark:}\verb*+ + matches the end of the token list, |##1|
3326 % is the same as in the \cs{@@_trim_spaces:nn} case, \emph{but} |##3|
3327 % is \cs{@@_trim_spaces_auxv:w}.
3328 % Like \cs{@@_trim_spaces_auxiv:w}, it hands the relevant
3329 % tokens to the \meta{continuation}.
3330 % Trimming just trailing spaces (see \cs{@@_trim_right_spaces:nn})
3331 % starts with the (second) loop \cs{@@_trim_spaces_auxiii:w}.
3332 % \begin{macrocode}
3333 \cs_set_protected:Npn \@@_tmp:w #1
3334 {
3335 \cs_new:Npn \@@_trim_spaces:nn ##1##2
3336 {
3337 \@@_trim_spaces_auxi:w
3338 \@@_trim_mark: ##2 \s_@@_nil
3339 \@@_trim_mark: \@@_trim_spaces_auxi:w
3340 \@@_trim_mark: #1
3341 \@@_trim_mark: \@@_trim_spaces_auxii:w
3342 {##1}
3343 }
3344 \cs_new:Npn \@@_trim_left_spaces:nn ##1##2
3345 {
3346 \@@_trim_spaces_auxi:w
3347 \@@_trim_mark: ##2 \s_@@_nil
3348 \@@_trim_mark: \@@_trim_spaces_auxi:w
3349 \@@_trim_mark: #1
3350 \@@_trim_mark: \@@_trim_spaces_auxv:w
3351 {##1}
3352 }
3353 \cs_new:Npn \@@_trim_right_spaces:nn ##1##2
3354 {
3355 \@@_trim_spaces_auxiii:w
3356 \@@_trim_mark: ##2 \s_@@_nil \@@_trim_spaces_auxiii:w
3357 #1 \s_@@_nil \@@_trim_spaces_auxiv:w
3358 {##1}
3359 }
3360 \cs_new:Npn \@@_trim_spaces_auxi:w
3361 ##1 \@@_trim_mark: #1 ##2 \@@_trim_mark: ##3
3362 { ##3 ##1 \@@_trim_mark: ##2 \@@_trim_mark: \@@_trim_spaces_auxi:w }
3363 \cs_new:Npn \@@_trim_spaces_auxii:w
3364 \@@_trim_mark: ##1 \@@_trim_mark: ##2 \@@_trim_spaces_auxi:w
3365 \@@_trim_mark: \@@_trim_mark: \@@_trim_spaces_auxi:w
3366 {
3367 \@@_trim_spaces_auxiii:w
3368 \@@_trim_mark: ##1 \@@_trim_spaces_auxiii:w
3369 #1 \s_@@_nil \@@_trim_spaces_auxiv:w
3370 }
3371 \cs_new:Npn \@@_trim_spaces_auxiii:w ##1 #1 \s_@@_nil ##2
3372 { ##2 ##1 \s_@@_nil \@@_trim_spaces_auxiii:w }
3373 \cs_new:Npn \@@_trim_spaces_auxiv:w
3374 ##1 \s_@@_nil
3375 \@@_trim_spaces_auxiii:w \s_@@_nil \@@_trim_spaces_auxiii:w
3376 ##2
3377 { ##2 {##1} }
3378 \cs_new:Npn \@@_trim_spaces_auxv:w
3379 ##1 \s_@@_nil
3380 \@@_trim_mark: \@@_trim_spaces_auxi:w \@@_trim_mark:
3381 \@@_trim_mark: \@@_trim_spaces_auxi:w ##2
3382 { ##2 {##1} }
3383 \cs_new:Npn \@@_trim_mark: {}
3384 }
3385 \@@_tmp:w { ~ }
3386 % \end{macrocode}
3387 % \end{macro}
3388 % \end{macro}
3389 % \end{macro}
3390 % \end{macro}
3391 %
3392 % \begin{macro}
3393 % {\tl_sort:Nn, \tl_sort:cn, \tl_gsort:Nn, \tl_gsort:cn, \tl_sort:nN}
3394 % Implemented in \pkg{l3sort}.
3395 % \end{macro}
3396 %
3397 % \subsection{The first token from a token list}
3398 %
3399 % \begin{macro}{\tl_head:N, \tl_head:n, \tl_head:V, \tl_head:v, \tl_head:f, \tl_head:e}
3400 % \begin{macro}{\@@_head_auxi:nw, \@@_head_auxii:n}
3401 % \begin{macro}{\tl_head:w,\@@_tl_head:w}
3402 % \begin{macro}{\tl_tail:N, \tl_tail:n, \tl_tail:V, \tl_tail:v, \tl_tail:f, \tl_tail:e}
3403 % Finding the head of a token list expandably always strips braces, which
3404 % is fine as this is consistent with for example mapping over a list. The
3405 % empty brace groups in \cs{tl_head:n} ensure that a blank argument gives an
3406 % empty result. The result is returned within the \tn{unexpanded} primitive.
3407 % The approach here is to use \cs{if_false:} to allow us to use |}| as
3408 % the closing delimiter: this is the only safe choice, as any other token
3409 % would not be able to parse it's own code. More detail in
3410 % \url{http://tex.stackexchange.com/a/70168}.
3411 % \begin{macrocode}
3412 \cs_new:Npn \tl_head:n #1
3413 {
3414 \__kernel_exp_not:w \tex_expanded:D
3415 { { \if_false: { \fi: \@@_head_aux:n #1 { } } } }
3416 }
3417 \cs_new:Npn \@@_head_aux:n #1
3418 {
3419 \__kernel_exp_not:w {#1}
3420 \exp_after:wN \use_none:n \exp_after:wN { \if_false: } \fi:
3421 }
3422 \cs_generate_variant:Nn \tl_head:n { V , v , f , e }
3423 \cs_new:Npn \tl_head:w #1#2 \q_stop {#1}
3424 \cs_new:Npn \@@_tl_head:w #1#2 \s_@@_stop {#1}
3425 \cs_new:Npn \tl_head:N { \exp_args:No \tl_head:n }
3426 % \end{macrocode}
3427 % To correctly leave the tail of a token list, it's important \emph{not} to
3428 % absorb any of the tail part as an argument. For example, the simple
3429 % definition
3430 % \begin{verbatim}
3431 % \cs_new:Npn \tl_tail:n #1 { \tl_tail:w #1 \q_stop }
3432 % \cs_new:Npn \tl_tail:w #1#2 \q_stop
3433 % \end{verbatim}
3434 % would give the wrong result for |\tl_tail:n { a { bc } }| (the braces would
3435 % be stripped). Thus the only safe way to proceed is to first check that
3436 % there is an item to grab (i.e., that the argument is not blank) and
3437 % assuming there is to dispose of the first item. As with \cs{tl_head:n},
3438 % the result is protected from further expansion by \tn{unexpanded}.
3439 % While we could optimize the test here, this would leave some tokens
3440 % \enquote{banned} in the input, which we do not have with this definition.
3441 % \begin{macrocode}
3442 \exp_args:Nno \use:n { \cs_new:Npn \tl_tail:n #1 }
3443 {
3444 \exp_after:wN \__kernel_exp_not:w
3445 \tl_if_blank:nTF {#1}
3446 { { } }
3447 { \exp_after:wN { \use_none:n #1 } }
3448 }
3449 \cs_generate_variant:Nn \tl_tail:n { V , v , f , e }
3450 \cs_new:Npn \tl_tail:N { \exp_args:No \tl_tail:n }
3451 % \end{macrocode}
3452 % \end{macro}
3453 % \end{macro}
3454 % \end{macro}
3455 % \end{macro}
3456 %
3457 % \begin{macro}[pTF]
3458 % {
3459 % \tl_if_head_eq_meaning:nN, \tl_if_head_eq_meaning:VN,
3460 % \tl_if_head_eq_meaning:eN
3461 % }
3462 % \begin{macro}[pTF]
3463 % {
3464 % \tl_if_head_eq_charcode:nN, \tl_if_head_eq_charcode:VN,
3465 % \tl_if_head_eq_charcode:eN, \tl_if_head_eq_charcode:fN
3466 % }
3467 % \begin{macro}[pTF]
3468 % {
3469 % \tl_if_head_eq_catcode:nN, \tl_if_head_eq_catcode:VN,
3470 % \tl_if_head_eq_catcode:eN, \tl_if_head_eq_catcode:oN
3471 % }
3472 % \begin{macro}[EXP]{\@@_head_exp_not:w}
3473 % \begin{macro}[EXP]{\@@_if_head_eq_empty_arg:w}
3474 % Accessing the first token of a token list is tricky in three cases:
3475 % when it has category code $1$ (begin-group token), when it is an
3476 % explicit space, with category code $10$ and character code $32$, or
3477 % when the token list is empty (obviously).
3478 %
3479 % Forgetting temporarily about this issue we would use the following
3480 % test in \cs{tl_if_head_eq_charcode:nN}. Here, \cs{tl_head:w} yields
3481 % the first token of the token list, then passed to \cs{exp_not:N}.
3482 % \begin{verbatim}
3483 % \if_charcode:w
3484 % \exp_after:wN \exp_not:N \tl_head:w #1 \q_nil \q_stop
3485 % \exp_not:N #2
3486 % \end{verbatim}
3487 % The two first special cases are detected by testing if the token
3488 % list starts with an \texttt{N}-type token (the extra |?| sends empty
3489 % token lists to the \texttt{true} branch of this test). In those
3490 % cases, the first token is a character, and since we only care about
3491 % its character code, we can use \cs{str_head:n} to access it (this
3492 % works even if it is a space character). An empty argument
3493 % results in \cs{tl_head:w} leaving two token: |^| and
3494 % \cs{@@_if_head_eq_empty_arg:w} which will result in the \cs{if_charcode:w}
3495 % test being false and remove \cs{exp_not:N} and |#2|.
3496 % \begin{macrocode}
3497 \prg_new_conditional:Npnn \tl_if_head_eq_charcode:nN #1#2 { p , T , F , TF }
3498 {
3499 \if_charcode:w
3500 \tl_if_head_is_N_type:nTF { #1 ? }
3501 { \@@_head_exp_not:w #1 { ^ \@@_if_head_eq_empty_arg:w } \s_@@_stop }
3502 { \str_head:n {#1} }
3503 \exp_not:N #2
3504 \prg_return_true:
3505 \else:
3506 \prg_return_false:
3507 \fi:
3508 }
3509 \prg_generate_conditional_variant:Nnn \tl_if_head_eq_charcode:nN
3510 { V , e , f } { p , TF , T , F }
3511 % \end{macrocode}
3512 % For \cs{tl_if_head_eq_catcode:nN}, again we detect special cases
3513 % with a \cs{tl_if_head_is_N_type:n}. Then we need to test if the
3514 % first token is a begin-group token or an explicit space token, and
3515 % produce the relevant token, either \cs{c_group_begin_token} or
3516 % \cs{c_space_token}. Again, for an empty argument, a hack is used,
3517 % removing the token given by the user and leaving two tokens in the input
3518 % stream which will make the \cs{if_catcode:w} test return \texttt{false}.
3519 % \begin{macrocode}
3520 \prg_new_conditional:Npnn \tl_if_head_eq_catcode:nN #1 #2 { p , T , F , TF }
3521 {
3522 \if_catcode:w
3523 \tl_if_head_is_N_type:nTF { #1 ? }
3524 { \@@_head_exp_not:w #1 { ^ \@@_if_head_eq_empty_arg:w } \s_@@_stop }
3525 {
3526 \tl_if_head_is_group:nTF {#1}
3527 \c_group_begin_token
3528 \c_space_token
3529 }
3530 \exp_not:N #2
3531 \prg_return_true:
3532 \else:
3533 \prg_return_false:
3534 \fi:
3535 }
3536 \prg_generate_conditional_variant:Nnn \tl_if_head_eq_catcode:nN
3537 { V , e , o } { p , TF , T , F }
3538 % \end{macrocode}
3539 % For \cs{tl_if_head_eq_meaning:nN}, again, detect special cases. In
3540 % the normal case, use \cs{tl_head:w}, with no \cs{exp_not:N} this
3541 % time, since \cs{if_meaning:w} causes no expansion. With an empty
3542 % argument, the test is \texttt{true}, and \cs{use_none:nnn} removes
3543 % |#2| and \cs{prg_return_true:} and \cs{else:} (it is safe this way here as
3544 % in this case \cs{prg_new_conditional:Npnn} didn't optimize these two away).
3545 % In the special cases, we know that the first token is a character,
3546 % hence \cs{if_charcode:w} and \cs{if_catcode:w} together are enough.
3547 % We combine them in some order, hopefully faster than the reverse.
3548 % Tests are not nested because the arguments may contain unmatched
3549 % primitive conditionals.
3550 % \begin{macrocode}
3551 \prg_new_conditional:Npnn \tl_if_head_eq_meaning:nN #1#2 { p , T , F , TF }
3552 {
3553 \tl_if_head_is_N_type:nTF { #1 ? }
3554 \@@_if_head_eq_meaning_normal:nN
3555 \@@_if_head_eq_meaning_special:nN
3556 {#1} #2
3557 }
3558 \prg_generate_conditional_variant:Nnn \tl_if_head_eq_meaning:nN
3559 { V , e } { p , TF , T , F }
3560 \cs_new:Npn \@@_if_head_eq_meaning_normal:nN #1 #2
3561 {
3562 \exp_after:wN \if_meaning:w
3563 \@@_tl_head:w #1 { ?? \use_none:nnn } \s_@@_stop #2
3564 \prg_return_true:
3565 \else:
3566 \prg_return_false:
3567 \fi:
3568 }
3569 \cs_new:Npn \@@_if_head_eq_meaning_special:nN #1 #2
3570 {
3571 \if_charcode:w \str_head:n {#1} \exp_not:N #2
3572 \exp_after:wN \use_ii:nn
3573 \else:
3574 \prg_return_false:
3575 \fi:
3576 \use_none:n
3577 {
3578 \if_catcode:w \exp_not:N #2
3579 \tl_if_head_is_group:nTF {#1}
3580 { \c_group_begin_token }
3581 { \c_space_token }
3582 \prg_return_true:
3583 \else:
3584 \prg_return_false:
3585 \fi:
3586 }
3587 }
3588 % \end{macrocode}
3589 %
3590 % Both \cs{tl_if_head_eq_charcode:nN} and \cs{tl_if_head_eq_catcode:nN} will
3591 % need to get the first token of their argument and apply \cs{exp_not:N} to
3592 % it. \cs{@@_head_exp_not:w} does exactly that.
3593 % \begin{macrocode}
3594 \cs_new:Npn \@@_head_exp_not:w #1 #2 \s_@@_stop
3595 { \exp_not:N #1 }
3596 % \end{macrocode}
3597 %
3598 % If the argument of \cs{tl_if_head_eq_charcode:nN} and
3599 % \cs{tl_if_head_eq_catcode:nN} was empty \cs{@@_if_head_eq_empty_arg:w} will
3600 % be left in the input stream. This macro has to remove \cs{exp_not:N} and
3601 % the following token from the input stream to make sure no unbalanced
3602 % if-construct is created and leave tokens there which make the two tests
3603 % return \texttt{false}.
3604 % \begin{macrocode}
3605 \cs_new:Npn \@@_if_head_eq_empty_arg:w \exp_not:N #1
3606 { ? }
3607 % \end{macrocode}
3608 % \end{macro}
3609 % \end{macro}
3610 % \end{macro}
3611 % \end{macro}
3612 % \end{macro}
3613 %
3614 % \begin{macro}[pTF]{\tl_if_head_is_N_type:n}
3615 % \begin{macro}[EXP]
3616 % {
3617 % \@@_if_head_is_N_type_auxi:w ,
3618 % \@@_if_head_is_N_type_auxii:n ,
3619 % }
3620 % A token list can be empty, can start with an explicit space
3621 % character (catcode 10 and charcode 32), can start with a begin-group
3622 % token (catcode 1), or start with an \texttt{N}-type argument. In
3623 % the first two cases, and when |#1~| starts with |{}~|,
3624 % \cs{@@_if_head_is_N_type_auxi:w} receives an empty argument hence
3625 % produces |f| and removes everything before the first
3626 % \cs{scan_stop:}. In the third case (except when |#1~| starts with
3627 % |{}~|), the second auxiliary removes the first copy of~|#1| that was
3628 % used for the space test, then expands \cs{token_to_str:N} which hits
3629 % the leading begin-group token, leaving a single closing brace to be
3630 % compared with \cs{scan_stop:}. In the last case,
3631 % \cs{token_to_str:N} does not change the brace balance so that only
3632 % \cs{scan_stop:} \cs{scan_stop:} remain, making the character code
3633 % test true. One cannot optimize by
3634 % moving one of the \cs{scan_stop:} to the beginning: if |#1| contains
3635 % primitive conditionals, all of its occurrences must be dealt with before
3636 % the \cs{if:w} tries to skip the \texttt{true} branch of the
3637 % conditional.
3638 % \begin{macrocode}
3639 \prg_new_conditional:Npnn \tl_if_head_is_N_type:n #1 { p , T , F , TF }
3640 {
3641 \if:w
3642 \if_false: { \fi: \@@_if_head_is_N_type_auxi:w #1 ~ }
3643 { \exp_after:wN { \token_to_str:N #1 } }
3644 \scan_stop: \scan_stop:
3645 \prg_return_true:
3646 \else:
3647 \prg_return_false:
3648 \fi:
3649 }
3650 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_head_is_N_type_auxi:w #1 ~ }
3651 {
3652 \tl_if_empty:nTF {#1}
3653 { f \exp_after:wN \use_none:nn }
3654 { \exp_after:wN \@@_if_head_is_N_type_auxii:n }
3655 \exp_after:wN { \if_false: } \fi:
3656 }
3657 \cs_new:Npn \@@_if_head_is_N_type_auxii:n #1
3658 { \exp_after:wN \use_none:n \exp_after:wN }
3659 % \end{macrocode}
3660 % \end{macro}
3661 % \end{macro}
3662 %
3663 % \begin{macro}[EXP,pTF]{\tl_if_head_is_group:n}
3664 % \begin{macro}[EXP]{\@@_if_head_is_group_fi_false:w}
3665 % Pass the first token of |#1| through \cs{token_to_str:N}, then check
3666 % for the brace balance. The extra \texttt{?} caters for an empty
3667 % argument. This could be made faster, but we need all brace tricks
3668 % to happen in one step of expansion, keeping the token list brace
3669 % balanced at all times.
3670 % \begin{macrocode}
3671 \prg_new_conditional:Npnn \tl_if_head_is_group:n #1 { p , T , F , TF }
3672 {
3673 \if:w
3674 \exp_after:wN \use_none:n
3675 \exp_after:wN { \exp_after:wN { \token_to_str:N #1 ? } }
3676 \scan_stop: \scan_stop:
3677 \@@_if_head_is_group_fi_false:w
3678 \fi:
3679 \if_true:
3680 \prg_return_true:
3681 \else:
3682 \prg_return_false:
3683 \fi:
3684 }
3685 \cs_new:Npn \@@_if_head_is_group_fi_false:w \fi: \if_true: { \fi: \if_false: }
3686 % \end{macrocode}
3687 % \end{macro}
3688 % \end{macro}
3689 %
3690 % \begin{macro}[EXP,pTF]{\tl_if_head_is_space:n}
3691 % \begin{macro}[EXP]{\@@_if_head_is_space:w}
3692 % The auxiliary's argument is all that is before the first explicit
3693 % space in |\prg_do_nothing:#1?~|. If that is a single~|\prg_do_nothing:| the
3694 % test yields \texttt{true}. Otherwise, that is more than one token, and the
3695 % test yields \texttt{false}. The work is done within braces (with an
3696 % |\if_false: { \fi: ... }| construction) both to hide potential
3697 % alignment tab characters from \TeX{} in a table, and to allow for
3698 % removing what remains of the token list after its first space. The use of
3699 % \cs{if:w} ensures that the result of a single step of expansion directly
3700 % yields a balanced token list (no trailing closing brace).
3701 % \begin{macrocode}
3702 \prg_new_conditional:Npnn \tl_if_head_is_space:n #1 { p , T , F , TF }
3703 {
3704 \if:w
3705 \if_false: { \fi: \@@_if_head_is_space:w \prg_do_nothing: #1 ? ~ }
3706 \scan_stop: \scan_stop:
3707 \prg_return_true:
3708 \else:
3709 \prg_return_false:
3710 \fi:
3711 }
3712 \exp_args:Nno \use:n { \cs_new:Npn \@@_if_head_is_space:w #1 ~ }
3713 {
3714 \@@_if_empty_if:o {#1} \else: f \fi:
3715 \exp_after:wN \use_none:n \exp_after:wN { \if_false: } \fi:
3716 }
3717 % \end{macrocode}
3718 % \end{macro}
3719 % \end{macro}
3720 %
3721 % \subsection{Token by token changes}
3722 %
3723 % \begin{variable}{\s_@@_act_stop}
3724 % The \cs[no-index]{@@_act_\ldots{}} functions may be applied to any token list.
3725 % Hence, we use a private quark, to allow any token, even quarks,
3726 % in the token list.
3727 % Only \cs{s_@@_act_stop} may not appear in the token lists manipulated by
3728 % \cs{@@_act:NNNn} functions.
3729 % \begin{macrocode}
3730 \scan_new:N \s_@@_act_stop
3731 % \end{macrocode}
3732 % \end{variable}
3733 %
3734 % \begin{macro}[EXP]{\@@_act:NNNn}
3735 % \begin{macro}[EXP]{\@@_act_output:n, \@@_act_reverse_output:n}
3736 % \begin{macro}[EXP]{\@@_act_loop:w}
3737 % \begin{macro}[EXP]{\@@_act_normal:NwNNN}
3738 % \begin{macro}[EXP]{\@@_act_group:nwNNN}
3739 % \begin{macro}[EXP]{\@@_act_space:wwNNN}
3740 % \begin{macro}[EXP]{\@@_act_end:wn}
3741 % \begin{macro}[EXP]
3742 % {
3743 % \@@_act_if_head_is_space:nTF,
3744 % \@@_act_if_head_is_space:w,
3745 % \@@_act_if_head_is_space_true:w
3746 % }
3747 % \begin{macro}[EXP]{\@@_use_none_delimit_by_q_act_stop:w}
3748 % To help control the expansion, \cs{@@_act:NNNn} should always
3749 % be preceded by \cs{exp:w} and ends by producing \cs{exp_end:}
3750 % once the result has been obtained. This way no internal token of it can be
3751 % accidentally end up in the input stream.
3752 % Because \cs{s_@@_act_stop} can't appear without braces around it in the
3753 % argument~|#1| of \cs{@@_act_loop:w}, we can use this marker to set up a fast
3754 % test for leading spaces.
3755 % \begin{macrocode}
3756 \cs_set_protected:Npn \@@_tmp:w #1
3757 {
3758 \cs_new:Npn \@@_act_if_head_is_space:nTF ##1
3759 {
3760 \@@_act_if_head_is_space:w
3761 \s_@@_act_stop ##1 \s_@@_act_stop \@@_act_if_head_is_space_true:w
3762 \s_@@_act_stop #1 \s_@@_act_stop \use_ii:nn
3763 }
3764 \cs_new:Npn \@@_act_if_head_is_space:w
3765 ##1 \s_@@_act_stop #1 ##2 \s_@@_act_stop
3766 {}
3767 \cs_new:Npn \@@_act_if_head_is_space_true:w
3768 \s_@@_act_stop #1 \s_@@_act_stop \use_ii:nn ##1 ##2
3769 {##1}
3770 }
3771 \@@_tmp:w { ~ }
3772 % \end{macrocode}
3773 % (We expand the definition \cs{@@_act_if_head_is_space:nTF} when
3774 % setting up \cs{@@_act_loop:w}, so we can then undefine the auxiliary.)
3775 % In the loop, we check how the token list begins and act
3776 % accordingly. In the \enquote{group} case, we may have
3777 % reached \cs{s_@@_act_stop}, the end of the list. Then
3778 % leave \cs{exp_end:} and the result in the input stream,
3779 % to terminate the expansion of \cs{exp:w}.
3780 % Otherwise, apply the relevant function to the
3781 % \enquote{arguments}, |#3|
3782 % and to the head of the token list. Then repeat the loop.
3783 % The scheme is the same if the token list starts with an |N|-type
3784 % or with a space, making sure that
3785 % \cs{@@_act_space:wwNNN} gobbles the space.
3786 % \begin{macrocode}
3787 \exp_args:Nne \use:n { \cs_new:Npn \@@_act_loop:w #1 \s_@@_act_stop }
3788 {
3789 \exp_not:o { \@@_act_if_head_is_space:nTF {#1} }
3790 \exp_not:N \@@_act_space:wwNNN
3791 {
3792 \exp_not:o { \tl_if_head_is_group:nTF {#1} }
3793 \exp_not:N \@@_act_group:nwNNN
3794 \exp_not:N \@@_act_normal:NwNNN
3795 }
3796 \exp_not:n {#1} \s_@@_act_stop
3797 }
3798 \cs_undefine:N \@@_act_if_head_is_space:nTF
3799 \cs_new:Npn \@@_act_normal:NwNNN #1 #2 \s_@@_act_stop #3
3800 {
3801 #3 #1
3802 \@@_act_loop:w #2 \s_@@_act_stop
3803 #3
3804 }
3805 \cs_new:Npn \@@_use_none_delimit_by_s_act_stop:w #1 \s_@@_act_stop { }
3806 \cs_new:Npn \@@_act_end:wn #1 \@@_act_result:n #2
3807 { \group_align_safe_end: \exp_end: #2 }
3808 \cs_new:Npn \@@_act_group:nwNNN #1 #2 \s_@@_act_stop #3#4#5
3809 {
3810 \@@_use_none_delimit_by_s_act_stop:w #1 \@@_act_end:wn \s_@@_act_stop
3811 #5 {#1}
3812 \@@_act_loop:w #2 \s_@@_act_stop
3813 #3 #4 #5
3814 }
3815 \exp_last_unbraced:NNo
3816 \cs_new:Npn \@@_act_space:wwNNN \c_space_tl #1 \s_@@_act_stop #2#3
3817 {
3818 #3
3819 \@@_act_loop:w #1 \s_@@_act_stop
3820 #2 #3
3821 }
3822 % \end{macrocode}
3823 % \cs{@@_act:NNNn} loops over tokens, groups, and spaces in |#4|.
3824 % |{\s_@@_act_stop}| serves as the end of token list marker, the |?| after it
3825 % avoids losing outer braces. The result is stored as an argument for the
3826 % dummy function \cs{@@_act_result:n}.
3827 % \begin{macrocode}
3828 \cs_new:Npn \@@_act:NNNn #1#2#3#4
3829 {
3830 \group_align_safe_begin:
3831 \@@_act_loop:w #4 { \s_@@_act_stop } ? \s_@@_act_stop
3832 #1 #3 #2
3833 \@@_act_result:n { }
3834 }
3835 % \end{macrocode}
3836 % Typically, the output is done to the right of what was already output,
3837 % using \cs{@@_act_output:n}, but for the \cs{@@_act_reverse} functions,
3838 % it should be done to the left.
3839 % \begin{macrocode}
3840 \cs_new:Npn \@@_act_output:n #1 #2 \@@_act_result:n #3
3841 { #2 \@@_act_result:n { #3 #1 } }
3842 \cs_new:Npn \@@_act_reverse_output:n #1 #2 \@@_act_result:n #3
3843 { #2 \@@_act_result:n { #1 #3 } }
3844 % \end{macrocode}
3845 % \end{macro}
3846 % \end{macro}
3847 % \end{macro}
3848 % \end{macro}
3849 % \end{macro}
3850 % \end{macro}
3851 % \end{macro}
3852 % \end{macro}
3853 % \end{macro}
3854 %
3855 % \begin{macro}[EXP]
3856 % {\tl_reverse:n, \tl_reverse:o, \tl_reverse:V, \tl_reverse:f, \tl_reverse:e}
3857 % \begin{macro}[EXP]{\@@_reverse_normal:nN}
3858 % \begin{macro}[EXP]{\@@_reverse_group_preserve:nn}
3859 % \begin{macro}[EXP]{\@@_reverse_space:n}
3860 % The goal here is to reverse without losing spaces nor braces.
3861 % This is done using the general internal function \cs{@@_act:NNNn}.
3862 % Spaces and \enquote{normal} tokens are output on the left of the current
3863 % output. Grouped tokens are output to the left but without any reversal
3864 % within the group.
3865 % \begin{macrocode}
3866 \cs_new:Npn \tl_reverse:n #1
3867 {
3868 \__kernel_exp_not:w \exp_after:wN
3869 {
3870 \exp:w
3871 \@@_act:NNNn
3872 \@@_reverse_normal:N
3873 \@@_reverse_group_preserve:n
3874 \@@_reverse_space:
3875 {#1}
3876 }
3877 }
3878 \cs_generate_variant:Nn \tl_reverse:n { o , V , f , e }
3879 \cs_new:Npn \@@_reverse_normal:N
3880 { \@@_act_reverse_output:n }
3881 \cs_new:Npn \@@_reverse_group_preserve:n #1
3882 { \@@_act_reverse_output:n { {#1} } }
3883 \cs_new:Npn \@@_reverse_space:
3884 { \@@_act_reverse_output:n { ~ } }
3885 % \end{macrocode}
3886 % \end{macro}
3887 % \end{macro}
3888 % \end{macro}
3889 % \end{macro}
3890 %
3891 % \begin{macro}{\tl_reverse:N, \tl_reverse:c, \tl_greverse:N, \tl_greverse:c}
3892 % This reverses the list, leaving \cs{exp_stop_f:} in front,
3893 % which stops the \texttt{f}-expansion.
3894 % \begin{macrocode}
3895 \cs_new_protected:Npn \tl_reverse:N #1
3896 { \__kernel_tl_set:Nx #1 { \exp_args:No \tl_reverse:n { #1 } } }
3897 \cs_new_protected:Npn \tl_greverse:N #1
3898 { \__kernel_tl_gset:Nx #1 { \exp_args:No \tl_reverse:n { #1 } } }
3899 \cs_generate_variant:Nn \tl_reverse:N { c }
3900 \cs_generate_variant:Nn \tl_greverse:N { c }
3901 % \end{macrocode}
3902 % \end{macro}
3903 %
3904 % \subsection{Using a single item}
3905 %
3906 % \begin{macro}{\tl_item:nn, \tl_item:Nn, \tl_item:cn}
3907 % \begin{macro}{\@@_item_aux:nn, \@@_item:nn}
3908 % The idea here is to find the offset of the item from the left, then use
3909 % a loop to grab the correct item. If the resulting offset is too large,
3910 % then \cs{@@_if_recursion_tail_break:nN} terminates the loop, and returns
3911 % nothing at all.
3912 % \begin{macrocode}
3913 \cs_new:Npn \tl_item:nn #1#2
3914 {
3915 \exp_args:Nf \@@_item:nn
3916 { \exp_args:Nf \@@_item_aux:nn { \int_eval:n {#2} } {#1} }
3917 #1
3918 \q_@@_recursion_tail
3919 \prg_break_point:
3920 }
3921 \cs_new:Npn \@@_item_aux:nn #1#2
3922 {
3923 \int_compare:nNnTF {#1} < 0
3924 { \int_eval:n { \tl_count:n {#2} + 1 + #1 } }
3925 {#1}
3926 }
3927 \cs_new:Npn \@@_item:nn #1#2
3928 {
3929 \@@_if_recursion_tail_break:nN {#2} \prg_break:
3930 \int_compare:nNnTF {#1} = 1
3931 { \prg_break:n { \exp_not:n {#2} } }
3932 { \exp_args:Nf \@@_item:nn { \int_eval:n { #1 - 1 } } }
3933 }
3934 \cs_new:Npn \tl_item:Nn { \exp_args:No \tl_item:nn }
3935 \cs_generate_variant:Nn \tl_item:Nn { c }
3936 % \end{macrocode}
3937 % \end{macro}
3938 % \end{macro}
3939 %
3940 % \begin{macro}{\tl_rand_item:n, \tl_rand_item:N, \tl_rand_item:c}
3941 % Importantly \cs{tl_item:nn} only evaluates its argument once.
3942 % \begin{macrocode}
3943 \cs_new:Npn \tl_rand_item:n #1
3944 {
3945 \tl_if_blank:nF {#1}
3946 { \tl_item:nn {#1} { \int_rand:nn { 1 } { \tl_count:n {#1} } } }
3947 }
3948 \cs_new:Npn \tl_rand_item:N { \exp_args:No \tl_rand_item:n }
3949 \cs_generate_variant:Nn \tl_rand_item:N { c }
3950 % \end{macrocode}
3951 % \end{macro}
3952 %
3953 % \begin{macro}{\__kernel_int_sep:}
3954 % See comments in the kernel functions file: has to be set up here as this
3955 % is the first module where it is needed.
3956 % \begin{macrocode}
3957 \cs_new_eq:NN \__kernel_int_sep: \tex_let:D
3958 % \end{macrocode}
3959 % \end{macro}
3960 %
3961 % \begin{macro}{\@@_sep:}
3962 % \begin{macrocode}
3963 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
3964 % \end{macrocode}
3965 % \end{macro}
3966 %
3967 % \begin{macro}{\tl_range:Nnn, \tl_range:cnn, \tl_range:nnn}
3968 % \begin{macro}
3969 % {
3970 % \@@_range:Nnnn, \@@_range:nnnNn, \@@_range:nnNn, \@@_range_skip:w,
3971 % \@@_range:w, \@@_range_skip_spaces:n, \@@_range_collect:nn,
3972 % \@@_range_collect:ff, \@@_range_collect_space:nw,
3973 % \@@_range_collect_N:nN, \@@_range_collect_group:nN,
3974 % }
3975 % To avoid checking for the end of the token list at every step, start
3976 % by counting the number $l$ of items and \enquote{normalizing} the
3977 % bounds, namely clamping them to the interval $[0,l]$ and dealing
3978 % with negative indices. More precisely, \cs{@@_range_items:nnNn}
3979 % receives the number of items to skip at the beginning of the token
3980 % list, the index of the last item to keep, a function which is either
3981 % \cs{@@_range:w} or the token list itself. If nothing should be kept,
3982 % leave |{}|: this stops the \texttt{f}-expansion of \cs{tl_head:f} and that
3983 % function produces an empty result. Otherwise, repeatedly call
3984 % \cs{@@_range_skip:w} to delete |#1| items from the input stream (the
3985 % extra brace group avoids an off-by-one shift). For the braced
3986 % version \cs{@@_range_braced:w} sets up
3987 % \cs{@@_range_collect_braced:w} which stores items one by one in an
3988 % argument after the \cs{@@_sep:}. Depending on the first token of the tail,
3989 % either just move it (if it is a space) or also decrement the number of
3990 % items left to find. Eventually, the result is a brace group followed by
3991 % the rest of the token list, and \cs{tl_head:f} cleans up and gives the
3992 % result in \cs{exp_not:n}.
3993 % \begin{macrocode}
3994 \cs_new:Npn \tl_range:Nnn { \exp_args:No \tl_range:nnn }
3995 \cs_generate_variant:Nn \tl_range:Nnn { c }
3996 \cs_new:Npn \tl_range:nnn { \@@_range:Nnnn \@@_range:w }
3997 \cs_new:Npn \@@_range:Nnnn #1#2#3#4
3998 {
3999 \tl_head:f
4000 {
4001 \exp_args:Nf \@@_range:nnnNn
4002 { \tl_count:n {#2} } {#3} {#4} #1 {#2}
4003 }
4004 }
4005 \cs_new:Npn \@@_range:nnnNn #1#2#3
4006 {
4007 \exp_args:Nff \@@_range:nnNn
4008 {
4009 \exp_args:Nf \@@_range_normalize:nn
4010 { \int_eval:n { #2 - 1 } } {#1}
4011 }
4012 {
4013 \exp_args:Nf \@@_range_normalize:nn
4014 { \int_eval:n {#3} } {#1}
4015 }
4016 }
4017 \cs_new:Npn \@@_range:nnNn #1#2#3#4
4018 {
4019 \if_int_compare:w #2 > #1 \exp_stop_f: \else:
4020 \exp_after:wN { \exp_after:wN }
4021 \fi:
4022 \exp_after:wN #3
4023 \int_value:w \int_eval:n { #2 - #1 } \exp_after:wN \@@_sep:
4024 \exp_after:wN { \exp:w \@@_range_skip:w #1 \@@_sep: { } #4 }
4025 }
4026 \cs_new:Npn \@@_range_skip:w #1 \@@_sep: #2
4027 {
4028 \if_int_compare:w #1 > \c_zero_int
4029 \exp_after:wN \@@_range_skip:w
4030 \int_value:w \int_eval:n { #1 - 1 } \exp_after:wN \@@_sep:
4031 \else:
4032 \exp_after:wN \exp_end:
4033 \fi:
4034 }
4035 \cs_new:Npn \@@_range:w #1 \@@_sep: #2
4036 {
4037 \exp_args:Nf \@@_range_collect:nn
4038 { \@@_range_skip_spaces:n {#2} } {#1}
4039 }
4040 \cs_new:Npn \@@_range_skip_spaces:n #1
4041 {
4042 \tl_if_head_is_space:nTF {#1}
4043 { \exp_args:Nf \@@_range_skip_spaces:n {#1} }
4044 { { } #1 }
4045 }
4046 \cs_new:Npn \@@_range_collect:nn #1#2
4047 {
4048 \int_compare:nNnTF {#2} = 0
4049 {#1}
4050 {
4051 \exp_args:No \tl_if_head_is_space:nTF { \use_none:n #1 }
4052 {
4053 \exp_args:Nf \@@_range_collect:nn
4054 { \@@_range_collect_space:nw #1 }
4055 {#2}
4056 }
4057 {
4058 \@@_range_collect:ff
4059 {
4060 \exp_args:No \tl_if_head_is_N_type:nTF { \use_none:n #1 }
4061 { \@@_range_collect_N:nN }
4062 { \@@_range_collect_group:nn }
4063 #1
4064 }
4065 { \int_eval:n { #2 - 1 } }
4066 }
4067 }
4068 }
4069 \cs_new:Npn \@@_range_collect_space:nw #1 ~ { { #1 ~ } }
4070 \cs_new:Npn \@@_range_collect_N:nN #1#2 { { #1 #2 } }
4071 \cs_new:Npn \@@_range_collect_group:nn #1#2 { { #1 {#2} } }
4072 \cs_generate_variant:Nn \@@_range_collect:nn { ff }
4073 % \end{macrocode}
4074 % \end{macro}
4075 % \end{macro}
4076 %
4077 % \begin{macro}[EXP]{\@@_range_normalize:nn}
4078 % This function converts an \meta{index} argument into an explicit
4079 % position in the token list (a result of $0$ denoting \enquote{out of
4080 % bounds}). Expects two explicit integer arguments: the \meta{index}
4081 % |#1| and the string count~|#2|. If |#1| is negative, replace it by
4082 % $|#1| + |#2| + 1$, then limit to the range $[0, |#2|]$.
4083 % \begin{macrocode}
4084 \cs_new:Npn \@@_range_normalize:nn #1#2
4085 {
4086 \int_eval:n
4087 {
4088 \if_int_compare:w #1 < \c_zero_int
4089 \if_int_compare:w #1 < -#2 \exp_stop_f:
4090 0
4091 \else:
4092 #1 + #2 + 1
4093 \fi:
4094 \else:
4095 \if_int_compare:w #1 < #2 \exp_stop_f:
4096 #1
4097 \else:
4098 #2
4099 \fi:
4100 \fi:
4101 }
4102 }
4103 % \end{macrocode}
4104 % \end{macro}
4105 %
4106 % \subsection{Viewing token lists}
4107 %
4108 % \begin{macro}{\tl_show:N, \tl_show:c, \tl_log:N, \tl_log:c, \@@_show:NN}
4109 % Showing token list variables is done after checking that the
4110 % variable is defined (see \cs{__kernel_register_show:N}).
4111 % \begin{macrocode}
4112 \cs_new_protected:Npn \tl_show:N { \@@_show:NN \tl_show:n }
4113 \cs_generate_variant:Nn \tl_show:N { c }
4114 \cs_new_protected:Npn \tl_log:N { \@@_show:NN \tl_log:n }
4115 \cs_generate_variant:Nn \tl_log:N { c }
4116 \cs_new_protected:Npn \@@_show:NN #1#2
4117 {
4118 \__kernel_chk_defined:NT #2
4119 {
4120 \exp_args:Nf \tl_if_empty:nTF
4121 { \cs_prefix_spec:N #2 \cs_parameter_spec:N #2 }
4122 {
4123 \exp_args:Ne #1
4124 { \token_to_str:N #2 = \__kernel_exp_not:w \exp_after:wN {#2} }
4125 }
4126 {
4127 \msg_error:nneee { kernel } { bad-type }
4128 { \token_to_str:N #2 } { \token_to_meaning:N #2 } { tl }
4129 }
4130 }
4131 }
4132 % \end{macrocode}
4133 % \end{macro}
4134 %
4135 % \begin{macro}{\tl_show:n, \tl_show:e, \tl_show:x, \@@_show:n}
4136 % \begin{macro}[EXP]{\@@_show:w}
4137 % Many |show| functions are based on \cs{tl_show:n}.
4138 % The argument of \cs{tl_show:n} is line-wrapped using
4139 % \cs{iow_wrap:nnnN} but with a leading |>~| and trailing period, both
4140 % removed before passing the wrapped text to the \tn{showtokens}
4141 % primitive. This primitive shows the result with a leading |>~| and
4142 % trailing period.
4143 %
4144 % The token list \cs{l_@@_tmpa_tl} containing the result
4145 % of all these manipulations is displayed to the terminal using
4146 % \cs{tex_showtokens:D} and an odd \cs{exp_after:wN} which expand the
4147 % closing brace to improve the output slightly. The calls to
4148 % \cs{__kernel_iow_with:Nnn} ensure that the \tn{newlinechar} is set to~$10$
4149 % so that the \cs{iow_newline:} inserted by the line-wrapping code
4150 % are correctly recognized by \TeX{}, and that \tn{errorcontextlines}
4151 % is $-1$ to avoid printing irrelevant context.
4152 % \begin{macrocode}
4153 \cs_new_protected:Npn \tl_show:n #1
4154 { \iow_wrap:nnnN { >~ \tl_to_str:n {#1} . } { } { } \@@_show:n }
4155 \cs_generate_variant:Nn \tl_show:n { e , x }
4156 \cs_new_protected:Npn \@@_show:n #1
4157 {
4158 \tl_set:Nf \l_@@_tmpa_tl { \@@_show:w #1 \s_@@_stop }
4159 \__kernel_iow_with:Nnn \tex_newlinechar:D { 10 }
4160 {
4161 \__kernel_iow_with:Nnn \tex_errorcontextlines:D { -1 }
4162 {
4163 \tex_showtokens:D \exp_after:wN \exp_after:wN \exp_after:wN
4164 { \exp_after:wN \l_@@_tmpa_tl }
4165 }
4166 }
4167 }
4168 \cs_new:Npn \@@_show:w #1 > #2 . \s_@@_stop {#2}
4169 % \end{macrocode}
4170 % \end{macro}
4171 % \end{macro}
4172 %
4173 % \begin{macro}{\tl_log:n, \tl_log:e, \tl_log:x}
4174 % Logging is much easier, simply line-wrap. The |>~| and trailing
4175 % period is there to match the output of \cs{tl_show:n}.
4176 % \begin{macrocode}
4177 \cs_new_protected:Npn \tl_log:n #1
4178 { \iow_wrap:nnnN { > ~ \tl_to_str:n {#1} . } { } { } \iow_log:n }
4179 \cs_generate_variant:Nn \tl_log:n { e , x }
4180 % \end{macrocode}
4181 % \end{macro}
4182 %
4183 % \begin{macro}{\__kernel_chk_tl_type:NnnT}
4184 % Helper for checking that |#1| has the correct internal structure to
4185 % be of a certain type. Make sure that it is defined and that it is a
4186 % token list, namely a macro with no \tn{long} nor \tn{protected}
4187 % prefix. Then compare |#1| to an attempt at reconstructing a valid
4188 % structure of the given type using |#2| (see implementation of
4189 % \cs{seq_show:N} for instance). If that is successful run the
4190 % requested code~|#4|.
4191 % \begin{macrocode}
4192 \cs_new_protected:Npn \__kernel_chk_tl_type:NnnT #1#2#3#4
4193 {
4194 \__kernel_chk_defined:NT #1
4195 {
4196 \exp_args:Nf \tl_if_empty:nTF
4197 { \cs_prefix_spec:N #1 \cs_parameter_spec:N #1 }
4198 {
4199 \tl_set:Ne \l_@@_tmpa_tl {#3}
4200 \tl_if_eq:NNTF #1 \l_@@_tmpa_tl
4201 {#4}
4202 {
4203 \msg_error:nneeee { kernel } { bad-type }
4204 { \token_to_str:N #1 } { \tl_to_str:N #1 }
4205 {#2} { \tl_to_str:N \l_@@_tmpa_tl }
4206 }
4207 }
4208 {
4209 \msg_error:nneee { kernel } { bad-type }
4210 { \token_to_str:N #1 } { \token_to_meaning:N #1 } {#2}
4211 }
4212 }
4213 }
4214 % \end{macrocode}
4215 % \end{macro}
4216 %
4217 % \subsection{Internal scan marks}
4218 %
4219 % \begin{variable}{\s_@@_nil,\s_@@_mark,\s_@@_stop}
4220 % Internal scan marks. These are defined here at the end because the
4221 % code for \cs{scan_new:N} depends on some \pkg{l3tl} functions.
4222 % \begin{macrocode}
4223 \scan_new:N \s_@@_nil
4224 \scan_new:N \s_@@_mark
4225 \scan_new:N \s_@@_stop
4226 % \end{macrocode}
4227 % \end{variable}
4228 %
4229 % \subsection{Scratch token lists}
4230 %
4231 % \begin{variable}{\g_tmpa_tl, \g_tmpb_tl}
4232 % Global temporary token list variables.
4233 % They are supposed to be set and used immediately,
4234 % with no delay between the definition and the use because you
4235 % can't count on other macros not to redefine them from under you.
4236 % \begin{macrocode}
4237 \tl_new:N \g_tmpa_tl
4238 \tl_new:N \g_tmpb_tl
4239 % \end{macrocode}
4240 % \end{variable}
4241 %
4242 % \begin{variable}{\l_tmpa_tl, \l_tmpb_tl}
4243 % These are local temporary token list variables. Be sure not to assume
4244 % that the value you put into them will survive for
4245 % long---see discussion above.
4246 % \begin{macrocode}
4247 \tl_new:N \l_tmpa_tl
4248 \tl_new:N \l_tmpb_tl
4249 % \end{macrocode}
4250 % \end{variable}
4251 %
4252 % We finally clean up a temporary control sequence that we have used at
4253 % various points to set up some definitions.
4254 % \begin{macrocode}
4255 \cs_undefine:N \@@_tmp:w
4256 % \end{macrocode}
4257 %
4258 % \begin{macrocode}
4259 %</code>
4260 % \end{macrocode}
4261 %
4262 % \end{implementation}
4263 %
4264 % \PrintIndex