Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3str-format.dtx
4 %
5 % Copyright (C) 2012-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]{l3doc}
27 \usepackage{amsmath}
28 \begin{document}
29 \DocInput{\jobname.dtx}
30 \end{document}
31 %</driver>
32 % \fi
33 %
34 %
35 % \title{^^A
36 % The \pkg{l3str-format} package\\ Formatting strings of characters^^A
37 % }
38 %
39 % \author{^^A
40 % The \LaTeX{} Project\thanks
41 % {^^A
42 % E-mail:
43 % \href{mailto:[email protected]}
44 % {[email protected]}^^A
45 % }^^A
46 % }
47 %
48 % \date{Released 2026-05-15}
49 %
50 % \maketitle
51 %
52 % \begin{documentation}
53 %
54 % \section{Format specifications}
55 %
56 % \label{sec:l3str-format:spec}
57 %
58 % \LaTeX3 has the notion of a string \meta{format}.
59 % The syntax follows that of Python's \texttt{format} built-in function.
60 % A \meta{format specification} is a string of the form
61 % \begin{equation*}
62 % \meta{format specification} = [[\meta{fill}]\meta{alignment}]
63 % [\meta{sign}] [\meta{width}] [.\meta{precision}] [\meta{style}]
64 % \end{equation*}
65 % where each $[\ldots]$ denotes an independent optional part.
66 % \begin{itemize}
67 % \item \meta{fill} can be any character: it is assumed to be present
68 % whenever the second character of the \meta{format specification}
69 % is a valid \meta{alignment} character.
70 % \item \meta{alignment} can be |<|~(left alignment), |>|~(right
71 % alignment), |^|~(centering), or |=|~(for numeric types only).
72 % \item \meta{sign} is allowed for numeric types; it can be |+|~(show
73 % a sign for positive and negative numbers), |-|~(only put a sign
74 % for negative numbers), or a space~(show a space or a~|-|).
75 % \item \meta{width} is the minimum number of characters of the
76 % result: if the result is naturally shorter than this \meta{width},
77 % then it is padded with copies of the character \meta{fill}, with a
78 % position depending on the choice of \meta{alignment}. If the
79 % result is naturally longer, it is not truncated.
80 % \item \meta{precision}, whose presence is indicated by a period,
81 % can have different meanings depending on the type.
82 % \item \meta{style} is one character, which controls how the given
83 % data should be formatted. The list of allowed \meta{styles}
84 % depends on the type.
85 % \end{itemize}
86 % The choice of \meta{alignment} |=| is only valid for numeric types: in
87 % this case the padding is inserted between the sign and the rest of the
88 % number.
89 %
90 % Details of the individual formatting functions are given in the relevant
91 % modules, e.g.~\pkg{l3fp} for \cs{fp_format:nn}.
92 %
93 % \end{documentation}
94 %
95 % \begin{implementation}
96 %
97 % \section{\pkg{l3str-format} implementation}
98 %
99 % \begin{macrocode}
100 %<*code>
101 % \end{macrocode}
102 %
103 % \begin{macrocode}
104 %<@@=str>
105 % \end{macrocode}
106 %
107 % \subsection{Helpers}
108 %
109 % \begin{macro}[EXP]{\use:nf, \use:fnf}
110 % A simple variant.
111 % \begin{macrocode}
112 \cs_generate_variant:Nn \use:nn { nf }
113 \cs_generate_variant:Nn \use:nnn { fnf }
114 % \end{macrocode}
115 % \end{macro}
116 %
117 % \begin{macro}[EXP]{\tl_to_str:f}
118 % A simple variant.
119 % \begin{macrocode}
120 \cs_generate_variant:Nn \tl_to_str:n { f }
121 % \end{macrocode}
122 % \end{macro}
123 %
124 % \begin{macro}[EXP]{\@@_format_if_digit:NTF}
125 % Here we expect |#1| to be a character with category other, or
126 % \cs{s_@@_stop}.
127 % \begin{macrocode}
128 \prg_new_conditional:Npnn \@@_format_if_digit:N #1 { TF }
129 {
130 \if_int_compare:w 9 < 1 #1 \exp_stop_f:
131 \prg_return_true: \else: \prg_return_false: \fi:
132 }
133 % \end{macrocode}
134 % \end{macro}
135 %
136 % \begin{macro}[EXP]
137 % {\@@_format_put:nw, \@@_format_put:ow, \@@_format_put:fw}
138 % Put |#1| after an \cs{s_@@_stop} delimiter.
139 % \begin{macrocode}
140 \cs_new:Npn \@@_format_put:nw #1 #2 \s_@@_stop { #2 \s_@@_stop #1 }
141 \cs_generate_variant:Nn \@@_format_put:nw { o , f }
142 % \end{macrocode}
143 % \end{macro}
144 %
145 % \begin{macro}[EXP, TF]{\@@_format_if_in:nN}
146 % \begin{macro}[EXP]{\@@_format_if_in_aux:NN}
147 % A copy of \cs{@@_if_contains_char:nNTF} to avoid relying on
148 % this weird internal string function.
149 % \begin{macrocode}
150 \prg_new_conditional:Npnn \@@_format_if_in:nN #1#2 { TF }
151 {
152 \@@_format_if_in_aux:NN #2 #1
153 { #2 \prg_return_false: \exp_after:wN \prg_break: \else: }
154 \prg_break_point:
155 }
156 \cs_new:Npn \@@_format_if_in_aux:NN #1#2
157 {
158 \if_charcode:w #1 #2
159 \prg_return_true:
160 \exp_after:wN \prg_break:
161 \fi:
162 \@@_format_if_in_aux:NN #1
163 }
164 % \end{macrocode}
165 % \end{macro}
166 % \end{macro}
167 %
168 % \subsection{Parsing a format specification}
169 %
170 % \begin{macro}[EXP]{\@@_format_parse:n}
171 % \begin{macro}[EXP]
172 % {
173 % \@@_format_parse_auxi:NN,
174 % \@@_format_parse_auxii:nN,
175 % \@@_format_parse_auxiii:nN,
176 % \@@_format_parse_auxiv:nwN,
177 % \@@_format_parse_auxv:nN,
178 % \@@_format_parse_auxvi:nwN,
179 % \@@_format_parse_auxvii:nN,
180 % \@@_format_parse_end:nwn,
181 % }
182 % The goal is to parse
183 % \begin{equation*}
184 % \meta{format specification} = [[\meta{fill}]\meta{alignment}]
185 % [\meta{sign}] [\meta{width}] [.\meta{precision}] [\meta{style}]
186 % \end{equation*}
187 % \begin{macrocode}
188 \cs_new:Npn \@@_format_parse:n #1
189 {
190 \exp_last_unbraced:Nf \@@_format_parse_auxi:NN
191 { \__kernel_str_to_other:n {#1} } \s_@@_stop \s_@@_stop {#1}
192 }
193 \cs_new:Npe \@@_format_parse_auxi:NN #1#2
194 {
195 \exp_not:N \@@_format_if_in:nNTF { < > = ^ } #2
196 { \exp_not:N \@@_format_parse_auxiii:nN { #1 #2 } }
197 {
198 \exp_not:N \@@_format_parse_auxii:nN
199 { \c_catcode_other_space_tl } #1 #2
200 }
201 }
202 \cs_new:Npn \@@_format_parse_auxii:nN #1#2
203 {
204 \@@_format_if_in:nNTF { < > = ^ } #2
205 { \@@_format_parse_auxiii:nN { #1 #2 } }
206 { \@@_format_parse_auxiii:nN { #1 ? } #2 }
207 }
208 \cs_new:Npe \@@_format_parse_auxiii:nN #1#2
209 {
210 \exp_not:N \@@_format_if_in:nNTF
211 { + - \c_catcode_other_space_tl }
212 #2
213 { \exp_not:N \@@_format_parse_auxiv:nwN { #1 #2 } ; }
214 { \exp_not:N \@@_format_parse_auxiv:nwN { #1 ? } ; #2 }
215 }
216 \cs_new:Npn \@@_format_parse_auxiv:nwN #1#2; #3
217 {
218 \@@_format_if_digit:NTF #3
219 { \@@_format_parse_auxiv:nwN {#1} #2 #3 ; }
220 { \@@_format_parse_auxv:nN { #1 {#2} } #3 }
221 }
222 \cs_new:Npn \@@_format_parse_auxv:nN #1#2
223 {
224 \token_if_eq_charcode:NNTF . #2
225 { \@@_format_parse_auxvi:nwN {#1} 0 ; }
226 { \@@_format_parse_auxvii:nN { #1 { } } #2 }
227 }
228 \cs_new:Npn \@@_format_parse_auxvi:nwN #1#2; #3
229 {
230 \@@_format_if_digit:NTF #3
231 { \@@_format_parse_auxvi:nwN {#1} #2 #3 ; }
232 { \@@_format_parse_auxvii:nN { #1 {#2} } #3 }
233 }
234 \cs_new:Npn \@@_format_parse_auxvii:nN #1#2
235 {
236 \token_if_eq_meaning:NNTF \s_@@_stop #2
237 { \@@_format_parse_end:nwn { #1 ? } #2 }
238 { \@@_format_parse_end:nwn { #1 #2 } }
239 }
240 \cs_new:Npn \@@_format_parse_end:nwn #1 #2 \s_@@_stop \s_@@_stop #3
241 {
242 \tl_if_empty:nF {#2}
243 { \msg_expandable_error:nnn { str } { invalid-format } {#3} }
244 #1
245 }
246 % \end{macrocode}
247 % \end{macro}
248 % \end{macro}
249 %
250 % \subsection{Alignment}
251 %
252 % The $4$ functions in this section receive an \meta{body}, a
253 % \meta{sign}, a \meta{width} and a \meta{fill} character (exactly one
254 % character). For non-numeric types, the \meta{sign} is empty and the
255 % \meta{body} is the (other) string we want to format. For numeric
256 % types, we wish to format \meta{sign} \meta{body} (both are other
257 % strings). The alignment types |<|, |>| and |^| keep \meta{sign} and
258 % \meta{body} together. The |=| alignment type, however, inserts the
259 % padding between the \meta{sign} and the \meta{body}, hence the need to
260 % keep those separate.
261 %
262 % \begin{macro}[EXP]{\@@_format_align_<:nnnN}
263 % \begin{quote}
264 % \cs{@@_format_align_<:nnnN} \Arg{body} \Arg{sign} \Arg{width}
265 % \meta{fill}
266 % \end{quote}
267 % Aligning \enquote{\meta{sign} \meta{body}} to the left
268 % entails appending |#4| the correct number of times. Then convert
269 % the result to a string.
270 % \begin{macrocode}
271 \cs_new:cpn { @@_format_align_<:nnnN } #1#2#3#4
272 {
273 \use:nf { #2 #1 }
274 {
275 \prg_replicate:nn
276 { \int_max:nn { #3 - \@@_count:n { #2 #1 } } { 0 } }
277 {#4}
278 }
279 }
280 % \end{macrocode}
281 % \end{macro}
282 %
283 % \begin{macro}[EXP]{\@@_format_align_>:nnnN}
284 % \begin{quote}
285 % \cs{@@_format_align_>:nnnN} \Arg{body} \Arg{sign} \Arg{width}
286 % \meta{fill}
287 % \end{quote}
288 % Aligning an \enquote{\meta{sign} \meta{body}} to the right
289 % entails prepending |#4| the correct number of times. Then convert
290 % the result to a string.
291 % \begin{macrocode}
292 \cs_new:cpn { @@_format_align_>:nnnN } #1#2#3#4
293 {
294 \prg_replicate:nn
295 { \int_max:nn { #3 - \@@_count:n { #2 #1 } } { 0 } }
296 {#4}
297 #2 #1
298 }
299 % \end{macrocode}
300 % \end{macro}
301 %
302 % \begingroup\catcode`\^=12
303 % \begin{macro}[EXP]{\@@_format_align_^:nnnN}
304 % \begin{quote}
305 % \cs{@@_format_align_^:nnnN} \Arg{body} \Arg{sign} \Arg{width}
306 % \meta{fill}
307 % \end{quote}
308 % Centering \enquote{\meta{sign} \meta{body}} entails
309 % prepending and appending |#4| the correct number of times. If the
310 % number of |#4| to be added is odd, we add one more after than
311 % before.
312 % \begin{macrocode}
313 \cs_new:cpn { @@_format_align_^:nnnN } #1#2#3#4
314 {
315 \use:fnf
316 {
317 \prg_replicate:nn
318 {
319 \int_max:nn { 0 }
320 { #3 - \@@_count:n { #2 #1 } - 1 }
321 / 2
322 }
323 {#4}
324 }
325 { #2 #1 }
326 {
327 \prg_replicate:nn
328 {
329 \int_max:nn { 0 }
330 { #3 - \@@_count:n { #2 #1 } }
331 / 2
332 }
333 {#4}
334 }
335 }
336 % \end{macrocode}
337 % \end{macro}
338 % \endgroup
339 %
340 % \begin{macro}[EXP]{\@@_format_align_=:nnnN}
341 % \begin{quote}
342 % \cs{@@_format_align_=:nnnN} \Arg{body} \Arg{sign} \Arg{width}
343 % \meta{fill}
344 % \end{quote}
345 % The special numeric alignment |=| means that we insert the
346 % appropriate number of copies of |#4| between the \meta{sign} and the
347 % \meta{body}. Then convert the result to a string.
348 % \begin{macrocode}
349 \cs_new:cpn { @@_format_align_=:nnnN } #1#2#3#4
350 {
351 \use:nf {#2}
352 {
353 \prg_replicate:nn
354 { \int_max:nn { #3 - \@@_count:n { #2 #1 } } { 0 } }
355 {#4}
356 }
357 #1
358 }
359 % \end{macrocode}
360 % \end{macro}
361 %
362 %
363 % \subsection{Formatting token lists}
364 %
365 % \begin{macro}[EXP]{\tl_format:Nn, \tl_format:cn, \tl_format:nn}
366 % Call \cs{@@_format_tl:NNNnnNn} to read the parsed \meta{format
367 % specification}. Then convert the result to a string.
368 % \begin{macrocode}
369 \cs_new:Npn \tl_format:Nn { \exp_args:No \tl_format:nn }
370 \cs_generate_variant:Nn \tl_format:Nn { c }
371 \cs_new:Npn \tl_format:nn #1#2
372 {
373 \tl_to_str:f
374 {
375 \exp_last_unbraced:Nf \@@_format_tl:NNNnnNn
376 { \@@_format_parse:n {#2} }
377 {#1}
378 }
379 }
380 % \end{macrocode}
381 % \end{macro}
382 %
383 % \begin{macro}[EXP]{\@@_format_tl:NNNnnNn}
384 % \begin{quote}
385 % \cs{@@_format_tl:NNNnnNn} \meta{fill} \meta{alignment} \meta{sign}
386 % \Arg{width} \Arg{precision} \meta{style} \Arg{token list}
387 % \end{quote}
388 % First check that the \meta{alignment} is not |=|, and set the
389 % default alignment |?| to |<|. Place the modified information after
390 % a trailing \cs{s_@@_stop} for later retrieval. Then check that there
391 % was no \meta{sign}. The width will be useful later, store it after
392 % \cs{s_@@_stop}. Afterwards, store the precision, and the function
393 % \cs{@@_range:nnn} that will be used to extract the first
394 % |#5| characters of the string.
395 % There is a need to use the internal function, as otherwise
396 % leading spaces would get stripped by |f|-expansion. Finally, check
397 % that the \meta{style} is |?| or |s|.
398 % \begin{macrocode}
399 \cs_new:Npn \@@_format_tl:NNNnnNn #1#2#3#4#5#6
400 {
401 \token_if_eq_charcode:NNTF #2 =
402 {
403 \msg_expandable_error:nnnn
404 { str } { invalid-align-format } {#2} {tl}
405 \@@_format_put:nw { #1 < }
406 }
407 {
408 \token_if_eq_charcode:NNTF #2 ?
409 { \@@_format_put:nw { #1 < } }
410 { \@@_format_put:nw { #1 #2 } }
411 }
412 \token_if_eq_charcode:NNF #3 ?
413 {
414 \msg_expandable_error:nnnn
415 { str } { invalid-sign-format } {#3} {tl}
416 }
417 \@@_format_put:nw { {#4} }
418 \tl_if_empty:nTF {#5}
419 { \@@_format_put:nw { \@@_range:nnn { {1} {-1} } } }
420 { \@@_format_put:nw { \@@_range:nnn { {1} {#5} } } }
421 \token_if_eq_charcode:NNF #6 s
422 {
423 \token_if_eq_charcode:NNF #6 ?
424 {
425 \msg_expandable_error:nnnn
426 { str } { invalid-style-format } {#6} {tl}
427 }
428 }
429 \@@_format_tl_s:NNnnNNn
430 \s_@@_stop
431 }
432 % \end{macrocode}
433 % \end{macro}
434 %
435 % \begin{macro}[EXP]{\@@_format_tl_s:NNnnNNn}
436 % \begin{quote}
437 % \cs{@@_format_tl_s:NNnnNNn} \cs{s_@@_stop} \meta{function}
438 % \Arg{arguments} \Arg{width} \meta{fill} \meta{alignment}
439 % \Arg{token list}
440 % \end{quote}
441 % The \meta{function} and \meta{arguments} are built in such a way
442 % that |f|-expanding \meta{function} \Arg{other string}
443 % \meta{arguments} yields the piece of the \meta{other string} that we
444 % want to output. The \meta{other string} is built from the
445 % \meta{token list} by |f|-expanding \cs{__kernel_str_to_other:n}.
446 % \begin{macrocode}
447 \cs_new:Npn \@@_format_tl_s:NNnnNNn #1#2#3#4#5#6#7
448 {
449 \exp_args:Nc \exp_args:Nf
450 { @@_format_align_#6:nnnN }
451 { \exp_args:Nf #2 { \__kernel_str_to_other:n {#7} } #3 }
452 { }
453 {#4} #5
454 }
455 % \end{macrocode}
456 % \end{macro}
457 %
458 % \subsection{Formatting sequences}
459 %
460 % \begin{macro}[EXP]{\seq_format:Nn, \seq_format:cn}
461 % Each item is formatted as a token list according to the
462 % specification. First parse the format and expand the sequence, then
463 % loop through the items. Eventually, convert to a string.
464 % \begin{macrocode}
465 \cs_new:Npn \seq_format:Nn #1#2
466 {
467 \tl_to_str:f
468 {
469 \@@_format_seq:ff
470 { \exp_after:wN \use_i:nn \exp_after:wN \exp_stop_f: #1 }
471 { \@@_format_parse:n {#2} }
472 }
473 }
474 \cs_generate_variant:Nn \seq_format:Nn { c }
475 % \end{macrocode}
476 % \end{macro}
477 %
478 % \begin{macro}[EXP]{\@@_format_seq:nn, \@@_format_seq:ff}
479 % The first argument is the contents of a \texttt{seq} variable. The
480 % second is a parsed \meta{format specification}. Set up the loop.
481 % \begin{macrocode}
482 \cs_new:Npn \@@_format_seq:nn #1#2
483 {
484 \@@_format_seq_loop:nnNn { } {#2}
485 #1
486 { ? \@@_format_seq_end:w } { }
487 }
488 \cs_generate_variant:Nn \@@_format_seq:nn { ff }
489 % \end{macrocode}
490 % \end{macro}
491 %
492 % \begin{macro}[EXP]{\@@_format_seq_loop:nnNn}
493 % \begin{quote}
494 % \cs{@@_format_seq_loop:nnNn} \Arg{done} \Arg{parsed format}
495 % \cs{__seq_item:n} \Arg{item}
496 % \end{quote}
497 % The first argument is the result of formatting the items read so
498 % far. The third argument is a single token (\cs{__seq_item:n}),
499 % until we reach the end of the sequence, where |\use_none:n #3| ends
500 % the loop.
501 % \begin{macrocode}
502 \cs_new:Npn \@@_format_seq_loop:nnNn #1#2#3#4
503 {
504 \use_none:n #3
505 \exp_args:Nf \@@_format_seq_loop:nnNn
506 { \use:nf {#1} { \@@_format_tl:NNNnnNn #2 {#4} } }
507 {#2}
508 }
509 % \end{macrocode}
510 % \end{macro}
511 %
512 % \begin{macro}[EXP]{\@@_format_seq_end:w}
513 % Pick the right piece in the loop above.
514 % \begin{macrocode}
515 \cs_new:Npn \@@_format_seq_end:w #1#2#3#4 { \use_ii:nnn #3 }
516 % \end{macrocode}
517 % \end{macro}
518 %
519 % \subsection{Formatting integers}
520 %
521 % \begin{macro}[EXP]{\int_format:nn}
522 % Evaluate the first argument and feed it to \cs{@@_format_int:nn}.
523 % \begin{macrocode}
524 \cs_new:Npn \int_format:nn #1
525 { \exp_args:Nf \@@_format_int:nn { \int_eval:n {#1} } }
526 % \end{macrocode}
527 % \end{macro}
528 %
529 % \begin{macro}[EXP]{\@@_format_int:nn}
530 % Parse the \meta{format specification} and feed it to
531 % \cs{@@_format_int:NNNnnNn}. Then convert the result to a string
532 % \begin{macrocode}
533 \cs_new:Npn \@@_format_int:nn #1#2
534 {
535 \tl_to_str:f
536 {
537 \exp_last_unbraced:Nf \@@_format_int:NNNnnNn
538 { \@@_format_parse:n {#2} }
539 {#1}
540 }
541 }
542 % \end{macrocode}
543 % \end{macro}
544 %
545 % \begin{macro}[EXP]{\@@_format_int:NNNnnNn}
546 % \begin{quote}
547 % \cs{@@_format_int:NNNnnNn} \meta{fill} \meta{alignment}
548 % \meta{sign} \Arg{width} \Arg{precision} \meta{style} \Arg{integer}
549 % \end{quote}
550 % First set the
551 % default alignment |?| to |>|. Place the modified information after
552 % a trailing \cs{s_@@_stop} for later retrieval. Then check the
553 % \meta{sign}: if the integer is negative, always put~|-|. Otherwise,
554 % if the format's \meta{sign} is |~|, put a space (with category
555 % \enquote{other}); if it is~|+| put |+|; if it is |-| (default), put
556 % nothing, represented as a brace group. The width |#4| will be
557 % useful later, store it after \cs{s_@@_stop}. Afterwards, check that
558 % the \meta{precision} was absent. Finally, dispatch depending on the
559 % \meta{style}.
560 % \begin{macrocode}
561 \cs_new:Npn \@@_format_int:NNNnnNn #1#2#3#4#5#6#7
562 {
563 \token_if_eq_charcode:NNTF #2 ?
564 { \@@_format_put:nw { #1 > } }
565 { \@@_format_put:nw { #1 #2 } }
566 \int_compare:nNnTF {#7} < 0
567 { \@@_format_put:nw { - } }
568 {
569 \str_case:nnF {#3}
570 {
571 { ~ } { \@@_format_put:ow { \c_catcode_other_space_tl } }
572 { + } { \@@_format_put:nw { + } }
573 }
574 { \@@_format_put:nw { { } } }
575 }
576 \@@_format_put:nw { {#4} }
577 \tl_if_empty:nF {#5}
578 {
579 \msg_expandable_error:nnnn
580 { str } { invalid-precision-format } {#5} {int}
581 }
582 \str_case:nnF {#6}
583 {
584 { ? } { \@@_format_int:NwnnNNn \use:n }
585 { d } { \@@_format_int:NwnnNNn \use:n }
586 { b } { \@@_format_int:NwnnNNn \int_to_bin:n }
587 { o } { \@@_format_int:NwnnNNn \int_to_oct:n }
588 { X } { \@@_format_int:NwnnNNn \int_to_Hex:n }
589 }
590 {
591 \msg_expandable_error:nnnn
592 { str } { invalid-style-format } {#6} { int }
593 \@@_format_int:NwnnNNn \use:n
594 }
595 \s_@@_stop {#7}
596 }
597 % \end{macrocode}
598 % \end{macro}
599 %
600 % \begin{macro}[EXP]{\@@_format_int:NwnnNNn}
601 % \begin{quote}
602 % \cs{@@_format_int:NwnnNNn} \meta{function} \cs{s_@@_stop}
603 % \Arg{width} \Arg{sign} \meta{fill} \meta{alignment} \Arg{integer}
604 % \end{quote}
605 % Use the |format_align| function corresponding to the
606 % \meta{alignment}, with the following arguments:
607 % \begin{itemize}
608 % \item the string formed by combining the sign |#4| with the result
609 % of converting the absolute value of the \meta{integer} |#7|
610 % according to the conversion function |#1|;
611 % \item the \meta{width};
612 % \item the \meta{fill} character.
613 % \end{itemize}
614 % \begin{macrocode}
615 \cs_new:Npn \@@_format_int:NwnnNNn #1#2 \s_@@_stop #3#4#5#6#7
616 {
617 \exp_args:Nc \exp_args:Nf
618 { @@_format_align_#6:nnnN }
619 { #1 { \int_abs:n {#7} } }
620 {#4}
621 {#3} #5
622 }
623 % \end{macrocode}
624 % ^^A todo: note similarity with \@@_format_tl_s:NNnnNNn
625 % \end{macro}
626 %
627 % \subsection{Formatting floating points}
628 %
629 % \begin{macro}[EXP]{\fp_format:nn}
630 % Evaluate the first argument to a floating point number, and feed it
631 % to \cs{@@_format_fp:nn}. It would be more efficient to use internal
632 % floating point numbers but efficiency is not essential and the code
633 % is cleaner this way.
634 % \begin{macrocode}
635 \cs_new:Npn \fp_format:nn #1
636 { \exp_args:Nf \@@_format_fp:nn { \fp_to_tl:n {#1} } }
637 % \end{macrocode}
638 % \end{macro}
639 %
640 % \begin{macro}[EXP]{\@@_format_fp:nn}
641 % Parse the \meta{format specification} and feed it to
642 % \cs{@@_format_fp:NNNnnNn}. Then convert the result to a string
643 % \begin{macrocode}
644 \cs_new:Npn \@@_format_fp:nn #1#2
645 {
646 \tl_to_str:f
647 {
648 \exp_last_unbraced:Nf \@@_format_fp:NNNnnNn
649 { \@@_format_parse:n {#2} }
650 {#1}
651 }
652 }
653 % \end{macrocode}
654 % \end{macro}
655 %
656 % \begin{macro}[EXP]{\@@_format_fp:NNNnnNn}
657 % \begin{quote}
658 % \cs{@@_format_fp:NNNnnNn} \meta{fill} \meta{alignment}
659 % \meta{format sign} \Arg{width} \Arg{precision} \meta{style}
660 % \Arg{floating point}
661 % \end{quote}
662 % First set the default alignment |?| to |>|. Place the modified
663 % information after a trailing \cs{s_@@_stop} for later retrieval. Then
664 % check the \meta{format sign} and the \meta{fp sign}: if the floating
665 % point is negative, always put~|-|. Otherwise (including
666 % \texttt{nan}), if the format's \meta{sign} is |~|, put a space (with
667 % category \enquote{other}); if it is~|+| put |+|; if it is |-|
668 % (default), put nothing, represented as a brace group. The width
669 % |#4| will be useful later, store it after \cs{s_@@_stop}. Afterwards,
670 % check the \meta{precision}: if it was not given, replace it by $6$
671 % (default precision) unless no \meta{style} was given: in that case
672 % we want to use whatever precision is needed to fully describe the
673 % number. Finally, dispatch depending on the \meta{style}.
674 % \begin{macrocode}
675 \cs_new:Npn \@@_format_fp:NNNnnNn
676 #1#2#3#4#5#6#7
677 {
678 \token_if_eq_charcode:NNTF #2 ?
679 { \@@_format_put:nw { #1 > } }
680 { \@@_format_put:nw { #1 #2 } }
681 \tl_if_head_eq_charcode:nNTF {#7} -
682 { \@@_format_put:nw { - } }
683 {
684 \str_case:nnF {#3}
685 {
686 { ~ } { \@@_format_put:ow { \c_catcode_other_space_tl } }
687 { + } { \@@_format_put:nw { + } }
688 }
689 { \@@_format_put:nw { { } } }
690 }
691 \@@_format_put:nw { {#4} }
692 \tl_if_empty:nTF {#5}
693 {
694 \token_if_eq_meaning:NNTF #6 ?
695 { \@@_format_put:nw { { } } }
696 { \@@_format_put:nw { { 6} } }
697 }
698 { \@@_format_put:nw { {#5} } }
699 \str_case:nnF {#6}
700 {
701 { e } { \@@_format_fp:wnnnNNn \@@_format_fp_e:nn }
702 { f } { \@@_format_fp:wnnnNNn \@@_format_fp_f:nn }
703 { g } { \@@_format_fp:wnnnNNn \@@_format_fp_g:nn }
704 { ? } { \@@_format_fp:wnnnNNn \@@_format_fp_g:nn }
705 }
706 {
707 \msg_expandable_error:nnnn
708 { str } { invalid-style-format } {#6} { fp }
709 \@@_format_fp:wnnnNNn \@@_format_fp_g:nn
710 }
711 \s_@@_stop {#7}
712 }
713 % \end{macrocode}
714 % \end{macro}
715 %
716 % \begin{macro}[EXP]{\@@_format_fp:wnnnNNn}
717 % \begin{quote}
718 % \cs{@@_format_fp:wnnnNNn} \meta{formatting function} \cs{s_@@_stop}
719 % \Arg{precision} \Arg{width} \Arg{sign} \meta{fill}
720 % \meta{alignment} \Arg{floating point}
721 % \end{quote}
722 % \begin{macrocode}
723 \cs_new:Npn \@@_format_fp:wnnnNNn
724 #1 \s_@@_stop #2 #3 #4 #5#6 #7
725 {
726 \exp_args:Nc \exp_args:Nf
727 { @@_format_align_#6:nnnN }
728 { #1 {#7} {#2} }
729 {#4}
730 {#3} #5
731 }
732 % \end{macrocode}
733 % \end{macro}
734 %
735 % \begin{macro}[EXP]{\@@_format_fp_round:nn}
736 % Round the given floating point and take the absolute value (in this
737 % order, to play nicely with unusual rounding modes if we ever
738 % implement these).
739 % \begin{macrocode}
740 \cs_new:Npn \@@_format_fp_round:nn #1 #2
741 { \fp_to_tl:n { abs ( round ( #1 , #2 - logb(#1) - 1 ) ) } }
742 % \end{macrocode}
743 % \end{macro}
744 %
745 % \begin{macro}[EXP]{\@@_format_fp_e:nn}
746 % \begin{macro}[EXP]{\@@_format_fp_e_aux:nn, \@@_format_fp_e_aux:wwn}
747 % With the |e| type, first round to |#4+1| significant figures (one
748 % before the decimal separator, |#4| after), then filter out special
749 % cases, then convert to scientific notations. This order is
750 % important because rounding can produce infinities or zeros and
751 % because \cs{fp_to_scientific:n} does not accept |nan| nor |inf|.
752 % \begin{macrocode}
753 \cs_new:Npn \@@_format_fp_e:nn #1#2
754 {
755 \exp_args:Nf \@@_format_fp_e_aux:nn
756 { \@@_format_fp_round:nn {#1} { #2 + 1 } }
757 {#2}
758 }
759 \cs_new:Npn \@@_format_fp_e_aux:nn #1#2
760 {
761 \str_case:nnF {#1}
762 {
763 { inf } { inf }
764 { nan } { nan }
765 }
766 {
767 \exp_last_unbraced:Nf \@@_format_fp_e_aux:wwn
768 { \fp_to_scientific:n {#1} } ;
769 {#2}
770 }
771 }
772 \cs_new:Npn \@@_format_fp_e_aux:wwn #1 . #2 e #3 ; #4
773 {
774 \@@_format_put:nw { e #3 }
775 \int_compare:nNnTF {#4} < \c__fp_prec_int
776 {
777 \@@_format_put:fw { \str_range:nnn { #2 } { 1 } { #4 } }
778 \@@_format_put:nw { #1 . }
779 }
780 {
781 \@@_format_put:fw
782 { \prg_replicate:nn { #4 - \c__fp_prec_int + 1 } { 0 } }
783 \@@_format_put:nw { #1 . #2 }
784 }
785 \use_none:n \s_@@_stop
786 }
787 % \end{macrocode}
788 % \end{macro}
789 % \end{macro}
790 %
791 % \begin{macro}[EXP]{\@@_format_fp_f:nn}
792 % \begin{macro}[EXP]{\@@_format_fp_f_aux:wwwn}
793 % With the |f| type, first round to |#4| (absolute) decimal places
794 % then filter out special cases, then in normal cases pad with zeros.
795 % \begin{macrocode}
796 \cs_new:Npn \@@_format_fp_f:nn #1#2
797 {
798 \exp_args:Nf \@@_format_fp_f_aux:nn
799 { \fp_to_tl:n { abs ( round ( #1 , #2 ) ) } }
800 {#2}
801 }
802 \cs_new:Npn \@@_format_fp_f_aux:nn #1#2
803 {
804 \str_case:nnF {#1}
805 {
806 { inf } { inf }
807 { nan } { nan }
808 }
809 {
810 \exp_last_unbraced:Nf \@@_format_fp_f_aux:wwwn
811 { \fp_to_decimal:n {#1} } . . ; {#2}
812 }
813 }
814 \cs_new:Npn \@@_format_fp_f_aux:wwwn #1 . #2 . #3 ; #4
815 {
816 \use:nf { #1 . #2 }
817 { \prg_replicate:nn { #4 - \@@_count:n {#2} } {0} }
818 }
819 % \end{macrocode}
820 % \end{macro}
821 % \end{macro}
822 %
823 % \begin{macro}[EXP]{\@@_format_fp_g:nn}
824 % \begin{macro}[EXP]{\@@_format_fp_g_aux:wn}
825 % \begin{macro}[EXP]{\@@_format_fp_to_scientific:n, \@@_format_fp_trim:w}
826 % With the |g| type, a special case is when |#2| is empty (no style
827 % nor precision in the original specification): then we output the
828 % number without rounding (and without its sign). Otherwise round to
829 % |#2| significant figures before filtering out special cases. (A
830 % \meta{precision} of $0$ is treated like a precision of $1$.)
831 % Distinguish exponents $-4\leq \meta{exponent} < \meta{precision}$
832 % from others and use essentially the |f| or |e| presentations in
833 % these two cases, but trimming trailing zeros. Because we don't need
834 % to keep a fixed number of digits after the decimal point we can
835 % simply use \cs{fp_to_decimal:n} and \cs{fp_to_scientific:n}, and in
836 % the second case post-process the result by trimming zeros and a
837 % period.
838 % \begin{macrocode}
839 \cs_new:Npn \@@_format_fp_g:nn #1#2
840 {
841 \tl_if_empty:nTF {#2} { \fp_to_tl:n { abs(#1) } }
842 {
843 \exp_args:Nf \@@_format_fp_g_aux:nn
844 { \@@_format_fp_round:nn {#1} { \int_max:nn {1} {#2} } }
845 { \int_max:nn {1} {#2} }
846 }
847 }
848 \cs_new:Npn \@@_format_fp_g_aux:nn #1#2
849 {
850 \str_case:nnF {#1}
851 {
852 { 0 } { 0 }
853 { inf } { inf }
854 { nan } { nan }
855 }
856 {
857 \fp_compare:nTF { 1e-4 <= #1 < 1e#2 }
858 { \fp_to_decimal:n {#1} }
859 {
860 \exp_last_unbraced:Nf \@@_format_fp_trim:w
861 { \fp_to_scientific:n {#1} }
862 }
863 }
864 }
865 % \end{macrocode}
866 % \end{macro}
867 % \end{macro}
868 % \end{macro}
869 %
870 % \begin{macro}[EXP]{\@@_format_fp_trim:w}
871 % \begin{macro}[EXP]
872 % {\@@_format_fp_trim_loop:w, \@@_format_fp_trim_dot:w, \@@_format_fp_trim_end:w}
873 % If |#1| ends with a $0$, the \texttt{loop} auxiliary takes that zero
874 % as an end-delimiter for its first argument, and the second argument
875 % is the same \texttt{loop} auxiliary. Once the last trailing zero is
876 % reached, the second argument is the \texttt{dot} auxiliary,
877 % which removes a trailing dot if any. We then clean-up with the
878 % \texttt{end} auxiliary, keeping only the number.
879 % \begin{macrocode}
880 \cs_new:Npn \@@_format_fp_trim:w #1 e
881 {
882 \@@_format_fp_trim_loop:w #1
883 ; \@@_format_fp_trim_loop:w 0; \@@_format_fp_trim_dot:w .; \s_@@_stop e
884 }
885 \cs_new:Npn \@@_format_fp_trim_loop:w #1 0; #2 { #2 #1 ; #2 }
886 \cs_new:Npn \@@_format_fp_trim_dot:w #1 .; { \@@_format_fp_trim_end:w #1 ; }
887 \cs_new:Npn \@@_format_fp_trim_end:w #1 ; #2 \s_@@_stop { #1 }
888 % \end{macrocode}
889 % \end{macro}
890 % \end{macro}
891 %
892 % \subsection{Messages}
893 %
894 % All of the messages are produced expandably, so there is no need for
895 % an extra-text.
896 % \begin{macrocode}
897 \msg_new:nnn { str } { invalid-format }
898 { Invalid~format~'#1'. }
899 \msg_new:nnn { str } { invalid-align-format }
900 { Invalid~alignment~'#1'~for~type~'#2'. }
901 \msg_new:nnn { str } { invalid-sign-format }
902 { Invalid~sign~'#1'~for~type~'#2'. }
903 \msg_new:nnn { str } { invalid-precision-format }
904 { Invalid~precision~'#1'~for~type~'#2'. }
905 \msg_new:nnn { str } { invalid-style-format }
906 { Invalid~style~'#1'~for~type~'#2'. }
907 % \end{macrocode}
908 %
909 % \begin{macrocode}
910 %</code>
911 % \end{macrocode}
912 %
913 % \end{implementation}
914 %
915 % \PrintIndex