Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3int.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{l3int} module\\ Integers^^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 % Calculation and comparison of integer values can be carried out
53 % using literal numbers, \texttt{int} registers, constants and
54 % integers stored in token list variables. The standard operators
55 % \texttt{+}, \texttt{-}, \texttt{/} and \texttt{*} and
56 % parentheses can be used within such expressions to carry
57 % arithmetic operations. This module carries out these functions
58 % on \emph{integer expressions} (\enquote{\meta{int expr}}).
59 %
60 % \section{Integer expressions}
61 %
62 % Throughout this module, (almost) all \texttt{n}-type argument allow
63 % for an \meta{intexpr} argument with the following syntax. The
64 % \meta{integer expression} should consist,
65 % after expansion, of \texttt{+}, \texttt{-}, \texttt{*}, \texttt{/},
66 % \texttt{(}, \texttt{)} and of course integer operands. The result
67 % is calculated by applying standard mathematical rules with the
68 % following peculiarities:
69 % \begin{itemize}
70 % \item \texttt{/} denotes division rounded to the closest integer with
71 % ties rounded away from zero;
72 % \item there is an error and the overall expression evaluates to zero
73 % whenever the absolute value of any intermediate result exceeds
74 % $2^{31}-1$, except in the case of scaling operations
75 % $a$\texttt{*}$b$\texttt{/}$c$, for which $a$\texttt{*}$b$ may be
76 % arbitrarily large (but the operands $a$, $b$, $c$ are still
77 % constrained to an absolute value at most $2^{31}-1$);
78 % \item parentheses may not appear after unary \texttt{+} or
79 % \texttt{-}, namely placing \texttt{+(} or \texttt{-(} at the start
80 % of an expression or after \texttt{+}, \texttt{-}, \texttt{*},
81 % \texttt{/} or~\texttt{(} leads to an error.
82 % \end{itemize}
83 % Each integer operand can be either an integer variable (with no need
84 % for \cs{int_use:N}) or an integer denotation. For example both
85 % \begin{verbatim}
86 % \int_show:n { 5 + 4 * 3 - ( 3 + 4 * 5 ) }
87 % \end{verbatim}
88 % and
89 % \begin{verbatim}
90 % \tl_new:N \l_my_tl
91 % \tl_set:Nn \l_my_tl { 5 }
92 % \int_new:N \l_my_int
93 % \int_set:Nn \l_my_int { 4 }
94 % \int_show:n { \l_my_tl + \l_my_int * 3 - ( 3 + 4 * 5 ) }
95 % \end{verbatim}
96 % show the same result $-6$ because \cs[no-index]{l_my_tl} expands to
97 % the integer denotation~|5| while the integer variable \cs[no-index]{l_my_int}
98 % takes the value~$4$. As the \meta{integer expression} is fully
99 % expanded from left to right during evaluation, fully expandable and
100 % restricted-expandable functions can both be used, and \cs{exp_not:n}
101 % and its variants have no effect while \cs{exp_not:N} may incorrectly
102 % interrupt the expression.
103 %
104 % \begin{function}[EXP]{\int_eval:n}
105 % \begin{syntax}
106 % \cs{int_eval:n} \Arg{int expr}
107 % \end{syntax}
108 % Evaluates the \meta{int expr} and leaves the result in the
109 % input stream as an integer denotation: for positive results an
110 % explicit sequence of decimal digits not starting with~\texttt{0},
111 % for negative results \texttt{-}~followed by such a sequence, and
112 % \texttt{0}~for zero.
113 % \begin{texnote}
114 % Exactly two expansions are needed to evaluate \cs{int_eval:n}.
115 % The result is \emph{not} an \meta{internal integer}, and therefore
116 % requires suitable termination if used in a \TeX{}-style integer
117 % assignment.
118 %
119 % As all \TeX{} integers, integer operands can also be dimension or
120 % skip variables, converted to integers in~\texttt{sp}, or octal
121 % numbers given as \texttt{'} followed by digits other than
122 % \texttt{8} and \texttt{9}, or hexadecimal numbers given as
123 % |"| followed by digits or upper case letters from
124 % \texttt{A} to~\texttt{F}, or the character code of some character
125 % or one-character control sequence, given as \texttt{`}\meta{char}.
126 % \end{texnote}
127 % \end{function}
128 %
129 % \begin{function}[EXP, added = 2018-03-30]{\int_eval:w}
130 % \begin{syntax}
131 % \cs{int_eval:w} \meta{int expr}
132 % \end{syntax}
133 % Evaluates the \meta{int expr} as described for
134 % \cs{int_eval:n}. The end of the expression is the first token
135 % encountered that cannot form part of such an expression. If that
136 % token is \cs{scan_stop:} it is removed, otherwise not. Spaces do
137 % \emph{not} terminate the expression. However, spaces terminate
138 % explicit integers, and this may terminate the expression: for
139 % instance, \cs{int_eval:w} \verb*|1 + 1 9| (with explicit space
140 % tokens inserted using |~| in a code setting) expands to \texttt{29}
141 % since the digit~\texttt{9} is not part of the expression. Expansion
142 % details, etc., are as given for \cs{int_eval:n}.
143 % \end{function}
144 %
145 % \begin{function}[EXP, added = 2018-11-03]{\int_sign:n}
146 % \begin{syntax}
147 % \cs{int_sign:n} \Arg{int expr}
148 % \end{syntax}
149 % Evaluates the \meta{int expr} then leaves $1$ or $0$ or
150 % $-1$ in the input stream according to the sign of the result.
151 % \end{function}
152 %
153 % \begin{function}[EXP, updated = 2012-09-26]{\int_abs:n}
154 % \begin{syntax}
155 % \cs{int_abs:n} \Arg{int expr}
156 % \end{syntax}
157 % Evaluates the \meta{int expr} as described for
158 % \cs{int_eval:n} and leaves the absolute value of the result in
159 % the input stream as an \meta{integer denotation} after two
160 % expansions.
161 % \end{function}
162 %
163 % \begin{function}[EXP, updated = 2012-09-26]{\int_div_round:nn}
164 % \begin{syntax}
165 % \cs{int_div_round:nn} \Arg{int expr_1} \Arg{int expr_2}
166 % \end{syntax}
167 % Evaluates the two \meta{int expr}s as described earlier,
168 % then divides the first value by the second, and rounds the result
169 % to the closest integer. Ties are rounded away from zero.
170 % Note that this is identical to using
171 % |/| directly in an \meta{int expr}. The result is left in
172 % the input stream as an \meta{integer denotation} after two expansions.
173 % \end{function}
174 %
175 % \begin{function}[EXP, updated = 2012-02-09]{\int_div_truncate:nn}
176 % \begin{syntax}
177 % \cs{int_div_truncate:nn} \Arg{int expr_1} \Arg{int expr_2}
178 % \end{syntax}
179 % Evaluates the two \meta{int expr}s as described earlier,
180 % then divides the first value by the second, and rounds the result
181 % towards zero. Note that division using |/|
182 % rounds to the closest integer instead.
183 % The result is left in the input stream as an
184 % \meta{integer denotation} after two expansions.
185 % \end{function}
186 %
187 % \begin{function}[EXP, updated = 2012-09-26]{\int_max:nn, \int_min:nn}
188 % \begin{syntax}
189 % \cs{int_max:nn} \Arg{int expr_1} \Arg{int expr_2}
190 % \cs{int_min:nn} \Arg{int expr_1} \Arg{int expr_2}
191 % \end{syntax}
192 % Evaluates the \meta{int expr}s as described for
193 % \cs{int_eval:n} and leaves either the larger or smaller value
194 % in the input stream as an \meta{integer denotation} after two
195 % expansions.
196 % \end{function}
197 %
198 % \begin{function}[EXP, updated = 2012-09-26]{\int_mod:nn}
199 % \begin{syntax}
200 % \cs{int_mod:nn} \Arg{int expr_1} \Arg{int expr_2}
201 % \end{syntax}
202 % Evaluates the two \meta{int expr}s as described earlier,
203 % then calculates the integer remainder of dividing the first
204 % expression by the second. This is obtained by subtracting
205 % \cs{int_div_truncate:nn} \Arg{int expr_1} \Arg{int expr_2} times
206 % \meta{int expr_2} from \meta{int expr_1}. Thus, the result has the
207 % same sign as \meta{int expr_1} and its absolute value is strictly
208 % less than that of \meta{int expr_2}. The result is left in the input
209 % stream as an \meta{integer denotation} after two expansions.
210 % \end{function}
211 %
212 % \section{Creating and initializing integers}
213 %
214 % \begin{function}{\int_new:N, \int_new:c}
215 % \begin{syntax}
216 % \cs{int_new:N} \meta{integer}
217 % \end{syntax}
218 % Creates a new \meta{integer} or raises an error if the name is
219 % already taken. The declaration is global. The \meta{integer} is
220 % initially equal to $0$.
221 % \end{function}
222 %
223 % \begin{function}[updated = 2011-10-22]{\int_const:Nn, \int_const:cn}
224 % \begin{syntax}
225 % \cs{int_const:Nn} \meta{integer} \Arg{int expr}
226 % \end{syntax}
227 % Creates a new constant \meta{integer} or raises an error if the name
228 % is already taken. The value of the \meta{integer} is set
229 % globally to the \meta{int expr}.
230 % \end{function}
231 %
232 % \begin{function}{\int_zero:N, \int_zero:c, \int_gzero:N, \int_gzero:c}
233 % \begin{syntax}
234 % \cs{int_zero:N} \meta{integer}
235 % \end{syntax}
236 % Sets \meta{integer} to $0$.
237 % \end{function}
238 %
239 % \begin{function}[added = 2011-12-13]
240 % {\int_zero_new:N, \int_zero_new:c, \int_gzero_new:N, \int_gzero_new:c}
241 % \begin{syntax}
242 % \cs{int_zero_new:N} \meta{integer}
243 % \end{syntax}
244 % Ensures that the \meta{integer} exists globally by applying
245 % \cs{int_new:N} if necessary, then applies
246 % \cs[index=int_zero:N]{int_(g)zero:N} to leave
247 % the \meta{integer} set to zero.
248 % \end{function}
249 %
250 % \begin{function}
251 % {
252 % \int_set_eq:NN, \int_set_eq:cN, \int_set_eq:Nc, \int_set_eq:cc,
253 % \int_gset_eq:NN, \int_gset_eq:cN, \int_gset_eq:Nc, \int_gset_eq:cc
254 % }
255 % \begin{syntax}
256 % \cs{int_set_eq:NN} \meta{integer_1} \meta{integer_2}
257 % \end{syntax}
258 % Sets the content of \meta{integer_1} equal to that of
259 % \meta{integer_2}.
260 % \end{function}
261 %
262 % \begin{function}[EXP, pTF, added=2012-03-03]
263 % {\int_if_exist:N, \int_if_exist:c}
264 % \begin{syntax}
265 % \cs{int_if_exist_p:N} \meta{integer}
266 % \cs{int_if_exist:NTF} \meta{integer} \Arg{true code} \Arg{false code}
267 % \end{syntax}
268 % Tests whether the \meta{integer} is currently defined. This does not
269 % check that the \meta{integer} really is an integer variable.
270 % \end{function}
271 %
272 % \section{Setting and incrementing integers}
273 %
274 % \begin{function}[updated = 2011-10-22]
275 % {\int_add:Nn, \int_add:cn, \int_gadd:Nn, \int_gadd:cn}
276 % \begin{syntax}
277 % \cs{int_add:Nn} \meta{integer} \Arg{int expr}
278 % \end{syntax}
279 % Adds the result of the \meta{int expr} to the current
280 % content of the \meta{integer}.
281 % \end{function}
282 %
283 % \begin{function}{\int_decr:N, \int_decr:c, \int_gdecr:N, \int_gdecr:c}
284 % \begin{syntax}
285 % \cs{int_decr:N} \meta{integer}
286 % \end{syntax}
287 % Decreases the value stored in \meta{integer} by $1$.
288 % \end{function}
289 %
290 % \begin{function}{\int_incr:N, \int_incr:c, \int_gincr:N, \int_gincr:c}
291 % \begin{syntax}
292 % \cs{int_incr:N} \meta{integer}
293 % \end{syntax}
294 % Increases the value stored in \meta{integer} by $1$.
295 % \end{function}
296 %
297 % \begin{function}[updated = 2011-10-22]
298 % {
299 % \int_set:Nn, \int_set:cn, \int_set:NV, \int_set:cV,
300 % \int_gset:Nn, \int_gset:cn, \int_gset:NV, \int_gset:cV
301 % }
302 % \begin{syntax}
303 % \cs{int_set:Nn} \meta{integer} \Arg{int expr}
304 % \end{syntax}
305 % Sets \meta{integer} to the value of \meta{int expr},
306 % which must evaluate to an integer (as described for
307 % \cs{int_eval:n}).
308 % \end{function}
309 %
310 % \begin{function}[added = 2024-12-08]
311 % {
312 % \int_set_regex_count:Nnn, \int_set_regex_count:cnn,
313 % \int_set_regex_count:NNn, \int_set_regex_count:cNn,
314 % \int_gset_regex_count:Nnn, \int_gset_regex_count:cnn,
315 % \int_gset_regex_count:NNn, \int_gset_regex_count:cNn,
316 % }
317 % \begin{syntax}
318 % \cs{int_set_regex_count:Nnn} \meta{integer} \Arg{regex} \Arg{token list}
319 % \cs{int_set_regex_count:NNn} \meta{integer} \meta{regex~var} \Arg{token list}
320 % \end{syntax}
321 % Sets \meta{integer} equal to the number of times
322 % \meta{regular expression} appears in \meta{token list}.
323 % The search starts by finding the left-most longest match,
324 % respecting greedy and lazy (non-greedy) operators. Then the search
325 % starts again from the character following the last character
326 % of the previous match, until reaching the end of the token list.
327 % Infinite loops are prevented in the case where the regular expression
328 % can match an empty token list: then we count one match between each
329 % pair of characters.
330 % For instance,
331 % \begin{verbatim}
332 % \int_set_regex_count:Nnn \l_foo_int { (b+|c) } { abbababcbb }
333 % \end{verbatim}
334 % results in \cs[no-index]{l_foo_int} taking the value $5$.
335 % Theses are alternative names for \cs{regex_count:nnN} and friends,
336 % with arguments re-ordered for \meta{integer} setting;
337 % see \pkg{l3regex} chapter for more details of the \meta{regex}
338 % format.
339 % \end{function}
340 %
341 % \begin{function}[updated = 2011-10-22]
342 % {\int_sub:Nn, \int_sub:cn, \int_gsub:Nn, \int_gsub:cn}
343 % \begin{syntax}
344 % \cs{int_sub:Nn} \meta{integer} \Arg{int expr}
345 % \end{syntax}
346 % Subtracts the result of the \meta{int expr} from the
347 % current content of the \meta{integer}.
348 % \end{function}
349 %
350 % \section{Using integers}
351 %
352 % \begin{function}[updated = 2011-10-22, EXP]{\int_use:N, \int_use:c}
353 % \begin{syntax}
354 % \cs{int_use:N} \meta{integer}
355 % \end{syntax}
356 % Recovers the content of an \meta{integer} and places it directly
357 % in the input stream. An error is raised if the variable does
358 % not exist or if it is invalid. Can be omitted in places where an
359 % \meta{integer} is required (such as in the first and third arguments
360 % of \cs{int_compare:nNnTF}).
361 % \begin{texnote}
362 % \cs{int_use:N} is the \TeX{} primitive \tn{the}: this is one of
363 % several \LaTeX3 names for this primitive.
364 % \end{texnote}
365 % \end{function}
366 %
367 % \section{Integer expression conditionals}
368 %
369 % \begin{function}[EXP,pTF]
370 % {
371 % \int_compare:nNn,
372 % \int_compare:vNn, \int_compare:nNv, \int_compare:vNv
373 % }
374 % \begin{syntax}
375 % \cs{int_compare_p:nNn} \Arg{int expr_1} \meta{relation} \Arg{int expr_2} \\
376 % \cs{int_compare:nNnTF}
377 % ~~\Arg{int expr_1} \meta{relation} \Arg{int expr_2}
378 % ~~\Arg{true code} \Arg{false code}
379 % \end{syntax}
380 % This function first evaluates each of the \meta{int expr}s
381 % as described for \cs{int_eval:n}. The two results are then
382 % compared using the \meta{relation}:
383 % \begin{center}
384 % \begin{tabular}{ll}
385 % Equal & |=| \\
386 % Greater than & |>| \\
387 % Less than & |<| \\
388 % \end{tabular}
389 % \end{center}
390 % This function is less flexible than \cs{int_compare:nTF} but around
391 % $5$~times faster.
392 %
393 % Variables may be used directly in the \meta{int exprs} here: as a result,
394 % there is no need for |V|- (or |e|-) type variants. The |v|-type
395 % variants are provided for convenience.
396 % \end{function}
397 %
398 % \begin{function}[updated = 2013-01-13, EXP, pTF]{\int_compare:n}
399 % \begin{syntax}
400 % \cs{int_compare_p:n} \\
401 % ~~\{ \\
402 % ~~~~\meta{int expr_1} \meta{relation_1} \\
403 % ~~~~\ldots{} \\
404 % ~~~~\meta{int expr_N} \meta{relation_N} \\
405 % ~~~~\meta{int expr_{N+1}} \\
406 % ~~\} \\
407 % \cs{int_compare:nTF}
408 % ~~\{ \\
409 % ~~~~\meta{int expr_1} \meta{relation_1} \\
410 % ~~~~\ldots{} \\
411 % ~~~~\meta{int expr_N} \meta{relation_N} \\
412 % ~~~~\meta{int expr_{N+1}} \\
413 % ~~\} \\
414 % ~~\Arg{true code} \Arg{false code}
415 % \end{syntax}
416 % This function evaluates the \meta{int expr}s as described
417 % for \cs{int_eval:n} and compares consecutive result using the
418 % corresponding \meta{relation}, namely it compares \meta{int expr_1}
419 % and \meta{int expr_2} using the \meta{relation_1}, then
420 % \meta{int expr_2} and \meta{int expr_3} using the \meta{relation_2},
421 % until finally comparing \meta{int expr_N} and \meta{int expr_{N+1}}
422 % using the \meta{relation_N}. The test yields \texttt{true} if all
423 % comparisons are \texttt{true}. Each \meta{int expr} is
424 % evaluated only once, and the evaluation is lazy, in the sense that
425 % if one comparison is \texttt{false}, then no other \meta{integer
426 % expression} is evaluated and no other comparison is performed.
427 % The \meta{relations} can be any of the following:
428 % \begin{center}
429 % \begin{tabular}{ll}
430 % Equal & |=| or |==| \\
431 % Greater than or equal to & |>=| \\
432 % Greater than & |>| \\
433 % Less than or equal to & |<=| \\
434 % Less than & |<| \\
435 % Not equal & |!=| \\
436 % \end{tabular}
437 % \end{center}
438 % This function is more flexible than \cs{int_compare:nNnTF} but
439 % around $5$~times slower.
440 % \end{function}
441 %
442 % \begin{function}[added = 2013-07-24, EXP, noTF]{\int_case:nn}
443 % \begin{syntax}
444 % \cs{int_case:nnTF} \Arg{test int expr} \\
445 % ~~|{| \\
446 % ~~~~\Arg{int expr case_1} \Arg{code case_1} \\
447 % ~~~~\Arg{int expr case_2} \Arg{code case_2} \\
448 % ~~~~\ldots \\
449 % ~~~~\Arg{int expr case_n} \Arg{code case_n} \\
450 % ~~|}| \\
451 % ~~\Arg{true code}
452 % ~~\Arg{false code}
453 % \end{syntax}
454 % This function evaluates the \meta{test int expr} and
455 % compares this in turn to each of the
456 % \meta{int expr case}s until a match is found.
457 % If the two are equal then the
458 % associated \meta{code} is left in the input stream
459 % and other cases are discarded. If any of the
460 % cases are matched, the \meta{true code} is also inserted into the
461 % input stream (after the code for the appropriate case), while if none
462 % match then the \meta{false code} is inserted. The function
463 % \cs{int_case:nn}, which does nothing if there is no match, is also
464 % available. For example
465 % \begin{verbatim}
466 % \int_case:nnF
467 % { 2 * 5 }
468 % {
469 % { 5 } { Small }
470 % { 4 + 6 } { Medium }
471 % { -2 * 10 } { Negative }
472 % }
473 % { No idea! }
474 % \end{verbatim}
475 % leaves \enquote{\texttt{Medium}} in the input stream.
476 % Since evaluation of the test expressions stops at the first
477 % successful case, the order of possible matches should normally
478 % be that the most likely are earlier: this will reduce the average
479 % steps required to complete expansion.
480 % \end{function}
481 %
482 % \begin{function}[EXP,pTF]{\int_if_even:n, \int_if_odd:n}
483 % \begin{syntax}
484 % \cs{int_if_odd_p:n} \Arg{int expr}
485 % \cs{int_if_odd:nTF} \Arg{int expr}
486 % ~~\Arg{true code} \Arg{false code}
487 % \end{syntax}
488 % This function first evaluates the \meta{int expr}
489 % as described for \cs{int_eval:n}. It then evaluates if this
490 % is odd or even, as appropriate.
491 % \end{function}
492 %
493 % \begin{function}[EXP,pTF, added = 2023-05-17]{\int_if_zero:n}
494 % \begin{syntax}
495 % \cs{int_if_zero_p:n} \Arg{int expr}
496 % \cs{int_if_zero:nTF} \Arg{int expr}
497 % ~~\Arg{true code} \Arg{false code}
498 % \end{syntax}
499 % This function first evaluates the \meta{int expr}
500 % as described for \cs{int_eval:n}. It then evaluates if this
501 % is zero or not.
502 % \end{function}
503 %
504 % \section{Integer expression loops}
505 %
506 % \begin{function}[rEXP]{\int_do_until:nNnn}
507 % \begin{syntax}
508 % \cs{int_do_until:nNnn} \Arg{int expr_1} \meta{relation} \Arg{int expr_2} \Arg{code}
509 % \end{syntax}
510 % Places the \meta{code} in the input stream for \TeX{} to process, and
511 % then evaluates the relationship between the two
512 % \meta{int expr}s as described for \cs{int_compare:nNnTF}.
513 % If the test is \texttt{false} then the \meta{code} is inserted
514 % into the input stream again and a loop occurs until the
515 % \meta{relation} is \texttt{true}.
516 % \end{function}
517 %
518 % \begin{function}[rEXP]{\int_do_while:nNnn}
519 % \begin{syntax}
520 % \cs{int_do_while:nNnn} \Arg{int expr_1} \meta{relation} \Arg{int expr_2} \Arg{code}
521 % \end{syntax}
522 % Places the \meta{code} in the input stream for \TeX{} to process, and
523 % then evaluates the relationship between the two
524 % \meta{int expr}s as described for \cs{int_compare:nNnTF}.
525 % If the test is \texttt{true} then the \meta{code} is inserted
526 % into the input stream again and a loop occurs until the
527 % \meta{relation} is \texttt{false}.
528 % \end{function}
529 %
530 % \begin{function}[rEXP]{\int_until_do:nNnn}
531 % \begin{syntax}
532 % \cs{int_until_do:nNnn} \Arg{int expr_1} \meta{relation} \Arg{int expr_2} \Arg{code}
533 % \end{syntax}
534 % Evaluates the relationship between the two \meta{int expr}s
535 % as described for \cs{int_compare:nNnTF}, and then places the
536 % \meta{code} in the input stream if the \meta{relation} is
537 % \texttt{false}. After the \meta{code} has been processed by \TeX{} the
538 % test is repeated, and a loop occurs until the test is
539 % \texttt{true}.
540 % \end{function}
541 %
542 % \begin{function}[rEXP]{\int_while_do:nNnn}
543 % \begin{syntax}
544 % \cs{int_while_do:nNnn} \Arg{int expr_1} \meta{relation} \Arg{int expr_2} \Arg{code}
545 % \end{syntax}
546 % Evaluates the relationship between the two \meta{int expr}s
547 % as described for \cs{int_compare:nNnTF}, and then places the
548 % \meta{code} in the input stream if the \meta{relation} is
549 % \texttt{true}. After the \meta{code} has been processed by \TeX{} the
550 % test is repeated, and a loop occurs until the test is
551 % \texttt{false}.
552 % \end{function}
553 %
554 % \begin{function}[updated = 2013-01-13, rEXP]{\int_do_until:nn}
555 % \begin{syntax}
556 % \cs{int_do_until:nn} \Arg{integer relation} \Arg{code}
557 % \end{syntax}
558 % Places the \meta{code} in the input stream for \TeX{} to process, and
559 % then evaluates the \meta{integer relation}
560 % as described for \cs{int_compare:nTF}.
561 % If the test is \texttt{false} then the \meta{code} is inserted
562 % into the input stream again and a loop occurs until the
563 % \meta{relation} is \texttt{true}.
564 % \end{function}
565 %
566 % \begin{function}[updated = 2013-01-13, rEXP]{\int_do_while:nn}
567 % \begin{syntax}
568 % \cs{int_do_while:nn} \Arg{integer relation} \Arg{code}
569 % \end{syntax}
570 % Places the \meta{code} in the input stream for \TeX{} to process, and
571 % then evaluates the \meta{integer relation}
572 % as described for \cs{int_compare:nTF}.
573 % If the test is \texttt{true} then the \meta{code} is inserted
574 % into the input stream again and a loop occurs until the
575 % \meta{relation} is \texttt{false}.
576 % \end{function}
577 %
578 % \begin{function}[updated = 2013-01-13, rEXP]{\int_until_do:nn}
579 % \begin{syntax}
580 % \cs{int_until_do:nn} \Arg{integer relation} \Arg{code}
581 % \end{syntax}
582 % Evaluates the \meta{integer relation}
583 % as described for \cs{int_compare:nTF}, and then places the
584 % \meta{code} in the input stream if the \meta{relation} is
585 % \texttt{false}. After the \meta{code} has been processed by \TeX{} the
586 % test is repeated, and a loop occurs until the test is
587 % \texttt{true}.
588 % \end{function}
589 %
590 % \begin{function}[updated = 2013-01-13, rEXP]{\int_while_do:nn}
591 % \begin{syntax}
592 % \cs{int_while_do:nn} \Arg{integer relation} \Arg{code}
593 % \end{syntax}
594 % Evaluates the \meta{integer relation}
595 % as described for \cs{int_compare:nTF}, and then places the
596 % \meta{code} in the input stream if the \meta{relation} is
597 % \texttt{true}. After the \meta{code} has been processed by \TeX{} the
598 % test is repeated, and a loop occurs until the test is
599 % \texttt{false}.
600 % \end{function}
601 %
602 % \section{Integer step functions}
603 %
604 % \begin{function}[added = 2012-06-04, updated = 2018-04-22, rEXP]
605 % {\int_step_function:nN, \int_step_function:nnN, \int_step_function:nnnN}
606 % \begin{syntax}
607 % \cs{int_step_function:nN} \Arg{final value} \meta{function}
608 % \cs{int_step_function:nnN} \Arg{initial value} \Arg{final value} \meta{function}
609 % \cs{int_step_function:nnnN} \Arg{initial value} \Arg{step} \Arg{final value} \meta{function}
610 % \end{syntax}
611 % This function first evaluates the \meta{initial value}, \meta{step}
612 % and \meta{final value}, all of which should be integer expressions.
613 % The \meta{function} is then placed in front of each \meta{value}
614 % from the \meta{initial value} to the \meta{final value} in turn
615 % (using \meta{step} between each \meta{value}). The \meta{step} must
616 % be non-zero. If the \meta{step} is positive, the loop stops when
617 % the \meta{value} becomes larger than the \meta{final value}. If the
618 % \meta{step} is negative, the loop stops when the \meta{value}
619 % becomes smaller than the \meta{final value}. The \meta{function}
620 % should absorb one numerical argument. For example
621 % \begin{verbatim}
622 % \cs_set:Npn \my_func:n #1 { [I~saw~#1] \quad }
623 % \int_step_function:nnnN { 1 } { 1 } { 5 } \my_func:n
624 % \end{verbatim}
625 % would print
626 % \begin{quote}
627 % [I saw 1] \quad
628 % [I saw 2] \quad
629 % [I saw 3] \quad
630 % [I saw 4] \quad
631 % [I saw 5] \quad
632 % \end{quote}
633 %
634 % The functions \cs{int_step_function:nN} and \cs{int_step_function:nnN}
635 % both use a fixed \meta{step} of $1$, and in the case of
636 % \cs{int_step_function:nN} the \meta{initial value} is also fixed as
637 % $1$. These functions are provided as simple short-cuts for code clarity.
638 % \end{function}
639 %
640 % \begin{function}[added = 2025-01-13, rEXP]
641 % {\int_step_tokens:nn, \int_step_tokens:nnn, \int_step_tokens:nnnn}
642 % \begin{syntax}
643 % \cs{int_step_tokens:nn} \Arg{final value} \Arg{code}
644 % \cs{int_step_tokens:nnn} \Arg{initial value} \Arg{final value} \Arg{code}
645 % \cs{int_step_tokens:nnnn} \Arg{initial value} \Arg{step} \Arg{final value} \Arg{code}
646 % \end{syntax}
647 % This function works just like \cs{int_step_function:nnnN} but
648 % instead of mapping a single function to each stepped \meta{value}
649 % between \meta{initial value} and \meta{final value} this maps
650 % the multiple tokens in \meta{code}, so that it gets the current
651 % \meta{value} as a braced argument following it. For instance
652 % \begin{verbatim}
653 % \cs_set:Npn \my_product:nn #1#2
654 % { $#1 \times #2 = \int_eval:n { #1 * #2 }$ \quad }
655 % \int_step_tokens:nnnn { 1 } { 1 } { 4 } { \my_product:nn { 2 } }
656 % \end{verbatim}
657 % would print
658 % \begin{quote}
659 % $2 \times 1 = 2$ \quad
660 % $2 \times 2 = 4$ \quad
661 % $2 \times 3 = 6$ \quad
662 % $2 \times 4 = 8$ \quad
663 % \end{quote}
664 % \end{function}
665 %
666 % \begin{function}[added = 2012-06-04, updated = 2018-04-22]
667 % {\int_step_inline:nn, \int_step_inline:nnn, \int_step_inline:nnnn}
668 % \begin{syntax}
669 % \cs{int_step_inline:nn} \Arg{final value} \Arg{code}
670 % \cs{int_step_inline:nnn} \Arg{initial value} \Arg{final value} \Arg{code}
671 % \cs{int_step_inline:nnnn} \Arg{initial value} \Arg{step} \Arg{final value} \Arg{code}
672 % \end{syntax}
673 % This function first evaluates the \meta{initial value}, \meta{step}
674 % and \meta{final value}, all of which should be integer expressions.
675 % Then for each \meta{value} from the \meta{initial value} to the
676 % \meta{final value} in turn (using \meta{step} between each
677 % \meta{value}), the \meta{code} is inserted into the input stream
678 % with |#1| replaced by the current \meta{value}. Thus the
679 % \meta{code} should define a function of one argument~(|#1|).
680 %
681 % The functions \cs{int_step_inline:nn} and \cs{int_step_inline:nnn}
682 % both use a fixed \meta{step} of $1$, and in the case of
683 % \cs{int_step_inline:nn} the \meta{initial value} is also fixed as
684 % $1$. These functions are provided as simple short-cuts for code clarity.
685 % \end{function}
686 %
687 % \begin{function}[added = 2012-06-04, updated = 2018-04-22]
688 % {\int_step_variable:nNn, \int_step_variable:nnNn, \int_step_variable:nnnNn}
689 % \begin{syntax}
690 % \cs{int_step_variable:nNn} \Arg{final value} \meta{tl~var} \Arg{code}
691 % \cs{int_step_variable:nnNn} \Arg{initial value} \Arg{final value} \meta{tl~var} \Arg{code}
692 % \cs{int_step_variable:nnnNn} \Arg{initial value} \Arg{step} \Arg{final value} \meta{tl~var} \Arg{code}
693 % \end{syntax}
694 % This function first evaluates the \meta{initial value}, \meta{step}
695 % and \meta{final value}, all of which should be integer expressions.
696 % Then for each \meta{value} from the \meta{initial value} to the
697 % \meta{final value} in turn (using \meta{step} between each
698 % \meta{value}), the \meta{code} is inserted into the input stream,
699 % with the \meta{tl~var} defined as the current \meta{value}. Thus
700 % the \meta{code} should make use of the \meta{tl~var}.
701 %
702 % The functions \cs{int_step_variable:nNn} and \cs{int_step_variable:nnNn}
703 % both use a fixed \meta{step} of $1$, and in the case of
704 % \cs{int_step_variable:nNn} the \meta{initial value} is also fixed as
705 % $1$. These functions are provided as simple short-cuts for code clarity.
706 % \end{function}
707 %
708 % \section{Formatting integers}
709 %
710 % Integers can be placed into the output stream with formatting. These
711 % conversions apply to any integer expressions.
712 %
713 % \begin{function}[updated = 2011-10-22, EXP]{\int_to_arabic:n, \int_to_arabic:v}
714 % \begin{syntax}
715 % \cs{int_to_arabic:n} \Arg{int expr}
716 % \end{syntax}
717 % Places the value of the \meta{int expr} in the input
718 % stream as digits, with category code $12$ (other).
719 % \end{function}
720 %
721 % \begin{function}[updated = 2011-09-17, EXP]{\int_to_alph:n, \int_to_Alph:n}
722 % \begin{syntax}
723 % \cs{int_to_alph:n} \Arg{int expr}
724 % \end{syntax}
725 % Evaluates the \meta{int expr} and converts the result
726 % into a series of letters, which are then left in the input stream.
727 % The conversion rule uses the $26$ letters of the English
728 % alphabet, in order, adding letters when necessary to increase the total
729 % possible range of representable numbers. Thus
730 % \begin{verbatim}
731 % \int_to_alph:n { 1 }
732 % \end{verbatim}
733 % places |a| in the input stream,
734 % \begin{verbatim}
735 % \int_to_alph:n { 26 }
736 % \end{verbatim}
737 % is represented as |z| and
738 % \begin{verbatim}
739 % \int_to_alph:n { 27 }
740 % \end{verbatim}
741 % is converted to |aa|. For conversions using other alphabets, use
742 % \cs{int_to_symbols:nnn} to define an alphabet-specific
743 % function. The basic \cs{int_to_alph:n} and \cs{int_to_Alph:n}
744 % functions should not be modified.
745 % The resulting tokens are digits with category code $12$ (other) and
746 % letters with category code $11$ (letter).
747 % \end{function}
748 %
749 % \begin{function}[updated = 2011-09-17, EXP]{\int_to_symbols:nnn}
750 % \begin{syntax}
751 % \cs{int_to_symbols:nnn}
752 % ~~\Arg{int expr} \Arg{total symbols}
753 % ~~\Arg{value to symbol mapping}
754 % \end{syntax}
755 % This is the low-level function for conversion of an
756 % \meta{int expr} into a symbolic form (often
757 % letters). The \meta{total symbols} available should be given
758 % as an integer expression. Values are actually converted to symbols
759 % according to the \meta{value to symbol mapping}. This should be given
760 % as \meta{total symbols} pairs of entries, a number and the
761 % appropriate symbol. Thus the \cs{int_to_alph:n} function is defined
762 % as
763 % \begin{verbatim}
764 % \cs_new:Npn \int_to_alph:n #1
765 % {
766 % \int_to_symbols:nnn {#1} { 26 }
767 % {
768 % { 1 } { a }
769 % { 2 } { b }
770 % ...
771 % { 26 } { z }
772 % }
773 % }
774 % \end{verbatim}
775 % \end{function}
776 %
777 % \begin{function}[added = 2014-02-11, EXP]{\int_to_bin:n}
778 % \begin{syntax}
779 % \cs{int_to_bin:n} \Arg{int expr}
780 % \end{syntax}
781 % Calculates the value of the \meta{int expr} and places
782 % the binary representation of the result in the input stream.
783 % \end{function}
784 %
785 % \begin{function}[added = 2014-02-11, EXP]{\int_to_hex:n, \int_to_Hex:n}
786 % \begin{syntax}
787 % \cs{int_to_hex:n} \Arg{int expr}
788 % \end{syntax}
789 % Calculates the value of the \meta{int expr} and places
790 % the hexadecimal (base~$16$) representation of the result in the
791 % input stream. Letters are used for digits beyond~$9$: lower
792 % case letters for \cs{int_to_hex:n} and upper case ones for
793 % \cs{int_to_Hex:n}.
794 % The resulting tokens are digits with category code $12$ (other) and
795 % letters with category code $11$ (letter).
796 % \end{function}
797 %
798 % \begin{function}[added = 2014-02-11, EXP]{\int_to_oct:n}
799 % \begin{syntax}
800 % \cs{int_to_oct:n} \Arg{int expr}
801 % \end{syntax}
802 % Calculates the value of the \meta{int expr} and places
803 % the octal (base~$8$) representation of the result in the input
804 % stream.
805 % The resulting tokens are digits with category code $12$ (other) and
806 % letters with category code $11$ (letter).
807 % \end{function}
808 %
809 % \begin{function}[updated = 2014-02-11, EXP]
810 % {\int_to_base:nn, \int_to_Base:nn}
811 % \begin{syntax}
812 % \cs{int_to_base:nn} \Arg{int expr} \Arg{base}
813 % \end{syntax}
814 % Calculates the value of the \meta{int expr} and
815 % converts it into the appropriate representation in the \meta{base};
816 % the later may be given as an integer expression. For bases greater
817 % than $10$ the higher \enquote{digits} are represented by
818 % letters from the English alphabet: lower
819 % case letters for \cs{int_to_base:n} and upper case ones for
820 % \cs{int_to_Base:n}.
821 % The maximum \meta{base} value is $36$.
822 % The resulting tokens are digits with category code $12$ (other) and
823 % letters with category code $11$ (letter).
824 % \begin{texnote}
825 % This is a generic version of \cs{int_to_bin:n}, etc.
826 % \end{texnote}
827 % \end{function}
828 %
829 % \begin{function}[updated = 2011-10-22, rEXP]{\int_to_roman:n, \int_to_Roman:n}
830 % \begin{syntax}
831 % \cs{int_to_roman:n} \Arg{int expr}
832 % \end{syntax}
833 % Places the value of the \meta{int expr} in the input
834 % stream as Roman numerals, either lower case (\cs{int_to_roman:n}) or
835 % upper case (\cs{int_to_Roman:n}). If the value is negative or zero,
836 % the output is empty. The Roman numerals are letters with category
837 % code $11$ (letter). The letters used are |mdclxvi|, repeated as
838 % needed: the notation with bars (such as $\bar{\mbox{v}}$ for $5000$)
839 % is \emph{not} used. For instance \cs{int_to_roman:n} |{| 8249 |}|
840 % expands to |mmmmmmmmccxlix|.
841 % \end{function}
842 %
843 % \begin{function}[EXP, added = 2025-06-09]{\int_format:nn}
844 % \begin{syntax}
845 % \cs{int_format:nn} \Arg{int expr} \Arg{format specification}
846 % \end{syntax}
847 % Evaluates the \meta{int expr} and converts the result to a
848 % string according to the \meta{format specification}. The
849 % \meta{precision} argument is not allowed. The \meta{style} can be
850 % |b| for binary output, |d| for decimal output (this is the default),
851 % |o| for octal output, |X| for hexadecimal output (using capital
852 % letters). The details of the \meta{format specification} are described
853 % in Section~\ref{sec:l3str-format:spec}.
854 % \end{function}
855 %
856 % \section{Converting from other formats to integers}
857 %
858 % \begin{function}[updated = 2014-08-25, EXP]{\int_from_alph:n}
859 % \begin{syntax}
860 % \cs{int_from_alph:n} \Arg{letters}
861 % \end{syntax}
862 % Converts the \meta{letters} into the integer (base~$10$)
863 % representation and leaves this in the input stream. The
864 % \meta{letters} are first converted to a string, with no expansion.
865 % Lower and upper case letters from the English alphabet may be used,
866 % with \enquote{a} equal to $1$ through to \enquote{z} equal to $26$.
867 % The function also accepts a leading sign, made of |+| and~|-|. This
868 % is the inverse function of \cs{int_to_alph:n} and
869 % \cs{int_to_Alph:n}.
870 % \end{function}
871 %
872 % \begin{function}[added = 2014-02-11, updated = 2014-08-25, EXP]
873 % {\int_from_bin:n}
874 % \begin{syntax}
875 % \cs{int_from_bin:n} \Arg{binary number}
876 % \end{syntax}
877 % Converts the \meta{binary number} into the integer (base~$10$)
878 % representation and leaves this in the input stream.
879 % The \meta{binary number} is first converted to a string, with no
880 % expansion. The function accepts a leading sign, made of |+|
881 % and~|-|, followed by binary digits. This is the inverse function
882 % of \cs{int_to_bin:n}.
883 % \end{function}
884 %
885 % \begin{function}[added = 2014-02-11, updated = 2014-08-25, EXP]
886 % {\int_from_hex:n}
887 % \begin{syntax}
888 % \cs{int_from_hex:n} \Arg{hexadecimal number}
889 % \end{syntax}
890 % Converts the \meta{hexadecimal number} into the integer (base~$10$)
891 % representation and leaves this in the input stream. Digits greater
892 % than $9$ may be represented in the \meta{hexadecimal number} by
893 % upper or lower case letters. The \meta{hexadecimal number} is first
894 % converted to a string, with no expansion. The function also accepts
895 % a leading sign, made of |+| and~|-|. This is the inverse function
896 % of \cs{int_to_hex:n} and \cs{int_to_Hex:n}.
897 % \end{function}
898 %
899 % \begin{function}[added = 2014-02-11, updated = 2014-08-25, EXP]
900 % {\int_from_oct:n}
901 % \begin{syntax}
902 % \cs{int_from_oct:n} \Arg{octal number}
903 % \end{syntax}
904 % Converts the \meta{octal number} into the integer (base~$10$)
905 % representation and leaves this in the input stream.
906 % The \meta{octal number} is first converted to a string, with no
907 % expansion. The function accepts a leading sign, made of |+|
908 % and~|-|, followed by octal digits. This is the inverse function
909 % of \cs{int_to_oct:n}.
910 % \end{function}
911 %
912 % \begin{function}[updated = 2014-08-25, updated = 2014-08-25, EXP]
913 % {\int_from_roman:n}
914 % \begin{syntax}
915 % \cs{int_from_roman:n} \Arg{roman numeral}
916 % \end{syntax}
917 % Converts the \meta{roman numeral} into the integer (base~$10$)
918 % representation and leaves this in the input stream. The \meta{roman
919 % numeral} is first converted to a string, with no expansion. The
920 % \meta{roman numeral} may be in upper or lower case; if the numeral
921 % contains characters besides |mdclxvi| or |MDCLXVI| then the
922 % resulting value is $-1$. This is the inverse function of
923 % \cs{int_to_roman:n} and \cs{int_to_Roman:n}.
924 % \end{function}
925 %
926 % \begin{function}[updated = 2014-08-25, EXP]{\int_from_base:nn}
927 % \begin{syntax}
928 % \cs{int_from_base:nn} \Arg{number} \Arg{base}
929 % \end{syntax}
930 % Converts the \meta{number} expressed in \meta{base} into the
931 % appropriate value in base $10$. The \meta{number} is first
932 % converted to a string, with no expansion. The \meta{number} should
933 % consist of digits and letters (either lower or upper case), plus
934 % optionally a leading sign. The maximum \meta{base} value is $36$.
935 % This is the inverse function of \cs{int_to_base:nn} and
936 % \cs{int_to_Base:nn}.
937 % \end{function}
938 %
939 % \section{Random integers}
940 %
941 % \begin{function}[EXP, added = 2016-12-06, updated = 2018-04-27]{\int_rand:nn}
942 % \begin{syntax}
943 % \cs{int_rand:nn} \Arg{int expr_1} \Arg{int expr_2}
944 % \end{syntax}
945 % Evaluates the two \meta{int expr}s and produces a
946 % pseudo-random number between the two (with bounds included).
947 % \end{function}
948 %
949 % \begin{function}[EXP, added = 2018-05-05]{\int_rand:n}
950 % \begin{syntax}
951 % \cs{int_rand:n} \Arg{int expr}
952 % \end{syntax}
953 % Evaluates the \meta{int expr} then produces a
954 % pseudo-random number between $1$ and the \meta{int expr} (included).
955 % \end{function}
956 %
957 % \section{Viewing integers}
958 %
959 % \begin{function}{\int_show:N, \int_show:c}
960 % \begin{syntax}
961 % \cs{int_show:N} \meta{integer}
962 % \end{syntax}
963 % Displays the value of the \meta{integer} on the terminal.
964 % \end{function}
965 %
966 % \begin{function}[added = 2011-11-22, updated = 2015-08-07]{\int_show:n}
967 % \begin{syntax}
968 % \cs{int_show:n} \Arg{int expr}
969 % \end{syntax}
970 % Displays the result of evaluating the \meta{int expr}
971 % on the terminal.
972 % \end{function}
973 %
974 % \begin{function}[added = 2014-08-22, updated = 2015-08-03]{\int_log:N, \int_log:c}
975 % \begin{syntax}
976 % \cs{int_log:N} \meta{integer}
977 % \end{syntax}
978 % Writes the value of the \meta{integer} in the log file.
979 % \end{function}
980 %
981 % \begin{function}[added = 2014-08-22, updated = 2015-08-07]{\int_log:n}
982 % \begin{syntax}
983 % \cs{int_log:n} \Arg{int expr}
984 % \end{syntax}
985 % Writes the result of evaluating the \meta{int expr}
986 % in the log file.
987 % \end{function}
988 %
989 % \section{Constant integers}
990 %
991 % \begin{variable}[added = 2018-05-07]{\c_zero_int, \c_one_int}
992 % Integer values used with primitive tests and assignments: their
993 % self-terminating nature makes these more convenient and faster than
994 % literal numbers.
995 % \end{variable}
996 %
997 % \begin{variable}[module = int]{\c_max_int}
998 % The maximum value that can be stored as an integer.
999 % \end{variable}
1000 %
1001 % \begin{variable}[module = int]{\c_max_register_int}
1002 % Maximum number of registers.
1003 % \end{variable}
1004 %
1005 % \begin{variable}[module = int]{\c_max_char_int}
1006 % Maximum character code completely supported by the engine.
1007 % \end{variable}
1008 %
1009 % \begin{variable}[module = int, added = 2026-04-22]{\c_max_intarray_int}
1010 % The maximum value that can be stored in an \texttt{intarray}.
1011 % \begin{texnote}
1012 % Due to the implementation, in \LuaTeX{} larger values could be
1013 % assigned if the programmer does not check values: this is
1014 % \emph{not} supported as part of the API.
1015 % \end{texnote}
1016 % \end{variable}
1017 %
1018 % \section{Scratch integers}
1019 %
1020 % \begin{variable}{\l_tmpa_int, \l_tmpb_int}
1021 % Scratch integer for local assignment. These are never used by
1022 % the kernel code, and so are safe for use with any \LaTeX3-defined
1023 % function. However, they may be overwritten by other non-kernel
1024 % code and so should only be used for short-term storage.
1025 % \end{variable}
1026 %
1027 % \begin{variable}{\g_tmpa_int, \g_tmpb_int}
1028 % Scratch integer for global assignment. These are never used by
1029 % the kernel code, and so are safe for use with any \LaTeX3-defined
1030 % function. However, they may be overwritten by other non-kernel
1031 % code and so should only be used for short-term storage.
1032 % \end{variable}
1033 %
1034 % \section{Direct number expansion}
1035 %
1036 % \begin{function}[EXP, added = 2018-03-27]{\int_value:w}
1037 % \begin{syntax}
1038 % \cs{int_value:w} \meta{integer}
1039 % \cs{int_value:w} \meta{integer denotation} \meta{optional space}
1040 % \end{syntax}
1041 % Expands the following tokens until an \meta{integer} is formed, and
1042 % leaves a normalized form (no leading sign except for negative
1043 % numbers, no leading digit~|0| except for zero) in the input stream
1044 % as category code $12$ (other) characters. The \meta{integer} can
1045 % consist of any number of signs (with intervening spaces) followed
1046 % by
1047 % \begin{itemize}
1048 % \item an integer variable (in fact, any \TeX{} register except
1049 % \tn{toks}) or
1050 % \item explicit digits (or by |'|\meta{octal digits} or |"|\meta{hexadecimal digits} or |`|\meta{character}).
1051 % \end{itemize}
1052 % In this last case expansion stops once a non-digit is found; if that is a
1053 % space it is removed as in \texttt{f}-expansion, and so \cs{exp_stop_f:}
1054 % may be employed as an end marker. Note that protected functions
1055 % \emph{are} expanded by this process.
1056 %
1057 % This function requires exactly one expansion to produce a value, and so
1058 % is suitable for use in cases where a number is required \enquote{directly}.
1059 % In general, \cs{int_eval:n} is the preferred approach to generating
1060 % numbers.
1061 % \begin{texnote}
1062 % This is the \TeX{} primitive \tn{number}.
1063 % \end{texnote}
1064 % \end{function}
1065 %
1066 % \section{Primitive conditionals}
1067 %
1068 % \begin{function}[EXP]{\if_int_compare:w}
1069 % \begin{syntax}
1070 % \cs{if_int_compare:w} \meta{integer_1} \meta{relation} \meta{integer_2}
1071 % ~~\meta{true code}
1072 % \cs{else:}
1073 % ~~\meta{false code}
1074 % \cs{fi:}
1075 % \end{syntax}
1076 % Compare two integers using \meta{relation}, which must be one of
1077 % |=|, |<| or |>| with category code $12$.
1078 % The \cs{else:} branch is optional.
1079 % \begin{texnote}
1080 % This is the \TeX{} primitive \tn{ifnum}.
1081 % \end{texnote}
1082 % \end{function}
1083 %
1084 % \begin{function}[EXP]{\if_case:w, \or:}
1085 % \begin{syntax}
1086 % \cs{if_case:w} \meta{integer} \meta{case_0}
1087 % ~~\cs{or:} \meta{case_1}
1088 % ~~\cs{or:} |...|
1089 % ~~\cs{else:} \meta{default}
1090 % \cs{fi:}
1091 % \end{syntax}
1092 % Selects a case to execute based on the value of the \meta{integer}. The
1093 % first case (\meta{case_0}) is executed if \meta{integer} is $0$, the second
1094 % (\meta{case_1}) if the \meta{integer} is $1$, etc. The
1095 % \meta{integer} may be a literal, a constant or an integer
1096 % expression (\emph{e.g.}~using \cs{int_eval:n}).
1097 % \begin{texnote}
1098 % These are the \TeX{} primitives \tn{ifcase} and \tn{or}.
1099 % \end{texnote}
1100 % \end{function}
1101 %
1102 % \begin{function}[EXP]{\if_int_odd:w}
1103 % \begin{syntax}
1104 % \cs{if_int_odd:w} \meta{tokens} \meta{optional space}
1105 % ~~\meta{true code}
1106 % \cs{else:}
1107 % ~~\meta{true code}
1108 % \cs{fi:}
1109 % \end{syntax}
1110 % Expands \meta{tokens} until a non-numeric token or a space is found, and
1111 % tests whether the resulting \meta{integer} is odd. If so, \meta{true code}
1112 % is executed. The \cs{else:} branch is optional.
1113 % \begin{texnote}
1114 % This is the \TeX{} primitive \tn{ifodd}.
1115 % \end{texnote}
1116 % \end{function}
1117 %
1118 % \end{documentation}
1119 %
1120 % \begin{implementation}
1121 %
1122 % \section{\pkg{l3int} implementation}
1123 %
1124 % \begin{macrocode}
1125 %<*code>
1126 % \end{macrocode}
1127 %
1128 % \begin{macrocode}
1129 %<@@=int>
1130 % \end{macrocode}
1131 %
1132 % \TestFiles{m3int001,m3int002,m3int03}
1133 %
1134 % \begin{variable}{\c_max_register_int}
1135 % Done in \pkg{l3basics}.
1136 % \end{variable}
1137 %
1138 % \begin{macro}{\@@_to_roman:w}
1139 % \begin{macro}{\if_int_compare:w}
1140 % Done in \pkg{l3basics}.
1141 % \end{macro}
1142 % \end{macro}
1143 %
1144 % \begin{macro}{\or:}
1145 % Done in \pkg{l3basics}.
1146 % \end{macro}
1147 %
1148 % \begin{macro}{\int_value:w}
1149 % \begin{macro}{\@@_eval:w}
1150 % \begin{macro}{\@@_eval_end:}
1151 % \begin{macro}{\if_int_odd:w}
1152 % \begin{macro}{\if_case:w}
1153 % Here are the remaining primitives for number comparisons and
1154 % expressions.
1155 % \begin{macrocode}
1156 \cs_new_eq:NN \int_value:w \tex_number:D
1157 \cs_new_eq:NN \@@_eval:w \tex_numexpr:D
1158 \cs_new_eq:NN \@@_eval_end: \tex_relax:D
1159 \cs_new_eq:NN \if_int_odd:w \tex_ifodd:D
1160 \cs_new_eq:NN \if_case:w \tex_ifcase:D
1161 % \end{macrocode}
1162 % \end{macro}
1163 % \end{macro}
1164 % \end{macro}
1165 % \end{macro}
1166 % \end{macro}
1167 %
1168 % \begin{variable}{\s_@@_mark,\s_@@_stop}
1169 % Scan marks used throughout the module.
1170 % \begin{macrocode}
1171 \scan_new:N \s_@@_mark
1172 \scan_new:N \s_@@_stop
1173 % \end{macrocode}
1174 % \end{variable}
1175 %
1176 % \begin{macro}[EXP]{\@@_use_none_delimit_by_s_stop:w}
1177 % Function to gobble until a scan mark.
1178 % \begin{macrocode}
1179 \cs_new:Npn \@@_use_none_delimit_by_s_stop:w #1 \s_@@_stop { }
1180 % \end{macrocode}
1181 % \end{macro}
1182 %
1183 % \begin{variable}{\q_@@_recursion_tail,\q_@@_recursion_stop}
1184 % Quarks for recursion.
1185 % \begin{macrocode}
1186 \quark_new:N \q_@@_recursion_tail
1187 \quark_new:N \q_@@_recursion_stop
1188 % \end{macrocode}
1189 % \end{variable}
1190 %
1191 % \begin{macro}[EXP]{\@@_if_recursion_tail_stop_do:Nn,\@@_if_recursion_tail_stop:N}
1192 % Functions to query quarks.
1193 % \begin{macrocode}
1194 \__kernel_quark_new_test:N \@@_if_recursion_tail_stop_do:Nn
1195 \__kernel_quark_new_test:N \@@_if_recursion_tail_stop:N
1196 % \end{macrocode}
1197 % \end{macro}
1198 %
1199 % \subsection{Integer expressions}
1200 %
1201 % \begin{macro}{\int_eval:n}
1202 % \begin{macro}{\int_eval:w}
1203 % Wrapper for \cs{@@_eval:w}: can be used in an integer expression or
1204 % directly in the input stream. It is very slightly faster to use
1205 % \tn{the} rather than \tn{number} to turn the expression to a number.
1206 % When debugging, we introduce parentheses to catch early termination (see \pkg{l3debug}).
1207 % \begin{macrocode}
1208 \cs_new:Npn \int_eval:n #1
1209 { \tex_the:D \@@_eval:w #1 \@@_eval_end: }
1210 \cs_new:Npn \int_eval:w { \tex_the:D \@@_eval:w }
1211 % \end{macrocode}
1212 % \end{macro}
1213 % \end{macro}
1214 %
1215 % \begin{macro}{\@@_sep:}
1216 % \begin{macrocode}
1217 \cs_new_eq:NN \@@_sep: \__kernel_int_sep:
1218 % \end{macrocode}
1219 % \end{macro}
1220 %
1221 % \begin{macro}[EXP]{\int_sign:n, \@@_sign:Nw}
1222 % See \cs{int_abs:n}. Evaluate the expression once (and when
1223 % debugging is enabled, check that the expression is well-formed),
1224 % then test the first character to determine the sign. This is
1225 % wrapped in \cs{int_value:w} \ldots{} \cs{exp_stop_f:} to ensure a
1226 % fixed number of expansions and to avoid dealing with closing the
1227 % conditionals.
1228 % \begin{macrocode}
1229 \cs_new:Npn \int_sign:n #1
1230 {
1231 \int_value:w \exp_after:wN \@@_sign:Nw
1232 \int_value:w \@@_eval:w #1 \@@_eval_end: \@@_sep:
1233 \exp_stop_f:
1234 }
1235 \cs_new:Npn \@@_sign:Nw #1#2 \@@_sep:
1236 {
1237 \if_meaning:w 0 #1
1238 0
1239 \else:
1240 \if_meaning:w - #1 - \fi: 1
1241 \fi:
1242 }
1243 % \end{macrocode}
1244 % \end{macro}
1245 %
1246 % \begin{macro}[EXP]{\int_abs:n}
1247 % \begin{macro}[EXP]{\@@_abs:N}
1248 % \UnitTested
1249 % \begin{macro}[EXP]{\int_max:nn}
1250 % \begin{macro}[EXP]{\int_min:nn}
1251 % \begin{macro}[EXP]{\@@_maxmin:wwN}
1252 % \UnitTested
1253 % \UnitTested
1254 % Functions for $\min$, $\max$, and absolute value with only one
1255 % evaluation. The absolute value is obtained by removing a leading
1256 % sign if any. All three functions expand in two steps.
1257 % \begin{macrocode}
1258 \cs_new:Npn \int_abs:n #1
1259 {
1260 \int_value:w \exp_after:wN \@@_abs:N
1261 \int_value:w \@@_eval:w #1 \@@_eval_end:
1262 \exp_stop_f:
1263 }
1264 \cs_new:Npn \@@_abs:N #1
1265 { \if_meaning:w - #1 \else: \exp_after:wN #1 \fi: }
1266 \cs_new:Npn \int_max:nn #1#2
1267 {
1268 \int_value:w \exp_after:wN \@@_maxmin:wwN
1269 \int_value:w \@@_eval:w #1 \exp_after:wN \@@_sep:
1270 \int_value:w \@@_eval:w #2 \@@_sep:
1271 >
1272 \exp_stop_f:
1273 }
1274 \cs_new:Npn \int_min:nn #1#2
1275 {
1276 \int_value:w \exp_after:wN \@@_maxmin:wwN
1277 \int_value:w \@@_eval:w #1 \exp_after:wN \@@_sep:
1278 \int_value:w \@@_eval:w #2 \@@_sep:
1279 <
1280 \exp_stop_f:
1281 }
1282 \cs_new:Npn \@@_maxmin:wwN #1 \@@_sep: #2 \@@_sep: #3
1283 {
1284 \if_int_compare:w #1 #3 #2 ~
1285 #1
1286 \else:
1287 #2
1288 \fi:
1289 }
1290 % \end{macrocode}
1291 % \end{macro}
1292 % \end{macro}
1293 % \end{macro}
1294 % \end{macro}
1295 % \end{macro}
1296 %
1297 % \begin{macro}[EXP]{\int_div_truncate:nn}
1298 % \UnitTested
1299 % \begin{macro}[EXP]{\int_div_round:nn}
1300 % \UnitTested
1301 % \begin{macro}[EXP]{\int_mod:nn}
1302 % \UnitTested
1303 % \begin{macro}[EXP]{\@@_div_truncate:NwNw}
1304 % \begin{macro}[EXP]{\@@_mod:ww}
1305 % As \cs{@@_eval:w} rounds the result of a division we also provide a
1306 % version that truncates the result. We use an auxiliary to make sure
1307 % numerator and denominator are only evaluated once: this comes in
1308 % handy when those are more expressions are expensive to evaluate
1309 % (\emph{e.g.}, \cs{tl_count:n}). If the numerator |#1#2| is $0$,
1310 % then we divide $0$ by the denominator (this ensures that $0/0$ is
1311 % correctly reported as an error). Otherwise, shift the numerator
1312 % |#1#2| towards $0$ by $(\left\vert\texttt{\#3\#4}\right\vert-1)/2$, which we
1313 % round away from zero. It turns out that this quantity exactly
1314 % compensates the difference between \eTeX{}'s rounding and the
1315 % truncating behavior that we want. The details are thanks to Heiko
1316 % Oberdiek: getting things right in all cases is not so easy.
1317 % \begin{macrocode}
1318 \cs_new:Npn \int_div_truncate:nn #1#2
1319 {
1320 \int_value:w \@@_eval:w
1321 \exp_after:wN \@@_div_truncate:NwNw
1322 \int_value:w \@@_eval:w #1 \exp_after:wN \@@_sep:
1323 \int_value:w \@@_eval:w #2 \@@_sep:
1324 \@@_eval_end:
1325 }
1326 \cs_new:Npn \@@_div_truncate:NwNw #1#2 \@@_sep: #3#4 \@@_sep:
1327 {
1328 \if_meaning:w 0 #1
1329 0
1330 \else:
1331 (
1332 #1#2
1333 \if_meaning:w - #1 + \else: - \fi:
1334 ( \if_meaning:w - #3 - \fi: #3#4 - 1 ) / 2
1335 )
1336 \fi:
1337 / #3#4
1338 }
1339 % \end{macrocode}
1340 % For the sake of completeness:
1341 % \begin{macrocode}
1342 \cs_new:Npn \int_div_round:nn #1#2
1343 { \int_value:w \@@_eval:w ( #1 ) / ( #2 ) \@@_eval_end: }
1344 % \end{macrocode}
1345 % Finally there's the modulus operation.
1346 % \begin{macrocode}
1347 \cs_new:Npn \int_mod:nn #1#2
1348 {
1349 \int_value:w \@@_eval:w \exp_after:wN \@@_mod:ww
1350 \int_value:w \@@_eval:w #1 \exp_after:wN \@@_sep:
1351 \int_value:w \@@_eval:w #2 \@@_sep:
1352 \@@_eval_end:
1353 }
1354 \cs_new:Npn \@@_mod:ww #1 \@@_sep: #2 \@@_sep:
1355 { #1 - ( \@@_div_truncate:NwNw #1 \@@_sep: #2 \@@_sep: ) * #2 }
1356 % \end{macrocode}
1357 % \end{macro}
1358 % \end{macro}
1359 % \end{macro}
1360 % \end{macro}
1361 % \end{macro}
1362 %
1363 % \begin{macro}[EXP]{\__kernel_int_add:nnn}
1364 % Equivalent to \cs{int_eval:n} |{#1+#2+#3}| except that overflow only
1365 % occurs if the final result overflows $[-2^{31}+1,2^{31}-1]$. The
1366 % idea is to choose the order in which the three numbers are added
1367 % together. If |#1| and |#2| have opposite signs (one is in
1368 % $[-2^{31}+1,-1]$ and the other in $[0,2^{31}-1]$) then |#1+#2|
1369 % cannot overflow so we compute the result as |#1+#2+#3|. If they
1370 % have the same sign, then either |#3| has the same sign and the order
1371 % does not matter, or |#3| has the opposite sign and any order in
1372 % which |#3| is not last will work. We use |#1+#3+#2|.
1373 % \begin{macrocode}
1374 \cs_new:Npn \__kernel_int_add:nnn #1#2#3
1375 {
1376 \int_value:w \@@_eval:w #1
1377 \if_int_compare:w #2 < \c_zero_int \exp_after:wN \reverse_if:N \fi:
1378 \if_int_compare:w #1 < \c_zero_int + #2 + #3 \else: + #3 + #2 \fi:
1379 \@@_eval_end:
1380 }
1381 % \end{macrocode}
1382 % \end{macro}
1383 %
1384 % \subsection{Creating and initializing integers}
1385 %
1386 % \begin{macro}{\int_new:N, \int_new:c}
1387 % \UnitTested
1388 % Two ways to do this: one for the format and one for the \LaTeXe{}
1389 % package. In plain \TeX{}, \cs{newcount} (and other allocators) are
1390 % \cs{outer}: to allow the code here to work in \enquote{generic} mode
1391 % this is therefore accessed by name. (The same applies to \cs{newbox},
1392 % \cs{newdimen} and so on.)
1393 % \begin{macrocode}
1394 \cs_new_protected:Npn \int_new:N #1
1395 {
1396 \__kernel_chk_if_free_cs:N #1
1397 \cs:w newcount \cs_end: #1
1398 }
1399 \cs_generate_variant:Nn \int_new:N { c }
1400 % \end{macrocode}
1401 % \end{macro}
1402 %
1403 % \begin{macro}{\int_const:Nn, \int_const:cn}
1404 % \begin{macro}{\@@_const:nN, \@@_const:eN}
1405 % \begin{macro}{\@@_constdef:Nw}
1406 % \begin{variable}{\c_@@_max_constdef_int}
1407 % \UnitTested
1408 % As stated, most constants can be defined as \tn{chardef} or
1409 % \tn{mathchardef} but that's engine dependent. As a result, there is some
1410 % set up code to determine what can be done. No full engine testing just yet
1411 % so everything is a little awkward.
1412 % We cannot use \cs{int_gset:Nn} because (when |check-declarations| is
1413 % enabled) this runs some checks that constants would fail.
1414 % \begin{macrocode}
1415 \cs_new_protected:Npn \int_const:Nn #1#2
1416 { \@@_const:eN { \int_eval:n {#2} } #1 }
1417 \cs_generate_variant:Nn \int_const:Nn { c }
1418 \cs_new_protected:Npn \@@_const:nN #1#2
1419 {
1420 \int_compare:nNnTF {#1} < \c_zero_int
1421 {
1422 \int_new:N #2
1423 \tex_global:D
1424 }
1425 {
1426 \int_compare:nNnTF {#1} > \c_@@_max_constdef_int
1427 {
1428 \int_new:N #2
1429 \tex_global:D
1430 }
1431 {
1432 \__kernel_chk_if_free_cs:N #2
1433 \tex_global:D \@@_constdef:Nw
1434 }
1435 }
1436 #2 = \@@_eval:w #1 \@@_eval_end:
1437 }
1438 \cs_generate_variant:Nn \@@_const:nN { e }
1439 \if_int_odd:w 0
1440 \cs_if_exist:NT \tex_luatexversion:D { 1 }
1441 \cs_if_exist:NT \tex_omathchardef:D { 1 }
1442 \cs_if_exist:NT \tex_XeTeXversion:D { 1 } ~
1443 \cs_if_exist:NTF \tex_omathchardef:D
1444 { \cs_new_eq:NN \@@_constdef:Nw \tex_omathchardef:D }
1445 { \cs_new_eq:NN \@@_constdef:Nw \tex_chardef:D }
1446 \tex_global:D \@@_constdef:Nw \c_@@_max_constdef_int 1114111 ~
1447 \else:
1448 \cs_new_eq:NN \@@_constdef:Nw \tex_mathchardef:D
1449 \tex_global:D \@@_constdef:Nw \c_@@_max_constdef_int 32767 ~
1450 \fi:
1451 % \end{macrocode}
1452 % \end{variable}
1453 % \end{macro}
1454 % \end{macro}
1455 % \end{macro}
1456 %
1457 % \begin{macro}{\int_zero:N, \int_zero:c}
1458 % \UnitTested
1459 % \begin{macro}{\int_gzero:N, \int_gzero:c}
1460 % \UnitTested
1461 % Functions that reset an \meta{integer} register to zero.
1462 % \begin{macrocode}
1463 \cs_new_protected:Npn \int_zero:N #1 { #1 = \c_zero_int }
1464 \cs_new_protected:Npn \int_gzero:N #1 { \tex_global:D #1 = \c_zero_int }
1465 \cs_generate_variant:Nn \int_zero:N { c }
1466 \cs_generate_variant:Nn \int_gzero:N { c }
1467 % \end{macrocode}
1468 % \end{macro}
1469 % \end{macro}
1470 %
1471 % \begin{macro}
1472 % {\int_zero_new:N, \int_zero_new:c, \int_gzero_new:N, \int_gzero_new:c}
1473 % Create a register if needed, otherwise clear it.
1474 % \begin{macrocode}
1475 \cs_new_protected:Npn \int_zero_new:N #1
1476 { \int_if_exist:NTF #1 { \int_zero:N #1 } { \int_new:N #1 } }
1477 \cs_new_protected:Npn \int_gzero_new:N #1
1478 { \int_if_exist:NTF #1 { \int_gzero:N #1 } { \int_new:N #1 } }
1479 \cs_generate_variant:Nn \int_zero_new:N { c }
1480 \cs_generate_variant:Nn \int_gzero_new:N { c }
1481 % \end{macrocode}
1482 % \end{macro}
1483 %
1484 % \begin{macro}{\int_set_eq:NN, \int_set_eq:cN, \int_set_eq:Nc, \int_set_eq:cc}
1485 % \UnitTested
1486 % \begin{macro}
1487 % {\int_gset_eq:NN, \int_gset_eq:cN, \int_gset_eq:Nc, \int_gset_eq:cc}
1488 % \UnitTested
1489 % Setting equal means using one integer inside the set function of
1490 % another. Check that assigned integer is local/global. No need to
1491 % check that the other one is defined as \TeX{} does it for us.
1492 % \begin{macrocode}
1493 \cs_new_protected:Npn \int_set_eq:NN #1#2 { #1 = #2 }
1494 \cs_generate_variant:Nn \int_set_eq:NN { c , Nc , cc }
1495 \cs_new_protected:Npn \int_gset_eq:NN #1#2 { \tex_global:D #1 = #2 }
1496 \cs_generate_variant:Nn \int_gset_eq:NN { c , Nc , cc }
1497 % \end{macrocode}
1498 % \end{macro}
1499 % \end{macro}
1500 %
1501 % \begin{macro}[pTF]{\int_if_exist:N, \int_if_exist:c}
1502 % Copies of the \texttt{cs} functions defined in \pkg{l3basics}.
1503 % \begin{macrocode}
1504 \prg_new_eq_conditional:NNn \int_if_exist:N \cs_if_exist:N
1505 { TF , T , F , p }
1506 \prg_new_eq_conditional:NNn \int_if_exist:c \cs_if_exist:c
1507 { TF , T , F , p }
1508 % \end{macrocode}
1509 % \end{macro}
1510 %
1511 % \subsection{Setting and incrementing integers}
1512 %
1513 % \begin{macro}{\int_add:Nn, \int_add:cn}
1514 % \UnitTested
1515 % \begin{macro}{\int_gadd:Nn, \int_gadd:cn}
1516 % \UnitTested
1517 % \begin{macro}{\int_sub:Nn, \int_sub:cn}
1518 % \UnitTested
1519 % \begin{macro}{\int_gsub:Nn, \int_gsub:cn}
1520 % \UnitTested
1521 % Adding and subtracting to and from a counter.
1522 % Including here the optional |by| would slow down these operations
1523 % by a few percent.
1524 % \begin{macrocode}
1525 \cs_new_protected:Npn \int_add:Nn #1#2
1526 { \tex_advance:D #1 \@@_eval:w #2 \@@_eval_end: }
1527 \cs_new_protected:Npn \int_sub:Nn #1#2
1528 { \tex_advance:D #1 - \@@_eval:w #2 \@@_eval_end: }
1529 \cs_new_protected:Npn \int_gadd:Nn #1#2
1530 { \tex_global:D \tex_advance:D #1 \@@_eval:w #2 \@@_eval_end: }
1531 \cs_new_protected:Npn \int_gsub:Nn #1#2
1532 { \tex_global:D \tex_advance:D #1 - \@@_eval:w #2 \@@_eval_end: }
1533 \cs_generate_variant:Nn \int_add:Nn { c }
1534 \cs_generate_variant:Nn \int_gadd:Nn { c }
1535 \cs_generate_variant:Nn \int_sub:Nn { c }
1536 \cs_generate_variant:Nn \int_gsub:Nn { c }
1537 % \end{macrocode}
1538 % \end{macro}
1539 % \end{macro}
1540 % \end{macro}
1541 % \end{macro}
1542 %
1543 % \begin{macro}{\int_incr:N, \int_incr:c}
1544 % \UnitTested
1545 % \begin{macro}{\int_gincr:N, \int_gincr:c}
1546 % \UnitTested
1547 % \begin{macro}{\int_decr:N, \int_decr:c}
1548 % \UnitTested
1549 % \begin{macro}{\int_gdecr:N, \int_gdecr:c}
1550 % \UnitTested
1551 % Incrementing and decrementing of integer registers is done with
1552 % the following functions.
1553 % \begin{macrocode}
1554 \cs_new_protected:Npn \int_incr:N #1
1555 { \tex_advance:D #1 \c_one_int }
1556 \cs_new_protected:Npn \int_decr:N #1
1557 { \tex_advance:D #1 - \c_one_int }
1558 \cs_new_protected:Npn \int_gincr:N #1
1559 { \tex_global:D \tex_advance:D #1 \c_one_int }
1560 \cs_new_protected:Npn \int_gdecr:N #1
1561 { \tex_global:D \tex_advance:D #1 - \c_one_int }
1562 \cs_generate_variant:Nn \int_incr:N { c }
1563 \cs_generate_variant:Nn \int_decr:N { c }
1564 \cs_generate_variant:Nn \int_gincr:N { c }
1565 \cs_generate_variant:Nn \int_gdecr:N { c }
1566 % \end{macrocode}
1567 % \end{macro}
1568 % \end{macro}
1569 % \end{macro}
1570 % \end{macro}
1571 %
1572 % \begin{macro}{\int_set:Nn, \int_set:cn, \int_set:NV, \int_set:cV}
1573 % \UnitTested
1574 % \begin{macro}{\int_gset:Nn, \int_gset:cn, \int_gset:NV, \int_gset:cV}
1575 % \UnitTested
1576 % As integers are register-based \TeX{} issues an error
1577 % if they are not defined. While the |=| sign is optional, this
1578 % version with |=| is slightly quicker than without, while adding the
1579 % optional space after |=| slows things down minutely.
1580 % \begin{macrocode}
1581 \cs_new_protected:Npn \int_set:Nn #1#2
1582 { #1 = \@@_eval:w #2 \@@_eval_end: }
1583 \cs_new_protected:Npn \int_gset:Nn #1#2
1584 { \tex_global:D #1 = \@@_eval:w #2 \@@_eval_end: }
1585 \cs_generate_variant:Nn \int_set:Nn { NV , c , cV }
1586 \cs_generate_variant:Nn \int_gset:Nn { NV , c , cV }
1587 % \end{macrocode}
1588 % \end{macro}
1589 % \end{macro}
1590 %
1591 % \begin{macro}
1592 % {
1593 % \int_set_regex_count:Nnn, \int_set_regex_count:cnn,
1594 % \int_gset_regex_count:Nnn, \int_gset_regex_count:cnn
1595 % }
1596 % \begin{macro}
1597 % {
1598 % \int_set_regex_count:NNn, \int_set_regex_count:cNn,
1599 % \int_gset_regex_count:NNn, \int_set_gregex_count:cNn
1600 % }
1601 % \begin{macrocode}
1602 \cs_new_protected:Npn \int_set_regex_count:Nnn #1#2#3
1603 { \regex_count:nnN {#2} {#3} #1 }
1604 \cs_generate_variant:Nn \int_set_regex_count:Nnn { c }
1605 \cs_new_protected:Npn \int_gset_regex_count:Nnn #1#2#3
1606 {
1607 \group_begin:
1608 \int_set_eq:NN \l_@@_tmpa_int #1
1609 \regex_count:nnN {#2} {#3} \l_@@_tmpa_int
1610 \int_gset_eq:NN #1 \l_@@_tmpa_int
1611 \group_end:
1612 }
1613 \cs_generate_variant:Nn \int_gset_regex_count:Nnn { c }
1614 \cs_new_protected:Npn \int_set_regex_count:NNn #1#2#3
1615 { \regex_count:NnN #2 {#3} #1 }
1616 \cs_generate_variant:Nn \int_set_regex_count:Nnn { c }
1617 \cs_new_protected:Npn \int_gset_regex_count:NNn #1#2#3
1618 {
1619 \group_begin:
1620 \int_set_eq:NN \l_@@_tmpa_int #1
1621 \regex_count:NnN #2 {#3} \l_@@_tmpa_int
1622 \int_gset_eq:NN #1 \l_@@_tmpa_int
1623 \group_end:
1624 }
1625 \cs_generate_variant:Nn \int_gset_regex_count:NNn { c }
1626 % \end{macrocode}
1627 % \end{macro}
1628 % \end{macro}
1629 %
1630 % \subsection{Using integers}
1631 %
1632 % \begin{macro}{\int_use:N, \int_use:c}
1633 % \UnitTested
1634 % Here is how counters are accessed.
1635 % We hand-code the |c| variant for some speed gain.
1636 % \begin{macrocode}
1637 \cs_new_eq:NN \int_use:N \tex_the:D
1638 \cs_new:Npn \int_use:c #1 { \tex_the:D \cs:w #1 \cs_end: }
1639 % \end{macrocode}
1640 % \end{macro}
1641 %
1642 % \subsection{Integer expression conditionals}
1643 %
1644 % \begin{macro}[EXP]{\@@_compare_error:, \@@_compare_error:Nw}
1645 % Those functions are used for comparison tests which use a simple
1646 % syntax where only one set of braces is required and additional
1647 % operators such as |!=| and |>=| are supported. The tests first
1648 % evaluate their left-hand side, with a trailing
1649 % \cs{@@_compare_error:}. This marker is normally not expanded,
1650 % but if the relation symbol is missing from the test's argument, then
1651 % the marker inserts |=| (and itself) after triggering the relevant
1652 % \TeX{} error. If the first token which appears after evaluating and
1653 % removing the left-hand side is not a known relation symbol, then a
1654 % judiciously placed \cs{@@_compare_error:Nw} gets expanded,
1655 % cleaning up the end of the test and telling the user what the
1656 % problem was.
1657 % \begin{macrocode}
1658 \cs_new_protected:Npn \@@_compare_error:
1659 {
1660 \if_int_compare:w \c_zero_int \c_zero_int \fi:
1661 =
1662 \@@_compare_error:
1663 }
1664 \cs_new:Npn \@@_compare_error:Nw
1665 #1#2 \s_@@_stop
1666 {
1667 { }
1668 \c_zero_int \fi:
1669 \msg_expandable_error:nnn
1670 { kernel } { unknown-comparison } {#1}
1671 \prg_return_false:
1672 }
1673 % \end{macrocode}
1674 % \end{macro}
1675 %
1676 % \begin{macro}[pTF, EXP]{\int_compare:n}
1677 % \begin{macro}[EXP]
1678 % {\@@_compare:w, \@@_compare:Nw, \@@_compare:NNw, \@@_compare:nnN}
1679 % \begin{macro}[EXP]
1680 % {
1681 % \@@_compare_end_=:NNw,
1682 % \@@_compare_=:NNw,
1683 % \@@_compare_<:NNw,
1684 % \@@_compare_>:NNw,
1685 % \@@_compare_==:NNw,
1686 % \@@_compare_!=:NNw,
1687 % \@@_compare_<=:NNw,
1688 % \@@_compare_>=:NNw
1689 % }
1690 % Comparison tests using a simple syntax where only one set of braces
1691 % is required, additional operators such as |!=| and |>=| are
1692 % supported, and multiple comparisons can be performed at once, for
1693 % instance |0 < 5 <= 1|. The idea is to loop through the argument,
1694 % finding one operand at a time, and comparing it to the previous one.
1695 % The looping auxiliary \cs{@@_compare:Nw} reads one \meta{operand}
1696 % and one \meta{comparison} symbol, and leaves roughly
1697 % \begin{quote}
1698 % \meta{operand} \cs{prg_return_false:} \cs{fi:} \\
1699 % \cs{reverse_if:N} \cs{if_int_compare:w} \meta{operand} \meta{comparison} \\
1700 % \cs{@@_compare:Nw}
1701 % \end{quote}
1702 % in the input stream. Each call to this auxiliary provides the
1703 % second operand of the last call's \cs{if_int_compare:w}. If one of
1704 % the \meta{comparisons} is \texttt{false}, the \texttt{true} branch
1705 % of the \TeX{} conditional is taken (because of \cs{reverse_if:N}),
1706 % immediately returning \texttt{false} as the result of the test.
1707 % There is no \TeX{} conditional waiting the first operand, so we add
1708 % an \cs{if_false:} and expand by hand with \cs{int_value:w}, thus
1709 % skipping \cs{prg_return_false:} on the first iteration.
1710 %
1711 % Before starting the loop, the first step is to make sure that there
1712 % is at least one relation symbol. We first let \TeX{} evaluate this
1713 % left hand side of the (in)equality using \cs{@@_eval:w}. Since the
1714 % relation symbols |<|, |>|, |=| and |!| are not allowed in integer
1715 % expressions, they would terminate the expression. If the argument contains no
1716 % relation symbol, \cs{@@_compare_error:} is expanded,
1717 % inserting~|=| and itself after an error. In all cases,
1718 % \cs{@@_compare:w} receives as its argument an integer, a relation
1719 % symbol, and some more tokens. We then setup the loop, which is
1720 % ended by the two odd-looking items |e| and |{=nd_}|, with a trailing
1721 % \cs{s_@@_stop} used to grab the entire argument when necessary.
1722 % \begin{macrocode}
1723 \prg_new_conditional:Npnn \int_compare:n #1 { p , T , F , TF }
1724 {
1725 \exp_after:wN \@@_compare:w
1726 \int_value:w \@@_eval:w #1 \@@_compare_error:
1727 }
1728 \cs_new:Npn \@@_compare:w #1 \@@_compare_error:
1729 {
1730 \exp_after:wN \if_false: \int_value:w
1731 \@@_compare:Nw #1 e { = nd_ } \s_@@_stop
1732 }
1733 % \end{macrocode}
1734 % The goal here is to find an \meta{operand} and a \meta{comparison}.
1735 % The \meta{operand} is already evaluated, but we cannot yet grab it
1736 % as an argument. To access the following relation symbol, we remove
1737 % the number by applying \cs{@@_to_roman:w}, after making sure that
1738 % the argument becomes non-positive: its roman numeral representation
1739 % is then empty. Then probe the first two tokens with
1740 % \cs{@@_compare:NNw} to determine the relation symbol, building a
1741 % control sequence from it (\cs{token_to_str:N} gives better errors if
1742 % |#1| is not a character). All the extended forms have an extra |=|
1743 % hence the test for that as a second token. If the relation symbol
1744 % is unknown, then the control sequence is turned by \TeX{} into
1745 % \cs{scan_stop:}, ignored thanks to \tn{unexpanded}, and
1746 % \cs{@@_compare_error:Nw} raises an error.
1747 % \begin{macrocode}
1748 \cs_new:Npn \@@_compare:Nw #1#2 \s_@@_stop
1749 {
1750 \exp_after:wN \@@_compare:NNw
1751 \@@_to_roman:w - 0 #2 \s_@@_mark
1752 #1#2 \s_@@_stop
1753 }
1754 \cs_new:Npn \@@_compare:NNw #1#2#3 \s_@@_mark
1755 {
1756 \__kernel_exp_not:w
1757 \use:c
1758 {
1759 @@_compare_ \token_to_str:N #1
1760 \if_meaning:w = #2 = \fi:
1761 :NNw
1762 }
1763 \@@_compare_error:Nw #1
1764 }
1765 % \end{macrocode}
1766 % When the last \meta{operand} is seen, \cs{@@_compare:NNw} receives
1767 % |e| and |=nd_| as arguments, hence calling
1768 % \cs{@@_compare_end_=:NNw} to end the loop: return the result of the
1769 % last comparison (involving the operand that we just found). When a
1770 % normal relation is found, the appropriate auxiliary calls
1771 % \cs{@@_compare:nnN} where |#1| is \cs{if_int_compare:w} or
1772 % \cs{reverse_if:N} \cs{if_int_compare:w}, |#2| is the \meta{operand},
1773 % and |#3| is one of |<|, |=|, or~|>|. As announced earlier, we leave
1774 % the \meta{operand} for the previous conditional. If this
1775 % conditional is true the result of the test is known, so we remove
1776 % all tokens and return \texttt{false}. Otherwise, we apply the
1777 % conditional |#1| to the \meta{operand} |#2| and the comparison |#3|,
1778 % and call \cs{@@_compare:Nw} to look for additional operands, after
1779 % evaluating the following expression.
1780 % \begin{macrocode}
1781 \cs_new:cpn { @@_compare_end_=:NNw } #1#2#3 e #4 \s_@@_stop
1782 {
1783 {#3} \exp_stop_f:
1784 \prg_return_false: \else: \prg_return_true: \fi:
1785 }
1786 \cs_new:Npn \@@_compare:nnN #1#2#3
1787 {
1788 {#2} \exp_stop_f:
1789 \prg_return_false: \exp_after:wN \@@_use_none_delimit_by_s_stop:w
1790 \fi:
1791 #1 #2 #3 \exp_after:wN \@@_compare:Nw \int_value:w \@@_eval:w
1792 }
1793 % \end{macrocode}
1794 % The actual comparisons are then simple function calls, using the
1795 % relation as delimiter for a delimited argument and discarding
1796 % \cs{@@_compare_error:Nw} \meta{token} responsible for error
1797 % detection.
1798 % \begin{macrocode}
1799 \cs_new:cpn { @@_compare_=:NNw } #1#2#3 =
1800 { \@@_compare:nnN { \reverse_if:N \if_int_compare:w } {#3} = }
1801 \cs_new:cpn { @@_compare_<:NNw } #1#2#3 <
1802 { \@@_compare:nnN { \reverse_if:N \if_int_compare:w } {#3} < }
1803 \cs_new:cpn { @@_compare_>:NNw } #1#2#3 >
1804 { \@@_compare:nnN { \reverse_if:N \if_int_compare:w } {#3} > }
1805 \cs_new:cpn { @@_compare_==:NNw } #1#2#3 ==
1806 { \@@_compare:nnN { \reverse_if:N \if_int_compare:w } {#3} = }
1807 \cs_new:cpn { @@_compare_!=:NNw } #1#2#3 !=
1808 { \@@_compare:nnN { \if_int_compare:w } {#3} = }
1809 \cs_new:cpn { @@_compare_<=:NNw } #1#2#3 <=
1810 { \@@_compare:nnN { \if_int_compare:w } {#3} > }
1811 \cs_new:cpn { @@_compare_>=:NNw } #1#2#3 >=
1812 { \@@_compare:nnN { \if_int_compare:w } {#3} < }
1813 % \end{macrocode}
1814 % \end{macro}
1815 % \end{macro}
1816 % \end{macro}
1817 %
1818 % \begin{macro}[pTF]
1819 % {
1820 % \int_compare:nNn ,
1821 % \int_compare:vNn ,
1822 % \int_compare:nNv ,
1823 % \int_compare:vNv
1824 % }
1825 % \UnitTested
1826 % More efficient but less natural in typing; we add hand-tuned variants as
1827 % the build-in behavior makes this much more efficient. No mappings yet
1828 % so the setup has to be written out in full.
1829 % \begin{macrocode}
1830 \prg_new_conditional:Npnn \int_compare:nNn #1#2#3 { p , T , F , TF }
1831 {
1832 \if_int_compare:w \@@_eval:w #1 #2 \@@_eval:w #3 \@@_eval_end:
1833 \prg_return_true:
1834 \else:
1835 \prg_return_false:
1836 \fi:
1837 }
1838 \cs_new:Npn \int_compare_p:vNn #1#2#3
1839 { \int_compare_p:nNn { \use:c {#1} } #2 {#3} }
1840 \cs_new:Npn \int_compare_p:nNv #1#2#3
1841 { \int_compare_p:nNn {#1} #2 { \use:c {#3} } }
1842 \cs_new:Npn \int_compare_p:vNv #1#2#3
1843 { \int_compare_p:nNn { \use:c {#1} } #2 { \use:c {#3} } }
1844 \cs_new:Npn \int_compare:vNnT #1#2#3
1845 { \int_compare:nNnT { \use:c {#1} } #2 {#3} }
1846 \cs_new:Npn \int_compare:nNvT #1#2#3
1847 { \int_compare:nNnT {#1} #2 { \use:c {#3} } }
1848 \cs_new:Npn \int_compare:vNvT #1#2#3
1849 { \int_compare:nNnT { \use:c {#1} } #2 { \use:c {#3} } }
1850 \cs_new:Npn \int_compare:vNnF #1#2#3
1851 { \int_compare:nNnF { \use:c {#1} } #2 {#3} }
1852 \cs_new:Npn \int_compare:nNvF #1#2#3
1853 { \int_compare:nNnF {#1} #2 { \use:c {#3} } }
1854 \cs_new:Npn \int_compare:vNvF #1#2#3
1855 { \int_compare:nNnF { \use:c {#1} } #2 { \use:c {#3} } }
1856 \cs_new:Npn \int_compare:vNnTF #1#2#3
1857 { \int_compare:nNnTF { \use:c {#1} } #2 {#3} }
1858 \cs_new:Npn \int_compare:nNvTF #1#2#3
1859 { \int_compare:nNnTF {#1} #2 { \use:c {#3} } }
1860 \cs_new:Npn \int_compare:vNvTF #1#2#3
1861 { \int_compare:nNnTF { \use:c {#1} } #2 { \use:c {#3} } }
1862 % \end{macrocode}
1863 % \end{macro}
1864 %
1865 % \begin{macro}[pTF]{\int_if_zero:n}
1866 % \begin{macrocode}
1867 \prg_new_conditional:Npnn \int_if_zero:n #1 { p , T , F , TF }
1868 {
1869 \if_int_compare:w \@@_eval:w #1 = \c_zero_int
1870 \prg_return_true:
1871 \else:
1872 \prg_return_false:
1873 \fi:
1874 }
1875 % \end{macrocode}
1876 % \end{macro}
1877 %
1878 % \begin{macro}[EXP, noTF]{\int_case:nn}
1879 % \begin{macro}{\@@_case:nnTF}
1880 % \begin{macro}{\@@_case:nw, \@@_case_end:nw}
1881 % For integer cases, the first task to fully expand the check
1882 % condition. The over all idea is then much the same as for
1883 % \cs{str_case:nnTF} as described in \pkg{l3str}.
1884 % \begin{macrocode}
1885 \cs_new:Npn \int_case:nnTF #1
1886 {
1887 \exp:w
1888 \exp_args:Nf \@@_case:nnTF { \int_eval:n {#1} }
1889 }
1890 \cs_new:Npn \int_case:nnT #1#2#3
1891 {
1892 \exp:w
1893 \exp_args:Nf \@@_case:nnTF { \int_eval:n {#1} } {#2} {#3} { }
1894 }
1895 \cs_new:Npn \int_case:nnF #1#2
1896 {
1897 \exp:w
1898 \exp_args:Nf \@@_case:nnTF { \int_eval:n {#1} } {#2} { }
1899 }
1900 \cs_new:Npn \int_case:nn #1#2
1901 {
1902 \exp:w
1903 \exp_args:Nf \@@_case:nnTF { \int_eval:n {#1} } {#2} { } { }
1904 }
1905 \cs_new:Npn \@@_case:nnTF #1#2#3#4
1906 { \@@_case:nw {#1} #2 {#1} { } \s_@@_mark {#3} \s_@@_mark {#4} \s_@@_stop }
1907 \cs_new:Npn \@@_case:nw #1#2#3
1908 {
1909 \int_compare:nNnTF {#1} = {#2}
1910 { \@@_case_end:nw {#3} }
1911 { \@@_case:nw {#1} }
1912 }
1913 \cs_new:Npn \@@_case_end:nw #1#2#3 \s_@@_mark #4#5 \s_@@_stop
1914 { \exp_end: #1 #4 }
1915 % \end{macrocode}
1916 % \end{macro}
1917 % \end{macro}
1918 % \end{macro}
1919 %
1920 % \begin{macro}[pTF]{\int_if_odd:n}
1921 % \UnitTested
1922 % \begin{macro}[pTF]{\int_if_even:n}
1923 % \UnitTested
1924 % A predicate function.
1925 % \begin{macrocode}
1926 \prg_new_conditional:Npnn \int_if_odd:n #1 { p , T , F , TF}
1927 {
1928 \if_int_odd:w \@@_eval:w #1 \@@_eval_end:
1929 \prg_return_true:
1930 \else:
1931 \prg_return_false:
1932 \fi:
1933 }
1934 \prg_new_conditional:Npnn \int_if_even:n #1 { p , T , F , TF}
1935 {
1936 \reverse_if:N \if_int_odd:w \@@_eval:w #1 \@@_eval_end:
1937 \prg_return_true:
1938 \else:
1939 \prg_return_false:
1940 \fi:
1941 }
1942 % \end{macrocode}
1943 % \end{macro}
1944 % \end{macro}
1945 %
1946 % \subsection{Integer expression loops}
1947 %
1948 % \begin{macro}{\int_while_do:nn}
1949 % \UnitTested
1950 % \TestMissing{Boundary cases}
1951 % \begin{macro}{\int_until_do:nn}
1952 % \UnitTested
1953 % \TestMissing{Boundary cases}
1954 % \begin{macro}{\int_do_while:nn}
1955 % \UnitTested
1956 % \TestMissing{Boundary cases}
1957 % \begin{macro}{\int_do_until:nn}
1958 % \UnitTested
1959 % \TestMissing{Boundary cases}
1960 % These are quite easy given the above functions. The |while| versions
1961 % test first and then execute the body. The |do_while| does it the
1962 % other way round.
1963 % \begin{macrocode}
1964 \cs_new:Npn \int_while_do:nn #1#2
1965 {
1966 \int_compare:nT {#1}
1967 {
1968 #2
1969 \int_while_do:nn {#1} {#2}
1970 }
1971 }
1972 \cs_new:Npn \int_until_do:nn #1#2
1973 {
1974 \int_compare:nF {#1}
1975 {
1976 #2
1977 \int_until_do:nn {#1} {#2}
1978 }
1979 }
1980 \cs_new:Npn \int_do_while:nn #1#2
1981 {
1982 #2
1983 \int_compare:nT {#1}
1984 { \int_do_while:nn {#1} {#2} }
1985 }
1986 \cs_new:Npn \int_do_until:nn #1#2
1987 {
1988 #2
1989 \int_compare:nF {#1}
1990 { \int_do_until:nn {#1} {#2} }
1991 }
1992 % \end{macrocode}
1993 % \end{macro}
1994 % \end{macro}
1995 % \end{macro}
1996 % \end{macro}
1997 %
1998 % \begin{macro}{\int_while_do:nNnn}
1999 % \begin{macro}{\int_until_do:nNnn}
2000 % \begin{macro}{\int_do_while:nNnn}
2001 % \begin{macro}{\int_do_until:nNnn}
2002 % As above but not using the more natural syntax.
2003 % \begin{macrocode}
2004 \cs_new:Npn \int_while_do:nNnn #1#2#3#4
2005 {
2006 \int_compare:nNnT {#1} #2 {#3}
2007 {
2008 #4
2009 \int_while_do:nNnn {#1} #2 {#3} {#4}
2010 }
2011 }
2012 \cs_new:Npn \int_until_do:nNnn #1#2#3#4
2013 {
2014 \int_compare:nNnF {#1} #2 {#3}
2015 {
2016 #4
2017 \int_until_do:nNnn {#1} #2 {#3} {#4}
2018 }
2019 }
2020 \cs_new:Npn \int_do_while:nNnn #1#2#3#4
2021 {
2022 #4
2023 \int_compare:nNnT {#1} #2 {#3}
2024 { \int_do_while:nNnn {#1} #2 {#3} {#4} }
2025 }
2026 \cs_new:Npn \int_do_until:nNnn #1#2#3#4
2027 {
2028 #4
2029 \int_compare:nNnF {#1} #2 {#3}
2030 { \int_do_until:nNnn {#1} #2 {#3} {#4} }
2031 }
2032 % \end{macrocode}
2033 % \end{macro}
2034 % \end{macro}
2035 % \end{macro}
2036 % \end{macro}
2037 %
2038 % \subsection{Integer step functions}
2039 %
2040 % \begin{macro}{\int_step_function:nnnN}
2041 % \begin{macro}{\@@_step:w, \@@_step:Nw}
2042 % \begin{macro}{\int_step_function:nN}
2043 % \begin{macro}{\int_step_function:nnN}
2044 % Before all else, evaluate the initial value, step, and final value.
2045 % Repeating a function by steps first needs a check on the direction
2046 % of the steps. After that, do the function for the start value then
2047 % step and loop around. It would be more symmetrical to test for a
2048 % step size of zero before checking the sign, but we optimize for the
2049 % most frequent case (positive step). And since when we're doing the
2050 % test the step size is the result of \cs{@@_eval:w} we know that only
2051 % the value $0$ has a leading token |0| which we can use for a faster
2052 % test than \cs{int_compare:nNnTF}.
2053 % \begin{macrocode}
2054 \cs_new:Npn \int_step_function:nnnN #1#2#3
2055 {
2056 \exp_after:wN \@@_step:w
2057 \int_value:w \@@_eval:w #1 \exp_after:wN \@@_sep:
2058 \int_value:w \@@_eval:w #2 \exp_after:wN \@@_sep:
2059 \int_value:w \@@_eval:w #3 \@@_sep:
2060 }
2061 \cs_new:Npn \@@_step:w #1 \@@_sep: #2 \@@_sep: #3 \@@_sep: #4
2062 {
2063 \int_compare:nNnTF {#2} > \c_zero_int
2064 { \@@_step:Nw > }
2065 {
2066 \if_meaning:w 0 #2
2067 \exp_after:wN \use_ii:nn
2068 \fi:
2069 \use_none:n
2070 {
2071 \msg_expandable_error:nnn
2072 { kernel } { zero-step } {#4}
2073 \prg_break:
2074 }
2075 \@@_step:Nw <
2076 }
2077 #1 \@@_sep: {#2} {#3} {#4}
2078 \prg_break_point:
2079 }
2080 \cs_new:Npn \@@_step:Nw #1#2 \@@_sep: #3#4#5
2081 {
2082 \if_int_compare:w #2 #1 #4 \exp_stop_f:
2083 \prg_break:n
2084 \fi:
2085 #5 {#2}
2086 \exp_after:wN \@@_step:Nw
2087 \exp_after:wN #1
2088 \int_value:w \@@_eval:w #2 + #3 \@@_sep: {#3} {#4} {#5}
2089 }
2090 \cs_new:Npn \int_step_function:nN
2091 { \int_step_function:nnnN \c_one_int \c_one_int }
2092 \cs_new:Npn \int_step_function:nnN #1
2093 { \int_step_function:nnnN {#1} \c_one_int }
2094 % \end{macrocode}
2095 % \end{macro}
2096 % \end{macro}
2097 % \end{macro}
2098 % \end{macro}
2099 %
2100 % \begin{macro}
2101 % {\int_step_tokens:nn, \int_step_tokens:nnn, \int_step_tokens:nnnn}
2102 % Because the internals \cs{@@_step:wwwn} and \cs{@@_step:Nwnnn} are
2103 % defined in such a way that they work with both a single token or a
2104 % braced group of tokens these are really the same as the |function|
2105 % variants.
2106 % \begin{macrocode}
2107 \cs_new_eq:NN \int_step_tokens:nn \int_step_function:nN
2108 \cs_new_eq:NN \int_step_tokens:nnn \int_step_function:nnN
2109 \cs_new_eq:NN \int_step_tokens:nnnn \int_step_function:nnnN
2110 % \end{macrocode}
2111 % \end{macro}
2112 %
2113 % \begin{macro}{\int_step_inline:nn, \int_step_inline:nnn, \int_step_inline:nnnn}
2114 % \begin{macro}{\int_step_variable:nNn, \int_step_variable:nnNn, \int_step_variable:nnnNn}
2115 % \UnitTested
2116 % \begin{macro}{\@@_step:NNnnnn}
2117 % The approach here is to build a function, with a global integer
2118 % required to make the nesting safe (as seen in other in line
2119 % functions), and map that function using \cs{int_step_function:nnnN}.
2120 % We put a \cs{prg_break_point:Nn} so that \texttt{map_break}
2121 % functions from other modules correctly decrement \cs{g__kernel_prg_map_int}
2122 % before looking for their own break point. The first argument is
2123 % \cs{scan_stop:}, so that no breaking function recognizes this break
2124 % point as its own.
2125 % \begin{macrocode}
2126 \cs_new_protected:Npn \int_step_inline:nn
2127 { \int_step_inline:nnnn { 1 } { 1 } }
2128 \cs_new_protected:Npn \int_step_inline:nnn #1
2129 { \int_step_inline:nnnn {#1} { 1 } }
2130 \cs_new_protected:Npn \int_step_inline:nnnn
2131 {
2132 \int_gincr:N \g__kernel_prg_map_int
2133 \exp_args:NNc \@@_step:NNnnnn
2134 \cs_gset_protected:Npn
2135 { @@_map_ \int_use:N \g__kernel_prg_map_int :w }
2136 }
2137 \cs_new_protected:Npn \int_step_variable:nNn
2138 { \int_step_variable:nnnNn { 1 } { 1 } }
2139 \cs_new_protected:Npn \int_step_variable:nnNn #1
2140 { \int_step_variable:nnnNn {#1} { 1 } }
2141 \cs_new_protected:Npn \int_step_variable:nnnNn #1#2#3#4#5
2142 {
2143 \int_gincr:N \g__kernel_prg_map_int
2144 \exp_args:NNc \@@_step:NNnnnn
2145 \cs_gset_protected:Npe
2146 { @@_map_ \int_use:N \g__kernel_prg_map_int :w }
2147 {#1}{#2}{#3}
2148 {
2149 \tl_set:Nn \exp_not:N #4 {##1}
2150 \exp_not:n {#5}
2151 }
2152 }
2153 \cs_new_protected:Npn \@@_step:NNnnnn #1#2#3#4#5#6
2154 {
2155 #1 #2 ##1 {#6}
2156 \int_step_function:nnnN {#3} {#4} {#5} #2
2157 \prg_break_point:Nn \scan_stop: { \int_gdecr:N \g__kernel_prg_map_int }
2158 }
2159 % \end{macrocode}
2160 % \end{macro}
2161 % \end{macro}
2162 % \end{macro}
2163 %
2164 % \subsection{Formatting integers}
2165 %
2166 % \begin{macro}{\int_to_arabic:n, \int_to_arabic:v}
2167 % \UnitTested
2168 % Nothing exciting here.
2169 % \begin{macrocode}
2170 \cs_new_eq:NN \int_to_arabic:n \int_eval:n
2171 \cs_generate_variant:Nn \int_to_arabic:n { v }
2172 % \end{macrocode}
2173 % \end{macro}
2174 %
2175 % \begin{macro}{\int_to_symbols:nnn}
2176 % \UnitTested
2177 % \begin{macro}{\@@_to_symbols:nnnn, \@@_to_symbols:ennn}
2178 % For conversion of integers to arbitrary symbols the method is in
2179 % general as follows. The input number (|#1|) is compared to the total
2180 % number of symbols available at each place (|#2|). If the input is larger
2181 % than the total number of symbols available then the modulus is needed,
2182 % with one added so that the positions don't have to number from
2183 % zero. Using an \texttt{f}-type expansion, this is done so that the system
2184 % is recursive. The actual conversion function therefore gets a `nice'
2185 % number at each stage. Of course, if the initial input was small enough
2186 % then there is no problem and everything is easy.
2187 % \begin{macrocode}
2188 \cs_new:Npn \int_to_symbols:nnn #1#2#3
2189 {
2190 \int_compare:nNnTF {#1} > {#2}
2191 {
2192 \@@_to_symbols:ennn
2193 {
2194 \int_case:nn
2195 { 1 + \int_mod:nn { #1 - 1 } {#2} }
2196 {#3}
2197 }
2198 {#1} {#2} {#3}
2199 }
2200 { \int_case:nn {#1} {#3} }
2201 }
2202 \cs_new:Npn \@@_to_symbols:nnnn #1#2#3#4
2203 {
2204 \exp_args:Nf \int_to_symbols:nnn
2205 { \int_div_truncate:nn { #2 - 1 } {#3} } {#3} {#4}
2206 #1
2207 }
2208 \cs_generate_variant:Nn \@@_to_symbols:nnnn { e }
2209 % \end{macrocode}
2210 % \end{macro}
2211 % \end{macro}
2212 %
2213 % \begin{macro}{\int_to_alph:n, \int_to_Alph:n}
2214 % \UnitTested
2215 % These both use the above function with input functions that make sense
2216 % for the alphabet in English.
2217 % \begin{macrocode}
2218 \cs_new:Npn \int_to_alph:n #1
2219 {
2220 \int_to_symbols:nnn {#1} { 26 }
2221 {
2222 { 1 } { a }
2223 { 2 } { b }
2224 { 3 } { c }
2225 { 4 } { d }
2226 { 5 } { e }
2227 { 6 } { f }
2228 { 7 } { g }
2229 { 8 } { h }
2230 { 9 } { i }
2231 { 10 } { j }
2232 { 11 } { k }
2233 { 12 } { l }
2234 { 13 } { m }
2235 { 14 } { n }
2236 { 15 } { o }
2237 { 16 } { p }
2238 { 17 } { q }
2239 { 18 } { r }
2240 { 19 } { s }
2241 { 20 } { t }
2242 { 21 } { u }
2243 { 22 } { v }
2244 { 23 } { w }
2245 { 24 } { x }
2246 { 25 } { y }
2247 { 26 } { z }
2248 }
2249 }
2250 \cs_new:Npn \int_to_Alph:n #1
2251 {
2252 \int_to_symbols:nnn {#1} { 26 }
2253 {
2254 { 1 } { A }
2255 { 2 } { B }
2256 { 3 } { C }
2257 { 4 } { D }
2258 { 5 } { E }
2259 { 6 } { F }
2260 { 7 } { G }
2261 { 8 } { H }
2262 { 9 } { I }
2263 { 10 } { J }
2264 { 11 } { K }
2265 { 12 } { L }
2266 { 13 } { M }
2267 { 14 } { N }
2268 { 15 } { O }
2269 { 16 } { P }
2270 { 17 } { Q }
2271 { 18 } { R }
2272 { 19 } { S }
2273 { 20 } { T }
2274 { 21 } { U }
2275 { 22 } { V }
2276 { 23 } { W }
2277 { 24 } { X }
2278 { 25 } { Y }
2279 { 26 } { Z }
2280 }
2281 }
2282 % \end{macrocode}
2283 % \end{macro}
2284 %
2285 % \begin{macro}{\int_to_base:nn, \int_to_Base:nn}
2286 % \UnitTested
2287 % \begin{macro}{\@@_to_base:nn, \@@_to_Base:nn,
2288 % \@@_to_base:nnN, \@@_to_Base:nnN, \@@_to_base:nnnN, \@@_to_Base:nnnN}
2289 % \begin{macro}{\@@_to_letter:n, \@@_to_Letter:n}
2290 % \UnitTested
2291 % Converting from base ten (|#1|) to a second base (|#2|) starts with
2292 % computing |#1|: if it is a complicated calculation, we shouldn't
2293 % perform it twice. Then check the sign, store it, either \texttt{-}
2294 % or \cs{c_empty_tl}, and feed the absolute value to the next auxiliary
2295 % function.
2296 % \begin{macrocode}
2297 \cs_new:Npn \int_to_base:nn #1
2298 { \exp_args:Nf \@@_to_base:nn { \int_eval:n {#1} } }
2299 \cs_new:Npn \int_to_Base:nn #1
2300 { \exp_args:Nf \@@_to_Base:nn { \int_eval:n {#1} } }
2301 \cs_new:Npn \@@_to_base:nn #1#2
2302 {
2303 \int_compare:nNnTF {#1} < 0
2304 { \exp_args:No \@@_to_base:nnN { \use_none:n #1 } {#2} - }
2305 { \@@_to_base:nnN {#1} {#2} \c_empty_tl }
2306 }
2307 \cs_new:Npn \@@_to_Base:nn #1#2
2308 {
2309 \int_compare:nNnTF {#1} < 0
2310 { \exp_args:No \@@_to_Base:nnN { \use_none:n #1 } {#2} - }
2311 { \@@_to_Base:nnN {#1} {#2} \c_empty_tl }
2312 }
2313 % \end{macrocode}
2314 % Here, the idea is to provide a recursive system to deal with the
2315 % input. The output is built up after the end of the function.
2316 % At each pass, the value in |#1| is checked to see if it is less
2317 % than the new base (|#2|). If it is, then it is converted directly,
2318 % putting the sign back in front.
2319 % On the other hand, if the value to convert is greater than or equal
2320 % to the new base then the modulus and remainder values are found. The
2321 % modulus is converted to a symbol and put on the right,
2322 % and the remainder is carried forward to the next round.
2323 % \begin{macrocode}
2324 \cs_new:Npn \@@_to_base:nnN #1#2#3
2325 {
2326 \int_compare:nNnTF {#1} < {#2}
2327 { \exp_last_unbraced:Nf #3 { \@@_to_letter:n {#1} } }
2328 {
2329 \exp_args:Nf \@@_to_base:nnnN
2330 { \@@_to_letter:n { \int_mod:nn {#1} {#2} } }
2331 {#1}
2332 {#2}
2333 #3
2334 }
2335 }
2336 \cs_new:Npn \@@_to_base:nnnN #1#2#3#4
2337 {
2338 \exp_args:Nf \@@_to_base:nnN
2339 { \int_div_truncate:nn {#2} {#3} }
2340 {#3}
2341 #4
2342 #1
2343 }
2344 \cs_new:Npn \@@_to_Base:nnN #1#2#3
2345 {
2346 \int_compare:nNnTF {#1} < {#2}
2347 { \exp_last_unbraced:Nf #3 { \@@_to_Letter:n {#1} } }
2348 {
2349 \exp_args:Nf \@@_to_Base:nnnN
2350 { \@@_to_Letter:n { \int_mod:nn {#1} {#2} } }
2351 {#1}
2352 {#2}
2353 #3
2354 }
2355 }
2356 \cs_new:Npn \@@_to_Base:nnnN #1#2#3#4
2357 {
2358 \exp_args:Nf \@@_to_Base:nnN
2359 { \int_div_truncate:nn {#2} {#3} }
2360 {#3}
2361 #4
2362 #1
2363 }
2364 % \end{macrocode}
2365 % Convert to a letter only if necessary, otherwise simply return the
2366 % value unchanged. It would be cleaner to use \cs{int_case:nn},
2367 % but in our case, the cases are contiguous, so it is forty times faster
2368 % to use the \cs{if_case:w} primitive. The first \cs{exp_after:wN}
2369 % expands the conditional, jumping to the correct case, the second one
2370 % expands after the resulting character to close the conditional.
2371 % Since |#1| might be an expression, and not directly a single digit,
2372 % we need to evaluate it properly, and expand the trailing \cs{fi:}.
2373 % \begin{macrocode}
2374 \cs_new:Npn \@@_to_letter:n #1
2375 {
2376 \exp_after:wN \exp_after:wN
2377 \if_case:w \@@_eval:w #1 - 10 \@@_eval_end:
2378 a
2379 \or: b
2380 \or: c
2381 \or: d
2382 \or: e
2383 \or: f
2384 \or: g
2385 \or: h
2386 \or: i
2387 \or: j
2388 \or: k
2389 \or: l
2390 \or: m
2391 \or: n
2392 \or: o
2393 \or: p
2394 \or: q
2395 \or: r
2396 \or: s
2397 \or: t
2398 \or: u
2399 \or: v
2400 \or: w
2401 \or: x
2402 \or: y
2403 \or: z
2404 \else: \int_value:w \@@_eval:w #1 \exp_after:wN \@@_eval_end:
2405 \fi:
2406 }
2407 \cs_new:Npn \@@_to_Letter:n #1
2408 {
2409 \exp_after:wN \exp_after:wN
2410 \if_case:w \@@_eval:w #1 - 10 \@@_eval_end:
2411 A
2412 \or: B
2413 \or: C
2414 \or: D
2415 \or: E
2416 \or: F
2417 \or: G
2418 \or: H
2419 \or: I
2420 \or: J
2421 \or: K
2422 \or: L
2423 \or: M
2424 \or: N
2425 \or: O
2426 \or: P
2427 \or: Q
2428 \or: R
2429 \or: S
2430 \or: T
2431 \or: U
2432 \or: V
2433 \or: W
2434 \or: X
2435 \or: Y
2436 \or: Z
2437 \else: \int_value:w \@@_eval:w #1 \exp_after:wN \@@_eval_end:
2438 \fi:
2439 }
2440 % \end{macrocode}
2441 %\end{macro}
2442 %\end{macro}
2443 %\end{macro}
2444 %
2445 % \begin{macro}{\int_to_bin:n, \int_to_hex:n, \int_to_Hex:n, \int_to_oct:n}
2446 % \UnitTested
2447 % Wrappers around the generic function.
2448 % \begin{macrocode}
2449 \cs_new:Npn \int_to_bin:n #1
2450 { \int_to_base:nn {#1} { 2 } }
2451 \cs_new:Npn \int_to_hex:n #1
2452 { \int_to_base:nn {#1} { 16 } }
2453 \cs_new:Npn \int_to_Hex:n #1
2454 { \int_to_Base:nn {#1} { 16 } }
2455 \cs_new:Npn \int_to_oct:n #1
2456 { \int_to_base:nn {#1} { 8 } }
2457 % \end{macrocode}
2458 % \end{macro}
2459 %
2460 % \begin{macro}{\int_to_roman:n, \int_to_Roman:n}
2461 % \UnitTested
2462 % \begin{macro}{\@@_to_roman:N, \@@_to_roman:N}
2463 % \begin{macro}
2464 % {
2465 % \@@_to_roman_i:w, \@@_to_roman_v:w, \@@_to_roman_x:w,
2466 % \@@_to_roman_l:w, \@@_to_roman_c:w, \@@_to_roman_d:w,
2467 % \@@_to_roman_m:w, \@@_to_roman_Q:w,
2468 % \@@_to_Roman_i:w, \@@_to_Roman_v:w, \@@_to_Roman_x:w,
2469 % \@@_to_Roman_l:w, \@@_to_Roman_c:w, \@@_to_Roman_d:w,
2470 % \@@_to_Roman_m:w, \@@_to_Roman_Q:w,
2471 % }
2472 % The \cs{@@_to_roman:w} primitive creates tokens of category
2473 % code $12$ (other). Usually, what is actually wanted is letters.
2474 % The approach here is to convert the output of the primitive into
2475 % letters using appropriate control sequence names. That keeps
2476 % everything expandable. The loop is terminated by the conversion
2477 % of the |Q|.
2478 % \begin{macrocode}
2479 \cs_new:Npn \int_to_roman:n #1
2480 {
2481 \exp_after:wN \@@_to_roman:N
2482 \@@_to_roman:w \int_eval:n {#1} Q
2483 }
2484 \cs_new:Npn \@@_to_roman:N #1
2485 {
2486 \use:c { @@_to_roman_ #1 :w }
2487 \@@_to_roman:N
2488 }
2489 \cs_new:Npn \int_to_Roman:n #1
2490 {
2491 \exp_after:wN \@@_to_Roman_aux:N
2492 \@@_to_roman:w \int_eval:n {#1} Q
2493 }
2494 \cs_new:Npn \@@_to_Roman_aux:N #1
2495 {
2496 \use:c { @@_to_Roman_ #1 :w }
2497 \@@_to_Roman_aux:N
2498 }
2499 \cs_new:Npn \@@_to_roman_i:w { i }
2500 \cs_new:Npn \@@_to_roman_v:w { v }
2501 \cs_new:Npn \@@_to_roman_x:w { x }
2502 \cs_new:Npn \@@_to_roman_l:w { l }
2503 \cs_new:Npn \@@_to_roman_c:w { c }
2504 \cs_new:Npn \@@_to_roman_d:w { d }
2505 \cs_new:Npn \@@_to_roman_m:w { m }
2506 \cs_new:Npn \@@_to_roman_Q:w #1 { }
2507 \cs_new:Npn \@@_to_Roman_i:w { I }
2508 \cs_new:Npn \@@_to_Roman_v:w { V }
2509 \cs_new:Npn \@@_to_Roman_x:w { X }
2510 \cs_new:Npn \@@_to_Roman_l:w { L }
2511 \cs_new:Npn \@@_to_Roman_c:w { C }
2512 \cs_new:Npn \@@_to_Roman_d:w { D }
2513 \cs_new:Npn \@@_to_Roman_m:w { M }
2514 \cs_new:Npn \@@_to_Roman_Q:w #1 { }
2515 % \end{macrocode}
2516 % \end{macro}
2517 % \end{macro}
2518 % \end{macro}
2519 %
2520 % \subsection{Converting from other formats to integers}
2521 %
2522 % \begin{macro}[rEXP]{\@@_pass_signs:wn, \@@_pass_signs_end:wn}
2523 % Called as \cs{@@_pass_signs:wn} \meta{signs and digits} \cs{s_@@_stop}
2524 % \Arg{code}, this function leaves in the input stream any sign it
2525 % finds, then inserts the \meta{code} before the first non-sign token
2526 % (and removes \cs{s_@@_stop}). More precisely, it deletes any~|+| and
2527 % passes any~|-| to the input stream, hence should be called in an
2528 % integer expression.
2529 % \begin{macrocode}
2530 \cs_new:Npn \@@_pass_signs:wn #1
2531 {
2532 \if:w + \if:w - \exp_not:N #1 + \fi: \exp_not:N #1
2533 \exp_after:wN \@@_pass_signs:wn
2534 \else:
2535 \exp_after:wN \@@_pass_signs_end:wn
2536 \exp_after:wN #1
2537 \fi:
2538 }
2539 \cs_new:Npn \@@_pass_signs_end:wn #1 \s_@@_stop #2 { #2 #1 }
2540 % \end{macrocode}
2541 % \end{macro}
2542 %
2543 % \begin{macro}{\int_from_alph:n}
2544 % \UnitTested
2545 % \begin{macro}{\@@_from_alph:nN, \@@_from_alph:N}
2546 % First take care of signs then loop through the input using the
2547 % \texttt{recursion} quarks. The \cs{@@_from_alph:nN} auxiliary
2548 % collects in its first argument the value obtained so far, and the
2549 % auxiliary \cs{@@_from_alph:N} converts one letter to an expression
2550 % which evaluates to the correct number.
2551 % \begin{macrocode}
2552 \cs_new:Npn \int_from_alph:n #1
2553 {
2554 \int_eval:n
2555 {
2556 \exp_after:wN \@@_pass_signs:wn \tl_to_str:n {#1}
2557 \s_@@_stop { \@@_from_alph:nN { 0 } }
2558 \q_@@_recursion_tail \q_@@_recursion_stop
2559 }
2560 }
2561 \cs_new:Npn \@@_from_alph:nN #1#2
2562 {
2563 \@@_if_recursion_tail_stop_do:Nn #2 {#1}
2564 \exp_args:Nf \@@_from_alph:nN
2565 { \int_eval:n { #1 * 26 + \@@_from_alph:N #2 } }
2566 }
2567 \cs_new:Npn \@@_from_alph:N #1
2568 { `#1 - \int_compare:nNnTF { `#1 } < { 91 } { 64 } { 96 } }
2569 % \end{macrocode}
2570 % \end{macro}
2571 % \end{macro}
2572 %
2573 % \begin{macro}{\int_from_base:nn}
2574 % \UnitTested
2575 % \begin{macro}{\@@_from_base:nnN, \@@_from_base:N}
2576 % Leave the signs into the integer expression, then loop through
2577 % characters, collecting the value found so far in the first argument
2578 % of \cs{@@_from_base:nnN}. To convert a single character,
2579 % \cs{@@_from_base:N} checks first for digits, then distinguishes
2580 % lower from upper case letters, turning them into the appropriate
2581 % number. Note that this auxiliary does not use \cs{int_eval:n},
2582 % hence is not safe for general use.
2583 % \begin{macrocode}
2584 \cs_new:Npn \int_from_base:nn #1#2
2585 {
2586 \int_eval:n
2587 {
2588 \exp_after:wN \@@_pass_signs:wn \tl_to_str:n {#1}
2589 \s_@@_stop { \@@_from_base:nnN { 0 } {#2} }
2590 \q_@@_recursion_tail \q_@@_recursion_stop
2591 }
2592 }
2593 \cs_new:Npn \@@_from_base:nnN #1#2#3
2594 {
2595 \@@_if_recursion_tail_stop_do:Nn #3 {#1}
2596 \exp_args:Nf \@@_from_base:nnN
2597 { \int_eval:n { #1 * #2 + \@@_from_base:N #3 } }
2598 {#2}
2599 }
2600 \cs_new:Npn \@@_from_base:N #1
2601 {
2602 \int_compare:nNnTF { `#1 } < { 58 }
2603 {#1}
2604 { `#1 - \int_compare:nNnTF { `#1 } < { 91 } { 55 } { 87 } }
2605 }
2606 % \end{macrocode}
2607 % \end{macro}
2608 % \end{macro}
2609 %
2610 % \begin{macro}{\int_from_bin:n, \int_from_hex:n, \int_from_oct:n}
2611 % \UnitTested
2612 % Wrappers around the generic function.
2613 % \begin{macrocode}
2614 \cs_new:Npn \int_from_bin:n #1
2615 { \int_from_base:nn {#1} { 2 } }
2616 \cs_new:Npn \int_from_hex:n #1
2617 { \int_from_base:nn {#1} { 16 } }
2618 \cs_new:Npn \int_from_oct:n #1
2619 { \int_from_base:nn {#1} { 8 } }
2620 % \end{macrocode}
2621 % \end{macro}
2622 %
2623 % \begin{variable}
2624 % {
2625 % \c_@@_from_roman_i_int, \c_@@_from_roman_v_int,
2626 % \c_@@_from_roman_x_int, \c_@@_from_roman_l_int,
2627 % \c_@@_from_roman_c_int, \c_@@_from_roman_d_int,
2628 % \c_@@_from_roman_m_int,
2629 % \c_@@_from_roman_I_int, \c_@@_from_roman_V_int,
2630 % \c_@@_from_roman_X_int, \c_@@_from_roman_L_int,
2631 % \c_@@_from_roman_C_int, \c_@@_from_roman_D_int,
2632 % \c_@@_from_roman_M_int
2633 % }
2634 % Constants used to convert from Roman numerals to integers.
2635 % \begin{macrocode}
2636 \int_const:cn { c_@@_from_roman_i_int } { 1 }
2637 \int_const:cn { c_@@_from_roman_v_int } { 5 }
2638 \int_const:cn { c_@@_from_roman_x_int } { 10 }
2639 \int_const:cn { c_@@_from_roman_l_int } { 50 }
2640 \int_const:cn { c_@@_from_roman_c_int } { 100 }
2641 \int_const:cn { c_@@_from_roman_d_int } { 500 }
2642 \int_const:cn { c_@@_from_roman_m_int } { 1000 }
2643 \int_const:cn { c_@@_from_roman_I_int } { 1 }
2644 \int_const:cn { c_@@_from_roman_V_int } { 5 }
2645 \int_const:cn { c_@@_from_roman_X_int } { 10 }
2646 \int_const:cn { c_@@_from_roman_L_int } { 50 }
2647 \int_const:cn { c_@@_from_roman_C_int } { 100 }
2648 \int_const:cn { c_@@_from_roman_D_int } { 500 }
2649 \int_const:cn { c_@@_from_roman_M_int } { 1000 }
2650 % \end{macrocode}
2651 % \end{variable}
2652 %
2653 % \begin{macro}{\int_from_roman:n}
2654 % \UnitTested
2655 % \TestMissing{boundary cases / wrong input?}
2656 % \begin{macro}{\@@_from_roman:NN}
2657 % \begin{macro}{\@@_from_roman_error:w}
2658 % The method here is to iterate through the input, finding the
2659 % appropriate value for each letter and building up a sum. This is
2660 % then evaluated by \TeX{}. If any unknown letter is found, skip to
2661 % the closing parenthesis and insert |*0-1| afterwards, to replace the
2662 % value by $-1$.
2663 % \begin{macrocode}
2664 \cs_new:Npn \int_from_roman:n #1
2665 {
2666 \int_eval:n
2667 {
2668 (
2669 0
2670 \exp_after:wN \@@_from_roman:NN \tl_to_str:n {#1}
2671 \q_@@_recursion_tail \q_@@_recursion_tail \q_@@_recursion_stop
2672 )
2673 }
2674 }
2675 \cs_new:Npn \@@_from_roman:NN #1#2
2676 {
2677 \@@_if_recursion_tail_stop:N #1
2678 \int_if_exist:cF { c_@@_from_roman_ #1 _int }
2679 { \@@_from_roman_error:w }
2680 \@@_if_recursion_tail_stop_do:Nn #2
2681 { + \use:c { c_@@_from_roman_ #1 _int } }
2682 \int_if_exist:cF { c_@@_from_roman_ #2 _int }
2683 { \@@_from_roman_error:w }
2684 \int_compare:nNnTF
2685 { \use:c { c_@@_from_roman_ #1 _int } }
2686 <
2687 { \use:c { c_@@_from_roman_ #2 _int } }
2688 {
2689 + \use:c { c_@@_from_roman_ #2 _int }
2690 - \use:c { c_@@_from_roman_ #1 _int }
2691 \@@_from_roman:NN
2692 }
2693 {
2694 + \use:c { c_@@_from_roman_ #1 _int }
2695 \@@_from_roman:NN #2
2696 }
2697 }
2698 \cs_new:Npn \@@_from_roman_error:w #1 \q_@@_recursion_stop #2
2699 { #2 * 0 - 1 }
2700 % \end{macrocode}
2701 % \end{macro}
2702 % \end{macro}
2703 % \end{macro}
2704 %
2705 % \subsection{Viewing integer}
2706 %
2707 % \begin{macro}{\int_show:N, \int_show:c}
2708 % \UnitTested
2709 % \begin{macro}{\@@_show:nN}
2710 % Diagnostics.
2711 % \begin{macrocode}
2712 \cs_new_eq:NN \int_show:N \__kernel_register_show:N
2713 \cs_generate_variant:Nn \int_show:N { c }
2714 % \end{macrocode}
2715 % \end{macro}
2716 % \end{macro}
2717 %
2718 % \begin{macro}{\int_show:n}
2719 % \UnitTested
2720 % We don't use the \TeX{} primitive \tn{showthe} to show integer
2721 % expressions: this gives a more unified output.
2722 % \begin{macrocode}
2723 \cs_new_protected:Npn \int_show:n
2724 { \__kernel_msg_show_eval:Nn \int_eval:n }
2725 % \end{macrocode}
2726 % \end{macro}
2727 %
2728 % \begin{macro}{\int_log:N, \int_log:c}
2729 % Diagnostics.
2730 % \begin{macrocode}
2731 \cs_new_eq:NN \int_log:N \__kernel_register_log:N
2732 \cs_generate_variant:Nn \int_log:N { c }
2733 % \end{macrocode}
2734 % \end{macro}
2735 %
2736 % \begin{macro}{\int_log:n}
2737 % Similar to \cs{int_show:n}.
2738 % \begin{macrocode}
2739 \cs_new_protected:Npn \int_log:n
2740 { \__kernel_msg_log_eval:Nn \int_eval:n }
2741 % \end{macrocode}
2742 % \end{macro}
2743 %
2744 %\subsection{Random integers}
2745 %
2746 % \begin{macro}{\int_rand:nn}
2747 % Defined in \pkg{l3fp-random}.
2748 % \end{macro}
2749 %
2750 % \subsection{Constant integers}
2751 %
2752 % \begin{variable}{\c_zero_int, \c_one_int}
2753 % The zero is defined in \pkg{l3basics}.
2754 % \begin{macrocode}
2755 \int_const:Nn \c_one_int { 1 }
2756 % \end{macrocode}
2757 % \end{variable}
2758 %
2759 % \begin{variable}{\c_max_int}
2760 % The largest number allowed is $2^{31}-1$
2761 % \begin{macrocode}
2762 \int_const:Nn \c_max_int { 2 147 483 647 }
2763 % \end{macrocode}
2764 % \end{variable}
2765 %
2766 % \begin{variable}{\c_max_char_int}
2767 % The largest character code is $1114111$ (hexadecimal |10FFFF|) in
2768 % \XeTeX{} and \LuaTeX{} and $255$ in other engines. In many places
2769 % \pTeX{} and \upTeX{} support larger character codes but for instance
2770 % the values of \tn{lccode} are restricted to $[0,255]$.
2771 % \begin{macrocode}
2772 \int_const:Nn \c_max_char_int
2773 {
2774 \if_int_odd:w 0
2775 \cs_if_exist:NT \tex_luatexversion:D { 1 }
2776 \cs_if_exist:NT \tex_XeTeXversion:D { 1 } ~
2777 "10FFFF
2778 \else:
2779 "FF
2780 \fi:
2781 }
2782 % \end{macrocode}
2783 % \end{variable}
2784 %
2785 % \begin{variable}{\c_max_intarray_int}
2786 % The largest number allowed is $2^{30}-1$.
2787 % \begin{macrocode}
2788 \int_const:Nn \c_max_intarrray_int { 1 073 741 823 }
2789 % \end{macrocode}
2790 % \end{variable}
2791 %
2792 % \subsection{Scratch integers}
2793 %
2794 % \begin{variable}{\l_tmpa_int, \l_tmpb_int}
2795 % \begin{variable}{\g_tmpa_int, \g_tmpb_int}
2796 % We provide two local and two global scratch counters, maybe we
2797 % need more or less.
2798 % \begin{macrocode}
2799 \int_new:N \l_tmpa_int
2800 \int_new:N \l_tmpb_int
2801 \int_new:N \g_tmpa_int
2802 \int_new:N \g_tmpb_int
2803 % \end{macrocode}
2804 % \end{variable}
2805 % \end{variable}
2806 %
2807 % \subsection{Integers for earlier modules}
2808 %
2809 %<@@=seq>
2810 %
2811 % \begin{variable}{\l_@@_tmpa_int, \l_@@_tmpb_int}
2812 % \begin{macrocode}
2813 \int_new:N \l_@@_tmpa_int
2814 \int_new:N \l_@@_tmpb_int
2815 % \end{macrocode}
2816 % \end{variable}
2817 %
2818 % \begin{macrocode}
2819 %</code>
2820 % \end{macrocode}
2821 %
2822 % \end{implementation}
2823 %
2824 % \PrintIndex