Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3str.dtx
4 %
5 % Copyright (C) 2011-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{l3str} module\\ Strings^^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{} associates each character with a category code: as such, there is no
53 % concept of a \enquote{string} as commonly understood in many other
54 % programming languages. However, there are places where we wish to manipulate
55 % token lists while in some sense \enquote{ignoring} category codes: this is
56 % done by treating token lists as strings in a \TeX{} sense.
57 %
58 % A \TeX{} string (and thus an \pkg{expl3} string) is a series of characters
59 % which have category code $12$ (\enquote{other}) with the exception of
60 % space characters which have category code $10$ (\enquote{space}). Thus
61 % at a technical level, a \TeX{} string is a token list with the appropriate
62 % category codes. In this documentation, these are simply referred to as
63 % strings.
64 %
65 % String variables are simply specialized token lists, but by convention
66 % should be named with the suffix \texttt{\ldots{}str}. Such variables
67 % should contain characters with category code $12$ (other), except
68 % spaces, which have category code $10$ (blank space). All the
69 % functions in this module which accept a token list argument first
70 % convert it to a string using \cs{tl_to_str:n} for internal processing,
71 % and do not treat a token list or the corresponding string
72 % representation differently.
73 %
74 % As a string is a subset of the more general token list, it is sometimes unclear
75 % when one should be used over the other.
76 % Use a string variable for data that isn't primarily intended for typesetting
77 % and for which a level of protection from unwanted expansion is suitable.
78 % This data type simplifies comparison of variables since there are no concerns
79 % about expansion of their contents.
80 %
81 % The functions \cs{cs_to_str:N}, \cs{tl_to_str:n}, \cs{tl_to_str:N} and
82 % \cs{token_to_str:N} (and variants) generate strings from the appropriate
83 % input: these are documented in \pkg{l3basics}, \pkg{l3tl} and \pkg{l3token},
84 % respectively.
85 %
86 % Most expandable functions in this module come in three flavors:
87 % \begin{itemize}
88 % \item \cs[no-index]{str_\ldots{}:N}, which expect a token list or string
89 % variable as their argument;
90 % \item \cs[no-index]{str_\ldots{}:n}, taking any token list (or string) as an
91 % argument;
92 % \item \cs[no-index]{str_\ldots{}_ignore_spaces:n}, which ignores any space
93 % encountered during the operation: these functions are typically
94 % faster than those which take care of escaping spaces
95 % appropriately.
96 % \end{itemize}
97 %
98 % \section{Creating and initializing string variables}
99 %
100 % \begin{function}[added = 2015-09-18]{\str_new:N, \str_new:c}
101 % \begin{syntax}
102 % \cs{str_new:N} \meta{str~var}
103 % \end{syntax}
104 % Creates a new \meta{str~var} or raises an error if the name is
105 % already taken. The declaration is global. The \meta{str~var} is
106 % initially empty.
107 % \end{function}
108 %
109 % \begin{function}[added = 2015-09-18, updated = 2018-07-28]
110 % {
111 % \str_const:Nn, \str_const:NV, \str_const:Ne,
112 % \str_const:cn, \str_const:cV, \str_const:ce
113 % }
114 % \begin{syntax}
115 % \cs{str_const:Nn} \meta{str~var} \Arg{token list}
116 % \end{syntax}
117 % Creates a new constant \meta{str~var} or raises an error if the name
118 % is already taken. The value of the \meta{str~var} is set
119 % globally to the \meta{token list}, converted to a string.
120 % \end{function}
121 %
122 % \begin{function}[added = 2015-09-18]
123 % {\str_clear:N, \str_clear:c, \str_gclear:N, \str_gclear:c}
124 % \begin{syntax}
125 % \cs{str_clear:N} \meta{str~var}
126 % \end{syntax}
127 % Clears the content of the \meta{str~var}.
128 % \end{function}
129 %
130 % \begin{function}[added = 2015-09-18]
131 % {
132 % \str_clear_new:N, \str_clear_new:c,
133 % \str_gclear_new:N, \str_gclear_new:c
134 % }
135 % \begin{syntax}
136 % \cs{str_clear_new:N} \meta{str~var}
137 % \end{syntax}
138 % Ensures that the \meta{str~var} exists globally by applying
139 % \cs{str_new:N} if necessary, then applies
140 % \cs[index=str_clear:N]{str_(g)clear:N} to leave
141 % the \meta{str~var} empty.
142 % \end{function}
143 %
144 % \begin{function}[added = 2015-09-18]
145 % {
146 % \str_set_eq:NN, \str_set_eq:cN, \str_set_eq:Nc, \str_set_eq:cc,
147 % \str_gset_eq:NN, \str_gset_eq:cN, \str_gset_eq:Nc, \str_gset_eq:cc
148 % }
149 % \begin{syntax}
150 % \cs{str_set_eq:NN} \meta{str~var_1} \meta{str~var_2}
151 % \end{syntax}
152 % Sets the content of \meta{str~var_1} equal to that of
153 % \meta{str~var_2}.
154 % \end{function}
155 %
156 % \begin{function}[added = 2017-10-08]
157 % {
158 % \str_concat:NNN, \str_concat:ccc,
159 % \str_gconcat:NNN, \str_gconcat:ccc
160 % }
161 % \begin{syntax}
162 % \cs{str_concat:NNN} \meta{str~var_1} \meta{str~var_2} \meta{str~var_3}
163 % \end{syntax}
164 % Concatenates the content of \meta{str~var_2} and \meta{str~var_3}
165 % together and saves the result in \meta{str~var_1}. The \meta{str~var_2}
166 % is placed at the left side of the new string variable.
167 % The \meta{str~var_2} and \meta{str~var_3} must indeed be strings, as
168 % this function does not convert their contents to a string.
169 % \end{function}
170 %
171 % \begin{function}[EXP, pTF, added = 2015-09-18]
172 % {\str_if_exist:N, \str_if_exist:c}
173 % \begin{syntax}
174 % \cs{str_if_exist_p:N} \meta{str~var}
175 % \cs{str_if_exist:NTF} \meta{str~var} \Arg{true code} \Arg{false code}
176 % \end{syntax}
177 % Tests whether the \meta{str~var} is currently defined. This does not
178 % check that the \meta{str~var} really is a string.
179 % \end{function}
180 %
181 % \section{Adding data to string variables}
182 %
183 % \begin{function}[added = 2015-09-18, updated = 2018-07-28]
184 % {
185 % \str_set:Nn, \str_set:NV, \str_set:Ne,
186 % \str_set:cn, \str_set:cV, \str_set:ce,
187 % \str_gset:Nn, \str_gset:NV, \str_gset:Ne,
188 % \str_gset:cn, \str_gset:cV, \str_gset:ce
189 % }
190 % \begin{syntax}
191 % \cs{str_set:Nn} \meta{str var} \Arg{token list}
192 % \end{syntax}
193 % Converts the \meta{token list} to a \meta{string}, and stores the
194 % result in \meta{str var}.
195 % \end{function}
196 %
197 % \begin{function}[added = 2015-09-18, updated = 2018-07-28]
198 % {
199 % \str_put_left:Nn, \str_put_left:NV, \str_put_left:Ne,
200 % \str_put_left:cn, \str_put_left:cV, \str_put_left:ce,
201 % \str_gput_left:Nn, \str_gput_left:NV, \str_gput_left:Ne,
202 % \str_gput_left:cn, \str_gput_left:cV, \str_gput_left:ce
203 % }
204 % \begin{syntax}
205 % \cs{str_put_left:Nn} \meta{str var} \Arg{token list}
206 % \end{syntax}
207 % Converts the \meta{token list} to a \meta{string}, and prepends the
208 % result to \meta{str var}. The current contents of the \meta{str
209 % var} are not automatically converted to a string.
210 % \end{function}
211 %
212 % \begin{function}[added = 2015-09-18, updated = 2018-07-28]
213 % {
214 % \str_put_right:Nn, \str_put_right:NV, \str_put_right:Ne,
215 % \str_put_right:cn, \str_put_right:cV, \str_put_right:ce,
216 % \str_gput_right:Nn, \str_gput_right:NV, \str_gput_right:Ne,
217 % \str_gput_right:cn, \str_gput_right:cV, \str_gput_right:ce
218 % }
219 % \begin{syntax}
220 % \cs{str_put_right:Nn} \meta{str var} \Arg{token list}
221 % \end{syntax}
222 % Converts the \meta{token list} to a \meta{string}, and appends the
223 % result to \meta{str var}. The current contents of the \meta{str
224 % var} are not automatically converted to a string.
225 % \end{function}
226 %
227 % \section{String conditionals}
228 %
229 % \begin{function}[EXP,pTF, added = 2015-09-18, updated = 2022-03-21]
230 % {\str_if_empty:N, \str_if_empty:c, \str_if_empty:n}
231 % \begin{syntax}
232 % \cs{str_if_empty_p:N} \meta{str~var}
233 % \cs{str_if_empty:NTF} \meta{str~var} \Arg{true code} \Arg{false code}
234 % \end{syntax}
235 % Tests if the \meta{string variable} is entirely empty
236 % (i.e., contains no characters at all).
237 % \end{function}
238 %
239 % \begin{function}[EXP,pTF, added = 2015-09-18]
240 % {\str_if_eq:NN, \str_if_eq:Nc, \str_if_eq:cN, \str_if_eq:cc}
241 % \begin{syntax}
242 % \cs{str_if_eq_p:NN} \meta{str~var_1} \meta{str~var_2}
243 % \cs{str_if_eq:NNTF} \meta{str~var_1} \meta{str~var_2} \Arg{true code} \Arg{false code}
244 % \end{syntax}
245 % Compares the content of two \meta{str variables} and
246 % is logically \texttt{true} if the two contain the same characters
247 % in the same order. See \cs{tl_if_eq:NNTF} to compare tokens
248 % (including their category codes) rather than characters.
249 % \end{function}
250 %
251 % \begin{function}[EXP,pTF, updated = 2018-06-18]
252 % {
253 % \str_if_eq:nn, \str_if_eq:Vn, \str_if_eq:on, \str_if_eq:no,
254 % \str_if_eq:nV, \str_if_eq:VV, \str_if_eq:vn, \str_if_eq:nv,
255 % \str_if_eq:ee
256 % }
257 % \begin{syntax}
258 % \cs{str_if_eq_p:nn} \Arg{tl_1} \Arg{tl_2}
259 % \cs{str_if_eq:nnTF} \Arg{tl_1} \Arg{tl_2} \Arg{true code} \Arg{false code}
260 % \end{syntax}
261 % Compares the two \meta{token lists} on a character by character
262 % basis (namely after converting them to strings),
263 % and is \texttt{true} if the two \meta{strings} contain the same
264 % characters in the same order. Thus for example
265 % \begin{verbatim}
266 % \str_if_eq_p:no { abc } { \tl_to_str:n { abc } }
267 % \end{verbatim}
268 % is logically \texttt{true}. See \cs{tl_if_eq:nnTF} to compare
269 % tokens (including their category codes) rather than characters.
270 % \end{function}
271 %
272 % \begin{function}[TF, added = 2017-10-08]{\str_if_in:Nn, \str_if_in:cn}
273 % \begin{syntax}
274 % \cs{str_if_in:NnTF} \meta{str~var} \Arg{token list} \Arg{true code} \Arg{false code}
275 % \end{syntax}
276 % Converts the \meta{token list} to a \meta{string} and
277 % tests if that \meta{string} is found in the content of the
278 % \meta{str~var}.
279 % \end{function}
280 %
281 % \begin{function}[TF, added = 2017-10-08]{\str_if_in:nn}
282 % \begin{syntax}
283 % \cs{str_if_in:nnTF} \Arg{tl_1} \Arg{tl_2} \Arg{true code} \Arg{false code}
284 % \end{syntax}
285 % Converts both \meta{token lists} to \meta{strings} and
286 % tests whether \meta{string_2} is found inside \meta{string_1}.
287 % \end{function}
288 %
289 % \begin{function}[added = 2013-07-24, updated = 2022-03-21, EXP, noTF]
290 % {
291 % \str_case:nn, \str_case:Vn, \str_case:Nn, \str_case:on, \str_case:en,
292 % \str_case:nV, \str_case:nv, \str_case:ne
293 % }
294 % \begin{syntax}
295 % \cs{str_case:nnTF} \Arg{test string} \\
296 % ~~|{| \\
297 % ~~~~\Arg{string case_1} \Arg{code case_1} \\
298 % ~~~~\Arg{string case_2} \Arg{code case_2} \\
299 % ~~~~\ldots \\
300 % ~~~~\Arg{string case_n} \Arg{code case_n} \\
301 % ~~|}| \\
302 % ~~\Arg{true code}
303 % ~~\Arg{false code}
304 % \end{syntax}
305 % Compares the \meta{test string} in turn with each
306 % of the \meta{string case}s until a match is found
307 % (all token lists are converted to strings).
308 % If the two are equal (as described for
309 % \cs{str_if_eq:nnTF}) then the associated \meta{code} is left in the
310 % input stream and other cases are discarded. If any of the
311 % cases are matched, the \meta{true code} is also inserted into the
312 % input stream (after the code for the appropriate case), while if none
313 % match then the \meta{false code} is inserted. The function
314 % \cs{str_case:nn}, which does nothing if there is no match, is also
315 % available.
316 %
317 % This set of functions performs no expansion on each
318 % \meta{string~case} argument, so any variable in there will be
319 % compared as a string. If expansion is needed in the
320 % \meta{string~case}s, then \cs[no-index]{str_case_e:nn(TF)} should
321 % be used instead.
322 % \end{function}
323 %
324 % \begin{function}[added = 2018-06-19, EXP, noTF]
325 % {\str_case_e:nn, \str_case_e:en}
326 % \begin{syntax}
327 % \cs{str_case_e:nnTF} \Arg{test string} \\
328 % ~~|{| \\
329 % ~~~~\Arg{string case_1} \Arg{code case_1} \\
330 % ~~~~\Arg{string case_2} \Arg{code case_2} \\
331 % ~~~~\ldots \\
332 % ~~~~\Arg{string case_n} \Arg{code case_n} \\
333 % ~~|}| \\
334 % ~~\Arg{true code}
335 % ~~\Arg{false code}
336 % \end{syntax}
337 % Compares the full expansion of the \meta{test string}
338 % in turn with the full expansion of the \meta{string case}s
339 % (all token lists are converted to strings). If the two
340 % full expansions are equal (as described for \cs{str_if_eq:eeTF}) then the
341 % associated \meta{code} is left in the input stream
342 % and other cases are discarded. If any of the
343 % cases are matched, the \meta{true code} is also inserted into the
344 % input stream (after the code for the appropriate case), while if none
345 % match then the \meta{false code} is inserted. The function
346 % \cs{str_case_e:nn}, which does nothing if there is no match, is also
347 % available.
348 % In \cs[index=str_case_e:nnTF]{str_case_e:nn(TF)}, the \meta{test string}
349 % is expanded in each comparison, and must always yield the same result:
350 % for example, random numbers must not be used within this string.
351 % \end{function}
352 %
353 % \begin{function}[EXP, pTF, added = 2021-05-17]{\str_compare:nNn, \str_compare:eNe}
354 % \begin{syntax}
355 % \cs{str_compare_p:nNn} \Arg{tl_1} \meta{relation} \Arg{tl_2}
356 % \cs{str_compare:nNnTF} \Arg{tl_1} \meta{relation} \Arg{tl_2} \Arg{true code} \Arg{false code}
357 % \end{syntax}
358 % Compares the two \meta{token lists} on a character by character
359 % basis (namely after converting them to strings) in a lexicographic
360 % order according to the character codes of the characters. The
361 % \meta{relation} can be |<|, |=|, or~|>| and the test is
362 % \texttt{true} under the following conditions:
363 % \begin{itemize}
364 % \item for |<|, if the first string is earlier than the second in lexicographic order;
365 % \item for |=|, if the two strings have exactly the same characters;
366 % \item for |>|, if the first string is later than the second in lexicographic order.
367 % \end{itemize}
368 % Thus for example the following is logically \texttt{true}:
369 % \begin{verbatim}
370 % \str_compare_p:nNn { ab } < { abc }
371 % \end{verbatim}
372 % \begin{texnote}
373 % This is a wrapper around the \TeX{} primitive
374 % \cs[index=pdfstrcmp]{(pdf)strcmp}. It is meant for programming
375 % and not for sorting textual contents, as it simply considers
376 % character codes and not more elaborate considerations of grapheme
377 % clusters, locale, etc.
378 % \end{texnote}
379 % \end{function}
380 %
381 % \section{Mapping over strings}
382 %
383 % All mappings are done at the current group level, i.e., any
384 % local assignments made by the \meta{function} or \meta{code} discussed
385 % below remain in effect after the loop.
386 %
387 % \begin{function}[added = 2017-11-14, rEXP]
388 % {\str_map_function:nN, \str_map_function:NN, \str_map_function:cN}
389 % \begin{syntax}
390 % \cs{str_map_function:nN} \Arg{token list} \meta{function}
391 % \cs{str_map_function:NN} \meta{str~var} \meta{function}
392 % \end{syntax}
393 % Converts the \meta{token list} to a \meta{string} then
394 % applies \meta{function} to every \meta{character} in the
395 % \meta{string} including spaces.
396 % \end{function}
397 %
398 % \begin{function}[added = 2017-11-14]
399 % {\str_map_inline:nn, \str_map_inline:Nn, \str_map_inline:cn}
400 % \begin{syntax}
401 % \cs{str_map_inline:nn} \Arg{token list} \Arg{inline function}
402 % \cs{str_map_inline:Nn} \meta{str~var} \Arg{inline function}
403 % \end{syntax}
404 % Converts the \meta{token list} to a \meta{string} then
405 % applies the \meta{inline function} to every \meta{character} in the
406 % \meta{str~var} including spaces.
407 % The \meta{inline function} should consist of code which
408 % receives the \meta{character} as |#1|.
409 % \end{function}
410 %
411 % \begin{function}[rEXP, added = 2021-05-05]
412 % {\str_map_tokens:nn, \str_map_tokens:Nn, \str_map_tokens:cn}
413 % \begin{syntax}
414 % \cs{str_map_tokens:nn} \Arg{token list} \Arg{code}
415 % \cs{str_map_tokens:Nn} \meta{str~var} \Arg{code}
416 % \end{syntax}
417 % Converts the \meta{token list} to a \meta{string} then applies
418 % \meta{code} to every \meta{character} in the \meta{string} including
419 % spaces. The \meta{code} receives each character as a trailing brace
420 % group. This is equivalent to \cs{str_map_function:nN} if the
421 % \meta{code} consists of a single function.
422 % \end{function}
423 %
424 % \begin{function}[added = 2017-11-14]
425 % {\str_map_variable:nNn, \str_map_variable:NNn, \str_map_variable:cNn}
426 % \begin{syntax}
427 % \cs{str_map_variable:nNn} \Arg{token list} \meta{variable} \Arg{code}
428 % \cs{str_map_variable:NNn} \meta{str~var} \meta{variable} \Arg{code}
429 % \end{syntax}
430 % Converts the \meta{token list} to a \meta{string} then stores each
431 % \meta{character} in the \meta{string} (including spaces) in turn in
432 % the (string or token list) \meta{variable} and applies the
433 % \meta{code}. The \meta{code} will usually make use of the
434 % \meta{variable}, but this is not enforced. The assignments to the
435 % \meta{variable} are local. Its value after the loop is the last
436 % \meta{character} in the \meta{string}, or its original value if the
437 % \meta{string} is empty. See also \cs{str_map_inline:Nn}.
438 % \end{function}
439 %
440 % \begin{function}[added = 2017-10-08, rEXP]{\str_map_break:}
441 % \begin{syntax}
442 % \cs{str_map_break:}
443 % \end{syntax}
444 % Used to terminate a \cs[no-index]{str_map_\ldots} function before all
445 % characters in the \meta{string} have been processed. This
446 % normally takes place within a conditional statement, for example
447 % \begin{verbatim}
448 % \str_map_inline:Nn \l_my_str
449 % {
450 % \str_if_eq:nnT { #1 } { bingo } { \str_map_break: }
451 % % Do something useful
452 % }
453 % \end{verbatim}
454 % See also \cs{str_map_break:n}.
455 % Use outside of a \cs[no-index]{str_map_\ldots} scenario leads to low
456 % level \TeX{} errors.
457 % \begin{texnote}
458 % When the mapping is broken, additional tokens may be inserted
459 % before continuing with the
460 % code that follows the loop.
461 % This depends on the design of the mapping function.
462 % \end{texnote}
463 % \end{function}
464 %
465 % \begin{function}[added = 2017-10-08, rEXP]{\str_map_break:n}
466 % \begin{syntax}
467 % \cs{str_map_break:n} \Arg{code}
468 % \end{syntax}
469 % Used to terminate a \cs[no-index]{str_map_\ldots} function before all
470 % characters in the \meta{string} have been processed, inserting
471 % the \meta{code} after the mapping has ended. This
472 % normally takes place within a conditional statement, for example
473 % \begin{verbatim}
474 % \str_map_inline:Nn \l_my_str
475 % {
476 % \str_if_eq:nnT { #1 } { bingo }
477 % { \str_map_break:n { <code> } }
478 % % Do something useful
479 % }
480 % \end{verbatim}
481 % Use outside of a \cs[no-index]{str_map_\ldots} scenario leads to low
482 % level \TeX{} errors.
483 % \begin{texnote}
484 % When the mapping is broken, additional tokens may be inserted
485 % before the \meta{code} is
486 % inserted into the input stream.
487 % This depends on the design of the mapping function.
488 % \end{texnote}
489 % \end{function}
490 %
491 % \section{Working with the content of strings}
492 %
493 % \begin{function}[EXP, added = 2015-09-18]{\str_use:N, \str_use:c}
494 % \begin{syntax}
495 % \cs{str_use:N} \meta{str~var}
496 % \end{syntax}
497 % Recovers the content of a \meta{str~var} and places it
498 % directly in the input stream. An error is raised if the variable
499 % does not exist or if it is invalid. Note that it is possible to use
500 % a \meta{str} directly without an accessor function.
501 % \end{function}
502 %
503 % \begin{function}[EXP, added = 2015-09-18]
504 % {
505 % \str_count:N, \str_count:c, \str_count:n, \str_count:e,
506 % \str_count_ignore_spaces:n, \str_count_ignore_spaces:e
507 % }
508 % \begin{syntax}
509 % \cs{str_count:n} \Arg{token list}
510 % \end{syntax}
511 % Leaves in the input stream the number of characters in the string
512 % representation of \meta{token list}, as an integer denotation. The
513 % functions differ in their treatment of spaces. In the case of
514 % \cs{str_count:N} and \cs{str_count:n}, all characters including
515 % spaces are counted. The \cs{str_count_ignore_spaces:n} function
516 % leaves the number of non-space characters in the input stream.
517 % \end{function}
518 %
519 % \begin{function}[EXP, added = 2015-09-18]
520 % {\str_count_spaces:N, \str_count_spaces:c, \str_count_spaces:n}
521 % \begin{syntax}
522 % \cs{str_count_spaces:n} \Arg{token list}
523 % \end{syntax}
524 % Leaves in the input stream the number of space characters in the
525 % string representation of \meta{token list}, as an integer
526 % denotation. Of course, this function has no \texttt{_ignore_spaces}
527 % variant.
528 % \end{function}
529 %
530 % \begin{function}[EXP, added = 2015-09-18]
531 % {\str_head:N, \str_head:c, \str_head:n, \str_head_ignore_spaces:n}
532 % \begin{syntax}
533 % \cs{str_head:n} \Arg{token list}
534 % \end{syntax}
535 % Converts the \meta{token list} into a \meta{string}. The first
536 % character in the \meta{string} is then left in the input stream,
537 % with category code \enquote{other}. The functions differ if the
538 % first character is a space: \cs{str_head:N} and \cs{str_head:n}
539 % return a space token with category code~$10$ (blank space), while
540 % the \cs{str_head_ignore_spaces:n} function ignores this space
541 % character and leaves the first non-space character in the input
542 % stream. If the \meta{string} is empty (or only contains spaces in
543 % the case of the \texttt{_ignore_spaces} function), then nothing is
544 % left on the input stream.
545 % \end{function}
546 %
547 % \begin{function}[EXP, added = 2015-09-18]
548 % {\str_tail:N, \str_tail:c, \str_tail:n, \str_tail_ignore_spaces:n}
549 % \begin{syntax}
550 % \cs{str_tail:n} \Arg{token list}
551 % \end{syntax}
552 % Converts the \meta{token list} to a \meta{string}, removes the first
553 % character, and leaves the remaining characters (if any) in the input
554 % stream, with category codes $12$ and $10$ (for spaces). The
555 % functions differ in the case where the first character is a space:
556 % \cs{str_tail:N} and \cs{str_tail:n} only trim that space, while
557 % \cs{str_tail_ignore_spaces:n} removes the first non-space character
558 % and any space before it. If the \meta{token list} is empty (or
559 % blank in the case of the \texttt{_ignore_spaces} variant), then
560 % nothing is left on the input stream.
561 % \end{function}
562 %
563 % \begin{function}[EXP, added = 2015-09-18]
564 % {\str_item:Nn, \str_item:cn, \str_item:nn, \str_item_ignore_spaces:nn}
565 % \begin{syntax}
566 % \cs{str_item:nn} \Arg{token list} \Arg{integer expression}
567 % \end{syntax}
568 % Converts the \meta{token list} to a \meta{string}, and leaves in the
569 % input stream the character in position \meta{integer expression} of
570 % the \meta{string}, starting at $1$ for the first (left-most)
571 % character. In the case of \cs{str_item:Nn} and \cs{str_item:nn},
572 % all characters including spaces are taken into account. The
573 % \cs{str_item_ignore_spaces:nn} function skips spaces when counting
574 % characters. If the \meta{integer expression} is negative,
575 % characters are counted from the end of the \meta{string}. Hence,
576 % $-1$ is the right-most character, etc.
577 % \end{function}
578 %
579 % \begin{function}[EXP, added = 2015-09-18]
580 % {
581 % \str_range:Nnn, \str_range:cnn, \str_range:nnn,
582 % \str_range_ignore_spaces:nnn
583 % }
584 % \begin{syntax}
585 % \cs{str_range:nnn} \Arg{token list} \Arg{start index} \Arg{end index}
586 % \end{syntax}
587 % Converts the \meta{token list} to a \meta{string}, and leaves in the
588 % input stream the characters from the \meta{start index} to the
589 % \meta{end index} inclusive. Spaces are preserved and counted as items
590 % (contrast this with \cs{tl_range:nnn} where spaces are not counted as
591 % items and are possibly discarded from the output).
592 %
593 % Here \meta{start index} and \meta{end index} should be integer denotations.
594 % For describing in detail the functions' behavior, let $m$ and $n$ be the start
595 % and end index respectively. If either is $0$, the result is empty. A positive
596 % index means `start counting from the left end', a negative index means
597 % `start counting from the right end'. Let $l$ be the count of the token list.
598 %
599 % The \emph{actual start point} is determined as $M=m$ if~$m>0$ and as $M=l+m+1$
600 % if~$m<0$. Similarly the \emph{actual end point} is $N=n$ if~$n>0$ and $N=l+n+1$
601 % if~$n<0$. If $M>N$, the result is empty. Otherwise it consists of all items from
602 % position $M$ to position $N$ inclusive; for the purpose of this rule, we can
603 % imagine that the token list extends at infinity on either side, with void items
604 % at positions $s$ for $s\le0$ or $s>l$.
605 % For instance,
606 % \begin{verbatim}
607 % \iow_term:e { \str_range:nnn { abcdef } { 2 } { 5 } }
608 % \iow_term:e { \str_range:nnn { abcdef } { -4 } { -1 } }
609 % \iow_term:e { \str_range:nnn { abcdef } { -2 } { -1 } }
610 % \iow_term:e { \str_range:nnn { abcdef } { 0 } { -1 } }
611 % \end{verbatim}
612 % prints \texttt{bcde}, \texttt{cdef}, \texttt{ef}, and an empty
613 % line to the terminal. The \meta{start index} must always be smaller than
614 % or equal to the \meta{end index}: if this is not the case then no output
615 % is generated. Thus
616 % \begin{verbatim}
617 % \iow_term:e { \str_range:nnn { abcdef } { 5 } { 2 } }
618 % \iow_term:e { \str_range:nnn { abcdef } { -1 } { -4 } }
619 % \end{verbatim}
620 % both yield empty strings.
621 % \end{function}
622 %
623 % ^^A If this stays in the same {function} environment, we get a really
624 % ^^A awful page break. Perhaps we should add a way to allow a page break
625 % ^^A in a function environment...
626 % The behavior of \cs{str_range_ignore_spaces:nnn} is similar, but spaces
627 % are removed before starting the job. The input
628 % \begin{verbatim}
629 % \iow_term:e { \str_range:nnn { abcdefg } { 2 } { 5 } }
630 % \iow_term:e { \str_range:nnn { abcdefg } { 2 } { -3 } }
631 % \iow_term:e { \str_range:nnn { abcdefg } { -6 } { 5 } }
632 % \iow_term:e { \str_range:nnn { abcdefg } { -6 } { -3 } }
633 %
634 % \iow_term:e { \str_range:nnn { abc~efg } { 2 } { 5 } }
635 % \iow_term:e { \str_range:nnn { abc~efg } { 2 } { -3 } }
636 % \iow_term:e { \str_range:nnn { abc~efg } { -6 } { 5 } }
637 % \iow_term:e { \str_range:nnn { abc~efg } { -6 } { -3 } }
638 %
639 % \iow_term:e { \str_range_ignore_spaces:nnn { abcdefg } { 2 } { 5 } }
640 % \iow_term:e { \str_range_ignore_spaces:nnn { abcdefg } { 2 } { -3 } }
641 % \iow_term:e { \str_range_ignore_spaces:nnn { abcdefg } { -6 } { 5 } }
642 % \iow_term:e { \str_range_ignore_spaces:nnn { abcdefg } { -6 } { -3 } }
643 %
644 % \iow_term:e { \str_range_ignore_spaces:nnn { abcd~efg } { 2 } { 5 } }
645 % \iow_term:e { \str_range_ignore_spaces:nnn { abcd~efg } { 2 } { -3 } }
646 % \iow_term:e { \str_range_ignore_spaces:nnn { abcd~efg } { -6 } { 5 } }
647 % \iow_term:e { \str_range_ignore_spaces:nnn { abcd~efg } { -6 } { -3 } }
648 % \end{verbatim}
649 % will print four instances of |bcde|, four instances of |bc e| and eight
650 % instances of |bcde|.
651 % ^^A\end{function}
652 %
653 % \section{Modifying string variables}
654 %
655 % \begin{function}[added = 2017-10-08]
656 % {
657 % \str_replace_once:Nnn, \str_replace_once:cnn,
658 % \str_greplace_once:Nnn, \str_greplace_once:cnn
659 % }
660 % \begin{syntax}
661 % \cs{str_replace_once:Nnn} \meta{str~var} \Arg{old} \Arg{new}
662 % \end{syntax}
663 % Converts the \meta{old} and \meta{new} token lists to strings, then
664 % replaces the first (leftmost) occurrence of \meta{old string} in the
665 % \meta{str~var} with \meta{new string}.
666 % \end{function}
667 %
668 % \begin{function}[added = 2017-10-08]
669 % {
670 % \str_replace_all:Nnn, \str_replace_all:cnn,
671 % \str_greplace_all:Nnn, \str_greplace_all:cnn
672 % }
673 % \begin{syntax}
674 % \cs{str_replace_all:Nnn} \meta{str~var} \Arg{old} \Arg{new}
675 % \end{syntax}
676 % Converts the \meta{old} and \meta{new} token lists to strings, then
677 % replaces all occurrences of \meta{old string} in the
678 % \meta{str~var} with \meta{new string}.
679 % As this function
680 % operates from left to right, the pattern \meta{old string}
681 % may remain after the replacement (see \cs{str_remove_all:Nn}
682 % for an example).
683 % \end{function}
684 %
685 % \begin{function}[added = 2017-10-08]
686 % {
687 % \str_remove_once:Nn, \str_remove_once:cn,
688 % \str_gremove_once:Nn, \str_gremove_once:cn
689 % }
690 % \begin{syntax}
691 % \cs{str_remove_once:Nn} \meta{str~var} \Arg{token list}
692 % \end{syntax}
693 % Converts the \meta{token list} to a \meta{string} then
694 % removes the first (leftmost) occurrence of \meta{string} from the
695 % \meta{str~var}.
696 % \end{function}
697 %
698 % \begin{function}[added = 2017-10-08]
699 % {
700 % \str_remove_all:Nn, \str_remove_all:cn,
701 % \str_gremove_all:Nn, \str_gremove_all:cn
702 % }
703 % \begin{syntax}
704 % \cs{str_remove_all:Nn} \meta{str~var} \Arg{token list}
705 % \end{syntax}
706 % Converts the \meta{token list} to a \meta{string} then
707 % removes all occurrences of \meta{string} from the
708 % \meta{str~var}.
709 % As this function
710 % operates from left to right, the pattern \meta{string}
711 % may remain after the removal, for instance,
712 % \begin{quote}
713 % \cs{str_set:Nn} \cs{l_tmpa_str} |{abbccd}|
714 % \cs{str_remove_all:Nn} \cs{l_tmpa_str} |{bc}|
715 % \end{quote}
716 % results in \cs{l_tmpa_str} containing \texttt{abcd}.
717 % \end{function}
718 %
719 % \section{String manipulation}
720 %
721 % \begin{function}[EXP, added = 2019-11-26]
722 % {
723 % \str_lowercase:n, \str_lowercase:f,
724 % \str_uppercase:n, \str_uppercase:f
725 % }
726 % \begin{syntax}
727 % \cs{str_lowercase:n} \Arg{tokens}
728 % \cs{str_uppercase:n} \Arg{tokens}
729 % \end{syntax}
730 % Converts the input \meta{tokens} to their string representation, as
731 % described for \cs{tl_to_str:n}, and then to the lower or upper
732 % case representation using a one-to-one mapping as described by the
733 % Unicode Consortium file |UnicodeData.txt|.
734 %
735 % These functions are intended for case changing programmatic data in
736 % places where upper/lower case distinctions are meaningful. One example
737 % would be automatically generating a function name from user input where
738 % some case changing is needed. In this situation the input is programmatic,
739 % not textual, case does have meaning and a language-independent one-to-one
740 % mapping is appropriate. For example
741 % \begin{verbatim}
742 % \cs_new_protected:Npn \myfunc:nn #1#2
743 % {
744 % \cs_set_protected:cpn
745 % {
746 % user
747 % \str_uppercase:f { \tl_head:n {#1} }
748 % \str_lowercase:f { \tl_tail:n {#1} }
749 % }
750 % { #2 }
751 % }
752 % \end{verbatim}
753 % would be used to generate a function with an auto-generated name consisting
754 % of the upper case equivalent of the supplied name followed by the lower
755 % case equivalent of the rest of the input.
756 %
757 % These functions should \emph{not} be used for
758 % \begin{itemize}
759 % \item Caseless comparisons: use \cs{str_casefold:n} for this
760 % situation (case folding is distinct from lower casing).
761 % \item Case changing text for typesetting: see the
762 % \cs[index=text_lowercase:n]{text_lowercase:n(n)},
763 % \cs[index=text_uppercase:n]{text_uppercase:n(n)} and
764 % \cs[index=text_titlecase_all:n]{text_titlecase_(all|first):n(n)} functions which
765 % correctly deal with context-dependence and other factors appropriate
766 % to text case changing.
767 % \end{itemize}
768 % \end{function}
769 %
770 % \begin{function}[EXP, added = 2022-10-16]
771 % {\str_casefold:n, \str_casefold:V}
772 % \begin{syntax}
773 % \cs{str_casefold:n} \Arg{tokens}
774 % \end{syntax}
775 % Converts the input \meta{tokens} to their string representation, as
776 % described for \cs{tl_to_str:n}, and then folds the case of the resulting
777 % \meta{string} to remove case information. The result of this process is
778 % left in the input stream.
779 %
780 % String folding is a process used for material such as identifiers rather
781 % than for \enquote{text}. The folding provided by \cs{str_casefold:n}
782 % follows the mappings provided by the \href{http://www.unicode.org}^^A
783 % {Unicode Consortium}, who
784 % \href{http://www.unicode.org/faq/casemap_charprop.html#2}{state}:
785 % \begin{quote}
786 % Case folding is primarily used for caseless comparison of text, such
787 % as identifiers in a computer program, rather than actual text
788 % transformation. Case folding in Unicode is based on the lowercase
789 % mapping, but includes additional changes to the source text to help make
790 % it language-insensitive and consistent. As a result, case-folded text
791 % should be used solely for internal processing and generally should not be
792 % stored or displayed to the end user.
793 % \end{quote}
794 % The folding approach implemented by \cs{str_casefold:n} follows the
795 % \enquote{full} scheme defined by the Unicode Consortium
796 % (\emph{e.g.}~\SS{} and \ss{} both fold to \texttt{ss}). As case-folding is
797 % a language-insensitive process, there is no special treatment of
798 % Turkic input (i.e., \texttt{I} always folds to \texttt{i} and
799 % not to \texttt{\i}).
800 % \end{function}
801 %
802 % \begin{function}[added = 2023-05-19, EXP]
803 % {
804 % \str_mdfive_hash:n, \str_mdfive_hash:V, \str_mdfive_hash:v,
805 % \str_mdfive_hash:o, \str_mdfive_hash:e
806 % }
807 % \begin{syntax}
808 % \cs{str_mdfive_hash:n} \Arg{tokens}
809 % \end{syntax}
810 % Expands to the MD5 sum generated from the \meta{tokens}, which is converted
811 % to a \meta{string} as described for \cs{tl_to_str:n}.
812 % \end{function}
813 %
814 % \section{Viewing strings}
815 %
816 % \begin{function}[added = 2015-09-18, updated = 2021-04-29]
817 % {\str_show:N, \str_show:c, \str_show:n}
818 % \begin{syntax}
819 % \cs{str_show:N} \meta{str~var}
820 % \end{syntax}
821 % Displays the content of the \meta{str~var} on the terminal.
822 % \end{function}
823 %
824 % \begin{function}[added = 2019-02-15, updated = 2021-04-29]
825 % {\str_log:N, \str_log:c, \str_log:n}
826 % \begin{syntax}
827 % \cs{str_log:N} \meta{str~var}
828 % \end{syntax}
829 % Writes the content of the \meta{str~var} in the log file.
830 % \end{function}
831 %
832 % \section{Constant strings}
833 %
834 % \begin{variable}[added = 2015-09-19, updated = 2020-12-22, module = str]
835 % {
836 % \c_ampersand_str,
837 % \c_atsign_str,
838 % \c_backslash_str,
839 % \c_left_brace_str,
840 % \c_right_brace_str,
841 % \c_circumflex_str,
842 % \c_colon_str,
843 % \c_dollar_str,
844 % \c_hash_str,
845 % \c_percent_str,
846 % \c_tilde_str,
847 % \c_underscore_str,
848 % \c_zero_str
849 % }
850 % Constant strings, containing a single character token, with category
851 % code $12$.
852 % \end{variable}
853 %
854 % \begin{variable}[added = 2023-12-07]{\c_empty_str}
855 % Constant that is always empty.
856 % \end{variable}
857 %
858 % \section{Scratch strings}
859 %
860 % \begin{variable}{\l_tmpa_str, \l_tmpb_str}
861 % Scratch strings for local assignment. These are never used by
862 % the kernel code, and so are safe for use with any \LaTeX3-defined
863 % function. However, they may be overwritten by other non-kernel
864 % code and so should only be used for short-term storage.
865 % \end{variable}
866 %
867 % \begin{variable}{\g_tmpa_str, \g_tmpb_str}
868 % Scratch strings for global assignment. These are never used by
869 % the kernel code, and so are safe for use with any \LaTeX3-defined
870 % function. However, they may be overwritten by other non-kernel
871 % code and so should only be used for short-term storage.
872 % \end{variable}
873 %
874 % \end{documentation}
875 %
876 % \begin{implementation}
877 %
878 % \section{\pkg{l3str} implementation}
879 %
880 % \begin{macrocode}
881 %<*code>
882 % \end{macrocode}
883 %
884 % \begin{macrocode}
885 %<@@=str>
886 % \end{macrocode}
887 %
888 % \subsection{Internal auxiliaries}
889 %
890 % \begin{variable}{\s_@@_mark,\s_@@_stop}
891 % Internal scan marks.
892 % \begin{macrocode}
893 \scan_new:N \s_@@_mark
894 \scan_new:N \s_@@_stop
895 % \end{macrocode}
896 % \end{variable}
897 %
898 % \begin{macro}[EXP]{
899 % \@@_use_none_delimit_by_s_stop:w,
900 % \@@_use_i_delimit_by_s_stop:nw
901 % }
902 % Functions to gobble up to a scan mark.
903 % \begin{macrocode}
904 \cs_new:Npn \@@_use_none_delimit_by_s_stop:w #1 \s_@@_stop { }
905 \cs_new:Npn \@@_use_i_delimit_by_s_stop:nw #1 #2 \s_@@_stop {#1}
906 % \end{macrocode}
907 % \end{macro}
908 %
909 % \begin{variable}{\q_@@_recursion_tail,\q_@@_recursion_stop}
910 % Internal recursion quarks.
911 % \begin{macrocode}
912 \quark_new:N \q_@@_recursion_tail
913 \quark_new:N \q_@@_recursion_stop
914 % \end{macrocode}
915 % \end{variable}
916 %
917 % \begin{macro}[EXP]{
918 % \@@_if_recursion_tail_break:NN,
919 % \@@_if_recursion_tail_stop_do:Nn
920 % }
921 % Functions to query recursion quarks.
922 % \begin{macrocode}
923 \__kernel_quark_new_test:N \@@_if_recursion_tail_break:NN
924 \__kernel_quark_new_test:N \@@_if_recursion_tail_stop_do:Nn
925 % \end{macrocode}
926 % \end{macro}
927 %
928 % \subsection{Creating and setting string variables}
929 %
930 % \begin{macro}
931 % {
932 % \str_new:N, \str_new:c,
933 % \str_use:N, \str_use:c,
934 % \str_clear:N, \str_clear:c,
935 % \str_gclear:N,\str_gclear:c,
936 % \str_clear_new:N, \str_clear_new:c,
937 % \str_gclear_new:N, \str_gclear_new:c
938 % }
939 % \begin{macro}
940 % {
941 % \str_set_eq:NN, \str_set_eq:cN, \str_set_eq:Nc, \str_set_eq:cc,
942 % \str_gset_eq:NN, \str_gset_eq:cN, \str_gset_eq:Nc, \str_gset_eq:cc
943 % }
944 % \begin{macro}
945 % {\str_concat:NNN, \str_concat:ccc, \str_gconcat:NNN, \str_gconcat:ccc}
946 % A string is simply a token list. The full mapping system isn't set up
947 % yet so do things by hand.
948 % \begin{macrocode}
949 \group_begin:
950 \cs_set_protected:Npn \@@_tmp:n #1
951 {
952 \tl_if_blank:nF {#1}
953 {
954 \cs_new_eq:cc { str_ #1 :N } { tl_ #1 :N }
955 \exp_args:Nc \cs_generate_variant:Nn { str_ #1 :N } { c }
956 \@@_tmp:n
957 }
958 }
959 \@@_tmp:n
960 { new }
961 { use }
962 { clear }
963 { gclear }
964 { clear_new }
965 { gclear_new }
966 { }
967 \group_end:
968 \cs_new_eq:NN \str_set_eq:NN \tl_set_eq:NN
969 \cs_new_eq:NN \str_gset_eq:NN \tl_gset_eq:NN
970 \cs_generate_variant:Nn \str_set_eq:NN { c , Nc , cc }
971 \cs_generate_variant:Nn \str_gset_eq:NN { c , Nc , cc }
972 \cs_new_eq:NN \str_concat:NNN \tl_concat:NNN
973 \cs_new_eq:NN \str_gconcat:NNN \tl_gconcat:NNN
974 \cs_generate_variant:Nn \str_concat:NNN { ccc }
975 \cs_generate_variant:Nn \str_gconcat:NNN { ccc }
976 % \end{macrocode}
977 % \end{macro}
978 % \end{macro}
979 % \end{macro}
980 %
981 % \begin{macro}
982 % {
983 % \str_set:Nn, \str_set:NV, \str_set:Ne, \str_set:Nx,
984 % \str_set:cn, \str_set:cV, \str_set:ce, \str_set:cx,
985 % \str_gset:Nn, \str_gset:NV, \str_gset:Ne, \str_gset:Nx,
986 % \str_gset:cn, \str_gset:cV, \str_gset:ce, \str_gset:cx,
987 % \str_const:Nn, \str_const:NV, \str_const:Ne, \str_const:Nx,
988 % \str_const:cn, \str_const:cV, \str_const:ce, \str_const:cx,
989 % \str_put_left:Nn, \str_put_left:NV, \str_put_left:Ne, \str_put_left:Nx,
990 % \str_put_left:cn, \str_put_left:cV, \str_put_left:ce, \str_put_left:cx,
991 % \str_gput_left:Nn, \str_gput_left:NV, \str_gput_left:Ne, \str_gput_left:Nx,
992 % \str_gput_left:cn, \str_gput_left:cV, \str_gput_left:ce, \str_gput_left:cx,
993 % \str_put_right:Nn, \str_put_right:NV, \str_put_right:Ne, \str_put_right:Nx,
994 % \str_put_right:cn, \str_put_right:cV, \str_put_right:ce, \str_put_right:cx,
995 % \str_gput_right:Nn, \str_gput_right:NV, \str_gput_right:Ne, \str_gput_right:Nx,
996 % \str_gput_right:cn, \str_gput_right:cV, \str_gput_right:ce, \str_gput_right:cx
997 % }
998 % Similar to corresponding \pkg{l3tl} base functions, except that
999 % \cs{__kernel_exp_not:w} is replaced with \cs{__kernel_tl_to_str:w}.
1000 % Just like token list, string constants use \cs{cs_gset_nopar:Npe}
1001 % instead of \cs{__kernel_tl_gset:Nx} so that the scope checking for
1002 % |c| is applied when \pkg{l3debug} is used.
1003 % To maintain backward compatibility, in
1004 % \cs[index=str_put_left:Nn]{str_(g)put_left:Nn} and
1005 % \cs[index=str_put_right:Nn]{str_(g)put_right:Nn},
1006 % contents of string variables are wrapped in \cs{__kernel_exp_not:w}
1007 % to prevent further expansion.
1008 % \begin{macrocode}
1009 \cs_new_protected:Npn \str_set:Nn #1#2
1010 { \__kernel_tl_set:Nx #1 { \__kernel_tl_to_str:w {#2} } }
1011 \cs_gset_protected:Npn \str_gset:Nn #1#2
1012 { \__kernel_tl_gset:Nx #1 { \__kernel_tl_to_str:w {#2} } }
1013 \cs_new_protected:Npn \str_const:Nn #1#2
1014 {
1015 \__kernel_chk_if_free_cs:N #1
1016 \cs_gset_nopar:Npe #1 { \__kernel_tl_to_str:w {#2} }
1017 }
1018 \cs_new_protected:Npn \str_put_left:Nn #1#2
1019 {
1020 \__kernel_tl_set:Nx #1
1021 { \__kernel_tl_to_str:w {#2} \__kernel_exp_not:w \exp_after:wN {#1} }
1022 }
1023 \cs_new_protected:Npn \str_gput_left:Nn #1#2
1024 {
1025 \__kernel_tl_gset:Nx #1
1026 { \__kernel_tl_to_str:w {#2} \__kernel_exp_not:w \exp_after:wN {#1} }
1027 }
1028 \cs_new_protected:Npn \str_put_right:Nn #1#2
1029 {
1030 \__kernel_tl_set:Nx #1
1031 { \__kernel_exp_not:w \exp_after:wN {#1} \__kernel_tl_to_str:w {#2} }
1032 }
1033 \cs_new_protected:Npn \str_gput_right:Nn #1#2
1034 {
1035 \__kernel_tl_gset:Nx #1
1036 { \__kernel_exp_not:w \exp_after:wN {#1} \__kernel_tl_to_str:w {#2} }
1037 }
1038 \cs_generate_variant:Nn \str_set:Nn { NV , Ne , Nx , c , cV , ce , cx }
1039 \cs_generate_variant:Nn \str_gset:Nn { NV , Ne , Nx , c , cV , ce , cx }
1040 \cs_generate_variant:Nn \str_const:Nn { NV , Ne , Nx , c , cV , ce , cx }
1041 \cs_generate_variant:Nn \str_put_left:Nn { NV , Ne , Nx , c , cV , ce , cx }
1042 \cs_generate_variant:Nn \str_gput_left:Nn { NV , Ne , Nx , c , cV , ce , cx }
1043 \cs_generate_variant:Nn \str_put_right:Nn { NV , Ne , Nx , c , cV , ce , cx }
1044 \cs_generate_variant:Nn \str_gput_right:Nn { NV , Ne , Nx , c , cV , ce , cx }
1045 % \end{macrocode}
1046 % \end{macro}
1047 %
1048 % \subsection{Modifying string variables}
1049 %
1050 % \begin{macro}
1051 % {
1052 % \str_replace_all:Nnn, \str_replace_all:cnn,
1053 % \str_greplace_all:Nnn, \str_greplace_all:cnn,
1054 % \str_replace_once:Nnn, \str_replace_once:cnn,
1055 % \str_greplace_once:Nnn, \str_greplace_once:cnn
1056 % }
1057 % \begin{macro}{\@@_replace:NNNnn}
1058 % \begin{macro}{\@@_replace_aux:NNNnnn}
1059 % \begin{macro}{\@@_replace_next:w}
1060 % Start by applying \cs{tl_to_str:n} to convert the old and new token
1061 % lists to strings, and also apply \cs{tl_to_str:N} to avoid any
1062 % issues if we are fed a token list variable. Then the code is a much
1063 % simplified version of the token list code because neither the
1064 % delimiter nor the replacement can contain macro parameters or
1065 % braces. The delimiter \cs{s_@@_mark} cannot appear in the string to
1066 % edit so it is used in all cases. Some |e|-expansion is unnecessary.
1067 % There is no need to avoid losing braces nor to protect against
1068 % expansion. The ending code is much simplified and does not need to
1069 % hide in braces.
1070 % \begin{macrocode}
1071 \cs_new_protected:Npn \str_replace_once:Nnn
1072 { \@@_replace:NNNnn \prg_do_nothing: \__kernel_tl_set:Nx }
1073 \cs_new_protected:Npn \str_greplace_once:Nnn
1074 { \@@_replace:NNNnn \prg_do_nothing: \__kernel_tl_gset:Nx }
1075 \cs_new_protected:Npn \str_replace_all:Nnn
1076 { \@@_replace:NNNnn \@@_replace_next:w \__kernel_tl_set:Nx }
1077 \cs_new_protected:Npn \str_greplace_all:Nnn
1078 { \@@_replace:NNNnn \@@_replace_next:w \__kernel_tl_gset:Nx }
1079 \cs_generate_variant:Nn \str_replace_once:Nnn { c }
1080 \cs_generate_variant:Nn \str_greplace_once:Nnn { c }
1081 \cs_generate_variant:Nn \str_replace_all:Nnn { c }
1082 \cs_generate_variant:Nn \str_greplace_all:Nnn { c }
1083 \cs_new_protected:Npn \@@_replace:NNNnn #1#2#3#4#5
1084 {
1085 \tl_if_empty:nTF {#4}
1086 {
1087 \msg_error:nne { kernel } { empty-search-pattern } {#5}
1088 }
1089 {
1090 \use:e
1091 {
1092 \exp_not:n { \@@_replace_aux:NNNnnn #1 #2 #3 }
1093 { \tl_to_str:N #3 }
1094 { \tl_to_str:n {#4} } { \tl_to_str:n {#5} }
1095 }
1096 }
1097 }
1098 \cs_new_protected:Npn \@@_replace_aux:NNNnnn #1#2#3#4#5#6
1099 {
1100 \cs_set:Npn \@@_replace_next:w ##1 #5 { ##1 #6 #1 }
1101 #2 #3
1102 {
1103 \@@_replace_next:w
1104 #4
1105 \@@_use_none_delimit_by_s_stop:w
1106 #5
1107 \s_@@_stop
1108 }
1109 }
1110 \cs_new_eq:NN \@@_replace_next:w ?
1111 % \end{macrocode}
1112 % \end{macro}
1113 % \end{macro}
1114 % \end{macro}
1115 % \end{macro}
1116 %
1117 % \begin{macro}{\str_remove_once:Nn, \str_remove_once:cn}
1118 % \begin{macro}{\str_gremove_once:Nn, \str_gremove_once:cn}
1119 % Removal is just a special case of replacement.
1120 % \begin{macrocode}
1121 \cs_new_protected:Npn \str_remove_once:Nn #1#2
1122 { \str_replace_once:Nnn #1 {#2} { } }
1123 \cs_new_protected:Npn \str_gremove_once:Nn #1#2
1124 { \str_greplace_once:Nnn #1 {#2} { } }
1125 \cs_generate_variant:Nn \str_remove_once:Nn { c }
1126 \cs_generate_variant:Nn \str_gremove_once:Nn { c }
1127 % \end{macrocode}
1128 % \end{macro}
1129 % \end{macro}
1130 %
1131 % \begin{macro}{\str_remove_all:Nn, \str_remove_all:cn}
1132 % \begin{macro}{\str_gremove_all:Nn, \str_gremove_all:cn}
1133 % Removal is just a special case of replacement.
1134 % \begin{macrocode}
1135 \cs_new_protected:Npn \str_remove_all:Nn #1#2
1136 { \str_replace_all:Nnn #1 {#2} { } }
1137 \cs_new_protected:Npn \str_gremove_all:Nn #1#2
1138 { \str_greplace_all:Nnn #1 {#2} { } }
1139 \cs_generate_variant:Nn \str_remove_all:Nn { c }
1140 \cs_generate_variant:Nn \str_gremove_all:Nn { c }
1141 % \end{macrocode}
1142 % \end{macro}
1143 % \end{macro}
1144 %
1145 % \subsection{String comparisons}
1146 %
1147 % \begin{macro}[pTF, EXP]
1148 % {
1149 % \str_if_empty:N, \str_if_empty:c, \str_if_empty:n,
1150 % \str_if_exist:N, \str_if_exist:c
1151 % }
1152 % More copy-paste!
1153 % \begin{macrocode}
1154 \prg_new_eq_conditional:NNn \str_if_exist:N \tl_if_exist:N
1155 { p , T , F , TF }
1156 \prg_new_eq_conditional:NNn \str_if_exist:c \tl_if_exist:c
1157 { p , T , F , TF }
1158 \prg_new_eq_conditional:NNn \str_if_empty:N \tl_if_empty:N
1159 { p , T , F , TF }
1160 \prg_new_eq_conditional:NNn \str_if_empty:c \tl_if_empty:c
1161 { p , T , F , TF }
1162 \prg_new_eq_conditional:NNn \str_if_empty:n \tl_if_empty:n
1163 { p , T , F , TF }
1164 % \end{macrocode}
1165 % \end{macro}
1166 %
1167 % \begin{macro}[EXP]{\@@_if_eq:nn}
1168 % String comparisons rely on the primitive \tn[index=pdfstrcmp]{(pdf)strcmp},
1169 % so we define a new name for it.
1170 % \begin{macrocode}
1171 \cs_new_eq:NN \@@_if_eq:nn \tex_strcmp:D
1172 % \end{macrocode}
1173 % \end{macro}
1174 %
1175 % \begin{macro}[pTF, EXP]{\str_compare:nNn, \str_compare:eNe}
1176 % Simply rely on \cs{@@_if_eq:nn}, which expands to |-1|, |0|
1177 % or~|1|. The |ee| version is created directly because it is more efficient.
1178 % \begin{macrocode}
1179 \prg_new_conditional:Npnn \str_compare:nNn #1#2#3 { p , T , F , TF }
1180 {
1181 \if_int_compare:w
1182 \@@_if_eq:nn { \exp_not:n {#1} } { \exp_not:n {#3} }
1183 #2 \c_zero_int
1184 \prg_return_true: \else: \prg_return_false: \fi:
1185 }
1186 \prg_new_conditional:Npnn \str_compare:eNe #1#2#3 { p , T , F , TF }
1187 {
1188 \if_int_compare:w \@@_if_eq:nn {#1} {#3} #2 \c_zero_int
1189 \prg_return_true: \else: \prg_return_false: \fi:
1190 }
1191 % \end{macrocode}
1192 % \end{macro}
1193 %
1194 % \begin{macro}[pTF, EXP]
1195 % {
1196 % \str_if_eq:nn, \str_if_eq:Vn, \str_if_eq:on, \str_if_eq:nV,
1197 % \str_if_eq:no, \str_if_eq:VV,
1198 % \str_if_eq:ee
1199 % }
1200 % Modern engines provide a direct way of comparing two token lists,
1201 % but returning a number. This set of conditionals therefore makes life
1202 % a bit clearer. The \texttt{nn} and \texttt{ee} versions are created
1203 % directly as this is most efficient. Since \cs{@@_if_eq:nn} will expand to
1204 % |0| as an explicit character with category 12 if the two lists match (and
1205 % either |-1| or |1| if they don't) we can use \cs{if:w} here which is faster
1206 % than using \cs{if_int_compare:w}.
1207 % \begin{macrocode}
1208 \prg_new_conditional:Npnn \str_if_eq:nn #1#2 { p , T , F , TF }
1209 {
1210 \if:w 0 \@@_if_eq:nn { \exp_not:n {#1} } { \exp_not:n {#2} }
1211 \prg_return_true: \else: \prg_return_false: \fi:
1212 }
1213 \prg_generate_conditional_variant:Nnn \str_if_eq:nn
1214 { V , v , o , nV , no , VV , nv } { p , T , F , TF }
1215 \prg_new_conditional:Npnn \str_if_eq:ee #1#2 { p , T , F , TF }
1216 {
1217 \if:w 0 \@@_if_eq:nn {#1} {#2}
1218 \prg_return_true: \else: \prg_return_false: \fi:
1219 }
1220 % \end{macrocode}
1221 % \end{macro}
1222 %
1223 % \begin{macro}[EXP, pTF]
1224 % {\str_if_eq:NN, \str_if_eq:Nc, \str_if_eq:cN, \str_if_eq:cc}
1225 % Note that \cs{str_if_eq:NNTF} is different from
1226 % \cs{tl_if_eq:NNTF} because it needs to ignore category codes.
1227 % \begin{macrocode}
1228 \prg_new_conditional:Npnn \str_if_eq:NN #1#2 { p , TF , T , F }
1229 {
1230 \if:w 0 \@@_if_eq:nn { \tl_to_str:N #1 } { \tl_to_str:N #2 }
1231 \prg_return_true: \else: \prg_return_false: \fi:
1232 }
1233 \prg_generate_conditional_variant:Nnn \str_if_eq:NN
1234 { c , Nc , cc } { T , F , TF , p }
1235 % \end{macrocode}
1236 % \end{macro}
1237 %
1238 % \begin{macro}[TF]{\str_if_in:Nn, \str_if_in:cn, \str_if_in:nn}
1239 % Everything here needs to be detokenized but beyond that it is a
1240 % simple token list test. It would be faster to fine-tune the |T|,
1241 % |F|, |TF| variants by calling the appropriate variant of
1242 % \cs{tl_if_in:nnTF} directly but that takes more code.
1243 % \begin{macrocode}
1244 \prg_new_protected_conditional:Npnn \str_if_in:Nn #1#2 { T , F , TF }
1245 {
1246 \use:e
1247 { \tl_if_in:nnTF { \tl_to_str:N #1 } { \tl_to_str:n {#2} } }
1248 { \prg_return_true: } { \prg_return_false: }
1249 }
1250 \prg_generate_conditional_variant:Nnn \str_if_in:Nn
1251 { c } { T , F , TF }
1252 \prg_new_protected_conditional:Npnn \str_if_in:nn #1#2 { T , F , TF }
1253 {
1254 \use:e
1255 { \tl_if_in:nnTF { \tl_to_str:n {#1} } { \tl_to_str:n {#2} } }
1256 { \prg_return_true: } { \prg_return_false: }
1257 }
1258 % \end{macrocode}
1259 % \end{macro}
1260 %
1261 % \begin{macro}[EXP, noTF]
1262 % {
1263 % \str_case:nn, \str_case:Vn, \str_case:Nn, \str_case:on, \str_case:en,
1264 % \str_case:nV, \str_case:nv, \str_case:ne,
1265 % \str_case_e:nn, \str_case_e:en
1266 % }
1267 % \begin{macro}[EXP]{\@@_case:nnTF, \@@_case_e:nnTF}
1268 % \begin{macro}[EXP]
1269 % {\@@_case:nw, \@@_case_e:nw, \@@_case_end:nw}
1270 % The aim here is to allow the case statement to be evaluated
1271 % using a known number of expansion steps (two), and without
1272 % needing to use an explicit \enquote{end of recursion} marker.
1273 % That is achieved by using the test input as the final case,
1274 % as this is always true. The trick is then to tidy up
1275 % the output such that the appropriate case code plus either
1276 % the \texttt{true} or \texttt{false} branch code is inserted.
1277 % \begin{macrocode}
1278 \cs_new:Npn \str_case:nn #1#2
1279 {
1280 \exp:w
1281 \@@_case:nnTF {#1} {#2} { } { }
1282 }
1283 \cs_new:Npn \str_case:nnT #1#2#3
1284 {
1285 \exp:w
1286 \@@_case:nnTF {#1} {#2} {#3} { }
1287 }
1288 \cs_new:Npn \str_case:nnF #1#2
1289 {
1290 \exp:w
1291 \@@_case:nnTF {#1} {#2} { }
1292 }
1293 \cs_new:Npn \str_case:nnTF #1#2
1294 {
1295 \exp:w
1296 \@@_case:nnTF {#1} {#2}
1297 }
1298 \cs_new:Npn \@@_case:nnTF #1#2#3#4
1299 { \@@_case:nw {#1} #2 {#1} { } \s_@@_mark {#3} \s_@@_mark {#4} \s_@@_stop }
1300 \cs_generate_variant:Nn \str_case:nn { V , o , e , nV , nv , ne }
1301 \prg_generate_conditional_variant:Nnn \str_case:nn
1302 { V , o , e , nV , nv , ne } { T , F , TF }
1303 \cs_new_eq:NN \str_case:Nn \str_case:Vn
1304 \cs_new_eq:NN \str_case:NnT \str_case:VnT
1305 \cs_new_eq:NN \str_case:NnF \str_case:VnF
1306 \cs_new_eq:NN \str_case:NnTF \str_case:VnTF
1307 \cs_new:Npn \@@_case:nw #1#2#3
1308 {
1309 \str_if_eq:nnTF {#1} {#2}
1310 { \@@_case_end:nw {#3} }
1311 { \@@_case:nw {#1} }
1312 }
1313 \cs_new:Npn \str_case_e:nn #1#2
1314 {
1315 \exp:w
1316 \@@_case_e:nnTF {#1} {#2} { } { }
1317 }
1318 \cs_new:Npn \str_case_e:nnT #1#2#3
1319 {
1320 \exp:w
1321 \@@_case_e:nnTF {#1} {#2} {#3} { }
1322 }
1323 \cs_new:Npn \str_case_e:nnF #1#2
1324 {
1325 \exp:w
1326 \@@_case_e:nnTF {#1} {#2} { }
1327 }
1328 \cs_new:Npn \str_case_e:nnTF #1#2
1329 {
1330 \exp:w
1331 \@@_case_e:nnTF {#1} {#2}
1332 }
1333 \cs_new:Npn \@@_case_e:nnTF #1#2#3#4
1334 { \@@_case_e:nw {#1} #2 {#1} { } \s_@@_mark {#3} \s_@@_mark {#4} \s_@@_stop }
1335 \cs_generate_variant:Nn \str_case_e:nn { e }
1336 \prg_generate_conditional_variant:Nnn \str_case_e:nn { e } { T , F , TF }
1337 \cs_new:Npn \@@_case_e:nw #1#2#3
1338 {
1339 \str_if_eq:eeTF {#1} {#2}
1340 { \@@_case_end:nw {#3} }
1341 { \@@_case_e:nw {#1} }
1342 }
1343 % \end{macrocode}
1344 % To tidy up the recursion, there are two outcomes. If there was a hit to
1345 % one of the cases searched for, then |#1| is the code to insert,
1346 % |#2| is the \emph{next} case to check on and |#3| is all of
1347 % the rest of the cases code. That means that |#4| is the \texttt{true}
1348 % branch code, and |#5| tidies up the spare \cs{s_@@_mark} and the
1349 % \texttt{false} branch. On the other hand, if none of the cases matched
1350 % then we arrive here using the \enquote{termination} case of comparing
1351 % the search with itself. That means that |#1| is empty, |#2| is
1352 % the first \cs{s_@@_mark} and so |#4| is the \texttt{false} code (the
1353 % \texttt{true} code is mopped up by |#3|).
1354 % \begin{macrocode}
1355 \cs_new:Npn \@@_case_end:nw #1#2#3 \s_@@_mark #4#5 \s_@@_stop
1356 { \exp_end: #1 #4 }
1357 % \end{macrocode}
1358 % \end{macro}
1359 % \end{macro}
1360 % \end{macro}
1361 %
1362 % \subsection{Mapping over strings}
1363 %
1364 % \begin{macro}[rEXP]{\str_map_function:NN, \str_map_function:cN}
1365 % \begin{macro}[rEXP]{\str_map_function:nN}
1366 % \begin{macro}{\str_map_inline:Nn, \str_map_inline:cn}
1367 % \begin{macro}{\str_map_inline:nn}
1368 % \begin{macro}{\str_map_variable:NNn, \str_map_variable:cNn}
1369 % \begin{macro}{\str_map_variable:nNn}
1370 % \begin{macro}{\str_map_break:}
1371 % \begin{macro}{\str_map_break:n}
1372 % \begin{macro}[rEXP]{\@@_map_function:w, \@@_map_function:nn}
1373 % \begin{macro}{\@@_map_inline:NN, \@@_map_variable:NnN}
1374 % The inline and variable mappings are similar to the usual token list
1375 % mappings but start out by turning the argument to an ``other
1376 % string''. Doing the same for the expandable function mapping would
1377 % require \cs{__kernel_str_to_other:n}, quadratic in the string length. To deal
1378 % with spaces in that case, \cs{@@_map_function:w} replaces the
1379 % following space by a braced space and a further call to itself.
1380 % These are received by \cs{@@_map_function:nn}, which passes
1381 % the space to |#1| and calls \cs{@@_map_function:w} to deal with the
1382 % next space. The space before the braced space allows to optimize
1383 % the \cs{q_@@_recursion_tail} test. Of course we need to include a
1384 % trailing space (the question mark is needed to avoid losing the
1385 % space when \TeX{} tokenizes the line).
1386 % At the cost of about three more auxiliaries this code could get a $9$
1387 % times speed up by testing only every $9$-th character for whether it
1388 % is \cs{q_@@_recursion_tail} (also by converting $9$ spaces at a time in
1389 % the \cs{str_map_function:nN} case).
1390 %
1391 % For the \texttt{map_variable} functions we use a string assignment
1392 % to store each character because spaces are made catcode~$12$ before
1393 % the loop.
1394 % \begin{macrocode}
1395 \cs_new:Npn \str_map_function:nN #1#2
1396 {
1397 \exp_after:wN \@@_map_function:w
1398 \exp_after:wN \@@_map_function:nn \exp_after:wN #2
1399 \__kernel_tl_to_str:w {#1}
1400 \q_@@_recursion_tail ? ~
1401 \prg_break_point:Nn \str_map_break: { }
1402 }
1403 \cs_new:Npn \str_map_function:NN
1404 { \exp_args:No \str_map_function:nN }
1405 \cs_new:Npn \@@_map_function:w #1 ~
1406 { #1 { ~ { ~ } \@@_map_function:w } }
1407 \cs_new:Npn \@@_map_function:nn #1#2
1408 {
1409 \if_meaning:w \q_@@_recursion_tail #2
1410 \exp_after:wN \str_map_break:
1411 \fi:
1412 #1 #2 \@@_map_function:nn {#1}
1413 }
1414 \cs_generate_variant:Nn \str_map_function:NN { c }
1415 \cs_new_protected:Npn \str_map_inline:nn #1#2
1416 {
1417 \int_gincr:N \g__kernel_prg_map_int
1418 \cs_gset_protected:cpn
1419 { @@_map_ \int_use:N \g__kernel_prg_map_int :w } ##1 {#2}
1420 \use:e
1421 {
1422 \exp_not:N \@@_map_inline:NN
1423 \exp_not:c { @@_map_ \int_use:N \g__kernel_prg_map_int :w }
1424 \__kernel_str_to_other_fast:n {#1}
1425 }
1426 \q_@@_recursion_tail
1427 \prg_break_point:Nn \str_map_break:
1428 { \int_gdecr:N \g__kernel_prg_map_int }
1429 }
1430 \cs_new_protected:Npn \str_map_inline:Nn
1431 { \exp_args:No \str_map_inline:nn }
1432 \cs_generate_variant:Nn \str_map_inline:Nn { c }
1433 \cs_new:Npn \@@_map_inline:NN #1#2
1434 {
1435 \@@_if_recursion_tail_break:NN #2 \str_map_break:
1436 \exp_args:No #1 { \token_to_str:N #2 }
1437 \@@_map_inline:NN #1
1438 }
1439 \cs_new_protected:Npn \str_map_variable:nNn #1#2#3
1440 {
1441 \use:e
1442 {
1443 \exp_not:n { \@@_map_variable:NnN #2 {#3} }
1444 \__kernel_str_to_other_fast:n {#1}
1445 }
1446 \q_@@_recursion_tail
1447 \prg_break_point:Nn \str_map_break: { }
1448 }
1449 \cs_new_protected:Npn \str_map_variable:NNn
1450 { \exp_args:No \str_map_variable:nNn }
1451 \cs_new_protected:Npn \@@_map_variable:NnN #1#2#3
1452 {
1453 \@@_if_recursion_tail_break:NN #3 \str_map_break:
1454 \str_set:Nn #1 {#3}
1455 \use:n {#2}
1456 \@@_map_variable:NnN #1 {#2}
1457 }
1458 \cs_generate_variant:Nn \str_map_variable:NNn { c }
1459 \cs_new:Npn \str_map_break:
1460 { \prg_map_break:Nn \str_map_break: { } }
1461 \cs_new:Npn \str_map_break:n
1462 { \prg_map_break:Nn \str_map_break: }
1463 % \end{macrocode}
1464 % \end{macro}
1465 % \end{macro}
1466 % \end{macro}
1467 % \end{macro}
1468 % \end{macro}
1469 % \end{macro}
1470 % \end{macro}
1471 % \end{macro}
1472 % \end{macro}
1473 % \end{macro}
1474 %
1475 % \begin{macro}[rEXP]{\str_map_tokens:Nn, \str_map_tokens:cn}
1476 % \begin{macro}[rEXP]{\str_map_tokens:nn}
1477 % Uses an auxiliary of \cs{str_map_function:NN}.
1478 % \begin{macrocode}
1479 \cs_new:Npn \str_map_tokens:nn #1#2
1480 {
1481 \exp_args:Nno \use:nn
1482 { \@@_map_function:w \@@_map_function:nn {#2} }
1483 { \__kernel_tl_to_str:w {#1} }
1484 \q_@@_recursion_tail ? ~
1485 \prg_break_point:Nn \str_map_break: { }
1486 }
1487 \cs_new:Npn \str_map_tokens:Nn { \exp_args:No \str_map_tokens:nn }
1488 \cs_generate_variant:Nn \str_map_tokens:Nn { c }
1489 % \end{macrocode}
1490 % \end{macro}
1491 % \end{macro}
1492 %
1493 % \subsection{Accessing specific characters in a string}
1494 %
1495 % \begin{macro}[EXP]{\__kernel_str_to_other:n}
1496 % \begin{macro}[EXP]{\@@_to_other_loop:w, \@@_to_other_end:w}
1497 % First apply \cs{tl_to_str:n}, then replace all spaces by
1498 % \enquote{other} spaces, $8$ at a time, storing the converted part of
1499 % the string between the \cs{s_@@_mark} and \cs{s_@@_stop} markers. The end
1500 % is detected when \cs{@@_to_other_loop:w} finds one of the trailing
1501 % |A|, distinguished from any contents of the initial token list by
1502 % their category. Then \cs{@@_to_other_end:w} is called, and finds
1503 % the result between \cs{s_@@_mark} and the first |A| (well, there is
1504 % also the need to remove a space).
1505 % \begin{macrocode}
1506 \cs_new:Npn \__kernel_str_to_other:n #1
1507 {
1508 \exp_after:wN \@@_to_other_loop:w
1509 \tl_to_str:n {#1} ~ A ~ A ~ A ~ A ~ A ~ A ~ A ~ A ~ \s_@@_mark \s_@@_stop
1510 }
1511 \group_begin:
1512 \tex_lccode:D `\* = `\ %
1513 \tex_lccode:D `\A = `\A %
1514 \tex_lowercase:D
1515 {
1516 \group_end:
1517 \cs_new:Npn \@@_to_other_loop:w
1518 #1 ~ #2 ~ #3 ~ #4 ~ #5 ~ #6 ~ #7 ~ #8 ~ #9 \s_@@_stop
1519 {
1520 \if_meaning:w A #8
1521 \@@_to_other_end:w
1522 \fi:
1523 \@@_to_other_loop:w
1524 #9 #1 * #2 * #3 * #4 * #5 * #6 * #7 * #8 * \s_@@_stop
1525 }
1526 \cs_new:Npn \@@_to_other_end:w \fi: #1 \s_@@_mark #2 * A #3 \s_@@_stop
1527 { \fi: #2 }
1528 }
1529 % \end{macrocode}
1530 % \end{macro}
1531 % \end{macro}
1532 %
1533 % \begin{macro}[rEXP]{\__kernel_str_to_other_fast:n}
1534 % \begin{macro}[rEXP]{\__kernel_str_to_other_fast_loop:w, \@@_to_other_fast_end:w}
1535 % The difference with \cs{__kernel_str_to_other:n} is that the converted part is
1536 % left in the input stream, making these commands only
1537 % restricted-expandable.
1538 % \begin{macrocode}
1539 \cs_new:Npn \__kernel_str_to_other_fast:n #1
1540 {
1541 \exp_after:wN \@@_to_other_fast_loop:w \tl_to_str:n {#1} ~
1542 A ~ A ~ A ~ A ~ A ~ A ~ A ~ A ~ A ~ \s_@@_stop
1543 }
1544 \group_begin:
1545 \tex_lccode:D `\* = `\ %
1546 \tex_lccode:D `\A = `\A %
1547 \tex_lowercase:D
1548 {
1549 \group_end:
1550 \cs_new:Npn \@@_to_other_fast_loop:w
1551 #1 ~ #2 ~ #3 ~ #4 ~ #5 ~ #6 ~ #7 ~ #8 ~ #9 ~
1552 {
1553 \if_meaning:w A #9
1554 \@@_to_other_fast_end:w
1555 \fi:
1556 #1 * #2 * #3 * #4 * #5 * #6 * #7 * #8 * #9
1557 \@@_to_other_fast_loop:w *
1558 }
1559 \cs_new:Npn \@@_to_other_fast_end:w #1 * A #2 \s_@@_stop {#1}
1560 }
1561 % \end{macrocode}
1562 % \end{macro}
1563 % \end{macro}
1564 %
1565 % \begin{macro}{\@@_sep:}
1566 % \begin{macrocode}
1567 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
1568 % \end{macrocode}
1569 % \end{macro}
1570 %
1571 % \begin{macro}[EXP]
1572 % {\str_item:Nn, \str_item:cn, \str_item:nn, \str_item_ignore_spaces:nn}
1573 % \begin{macro}[EXP]{\@@_item:nn, \@@_item:w}
1574 % The \cs{str_item:nn} hands its argument with spaces escaped to
1575 % \cs{@@_item:nn}, and makes sure to turn the result back into
1576 % a proper string (with category code~$10$ spaces) eventually. The
1577 % \cs{str_item_ignore_spaces:nn} function does not escape spaces,
1578 % which are thus ignored by \cs{@@_item:nn} since
1579 % everything else is done with undelimited arguments.
1580 % Evaluate the \meta{index} argument~|#2| and count characters in
1581 % the string, passing those two numbers to \cs{@@_item:w} for
1582 % further analysis. If the \meta{index} is negative, shift it by
1583 % the \meta{count} to know the how many character to discard, and if
1584 % that is still negative give an empty result. If the \meta{index}
1585 % is larger than the \meta{count}, give an empty result, and
1586 % otherwise discard $\meta{index}-1$ characters before returning the
1587 % following one. The shift by $-1$ is obtained by inserting an empty
1588 % brace group before the string in that case: that brace group also
1589 % covers the case where the \meta{index} is zero.
1590 % \begin{macrocode}
1591 \cs_new:Npn \str_item:Nn { \exp_args:No \str_item:nn }
1592 \cs_generate_variant:Nn \str_item:Nn { c }
1593 \cs_new:Npn \str_item:nn #1#2
1594 {
1595 \exp_args:Nf \tl_to_str:n
1596 {
1597 \exp_args:Nf \@@_item:nn
1598 { \__kernel_str_to_other:n {#1} } {#2}
1599 }
1600 }
1601 \cs_new:Npn \str_item_ignore_spaces:nn #1
1602 { \exp_args:No \@@_item:nn { \tl_to_str:n {#1} } }
1603 \cs_new:Npn \@@_item:nn #1#2
1604 {
1605 \exp_after:wN \@@_item:w
1606 \int_value:w \int_eval:n {#2} \exp_after:wN \@@_sep:
1607 \int_value:w \@@_count:n {#1} \@@_sep:
1608 #1 \s_@@_stop
1609 }
1610 \cs_new:Npn \@@_item:w #1 \@@_sep: #2 \@@_sep:
1611 {
1612 \int_compare:nNnTF {#1} < 0
1613 {
1614 \int_compare:nNnTF {#1} < {-#2}
1615 { \@@_use_none_delimit_by_s_stop:w }
1616 {
1617 \exp_after:wN \@@_use_i_delimit_by_s_stop:nw
1618 \exp:w \exp_after:wN \@@_skip_exp_end:w
1619 \int_value:w \int_eval:n { #1 + #2 } \@@_sep:
1620 }
1621 }
1622 {
1623 \int_compare:nNnTF {#1} > {#2}
1624 { \@@_use_none_delimit_by_s_stop:w }
1625 {
1626 \exp_after:wN \@@_use_i_delimit_by_s_stop:nw
1627 \exp:w \@@_skip_exp_end:w #1 \@@_sep: { }
1628 }
1629 }
1630 }
1631 % \end{macrocode}
1632 % \end{macro}
1633 % \end{macro}
1634 %
1635 % \begin{macro}[EXP]{\@@_skip_exp_end:w}
1636 % \begin{macro}[EXP]
1637 % {\@@_skip_loop:wNNNNNNNN, \@@_skip_end:w, \@@_skip_end:NNNNNNNN}
1638 % Removes |max(#1,0)| characters from the input stream, and then
1639 % leaves \cs{exp_end:}. This should be expanded using
1640 % \cs{exp:w}. We remove characters $8$ at a time until
1641 % there are at most $8$ to remove. Then we do a dirty trick: the
1642 % \cs{if_case:w} construction leaves between $0$ and $8$ times the
1643 % \cs{or:} control sequence, and those \cs{or:} become arguments of
1644 % \cs{@@_skip_end:NNNNNNNN}. If the number of characters to remove
1645 % is $6$, say, then there are two \cs{or:} left, and the $8$ arguments
1646 % of \cs{@@_skip_end:NNNNNNNN} are the two \cs{or:}, and $6$
1647 % characters from the input stream, exactly what we wanted to
1648 % remove. Then close the \cs{if_case:w} conditional with \cs{fi:}, and
1649 % stop the initial expansion with \cs{exp_end:} (see places where
1650 % \cs{@@_skip_exp_end:w} is called).
1651 % \begin{macrocode}
1652 \cs_new:Npn \@@_skip_exp_end:w #1 \@@_sep:
1653 {
1654 \if_int_compare:w #1 > 8 \exp_stop_f:
1655 \exp_after:wN \@@_skip_loop:wNNNNNNNN
1656 \else:
1657 \exp_after:wN \@@_skip_end:w
1658 \int_value:w \int_eval:w
1659 \fi:
1660 #1 \@@_sep:
1661 }
1662 \cs_new:Npn \@@_skip_loop:wNNNNNNNN #1 \@@_sep: #2#3#4#5#6#7#8#9
1663 {
1664 \exp_after:wN \@@_skip_exp_end:w
1665 \int_value:w \int_eval:n { #1 - 8 } \@@_sep:
1666 }
1667 \cs_new:Npn \@@_skip_end:w #1 \@@_sep:
1668 {
1669 \exp_after:wN \@@_skip_end:NNNNNNNN
1670 \if_case:w #1 \exp_stop_f: \or: \or: \or: \or: \or: \or: \or: \or:
1671 }
1672 \cs_new:Npn \@@_skip_end:NNNNNNNN #1#2#3#4#5#6#7#8 { \fi: \exp_end: }
1673 % \end{macrocode}
1674 % \end{macro}
1675 % \end{macro}
1676 %
1677 % \begin{macro}[EXP]
1678 % {\str_range:Nnn, \str_range:nnn, \str_range_ignore_spaces:nnn}
1679 % \begin{macro}[EXP]{\@@_range:nnn}
1680 % \begin{macro}[EXP]{\@@_range:w, \@@_range:nnw}
1681 % Sanitize the string. Then evaluate the arguments. At this stage we
1682 % also decrement the \meta{start index}, since our goal is to know how
1683 % many characters should be removed. Then limit the range to be
1684 % non-negative and at most the length of the string (this avoids
1685 % needing to check for the end of the string when grabbing
1686 % characters), shifting negative numbers by the appropriate amount.
1687 % Afterwards, skip characters, then keep some more, and finally drop
1688 % the end of the string.
1689 % \begin{macrocode}
1690 \cs_new:Npn \str_range:Nnn { \exp_args:No \str_range:nnn }
1691 \cs_generate_variant:Nn \str_range:Nnn { c }
1692 \cs_new:Npn \str_range:nnn #1#2#3
1693 {
1694 \exp_args:Nf \tl_to_str:n
1695 {
1696 \exp_args:Nf \@@_range:nnn
1697 { \__kernel_str_to_other:n {#1} } {#2} {#3}
1698 }
1699 }
1700 \cs_new:Npn \str_range_ignore_spaces:nnn #1
1701 { \exp_args:No \@@_range:nnn { \tl_to_str:n {#1} } }
1702 \cs_new:Npn \@@_range:nnn #1#2#3
1703 {
1704 \exp_after:wN \@@_range:w
1705 \int_value:w \@@_count:n {#1} \exp_after:wN \@@_sep:
1706 \int_value:w \int_eval:n { (#2) - 1 } \exp_after:wN \@@_sep:
1707 \int_value:w \int_eval:n {#3} \@@_sep:
1708 #1 \s_@@_stop
1709 }
1710 \cs_new:Npn \@@_range:w #1 \@@_sep: #2 \@@_sep: #3 \@@_sep:
1711 {
1712 \exp_args:Nf \@@_range:nnw
1713 { \@@_range_normalize:nn {#2} {#1} }
1714 { \@@_range_normalize:nn {#3} {#1} }
1715 }
1716 \cs_new:Npn \@@_range:nnw #1#2
1717 {
1718 \exp_after:wN \@@_collect_delimit_by_q_stop:w
1719 \int_value:w \int_eval:n { #2 - #1 } \exp_after:wN \@@_sep:
1720 \exp:w \@@_skip_exp_end:w #1 \@@_sep:
1721 }
1722 % \end{macrocode}
1723 % \end{macro}
1724 % \end{macro}
1725 % \end{macro}
1726 % \begin{macro}[EXP]{\@@_range_normalize:nn}
1727 % This function converts an \meta{index} argument into an explicit
1728 % position in the string (a result of $0$ denoting \enquote{out of
1729 % bounds}). Expects two explicit integer arguments: the
1730 % \meta{index} |#1| and the string count~|#2|. If |#1| is negative,
1731 % replace it by $|#1| + |#2| + 1$, then limit to the range $[0,
1732 % |#2|]$.
1733 % \begin{macrocode}
1734 \cs_new:Npn \@@_range_normalize:nn #1#2
1735 {
1736 \int_eval:n
1737 {
1738 \if_int_compare:w #1 < \c_zero_int
1739 \if_int_compare:w #1 < -#2 \exp_stop_f:
1740 0
1741 \else:
1742 #1 + #2 + 1
1743 \fi:
1744 \else:
1745 \if_int_compare:w #1 < #2 \exp_stop_f:
1746 #1
1747 \else:
1748 #2
1749 \fi:
1750 \fi:
1751 }
1752 }
1753 % \end{macrocode}
1754 % \end{macro}
1755 % \begin{macro}[EXP]{\@@_collect_delimit_by_q_stop:w}
1756 % \begin{macro}[EXP]
1757 % {
1758 % \@@_collect_loop:wn, \@@_collect_loop:wnNNNNNNN,
1759 % \@@_collect_end:wn, \@@_collect_end:nnnnnnnnw
1760 % }
1761 % Collects |max(#1,0)| characters, and removes everything else until
1762 % \cs{s_@@_stop}. This is somewhat similar to \cs{@@_skip_exp_end:w}, but
1763 % accepts integer expression arguments. This time we can only grab
1764 % $7$ characters at a time. At the end, we use an \cs{if_case:w}
1765 % trick again, so that the $8$ first arguments of
1766 % \cs{@@_collect_end:nnnnnnnnw} are some \cs{or:}, followed by an
1767 % \cs{fi:}, followed by |#1| characters from the input stream. Simply
1768 % leaving this in the input stream closes the conditional properly
1769 % and the \cs{or:} disappear.
1770 % \begin{macrocode}
1771 \cs_new:Npn \@@_collect_delimit_by_q_stop:w #1 \@@_sep:
1772 { \@@_collect_loop:wn #1 \@@_sep: { } }
1773 \cs_new:Npn \@@_collect_loop:wn #1 \@@_sep:
1774 {
1775 \if_int_compare:w #1 > 7 \exp_stop_f:
1776 \exp_after:wN \@@_collect_loop:wnNNNNNNN
1777 \else:
1778 \exp_after:wN \@@_collect_end:wn
1779 \fi:
1780 #1 \@@_sep:
1781 }
1782 \cs_new:Npn \@@_collect_loop:wnNNNNNNN #1 \@@_sep: #2 #3#4#5#6#7#8#9
1783 {
1784 \exp_after:wN \@@_collect_loop:wn
1785 \int_value:w \int_eval:n { #1 - 7 } \@@_sep:
1786 { #2 #3#4#5#6#7#8#9 }
1787 }
1788 \cs_new:Npn \@@_collect_end:wn #1 \@@_sep:
1789 {
1790 \exp_after:wN \@@_collect_end:nnnnnnnnw
1791 \if_case:w \if_int_compare:w #1 > \c_zero_int
1792 #1 \else: 0 \fi: \exp_stop_f:
1793 \or: \or: \or: \or: \or: \or: \fi:
1794 }
1795 \cs_new:Npn \@@_collect_end:nnnnnnnnw #1#2#3#4#5#6#7#8 #9 \s_@@_stop
1796 { #1#2#3#4#5#6#7#8 }
1797 % \end{macrocode}
1798 % \end{macro}
1799 % \end{macro}
1800 %
1801 % \subsection{Counting characters}
1802 %
1803 % \begin{macro}[EXP]
1804 % {\str_count_spaces:N, \str_count_spaces:c, \str_count_spaces:n}
1805 % \begin{macro}[EXP]{\@@_count_spaces_loop:w}
1806 % To speed up this function, we grab and discard $9$ space-delimited
1807 % arguments in each iteration of the loop. The loop stops when the
1808 % last argument is one of the trailing |X|\meta{number}, and that
1809 % \meta{number} is added to the sum of $9$ that precedes, to adjust
1810 % the result.
1811 % \begin{macrocode}
1812 \cs_new:Npn \str_count_spaces:N
1813 { \exp_args:No \str_count_spaces:n }
1814 \cs_generate_variant:Nn \str_count_spaces:N { c }
1815 \cs_new:Npn \str_count_spaces:n #1
1816 {
1817 \int_eval:n
1818 {
1819 \exp_after:wN \@@_count_spaces_loop:w
1820 \tl_to_str:n {#1} ~
1821 X 7 ~ X 6 ~ X 5 ~ X 4 ~ X 3 ~ X 2 ~ X 1 ~ X 0 ~ X -1 ~
1822 \s_@@_stop
1823 }
1824 }
1825 \cs_new:Npn \@@_count_spaces_loop:w #1~#2~#3~#4~#5~#6~#7~#8~#9~
1826 {
1827 \if_meaning:w X #9
1828 \@@_use_i_delimit_by_s_stop:nw
1829 \fi:
1830 9 + \@@_count_spaces_loop:w
1831 }
1832 % \end{macrocode}
1833 % \end{macro}
1834 % \end{macro}
1835 %
1836 % \begin{macro}[EXP]
1837 % {
1838 % \str_count:N, \str_count:c, \str_count:n, \str_count:e,
1839 % \str_count_ignore_spaces:n, \str_count_ignore_spaces:e
1840 % }
1841 % \begin{macro}[EXP]{\@@_count:n}
1842 % \begin{macro}[EXP]{\@@_count_aux:n, \@@_count_loop:NNNNNNNNN}
1843 % To count characters in a string we could first escape all spaces
1844 % using \cs{__kernel_str_to_other:n}, then pass the result to \cs{tl_count:n}.
1845 % However, the escaping step would be quadratic in the number of
1846 % characters in the string, and we can do better. Namely, sum the
1847 % number of spaces (\cs{str_count_spaces:n}) and the result of
1848 % \cs{tl_count:n}, which ignores spaces. Since strings tend to be
1849 % longer than token lists, we use specialized functions to count
1850 % characters ignoring spaces. Namely, loop, grabbing $9$ non-space
1851 % characters at each step, and end as soon as we reach one of the $9$
1852 % trailing items. The internal function \cs{@@_count:n}, used in
1853 % \cs{str_item:nn} and \cs{str_range:nnn}, is similar to
1854 % \cs{str_count_ignore_spaces:n} but expects its argument to already
1855 % be a string or a string with spaces escaped.
1856 % \begin{macrocode}
1857 \cs_new:Npn \str_count:N { \exp_args:No \str_count:n }
1858 \cs_generate_variant:Nn \str_count:N { c }
1859 \cs_new:Npn \str_count:n #1
1860 {
1861 \@@_count_aux:n
1862 {
1863 \str_count_spaces:n {#1}
1864 + \exp_after:wN \@@_count_loop:NNNNNNNNN \tl_to_str:n {#1}
1865 }
1866 }
1867 \cs_generate_variant:Nn \str_count:n { e }
1868 \cs_new:Npn \@@_count:n #1
1869 {
1870 \@@_count_aux:n
1871 { \@@_count_loop:NNNNNNNNN #1 }
1872 }
1873 \cs_new:Npn \str_count_ignore_spaces:n #1
1874 {
1875 \@@_count_aux:n
1876 { \exp_after:wN \@@_count_loop:NNNNNNNNN \tl_to_str:n {#1} }
1877 }
1878 \cs_generate_variant:Nn \str_ignore_spaces:n { e }
1879 \cs_new:Npn \@@_count_aux:n #1
1880 {
1881 \int_eval:n
1882 {
1883 #1
1884 { X 8 } { X 7 } { X 6 }
1885 { X 5 } { X 4 } { X 3 }
1886 { X 2 } { X 1 } { X 0 }
1887 \s_@@_stop
1888 }
1889 }
1890 \cs_new:Npn \@@_count_loop:NNNNNNNNN #1#2#3#4#5#6#7#8#9
1891 {
1892 \if_meaning:w X #9
1893 \exp_after:wN \@@_use_none_delimit_by_s_stop:w
1894 \fi:
1895 9 + \@@_count_loop:NNNNNNNNN
1896 }
1897 % \end{macrocode}
1898 % \end{macro}
1899 % \end{macro}
1900 % \end{macro}
1901 %
1902 % \subsection{The first character in a string}
1903 %
1904 % \begin{macro}[EXP]
1905 % {\str_head:N, \str_head:c, \str_head:n, \str_head_ignore_spaces:n}
1906 % \begin{macro}[EXP]{\@@_head:w}
1907 % The \texttt{_ignore_spaces} variant applies \cs{tl_to_str:n} then
1908 % grabs the first item, thus skipping spaces.
1909 % As usual, \cs{str_head:N} expands its argument and
1910 % hands it to \cs{str_head:n}. To circumvent the fact that \TeX{}
1911 % skips spaces when grabbing undelimited macro parameters,
1912 % \cs{@@_head:w} takes an argument delimited by a space. If |#1|
1913 % starts with a non-space character, \cs{@@_use_i_delimit_by_s_stop:nw}
1914 % leaves that in the input stream. On the other hand, if |#1| starts
1915 % with a space, the \cs{@@_head:w} takes an empty argument, and the
1916 % single (initially braced) space in the definition of \cs{@@_head:w}
1917 % makes its way to the output. Finally, for an empty argument, the
1918 % (braced) empty brace group in the definition of \cs{str_head:n}
1919 % gives an empty result after passing through
1920 % \cs{@@_use_i_delimit_by_s_stop:nw}.
1921 % \begin{macrocode}
1922 \cs_new:Npn \str_head:N { \exp_args:No \str_head:n }
1923 \cs_generate_variant:Nn \str_head:N { c }
1924 \cs_new:Npn \str_head:n #1
1925 {
1926 \exp_after:wN \@@_head:w
1927 \tl_to_str:n {#1}
1928 { { } } ~ \s_@@_stop
1929 }
1930 \cs_new:Npn \@@_head:w #1 ~ %
1931 { \@@_use_i_delimit_by_s_stop:nw #1 { ~ } }
1932 \cs_new:Npn \str_head_ignore_spaces:n #1
1933 {
1934 \exp_after:wN \@@_use_i_delimit_by_s_stop:nw
1935 \tl_to_str:n {#1} { } \s_@@_stop
1936 }
1937 % \end{macrocode}
1938 % \end{macro}
1939 % \end{macro}
1940 %
1941 % \begin{macro}[EXP]
1942 % {\str_tail:N, \str_tail:c, \str_tail:n, \str_tail_ignore_spaces:n}
1943 % \begin{macro}[EXP]{\@@_tail_auxi:w, \@@_tail_auxii:w}
1944 % Getting the tail is a little bit more convoluted than the head of a
1945 % string. We hit the front of the string with \cs{reverse_if:N}
1946 % \cs{if_charcode:w} \cs{scan_stop:}. This removes the first
1947 % character, and necessarily makes the test true, since the character
1948 % cannot match \cs{scan_stop:}. The auxiliary function then inserts
1949 % the required \cs{fi:} to close the conditional, and leaves the tail
1950 % of the string in the input stream. The details are such that an
1951 % empty string has an empty tail (this requires in particular that the
1952 % end-marker |X| be unexpandable and not a control sequence). The
1953 % \texttt{_ignore_spaces} is rather simpler: after converting the
1954 % input to a string, \cs{@@_tail_auxii:w} removes one undelimited
1955 % argument and leaves everything else until an end-marker \cs{s_@@_mark}.
1956 % One can check that an empty (or blank) string yields an empty
1957 % tail.
1958 % \begin{macrocode}
1959 \cs_new:Npn \str_tail:N { \exp_args:No \str_tail:n }
1960 \cs_generate_variant:Nn \str_tail:N { c }
1961 \cs_new:Npn \str_tail:n #1
1962 {
1963 \exp_after:wN \@@_tail_auxi:w
1964 \reverse_if:N \if_charcode:w
1965 \scan_stop: \tl_to_str:n {#1} X X \s_@@_stop
1966 }
1967 \cs_new:Npn \@@_tail_auxi:w #1 X #2 \s_@@_stop { \fi: #1 }
1968 \cs_new:Npn \str_tail_ignore_spaces:n #1
1969 {
1970 \exp_after:wN \@@_tail_auxii:w
1971 \tl_to_str:n {#1} \s_@@_mark \s_@@_mark \s_@@_stop
1972 }
1973 \cs_new:Npn \@@_tail_auxii:w #1 #2 \s_@@_mark #3 \s_@@_stop { #2 }
1974 % \end{macrocode}
1975 % \end{macro}
1976 % \end{macro}
1977 %
1978 % \subsection{String manipulation}
1979 %
1980 % \begin{macro}[EXP]
1981 % {
1982 % \str_casefold:n, \str_casefold:V,
1983 % \str_lowercase:n, \str_lowercase:f,
1984 % \str_uppercase:n, \str_uppercase:f
1985 % }
1986 % \begin{macro}[EXP]{\@@_change_case:nn}
1987 % \begin{macro}[EXP]{\@@_change_case_aux:nn}
1988 % \begin{macro}[EXP]{\@@_change_case_result:n}
1989 % \begin{macro}[EXP]{\@@_change_case_output:nw, \@@_change_case_output:fw}
1990 % \begin{macro}[EXP]{\@@_change_case_end:nw}
1991 % \begin{macro}[EXP]{\@@_change_case_loop:nw}
1992 % \begin{macro}[EXP]{\@@_change_case_space:n}
1993 % \begin{macro}[EXP]
1994 % {\@@_change_case_char:nN, \@@_change_case_char_auxi:nN, \@@_change_case_char_auxii:nN}
1995 % \begin{macro}[EXP]{\@@_change_case_codepoint:nN}
1996 % \begin{macro}[EXP]{\@@_change_case_codepoint:nNN}
1997 % \begin{macro}[EXP]{\@@_change_case_codepoint:nNNN}
1998 % \begin{macro}[EXP]{\@@_change_case_codepoint:nNNNN}
1999 % \begin{macro}[EXP]{\@@_change_case_char:nnn, \@@_change_case_char_aux:nnn}
2000 % \begin{macro}[EXP]{\@@_change_case_char:nnnnn}
2001 % Case changing for programmatic reasons is done by first detokenizing
2002 % input then doing a simple loop that only has to worry about spaces
2003 % and everything else. The output is detokenized to allow data sharing
2004 % with text-based case changing. Similarly, for $8$-bit engines the
2005 % multi-byte information is shared.
2006 % \begin{macrocode}
2007 \cs_new:Npn \str_casefold:n #1 { \@@_change_case:nn {#1} { casefold } }
2008 \cs_new:Npn \str_lowercase:n #1 { \@@_change_case:nn {#1} { lowercase } }
2009 \cs_new:Npn \str_uppercase:n #1 { \@@_change_case:nn {#1} { uppercase } }
2010 \cs_generate_variant:Nn \str_casefold:n { V }
2011 \cs_generate_variant:Nn \str_lowercase:n { f }
2012 \cs_generate_variant:Nn \str_uppercase:n { f }
2013 \cs_new:Npn \@@_change_case:nn #1
2014 {
2015 \exp_after:wN \@@_change_case_aux:nn \exp_after:wN
2016 { \tl_to_str:n {#1} }
2017 }
2018 \cs_new:Npn \@@_change_case_aux:nn #1#2
2019 {
2020 \@@_change_case_loop:nw {#2} #1 \q_@@_recursion_tail \q_@@_recursion_stop
2021 \@@_change_case_result:n { }
2022 }
2023 \cs_new:Npn \@@_change_case_output:nw #1#2 \@@_change_case_result:n #3
2024 { #2 \@@_change_case_result:n { #3 #1 } }
2025 \cs_generate_variant:Nn \@@_change_case_output:nw { f }
2026 \cs_new:Npn \@@_change_case_end:wn #1 \@@_change_case_result:n #2
2027 { \tl_to_str:n {#2} }
2028 \cs_new:Npn \@@_change_case_loop:nw #1#2 \q_@@_recursion_stop
2029 {
2030 \tl_if_head_is_space:nTF {#2}
2031 { \@@_change_case_space:n }
2032 { \@@_change_case_char:nN }
2033 {#1} #2 \q_@@_recursion_stop
2034 }
2035 \exp_last_unbraced:NNNNo
2036 \cs_new:Npn \@@_change_case_space:n #1 \c_space_tl
2037 {
2038 \@@_change_case_output:nw { ~ }
2039 \@@_change_case_loop:nw {#1}
2040 }
2041 \cs_new:Npn \@@_change_case_char:nN #1#2
2042 {
2043 \@@_if_recursion_tail_stop_do:Nn #2
2044 { \@@_change_case_end:wn }
2045 \@@_change_case_codepoint:nN {#1} #2
2046 }
2047 \if_int_compare:w 0
2048 \cs_if_exist:NT \tex_XeTeXversion:D { 1 }
2049 \cs_if_exist:NT \tex_luatexversion:D { 1 }
2050 > 0 \exp_stop_f:
2051 \cs_new:Npn \@@_change_case_codepoint:nN #1#2
2052 { \@@_change_case_char:fnn { \int_eval:n {`#2} } {#1} {#2} }
2053 \else:
2054 \cs_new:Npe \@@_change_case_codepoint:nN #1#2
2055 {
2056 \exp_not:N \int_compare:nNnTF {`#2} > { "80 }
2057 {
2058 \cs_if_exist:NTF \tex_pdftexversion:D
2059 { \exp_not:N \@@_change_case_char_auxi:nN }
2060 {
2061 \exp_not:N \int_compare:nNnTF {`#2} > { "FF }
2062 { \exp_not:N \@@_change_case_char_auxii:nN }
2063 { \exp_not:N \@@_change_case_char_auxi:nN }
2064 }
2065 }
2066 { \exp_not:N \@@_change_case_char_auxii:nN }
2067 {#1} #2
2068 }
2069 \cs_new:Npn \@@_change_case_char_auxi:nN #1#2
2070 {
2071 \int_compare:nNnTF {`#2} < { "E0 }
2072 { \@@_change_case_codepoint:nNN }
2073 {
2074 \int_compare:nNnTF {`#2} < { "F0 }
2075 { \@@_change_case_codepoint:nNNN }
2076 { \@@_change_case_codepoint:nNNNNN }
2077 }
2078 {#1} #2
2079 }
2080 \cs_new:Npn \@@_change_case_char_auxii:nN #1#2
2081 { \@@_change_case_char:fnn { \int_eval:n {`#2} } {#1} {#2} }
2082 \cs_new:Npn \@@_change_case_codepoint:nNN #1#2#3
2083 {
2084 \@@_change_case_char:fnn
2085 { \int_eval:n { (`#2 - "C0) * "40 + `#3 - "80 } }
2086 {#1} {#2#3}
2087 }
2088 \cs_new:Npn \@@_change_case_codepoint:nNNN #1#2#3#4
2089 {
2090 \@@_change_case_char:fnn
2091 {
2092 \int_eval:n
2093 { (`#2 - "E0) * "1000 + (`#3 - "80) * "40 + `#4 - "80 }
2094 }
2095 {#1} {#2#3#4}
2096 }
2097 \cs_new:Npn \@@_change_case_codepoint:nNNNN #1#2#3#4#5
2098 {
2099 \@@_change_case_char:fnn
2100 {
2101 \int_eval:n
2102 {
2103 (`#2 - "F0) * "40000
2104 + (`#3 - "80) * "1000
2105 + (`#4 - "80) * "40
2106 + `#5 - "80
2107 }
2108 }
2109 {#1} {#2#3#4#5}
2110 }
2111 \fi:
2112 \cs_new:Npn \@@_change_case_char:nnn #1#2#3
2113 {
2114 \@@_change_case_output:fw
2115 {
2116 \exp_args:Ne \@@_change_case_char_aux:nnn
2117 { \__kernel_codepoint_case:nn {#2} {#1} } {#1} {#3}
2118 }
2119 \@@_change_case_loop:nw {#2}
2120 }
2121 \cs_generate_variant:Nn \@@_change_case_char:nnn { f }
2122 \cs_new:Npn \@@_change_case_char_aux:nnn #1#2#3
2123 {
2124 \use:e { \@@_change_case_char:nnnnn #1 {#2} {#3} }
2125 }
2126 \cs_new:Npn \@@_change_case_char:nnnnn #1#2#3#4#5
2127 {
2128 \int_compare:nNnTF {#1} = {#4}
2129 { \tl_to_str:n {#5} }
2130 {
2131 \codepoint_str_generate:n {#1}
2132 \tl_if_blank:nF {#2}
2133 {
2134 \codepoint_str_generate:n {#2}
2135 \tl_if_blank:nF {#3}
2136 { \codepoint_str_generate:n {#3} }
2137 }
2138 }
2139 }
2140 % \end{macrocode}
2141 % \end{macro}
2142 % \end{macro}
2143 % \end{macro}
2144 % \end{macro}
2145 % \end{macro}
2146 % \end{macro}
2147 % \end{macro}
2148 % \end{macro}
2149 % \end{macro}
2150 % \end{macro}
2151 % \end{macro}
2152 % \end{macro}
2153 % \end{macro}
2154 % \end{macro}
2155 % \end{macro}
2156 %
2157 % \begin{macro}[EXP]
2158 % {
2159 % \str_mdfive_hash:n,
2160 % \str_mdfive_hash:V,
2161 % \str_mdfive_hash:v,
2162 % \str_mdfive_hash:o,
2163 % \str_mdfive_hash:e
2164 % }
2165 % \begin{macrocode}
2166 \cs_new:Npn \str_mdfive_hash:n #1 { \tex_mdfivesum:D { \tl_to_str:n {#1} } }
2167 \cs_generate_variant:Nn \str_mdfive_hash:n { V , v , o }
2168 \cs_new:Npn \str_mdfive_hash:e #1 { \tex_mdfivesum:D {#1} }
2169 % \end{macrocode}
2170 % \end{macro}
2171 %
2172 % \begin{variable}
2173 % {
2174 % \c_ampersand_str,
2175 % \c_atsign_str,
2176 % \c_backslash_str,
2177 % \c_left_brace_str,
2178 % \c_right_brace_str,
2179 % \c_circumflex_str,
2180 % \c_colon_str,
2181 % \c_dollar_str,
2182 % \c_hash_str,
2183 % \c_percent_str,
2184 % \c_tilde_str,
2185 % \c_underscore_str,
2186 % \c_zero_str
2187 % }
2188 % For all of those strings, use \cs{cs_to_str:N} to get characters with
2189 % the correct category code without worries
2190 % \begin{macrocode}
2191 \str_const:Ne \c_ampersand_str { \cs_to_str:N \& }
2192 \str_const:Ne \c_atsign_str { \cs_to_str:N \@ }
2193 \str_const:Ne \c_backslash_str { \cs_to_str:N \\ }
2194 \str_const:Ne \c_left_brace_str { \cs_to_str:N \{ }
2195 \str_const:Ne \c_right_brace_str { \cs_to_str:N \} }
2196 \str_const:Ne \c_circumflex_str { \cs_to_str:N \^ }
2197 \str_const:Ne \c_colon_str { \cs_to_str:N \: }
2198 \str_const:Ne \c_dollar_str { \cs_to_str:N \$ }
2199 \str_const:Ne \c_hash_str { \cs_to_str:N \# }
2200 \str_const:Ne \c_percent_str { \cs_to_str:N \% }
2201 \str_const:Ne \c_tilde_str { \cs_to_str:N \~ }
2202 \str_const:Ne \c_underscore_str { \cs_to_str:N \_ }
2203 \str_const:Ne \c_zero_str { 0 }
2204 % \end{macrocode}
2205 % \end{variable}
2206 %
2207 % \begin{variable}{\c_empty_str}
2208 % An empty string is simply an empty token list.
2209 % \begin{macrocode}
2210 \cs_new_eq:NN \c_empty_str \c_empty_tl
2211 % \end{macrocode}
2212 % \end{variable}
2213 %
2214 % \begin{variable}{\l_tmpa_str, \l_tmpb_str, \g_tmpa_str, \g_tmpb_str}
2215 % Scratch strings.
2216 % \begin{macrocode}
2217 \str_new:N \l_tmpa_str
2218 \str_new:N \l_tmpb_str
2219 \str_new:N \g_tmpa_str
2220 \str_new:N \g_tmpb_str
2221 % \end{macrocode}
2222 % \end{variable}
2223 %
2224 % \subsection{Viewing strings}
2225 %
2226 % \begin{macro}{\str_show:n, \str_show:N, \str_show:c}
2227 % \begin{macro}{\str_log:n, \str_log:N, \str_log:c}
2228 % Displays a string on the terminal.
2229 % \begin{macrocode}
2230 \cs_new_eq:NN \str_show:n \tl_show:n
2231 \cs_new_protected:Npn \str_show:N #1
2232 {
2233 \__kernel_chk_tl_type:NnnT #1 { str } { \tl_to_str:N #1 }
2234 { \tl_show:N #1 }
2235 }
2236 \cs_generate_variant:Nn \str_show:N { c }
2237 \cs_new_eq:NN \str_log:n \tl_log:n
2238 \cs_new_protected:Npn \str_log:N #1
2239 {
2240 \__kernel_chk_tl_type:NnnT #1 { str } { \tl_to_str:N #1 }
2241 { \tl_log:N #1 }
2242 }
2243 \cs_generate_variant:Nn \str_log:N { c }
2244 % \end{macrocode}
2245 % \end{macro}
2246 % \end{macro}
2247 %
2248 % \subsection{Held-over functions}
2249 % \begin{macrocode}
2250 %<@@=cs>
2251 % \end{macrocode}
2252 % \begin{macro}{\@@_generate_variant_chk:nnTF}
2253 % From the \pkg{l3expan} module.
2254 % \begin{macrocode}
2255 \cs_gset_eq:NN \@@_generate_variant_chk:nnTF \str_if_eq:nnTF
2256 % \end{macrocode}
2257 % \end{macro}
2258 %
2259 % \begin{macrocode}
2260 %</code>
2261 % \end{macrocode}
2262 %
2263 % \end{implementation}
2264 %
2265 % \PrintIndex