| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
%% File: l3tl-build.dtx |
| 4 |
% |
| 5 |
% Copyright (C) 2018-2026 The LaTeX Project |
| 6 |
% |
| 7 |
% It may be distributed and/or modified under the conditions of the |
| 8 |
% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 9 |
% license or (at your option) any later version. The latest version |
| 10 |
% of this license is in the file |
| 11 |
% |
| 12 |
% https://www.latex-project.org/lppl.txt |
| 13 |
% |
| 14 |
% This file is part of the "l3kernel bundle" (The Work in LPPL) |
| 15 |
% and all files in that bundle must be distributed together. |
| 16 |
% |
| 17 |
% ----------------------------------------------------------------------- |
| 18 |
% |
| 19 |
% The development version of the bundle can be found at |
| 20 |
% |
| 21 |
% https://github.com/latex3/latex3 |
| 22 |
% |
| 23 |
% for those people who are interested. |
| 24 |
% |
| 25 |
%<*driver> |
| 26 |
\documentclass[full,kernel]{l3doc} |
| 27 |
\begin{document} |
| 28 |
\DocInput{\jobname.dtx} |
| 29 |
\end{document} |
| 30 |
%</driver> |
| 31 |
% \fi |
| 32 |
% |
| 33 |
% \title{^^A |
| 34 |
% The \pkg{l3tl-build} module\\ Piecewise \texttt{tl} constructions^^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 |
% \section{Constructing \meta{tl~var} by accumulation} |
| 53 |
% |
| 54 |
% When creating a \meta{tl~var} by accumulation of many tokens, the performance |
| 55 |
% available using a combination of \cs{tl_set:Nn} and \cs{tl_put_right:Nn} or |
| 56 |
% similar begins to become an issue. To address this, a set of functions are |
| 57 |
% available to \enquote{build} a \meta{tl~var}. The performance of this approach |
| 58 |
% is much more efficient than the standard \cs{tl_put_right:Nn}, but the |
| 59 |
% constructed token list cannot be accessed during construction other than |
| 60 |
% by methods provided in this section. |
| 61 |
% |
| 62 |
% Whilst the exact performance difference is dependent on the size of each |
| 63 |
% added block of tokens and the total number of blocks, in general, the |
| 64 |
% \cs[no-index]{tl_build_(g)put...} functions will out-perform the basic |
| 65 |
% \cs[no-index]{tl_(g)put...} equivalent if more than 100 non-empty addition |
| 66 |
% operations occur. See |
| 67 |
% \url{https://github.com/latex3/latex3/issues/1393#issuecomment-1880164756} |
| 68 |
% for a more detailed analysis. |
| 69 |
% |
| 70 |
% \begin{function}[added = 2018-04-01]{\tl_build_begin:N, \tl_build_gbegin:N} |
| 71 |
% \begin{syntax} |
| 72 |
% \cs{tl_build_begin:N} \meta{tl~var} |
| 73 |
% \end{syntax} |
| 74 |
% Clears the \meta{tl~var} and sets it up to support other |
| 75 |
% \cs[no-index]{tl_build_\ldots{}} functions. Until \cs{tl_build_end:N} |
| 76 |
% \meta{tl~var} or \cs{tl_build_gend:N} \meta{tl~var} is called, |
| 77 |
% applying any function from \pkg{l3tl} other |
| 78 |
% than \cs[no-index]{tl_build_\ldots{}} will lead to incorrect |
| 79 |
% results. The |begin| and |gbegin| functions must be used for local |
| 80 |
% and global \meta{tl~var} respectively. |
| 81 |
% \end{function} |
| 82 |
% |
| 83 |
% \begin{function}[added = 2018-04-01] |
| 84 |
% { |
| 85 |
% \tl_build_put_left:Nn, \tl_build_put_left:Ne, |
| 86 |
% \tl_build_gput_left:Nn, \tl_build_gput_left:Ne, |
| 87 |
% \tl_build_put_right:Nn, \tl_build_put_right:Ne, |
| 88 |
% \tl_build_gput_right:Nn, \tl_build_gput_right:Ne |
| 89 |
% } |
| 90 |
% \begin{syntax} |
| 91 |
% \cs{tl_build_put_left:Nn} \meta{tl~var} \Arg{tokens} |
| 92 |
% \cs{tl_build_put_right:Nn} \meta{tl~var} \Arg{tokens} |
| 93 |
% \end{syntax} |
| 94 |
% Adds \meta{tokens} to the left or right side of the current contents |
| 95 |
% of \meta{tl~var}. The \meta{tl~var} must have been set up with |
| 96 |
% \cs{tl_build_begin:N} or \cs{tl_build_gbegin:N}. The |put| and |
| 97 |
% |gput| functions must be used for local and global \meta{tl~var} |
| 98 |
% respectively. The |right| functions are about twice faster than the |
| 99 |
% |left| functions. |
| 100 |
% \end{function} |
| 101 |
% |
| 102 |
% \begin{function}[added = 2018-04-01]{\tl_build_end:N, \tl_build_gend:N} |
| 103 |
% \begin{syntax} |
| 104 |
% \cs{tl_build_end:N} \meta{tl~var} |
| 105 |
% \end{syntax} |
| 106 |
% Gets the contents of \meta{tl~var} and stores that into the |
| 107 |
% \meta{tl~var} using \cs{tl_set:Nn} or \cs{tl_gset:Nn}. |
| 108 |
% The \meta{tl~var} must have |
| 109 |
% been set up with \cs{tl_build_begin:N} or \cs{tl_build_gbegin:N}. |
| 110 |
% The |end| and |gend| functions must be used for local and global |
| 111 |
% \meta{tl~var} respectively. These functions completely remove the |
| 112 |
% setup code that enabled \meta{tl~var} to be used for other |
| 113 |
% \cs[no-index]{tl_build_\ldots{}} functions. After the action of |
| 114 |
% |end|/|gend|, the \meta{tl~var} may be manipulated using standard |
| 115 |
% \texttt{tl} functions. |
| 116 |
% \end{function} |
| 117 |
% |
| 118 |
% \begin{function}[added = 2023-12-14]{\tl_build_get_intermediate:NN} |
| 119 |
% \begin{syntax} |
| 120 |
% \cs{tl_build_get_intermediate:NN} \meta{tl~var_1} \meta{tl~var_2} |
| 121 |
% \end{syntax} |
| 122 |
% Stores the contents of the \meta{tl~var_1} in the \meta{tl~var_2}. |
| 123 |
% The \meta{tl~var_1} must have been set up with \cs{tl_build_begin:N} |
| 124 |
% or \cs{tl_build_gbegin:N}. The \meta{tl~var_2} is a |
| 125 |
% \enquote{normal} token list variable, assigned locally using |
| 126 |
% \cs{tl_set:Nn}. |
| 127 |
% \end{function} |
| 128 |
% |
| 129 |
% \end{documentation} |
| 130 |
% |
| 131 |
% \begin{implementation} |
| 132 |
% |
| 133 |
% \section{\pkg{l3tl-build} implementation} |
| 134 |
% |
| 135 |
% \begin{macrocode} |
| 136 |
%<*code> |
| 137 |
% \end{macrocode} |
| 138 |
% |
| 139 |
% \begin{macrocode} |
| 140 |
%<@@=tl> |
| 141 |
% \end{macrocode} |
| 142 |
% |
| 143 |
% Between \cs{tl_build_begin:N} \meta{tl~var} and \cs{tl_build_end:N} |
| 144 |
% \meta{tl~var}, the \meta{tl~var} has the structure |
| 145 |
% \begin{quote} |
| 146 |
% \cs{exp_end:} \ldots{} \cs{exp_end:} \cs{@@_build_last:NNn} |
| 147 |
% \meta{assignment} \meta{next~tl} \Arg{left} \meta{right} |
| 148 |
% \end{quote} |
| 149 |
% where \meta{right} is not braced. The \enquote{data} it represents is |
| 150 |
% \meta{left} followed by the \enquote{data} of \meta{next~tl} followed |
| 151 |
% by \meta{right}. The \meta{next~tl} is a token list variable whose |
| 152 |
% name is that of \meta{tl~var} followed by~|'|. There are between $0$ |
| 153 |
% and $4$ \cs{exp_end:} to keep track of when \meta{left} and |
| 154 |
% \meta{right} should be put into the \meta{next~tl}. The |
| 155 |
% \meta{assignment} is \cs{cs_set_nopar:Npe} if the variable is local, |
| 156 |
% and \cs{cs_gset_nopar:Npe} if it is global. |
| 157 |
% |
| 158 |
% \begin{macro}{\tl_build_begin:N, \tl_build_gbegin:N} |
| 159 |
% \begin{macro}{\@@_build_begin:NN, \@@_build_begin:NNN} |
| 160 |
% First construct the \meta{next~tl}: using a prime here conflicts |
| 161 |
% with the usual \pkg{expl3} convention but we need a name that can be |
| 162 |
% derived from |#1| without any external data such as a counter. |
| 163 |
% Empty that \meta{next~tl} and setup the structure. The local and |
| 164 |
% global versions only differ by a single function |
| 165 |
% \cs[no-index]{cs_(g)set_nopar:Npe} used for all assignments: this is |
| 166 |
% important because only that function is stored in the \meta{tl~var} |
| 167 |
% and \meta{next~tl} for subsequent assignments. In principle |
| 168 |
% \cs{@@_build_begin:NNN} could use \cs[no-index]{tl_(g)clear_new:N} |
| 169 |
% to empty |#1| and make sure it is defined, but logging the |
| 170 |
% definition does not seem useful so we just do |#3| |#1| |{}| to |
| 171 |
% clear it locally or globally as appropriate. |
| 172 |
% \begin{macrocode} |
| 173 |
\cs_new_protected:Npn \tl_build_begin:N #1 |
| 174 |
{ \@@_build_begin:NN \cs_set_nopar:Npe #1 } |
| 175 |
\cs_new_protected:Npn \tl_build_gbegin:N #1 |
| 176 |
{ \@@_build_begin:NN \cs_gset_nopar:Npe #1 } |
| 177 |
\cs_new_protected:Npn \@@_build_begin:NN #1#2 |
| 178 |
{ \exp_args:Nc \@@_build_begin:NNN { \cs_to_str:N #2 ' } #2 #1 } |
| 179 |
\cs_new_protected:Npn \@@_build_begin:NNN #1#2#3 |
| 180 |
{ |
| 181 |
#3 #1 { } |
| 182 |
#3 #2 |
| 183 |
{ |
| 184 |
\exp_not:n { \exp_end: \exp_end: \exp_end: \exp_end: } |
| 185 |
\exp_not:n { \@@_build_last:NNn #3 #1 { } } |
| 186 |
} |
| 187 |
} |
| 188 |
% \end{macrocode} |
| 189 |
% \end{macro} |
| 190 |
% \end{macro} |
| 191 |
% |
| 192 |
% \begin{macro} |
| 193 |
% { |
| 194 |
% \tl_build_put_right:Nn, \tl_build_put_right:Ne, \tl_build_put_right:Nx, |
| 195 |
% \tl_build_gput_right:Nn, \tl_build_gput_right:Ne, \tl_build_gput_right:Nx |
| 196 |
% } |
| 197 |
% \begin{macro}{\@@_build_last:NNn} |
| 198 |
% \begin{macro}{\@@_build_put:nn} |
| 199 |
% \begin{macro}{\@@_build_put:nw} |
| 200 |
% Similar to \cs{tl_put_right:Nn}, but apply \cs{exp:w} to |#1|. Most |
| 201 |
% of the time this just removes one \cs{exp_end:}. When there are |
| 202 |
% none left, \cs{@@_build_last:NNn} is expanded instead. It resets |
| 203 |
% the definition of the \meta{tl~var} by ending the \cs{exp_not:n} and |
| 204 |
% the definition early. Then it makes sure the \meta{next~tl} (its |
| 205 |
% argument |#1|) is set-up and starts a new definition. Then |
| 206 |
% \cs{@@_build_put:nn} and \cs{@@_build_put:nw} place the \meta{left} |
| 207 |
% part of the original \meta{tl~var} as appropriate for the definition |
| 208 |
% of the \meta{next~tl} (the \meta{right} part is left in the right |
| 209 |
% place without ever becoming a macro argument). We use |
| 210 |
% \cs{exp_after:wN} rather than some \cs{exp_args:No} to avoid reading |
| 211 |
% arguments that are likely very long token lists. We use |
| 212 |
% \cs[no-index]{cs_(g)set_nopar:Npe} rather than |
| 213 |
% \cs[no-index]{tl_(g)set:Ne} partly for the same reason and partly |
| 214 |
% because the assignments are interrupted by brace tricks, which |
| 215 |
% implies that the assignment does not simply set the token list to an |
| 216 |
% |e|-expansion of the second argument. |
| 217 |
% \begin{macrocode} |
| 218 |
\cs_new_protected:Npn \tl_build_put_right:Nn #1#2 |
| 219 |
{ |
| 220 |
\cs_set_nopar:Npe #1 |
| 221 |
{ \__kernel_exp_not:w \exp_after:wN { \exp:w #1 #2 } } |
| 222 |
} |
| 223 |
\cs_generate_variant:Nn \tl_build_put_right:Nn { Ne , Nx } |
| 224 |
\cs_new_protected:Npn \tl_build_gput_right:Nn #1#2 |
| 225 |
{ |
| 226 |
\cs_gset_nopar:Npe #1 |
| 227 |
{ \__kernel_exp_not:w \exp_after:wN { \exp:w #1 #2 } } |
| 228 |
} |
| 229 |
\cs_generate_variant:Nn \tl_build_gput_right:Nn { Ne , Nx } |
| 230 |
\cs_new_protected:Npn \@@_build_last:NNn #1#2 |
| 231 |
{ |
| 232 |
\if_false: { { \fi: |
| 233 |
\exp_end: \exp_end: \exp_end: \exp_end: \exp_end: |
| 234 |
\@@_build_last:NNn #1 #2 { } |
| 235 |
} |
| 236 |
} |
| 237 |
\if_meaning:w \c_empty_tl #2 |
| 238 |
\@@_build_begin:NN #1 #2 |
| 239 |
\fi: |
| 240 |
#1 #2 |
| 241 |
{ |
| 242 |
\__kernel_exp_not:w \exp_after:wN |
| 243 |
{ |
| 244 |
\exp:w \if_false: } } \fi: |
| 245 |
\exp_after:wN \@@_build_put:nn \exp_after:wN {#2} |
| 246 |
} |
| 247 |
\cs_new_protected:Npn \@@_build_put:nn #1#2 { \@@_build_put:nw {#2} #1 } |
| 248 |
\cs_new_protected:Npn \@@_build_put:nw #1#2 \@@_build_last:NNn #3#4#5 |
| 249 |
{ #2 \@@_build_last:NNn #3 #4 { #1 #5 } } |
| 250 |
% \end{macrocode} |
| 251 |
% \end{macro} |
| 252 |
% \end{macro} |
| 253 |
% \end{macro} |
| 254 |
% \end{macro} |
| 255 |
% |
| 256 |
% \begin{macro} |
| 257 |
% { |
| 258 |
% \tl_build_put_left:Nn, \tl_build_put_left:Ne, \tl_build_put_left:Nx, |
| 259 |
% \tl_build_gput_left:Nn, \tl_build_gput_left:Ne, \tl_build_gput_left:Nx |
| 260 |
% } |
| 261 |
% \begin{macro}{\@@_build_put_left:NNn} |
| 262 |
% See \cs{tl_build_put_right:Nn} for all the machinery. We could |
| 263 |
% easily provide \cs[no-index]{tl_build_put_left_right:Nnn}, by just |
| 264 |
% adding the \meta{right} material after the \Arg{left} in the |
| 265 |
% |e|-expanding assignment. |
| 266 |
% \begin{macrocode} |
| 267 |
\cs_new_protected:Npn \tl_build_put_left:Nn #1 |
| 268 |
{ \@@_build_put_left:NNn \cs_set_nopar:Npe #1 } |
| 269 |
\cs_generate_variant:Nn \tl_build_put_left:Nn { Ne , Nx } |
| 270 |
\cs_new_protected:Npn \tl_build_gput_left:Nn #1 |
| 271 |
{ \@@_build_put_left:NNn \cs_gset_nopar:Npe #1 } |
| 272 |
\cs_generate_variant:Nn \tl_build_gput_left:Nn { Ne , Nx } |
| 273 |
\cs_new_protected:Npn \@@_build_put_left:NNn #1#2#3 |
| 274 |
{ |
| 275 |
#1 #2 |
| 276 |
{ |
| 277 |
\__kernel_exp_not:w \exp_after:wN |
| 278 |
{ |
| 279 |
\exp:w \exp_after:wN \@@_build_put:nn |
| 280 |
\exp_after:wN {#2} {#3} |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
% \end{macrocode} |
| 285 |
% \end{macro} |
| 286 |
% \end{macro} |
| 287 |
% |
| 288 |
% \begin{macro}{\tl_build_end:N, \tl_build_gend:N} |
| 289 |
% \begin{macro}{\@@_build_end_loop:NN} |
| 290 |
% Get the data then clear the \meta{next~tl} recursively until finding |
| 291 |
% an empty one. It is perhaps wasteful to repeatedly use |
| 292 |
% \cs{cs_to_str:N}. The local/global scope is checked by |
| 293 |
% \cs{tl_set:Ne} or \cs{tl_gset:Ne}. |
| 294 |
% \begin{macrocode} |
| 295 |
\cs_new_protected:Npn \tl_build_end:N #1 |
| 296 |
{ |
| 297 |
\@@_build_get:NNN \__kernel_tl_set:Nx #1 #1 |
| 298 |
\exp_args:Nc \@@_build_end_loop:NN { \cs_to_str:N #1 ' } \tl_clear:N |
| 299 |
} |
| 300 |
\cs_new_protected:Npn \tl_build_gend:N #1 |
| 301 |
{ |
| 302 |
\@@_build_get:NNN \__kernel_tl_gset:Nx #1 #1 |
| 303 |
\exp_args:Nc \@@_build_end_loop:NN { \cs_to_str:N #1 ' } \tl_gclear:N |
| 304 |
} |
| 305 |
\cs_new_protected:Npn \@@_build_end_loop:NN #1#2 |
| 306 |
{ |
| 307 |
\if_meaning:w \c_empty_tl #1 |
| 308 |
\exp_after:wN \use_none:nnnnnn |
| 309 |
\fi: |
| 310 |
#2 #1 |
| 311 |
\exp_args:Nc \@@_build_end_loop:NN { \cs_to_str:N #1 ' } #2 |
| 312 |
} |
| 313 |
% \end{macrocode} |
| 314 |
% \end{macro} |
| 315 |
% \end{macro} |
| 316 |
% |
| 317 |
% \begin{macro}{\tl_build_get_intermediate:NN} |
| 318 |
% \begin{macrocode} |
| 319 |
\cs_new_protected:Npn \tl_build_get_intermediate:NN |
| 320 |
{ \@@_build_get:NNN \__kernel_tl_set:Nx } |
| 321 |
% \end{macrocode} |
| 322 |
% \end{macro} |
| 323 |
% |
| 324 |
% \begin{macro}{\@@_build_get:NNN} |
| 325 |
% \begin{macro}{\@@_build_get:w, \@@_build_get_end:w} |
| 326 |
% The idea is to expand the \meta{tl~var} then the \meta{next~tl} and |
| 327 |
% so on, all within an |e|-expanding assignment, and wrap as |
| 328 |
% appropriate in \cs{exp_not:n}. The various \meta{left} parts are |
| 329 |
% left in the assignment as we go, which enables us to expand the |
| 330 |
% \meta{next~tl} at the right place. The various \meta{right} parts |
| 331 |
% are eventually picked up in one last \cs{exp_not:n}, with a brace |
| 332 |
% trick to wrap all the \meta{right} parts together. |
| 333 |
% \begin{macrocode} |
| 334 |
\cs_new_protected:Npn \@@_build_get:NNN #1#2#3 |
| 335 |
{ #1 #3 { \if_false: { \fi: \exp_after:wN \@@_build_get:w #2 } } } |
| 336 |
\cs_new:Npn \@@_build_get:w #1 \@@_build_last:NNn #2#3#4 |
| 337 |
{ |
| 338 |
\exp_not:n {#4} |
| 339 |
\if_meaning:w \c_empty_tl #3 |
| 340 |
\exp_after:wN \@@_build_get_end:w |
| 341 |
\fi: |
| 342 |
\exp_after:wN \@@_build_get:w #3 |
| 343 |
} |
| 344 |
\cs_new:Npn \@@_build_get_end:w #1#2#3 |
| 345 |
{ \__kernel_exp_not:w \exp_after:wN { \if_false: } \fi: } |
| 346 |
% \end{macrocode} |
| 347 |
% \end{macro} |
| 348 |
% \end{macro} |
| 349 |
% |
| 350 |
% \begin{macrocode} |
| 351 |
%</code> |
| 352 |
% \end{macrocode} |
| 353 |
% |
| 354 |
% \end{implementation} |
| 355 |
% |
| 356 |
% \PrintIndex |