-
Notifications
You must be signed in to change notification settings - Fork 178
/
l3clist.dtx
2234 lines (2234 loc) · 80.8 KB
/
l3clist.dtx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% \iffalse meta-comment
%
%% File: l3clist.dtx
%
% Copyright (C) 2004-2011 Frank Mittelbach, The LaTeX Project
% (C) 2012-2022 The LaTeX Project
%
% It may be distributed and/or modified under the conditions of the
% LaTeX Project Public License (LPPL), either version 1.3c of this
% license or (at your option) any later version. The latest version
% of this license is in the file
%
% https://www.latex-project.org/lppl.txt
%
% This file is part of the "l3kernel bundle" (The Work in LPPL)
% and all files in that bundle must be distributed together.
%
% -----------------------------------------------------------------------
%
% The development version of the bundle can be found at
%
% https://github.com/latex3/latex3
%
% for those people who are interested.
%
%<*driver>
\documentclass[full,kernel]{l3doc}
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% \title{^^A
% The \pkg{l3clist} package\\ Comma separated lists^^A
% }
%
% \author{^^A
% The \LaTeX{} Project\thanks
% {^^A
% E-mail:
% \href{mailto:latex-team@latex-project.org}
% {latex-team@latex-project.org}^^A
% }^^A
% }
%
% \date{Released 2022-08-30}
%
% \maketitle
%
% \begin{documentation}
%
% Comma lists (in short, |clist|) contain ordered data where items can
% be added to the left or right end of the list. This data type allows
% basic list
% manipulations such as adding/removing items, applying a function to
% every item, removing duplicate items, extracting a given item, using
% the comma list with specified separators, and so on. Sequences
% (defined in \pkg{l3seq}) are safer, faster, and provide more features,
% so they should often be preferred to comma lists. Comma lists are
% mostly useful when interfacing with \LaTeXe{} or other code that
% expects or provides items separated by commas.
%
% Several items can be added at once. To ease input of comma lists from
% data provided by a user outside an \cs{ExplSyntaxOn} \ldots{}
% \cs{ExplSyntaxOff} block, spaces are removed from both sides of each
% comma-delimited argument upon input. Blank arguments are ignored, to
% allow for trailing commas or repeated commas (which may otherwise
% arise when concatenating comma lists \enquote{by hand}). In addition,
% a set of braces is removed if the result of space-trimming is braced:
% this allows the storage of any item in a comma list. For instance,
% \begin{verbatim}
% \clist_new:N \l_my_clist
% \clist_put_left:Nn \l_my_clist { ~a~ , ~{b}~ , c~\d }
% \clist_put_right:Nn \l_my_clist { ~{e~} , , {{f}} , }
% \end{verbatim}
% results in |\l_my_clist| containing |a,b,c~\d,{e~},{{f}}| namely the
% five items |a|, |b|, |c~\d|, |e~| and~|{f}|. Comma lists normally do
% not contain empty or blank items so the following gives an empty comma list:
% \begin{verbatim}
% \clist_clear_new:N \l_my_clist
% \clist_set:Nn \l_my_clist { , ~ , , }
% \clist_if_empty:NTF \l_my_clist { true } { false }
% \end{verbatim}
% and it leaves \texttt{true} in the input stream. To include an
% \enquote{unsafe} item (empty, or one that contains a comma, or starts
% or ends with a space, or is a single brace group), surround it with
% braces.
%
% Any |n|-type token list is a valid comma list input for \pkg{l3clist}
% functions, which will split the token list at every comma and process
% the items as described above. On the other hand, |N|-type functions
% expect comma list variables, which are particular token list variables
% in which this processing of items (and removal of blank items) has
% already occurred. Because comma list variables are token list
% variables, expanding them once yields their items separated by commas,
% and \pkg{l3tl} functions such as \cs{tl_show:N} can be applied to
% them. (These functions often have \pkg{l3clist} analogues, which
% should be preferred.)
%
% Almost all operations on comma lists are
% noticeably slower than those on sequences so converting the data to
% sequences using \cs{seq_set_from_clist:Nn} (see \pkg{l3seq}) may be
% advisable if speed is important. The exception is that
% \cs{clist_if_in:NnTF} and \cs{clist_remove_duplicates:N} may be faster
% than their sequence analogues for large lists. However, these
% functions work slowly for \enquote{unsafe} items that must be braced,
% and may produce errors when their argument contains |{|, |}| or |#|
% (assuming the usual \TeX{} category codes apply). The sequence
% data type should thus certainly be preferred to comma lists to store
% such items.
%
% \section{Creating and initialising comma lists}
%
% \begin{function}{\clist_new:N, \clist_new:c}
% \begin{syntax}
% \cs{clist_new:N} \meta{comma list}
% \end{syntax}
% Creates a new \meta{comma list} or raises an error if the name is
% already taken. The declaration is global. The \meta{comma list}
% initially contains no items.
% \end{function}
%
% \begin{function}[added = 2014-07-05]
% {
% \clist_const:Nn, \clist_const:Nx,
% \clist_const:cn, \clist_const:cx
% }
% \begin{syntax}
% \cs{clist_const:Nn} \meta{clist~var} \Arg{comma list}
% \end{syntax}
% Creates a new constant \meta{clist~var} or raises an error
% if the name is already taken. The value of the
% \meta{clist~var} is set globally to the
% \meta{comma list}.
% \end{function}
%
% \begin{function}
% {\clist_clear:N, \clist_clear:c, \clist_gclear:N, \clist_gclear:c}
% \begin{syntax}
% \cs{clist_clear:N} \meta{comma list}
% \end{syntax}
% Clears all items from the \meta{comma list}.
% \end{function}
%
% \begin{function}
% {
% \clist_clear_new:N, \clist_clear_new:c,
% \clist_gclear_new:N, \clist_gclear_new:c
% }
% \begin{syntax}
% \cs{clist_clear_new:N} \meta{comma list}
% \end{syntax}
% Ensures that the \meta{comma list} exists globally by applying
% \cs{clist_new:N} if necessary, then applies
% \cs[index=clist_clear:N]{clist_(g)clear:N} to leave
% the list empty.
% \end{function}
%
% \begin{function}
% {
% \clist_set_eq:NN, \clist_set_eq:cN,
% \clist_set_eq:Nc, \clist_set_eq:cc,
% \clist_gset_eq:NN, \clist_gset_eq:cN,
% \clist_gset_eq:Nc, \clist_gset_eq:cc
% }
% \begin{syntax}
% \cs{clist_set_eq:NN} \meta{comma list_1} \meta{comma list_2}
% \end{syntax}
% Sets the content of \meta{comma list_1} equal to that of
% \meta{comma list_2}. To set a token list variable equal to a comma
% list variable, use \cs{tl_set_eq:NN}. Conversely, setting a comma
% list variable to a token list is unadvisable unless one checks
% space-trimming and related issues.
% \end{function}
%
% \begin{function}[added = 2014-07-17]
% {
% \clist_set_from_seq:NN, \clist_set_from_seq:cN,
% \clist_set_from_seq:Nc, \clist_set_from_seq:cc,
% \clist_gset_from_seq:NN, \clist_gset_from_seq:cN,
% \clist_gset_from_seq:Nc, \clist_gset_from_seq:cc
% }
% \begin{syntax}
% \cs{clist_set_from_seq:NN} \meta{comma list} \meta{sequence}
% \end{syntax}
% Converts the data in the \meta{sequence} into a \meta{comma list}:
% the original \meta{sequence} is unchanged.
% Items which contain either spaces or commas are surrounded by braces.
% \end{function}
%
% \begin{function}
% {
% \clist_concat:NNN, \clist_concat:ccc,
% \clist_gconcat:NNN, \clist_gconcat:ccc
% }
% \begin{syntax}
% \cs{clist_concat:NNN} \meta{comma list_1} \meta{comma list_2} \meta{comma list_3}
% \end{syntax}
% Concatenates the content of \meta{comma list_2} and \meta{comma list_3}
% together and saves the result in \meta{comma list_1}. The items in
% \meta{comma list_2} are placed at the left side of the new comma list.
% \end{function}
%
% \begin{function}[EXP, pTF, added=2012-03-03]
% {\clist_if_exist:N, \clist_if_exist:c}
% \begin{syntax}
% \cs{clist_if_exist_p:N} \meta{comma list}
% \cs{clist_if_exist:NTF} \meta{comma list} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests whether the \meta{comma list} is currently defined. This does
% not check that the \meta{comma list} really is a comma list.
% \end{function}
%
% \section{Adding data to comma lists}
%
% \begin{function}[added = 2011-09-06]
% {
% \clist_set:Nn, \clist_set:NV,
% \clist_set:No, \clist_set:Nx,
% \clist_set:cn, \clist_set:cV,
% \clist_set:co, \clist_set:cx,
% \clist_gset:Nn, \clist_gset:NV,
% \clist_gset:No, \clist_gset:Nx,
% \clist_gset:cn, \clist_gset:cV,
% \clist_gset:co, \clist_gset:cx
% }
% \begin{syntax}
% \cs{clist_set:Nn} \meta{comma list} |{|\meta{item_1},\ldots{},\meta{item_n}|}|
% \end{syntax}
% Sets \meta{comma list} to contain the \meta{items},
% removing any previous content from the variable.
% Blank items are omitted, spaces are removed from both sides of each
% item, then a set of braces is removed if the resulting space-trimmed
% item is braced.
% To store some \meta{tokens} as a single \meta{item} even if the
% \meta{tokens} contain commas or spaces, add a set of braces:
% \cs{clist_set:Nn} \meta{comma list} |{| \Arg{tokens} |}|.
% \end{function}
%
% \begin{function}[updated = 2011-09-05]
% {
% \clist_put_left:Nn, \clist_put_left:NV,
% \clist_put_left:No, \clist_put_left:Nx,
% \clist_put_left:cn, \clist_put_left:cV,
% \clist_put_left:co, \clist_put_left:cx,
% \clist_gput_left:Nn, \clist_gput_left:NV,
% \clist_gput_left:No, \clist_gput_left:Nx,
% \clist_gput_left:cn, \clist_gput_left:cV,
% \clist_gput_left:co, \clist_gput_left:cx
% }
% \begin{syntax}
% \cs{clist_put_left:Nn} \meta{comma list} |{|\meta{item_1},\ldots{},\meta{item_n}|}|
% \end{syntax}
% Appends the \meta{items} to the left of the \meta{comma list}.
% Blank items are omitted, spaces are removed from both sides of each
% item, then a set of braces is removed if the resulting space-trimmed
% item is braced.
% To append some \meta{tokens} as a single \meta{item} even if the
% \meta{tokens} contain commas or spaces, add a set of braces:
% \cs{clist_put_left:Nn} \meta{comma list} |{| \Arg{tokens} |}|.
% \end{function}
%
% \begin{function}[updated = 2011-09-05]
% {
% \clist_put_right:Nn, \clist_put_right:NV,
% \clist_put_right:No, \clist_put_right:Nx,
% \clist_put_right:cn, \clist_put_right:cV,
% \clist_put_right:co, \clist_put_right:cx,
% \clist_gput_right:Nn, \clist_gput_right:NV,
% \clist_gput_right:No, \clist_gput_right:Nx,
% \clist_gput_right:cn, \clist_gput_right:cV,
% \clist_gput_right:co, \clist_gput_right:cx
% }
% \begin{syntax}
% \cs{clist_put_right:Nn} \meta{comma list} |{|\meta{item_1},\ldots{},\meta{item_n}|}|
% \end{syntax}
% Appends the \meta{items} to the right of the \meta{comma list}.
% Blank items are omitted, spaces are removed from both sides of each
% item, then a set of braces is removed if the resulting space-trimmed
% item is braced.
% To append some \meta{tokens} as a single \meta{item} even if the
% \meta{tokens} contain commas or spaces, add a set of braces:
% \cs{clist_put_right:Nn} \meta{comma list} |{| \Arg{tokens} |}|.
% \end{function}
%
% \section{Modifying comma lists}
%
% While comma lists are normally used as ordered lists, it may be
% necessary to modify the content. The functions here may be used
% to update comma lists, while retaining the order of the unaffected
% entries.
%
% \begin{function}
% {
% \clist_remove_duplicates:N, \clist_remove_duplicates:c,
% \clist_gremove_duplicates:N, \clist_gremove_duplicates:c
% }
% \begin{syntax}
% \cs{clist_remove_duplicates:N} \meta{comma list}
% \end{syntax}
% Removes duplicate items from the \meta{comma list}, leaving the
% left most copy of each item in the \meta{comma list}. The \meta{item}
% comparison takes place on a token basis, as for \cs{tl_if_eq:nnTF}.
% \begin{texnote}
% This function iterates through every item in the \meta{comma list} and
% does a comparison with the \meta{items} already checked. It is therefore
% relatively slow with large comma lists.
% Furthermore, it may fail if any of the items in the
% \meta{comma list} contains |{|, |}|, or |#|
% (assuming the usual \TeX{} category codes apply).
% \end{texnote}
% \end{function}
%
% \begin{function}[updated = 2011-09-06]
% {
% \clist_remove_all:Nn, \clist_remove_all:cn,
% \clist_remove_all:NV, \clist_remove_all:cV,
% \clist_gremove_all:Nn, \clist_gremove_all:cn,
% \clist_gremove_all:NV, \clist_gremove_all:cV
% }
% \begin{syntax}
% \cs{clist_remove_all:Nn} \meta{comma list} \Arg{item}
% \end{syntax}
% Removes every occurrence of \meta{item} from the \meta{comma list}.
% The \meta{item} comparison takes place on a token basis, as for
% \cs{tl_if_eq:nnTF}.
% \begin{texnote}
% The function may fail if the \meta{item} contains |{|, |}|, or |#|
% (assuming the usual \TeX{} category codes apply).
% \end{texnote}
% \end{function}
%
% \begin{function}[added = 2014-07-18]
% {
% \clist_reverse:N, \clist_reverse:c,
% \clist_greverse:N, \clist_greverse:c
% }
% \begin{syntax}
% \cs{clist_reverse:N} \meta{comma list}
% \end{syntax}
% Reverses the order of items stored in the \meta{comma list}.
% \end{function}
%
% \begin{function}[added = 2014-07-18]{\clist_reverse:n}
% \begin{syntax}
% \cs{clist_reverse:n} \Arg{comma list}
% \end{syntax}
% Leaves the items in the \meta{comma list} in the input stream in
% reverse order. Contrarily to other what is done for other
% \texttt{n}-type \meta{comma list} arguments, braces and spaces are
% preserved by this process.
% \begin{texnote}
% The result is returned within \tn{unexpanded}, which means that the
% comma list does not expand further when appearing in an
% \texttt{x}-type or \texttt{e}-type argument expansion.
% \end{texnote}
% \end{function}
%
% \begin{function}[added = 2017-02-06]
% {\clist_sort:Nn, \clist_sort:cn, \clist_gsort:Nn, \clist_gsort:cn}
% \begin{syntax}
% \cs{clist_sort:Nn} \meta{clist var} \Arg{comparison code}
% \end{syntax}
% Sorts the items in the \meta{clist var} according to the
% \meta{comparison code}, and assigns the result to
% \meta{clist var}. The details of sorting comparison are
% described in Section~\ref{sec:l3sort:mech}.
% \end{function}
%
% \section{Comma list conditionals}
%
% \begin{function}[EXP,pTF]{\clist_if_empty:N, \clist_if_empty:c}
% \begin{syntax}
% \cs{clist_if_empty_p:N} \meta{comma list}
% \cs{clist_if_empty:NTF} \meta{comma list} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the \meta{comma list} is empty (containing no items).
% \end{function}
%
% \begin{function}[EXP, pTF, added = 2014-07-05]{\clist_if_empty:n}
% \begin{syntax}
% \cs{clist_if_empty_p:n} \Arg{comma list}
% \cs{clist_if_empty:nTF} \Arg{comma list} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the \meta{comma list} is empty (containing no items).
% The rules for space trimming are as for other \texttt{n}-type
% comma-list functions, hence the comma list |{~,~,,~}| (without
% outer braces) is empty, while |{~,{},}| (without outer braces)
% contains one element, which happens to be empty: the comma-list
% is not empty.
% \end{function}
%
% \begin{function}[updated = 2011-09-06, TF]
% {
% \clist_if_in:Nn, \clist_if_in:NV, \clist_if_in:No,
% \clist_if_in:cn, \clist_if_in:cV, \clist_if_in:co,
% \clist_if_in:nn, \clist_if_in:nV, \clist_if_in:no
% }
% \begin{syntax}
% \cs{clist_if_in:NnTF} \meta{comma list} \Arg{item} \Arg{true code} \Arg{false code}
% \end{syntax}
% Tests if the \meta{item} is present in the \meta{comma list}.
% In the case of an \texttt{n}-type \meta{comma list}, the usual rules
% of space trimming and brace stripping apply. Hence,
% \begin{verbatim}
% \clist_if_in:nnTF { a , {b}~ , {b} , c } { b } {true} {false}
% \end{verbatim}
% yields \texttt{true}.
% \begin{texnote}
% The function may fail if the \meta{item} contains |{|, |}|, or |#|
% (assuming the usual \TeX{} category codes apply).
% \end{texnote}
% \end{function}
%
% \section{Mapping over comma lists}
%
% The functions described in this section apply a specified function
% to each item of a comma list.
% All mappings are done at the current group level, \emph{i.e.}~any
% local assignments made by the \meta{function} or \meta{code} discussed
% below remain in effect after the loop.
%
% When the comma list is given explicitly, as an \texttt{n}-type argument,
% spaces are trimmed around each item.
% If the result of trimming spaces is empty, the item is ignored.
% Otherwise, if the item is surrounded by braces, one set is removed,
% and the result is passed to the mapped function. Thus, if the
% comma list that is being mapped is \verb*|{a , {{b} }, ,{}, {c},}|
% then the arguments passed to the mapped function are
% `\verb*|a|', `\verb*|{b} |', an empty argument, and `\verb*|c|'.
%
% When the comma list is given as an \texttt{N}-type argument, spaces
% have already been trimmed on input, and items are simply stripped
% of one set of braces if any. This case is more efficient than using
% \texttt{n}-type comma lists.
%
% \begin{function}[rEXP, updated = 2012-06-29]
% {\clist_map_function:NN, \clist_map_function:cN, \clist_map_function:nN}
% \begin{syntax}
% \cs{clist_map_function:NN} \meta{comma list} \meta{function}
% \end{syntax}
% Applies \meta{function} to every \meta{item} stored in the
% \meta{comma list}. The \meta{function} receives one argument for
% each iteration. The \meta{items} are returned from left to right.
% The function \cs{clist_map_inline:Nn} is in general more efficient
% than \cs{clist_map_function:NN}.
% \end{function}
%
% \begin{function}[updated = 2012-06-29]
% {\clist_map_inline:Nn, \clist_map_inline:cn, \clist_map_inline:nn}
% \begin{syntax}
% \cs{clist_map_inline:Nn} \meta{comma list} \Arg{inline function}
% \end{syntax}
% Applies \meta{inline function} to every \meta{item} stored
% within the \meta{comma list}. The \meta{inline function} should
% consist of code which receives the \meta{item} as |#1|.
% The \meta{items} are returned from left to right.
% \end{function}
%
% \begin{function}[updated = 2012-06-29]
% {\clist_map_variable:NNn, \clist_map_variable:cNn, \clist_map_variable:nNn}
% \begin{syntax}
% \cs{clist_map_variable:NNn} \meta{comma list} \meta{variable} \Arg{code}
% \end{syntax}
% Stores each \meta{item} of the \meta{comma list} in turn in the
% (token list) \meta{variable} and applies the \meta{code}. The
% \meta{code} will usually make use of the \meta{variable}, but this
% is not enforced. The assignments to the \meta{variable} are local.
% Its value after the loop is the last \meta{item} in the \meta{comma
% list}, or its original value if there were no \meta{item}. The
% \meta{items} are returned from left to right.
% \end{function}
%
% \begin{function}[rEXP, added = 2021-05-05]
% {\clist_map_tokens:Nn, \clist_map_tokens:cn, \clist_map_tokens:nn}
% \begin{syntax}
% \cs{clist_map_tokens:Nn} \meta{clist~var} \Arg{code}
% \cs{clist_map_tokens:nn} \Arg{comma list} \Arg{code}
% \end{syntax}
% Calls \meta{code} \Arg{item} for every \meta{item} stored in the
% \meta{comma list}. The \meta{code} receives each \meta{item} as a
% trailing brace group. If the \meta{code} consists of a single
% function this is equivalent to \cs{clist_map_function:nN}.
% \end{function}
%
% \begin{function}[rEXP, updated = 2012-06-29]{\clist_map_break:}
% \begin{syntax}
% \cs{clist_map_break:}
% \end{syntax}
% Used to terminate a \cs[no-index]{clist_map_\ldots{}} function before all
% entries in the \meta{comma list} have been processed. This
% normally takes place within a conditional statement, for example
% \begin{verbatim}
% \clist_map_inline:Nn \l_my_clist
% {
% \str_if_eq:nnTF { #1 } { bingo }
% { \clist_map_break: }
% {
% % Do something useful
% }
% }
% \end{verbatim}
% Use outside of a \cs[no-index]{clist_map_\ldots{}} scenario leads to low
% level \TeX{} errors.
% \begin{texnote}
% When the mapping is broken, additional tokens may be inserted
% before further items are taken
% from the input stream. This depends on the design of the mapping
% function.
% \end{texnote}
% \end{function}
%
% \begin{function}[updated = 2012-06-29, rEXP]{\clist_map_break:n}
% \begin{syntax}
% \cs{clist_map_break:n} \Arg{code}
% \end{syntax}
% Used to terminate a \cs[no-index]{clist_map_\ldots{}} function before all
% entries in the \meta{comma list} have been processed, inserting
% the \meta{code} after the mapping has ended. This
% normally takes place within a conditional statement, for example
% \begin{verbatim}
% \clist_map_inline:Nn \l_my_clist
% {
% \str_if_eq:nnTF { #1 } { bingo }
% { \clist_map_break:n { <code> } }
% {
% % Do something useful
% }
% }
% \end{verbatim}
% Use outside of a \cs[no-index]{clist_map_\ldots{}} scenario leads to low
% level \TeX{} errors.
% \begin{texnote}
% When the mapping is broken, additional tokens may be inserted
% before the \meta{code} is
% inserted into the input stream.
% This depends on the design of the mapping function.
% \end{texnote}
% \end{function}
%
% \begin{function}[EXP, added = 2012-07-13]
% {\clist_count:N, \clist_count:c, \clist_count:n}
% \begin{syntax}
% \cs{clist_count:N} \meta{comma list}
% \end{syntax}
% Leaves the number of items in the \meta{comma list} in the input
% stream as an \meta{integer denotation}. The total number of items
% in a \meta{comma list} includes those which are duplicates,
% \emph{i.e.}~every item in a \meta{comma list} is counted.
% \end{function}
%
% \section{Using the content of comma lists directly}
%
% \begin{function}[EXP, added = 2013-05-26]{\clist_use:Nnnn, \clist_use:cnnn}
% \begin{syntax}
% \cs{clist_use:Nnnn} \meta{clist~var} \Arg{separator~between~two} \Arg{separator~between~more~than~two} \Arg{separator~between~final~two}
% \end{syntax}
% Places the contents of the \meta{clist~var} in the input stream,
% with the appropriate \meta{separator} between the items. Namely, if
% the comma list has more than two items, the \meta{separator between
% more than two} is placed between each pair of items except the
% last, for which the \meta{separator between final two} is used. If
% the comma list has exactly two items, then they are placed in the input
% stream separated by the \meta{separator between two}. If the comma
% list has a single item, it is placed in the input stream, and a comma
% list with no items produces no output. An error is raised if
% the variable does not exist or if it is invalid.
%
% For example,
% \begin{verbatim}
% \clist_set:Nn \l_tmpa_clist { a , b , , c , {de} , f }
% \clist_use:Nnnn \l_tmpa_clist { ~and~ } { ,~ } { ,~and~ }
% \end{verbatim}
% inserts \enquote{\texttt{a, b, c, de, and f}} in the input
% stream. The first separator argument is not used in this case
% because the comma list has more than $2$ items.
% \begin{texnote}
% The result is returned within the \tn{unexpanded}
% primitive (\cs{exp_not:n}), which means that the \meta{items}
% do not expand further when appearing in an \texttt{x}-type
% or \texttt{e}-type argument expansion.
% \end{texnote}
% \end{function}
%
% \begin{function}[EXP, added = 2013-05-26]{\clist_use:Nn, \clist_use:cn}
% \begin{syntax}
% \cs{clist_use:Nn} \meta{clist~var} \Arg{separator}
% \end{syntax}
% Places the contents of the \meta{clist~var} in the input stream,
% with the \meta{separator} between the items. If the comma
% list has a single item, it is placed in the input stream, and a comma
% list with no items produces no output. An error is raised if
% the variable does not exist or if it is invalid.
%
% For example,
% \begin{verbatim}
% \clist_set:Nn \l_tmpa_clist { a , b , , c , {de} , f }
% \clist_use:Nn \l_tmpa_clist { ~and~ }
% \end{verbatim}
% inserts \enquote{\texttt{a and b and c and de and f}} in the input
% stream.
% \begin{texnote}
% The result is returned within the \tn{unexpanded}
% primitive (\cs{exp_not:n}), which means that the \meta{items}
% do not expand further when appearing in an \texttt{x}-type
% or \texttt{e}-type argument expansion.
% \end{texnote}
% \end{function}
%
% \begin{function}[EXP, added = 2021-05-10]{\clist_use:nnnn, \clist_use:nn}
% \begin{syntax}
% \cs{clist_use:nnnn} \meta{comma~list} \Arg{separator~between~two} \Arg{separator~between~more~than~two} \Arg{separator~between~final~two}
% \cs{clist_use:nn} \meta{comma~list} \Arg{separator}
% \end{syntax}
% Places the contents of the \meta{comma~list} in the input stream,
% with the appropriate \meta{separator} between the items. As for
% \cs{clist_set:Nn}, blank items are omitted, spaces are removed from
% both sides of each item, then a set of braces is removed if the
% resulting space-trimmed item is braced. The \meta{separators} are
% then inserted in the same way as for \cs{clist_use:Nnnn} and
% \cs{clist_use:Nn}, respectively.
% \begin{texnote}
% The result is returned within the \tn{unexpanded}
% primitive (\cs{exp_not:n}), which means that the \meta{items}
% do not expand further when appearing in an \texttt{x}-type
% or \texttt{e}-type argument expansion.
% \end{texnote}
% \end{function}
%
% \section{Comma lists as stacks}
%
% Comma lists can be used as stacks, where data is pushed to and popped
% from the top of the comma list. (The left of a comma list is the top, for
% performance reasons.) The stack functions for comma lists are not
% intended to be mixed with the general ordered data functions detailed
% in the previous section: a comma list should either be used as an
% ordered data type or as a stack, but not in both ways.
%
% \begin{function}[noTF, added = 2012-05-14, updated = 2019-02-16]
% {\clist_get:NN, \clist_get:cN}
% \begin{syntax}
% \cs{clist_get:NN} \meta{comma list} \meta{token list variable}
% \end{syntax}
% Stores the left-most item from a \meta{comma list} in the
% \meta{token list variable} without removing it from the
% \meta{comma list}. The \meta{token list variable} is assigned locally.
% In the non-branching version, if the \meta{comma list} is empty the
% \meta{token list variable} is set to the marker value \cs{q_no_value}.
% \end{function}
%
% \begin{function}[updated = 2011-09-06]{\clist_pop:NN, \clist_pop:cN}
% \begin{syntax}
% \cs{clist_pop:NN} \meta{comma list} \meta{token list variable}
% \end{syntax}
% Pops the left-most item from a \meta{comma list} into the
% \meta{token list variable}, \emph{i.e.}~removes the item from the
% comma list and stores it in the \meta{token list variable}.
% Both of the variables are assigned locally.
% \end{function}
%
% \begin{function}{\clist_gpop:NN, \clist_gpop:cN}
% \begin{syntax}
% \cs{clist_gpop:NN} \meta{comma list} \meta{token list variable}
% \end{syntax}
% Pops the left-most item from a \meta{comma list} into the
% \meta{token list variable}, \emph{i.e.}~removes the item from the
% comma list and stores it in the \meta{token list variable}.
% The \meta{comma list} is modified globally, while the assignment of
% the \meta{token list variable} is local.
% \end{function}
%
% \begin{function}[TF, added = 2012-05-14]{\clist_pop:NN, \clist_pop:cN}
% \begin{syntax}
% \cs{clist_pop:NNTF} \meta{comma list} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{comma list} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{comma list} is non-empty, pops the top item from the
% \meta{comma list} in the \meta{token list variable}, \emph{i.e.}~removes
% the item from the \meta{comma list}. Both the \meta{comma list} and the
% \meta{token list variable} are assigned locally.
% \end{function}
%
% \begin{function}[TF, added = 2012-05-14]{\clist_gpop:NN, \clist_gpop:cN}
% \begin{syntax}
% \cs{clist_gpop:NNTF} \meta{comma list} \meta{token list variable} \Arg{true code} \Arg{false code}
% \end{syntax}
% If the \meta{comma list} is empty, leaves the \meta{false code} in the
% input stream. The value of the \meta{token list variable} is
% not defined in this case and should not be relied upon. If the
% \meta{comma list} is non-empty, pops the top item from the
% \meta{comma list} in the \meta{token list variable}, \emph{i.e.}~removes
% the item from the \meta{comma list}. The \meta{comma list} is modified
% globally, while the \meta{token list variable} is assigned locally.
% \end{function}
%
% \begin{function}
% {
% \clist_push:Nn, \clist_push:NV, \clist_push:No, \clist_push:Nx,
% \clist_push:cn, \clist_push:cV, \clist_push:co, \clist_push:cx,
% \clist_gpush:Nn, \clist_gpush:NV, \clist_gpush:No, \clist_gpush:Nx,
% \clist_gpush:cn, \clist_gpush:cV, \clist_gpush:co, \clist_gpush:cx
% }
% \begin{syntax}
% \cs{clist_push:Nn} \meta{comma list} \Arg{items}
% \end{syntax}
% Adds the \Arg{items} to the top of the \meta{comma list}.
% Spaces are removed from both sides of each item as for any
% \texttt{n}-type comma list.
% \end{function}
%
% \section{Using a single item}
%
% \begin{function}[added = 2014-07-17, EXP]
% {\clist_item:Nn, \clist_item:cn, \clist_item:nn}
% \begin{syntax}
% \cs{clist_item:Nn} \meta{comma list} \Arg{integer expression}
% \end{syntax}
% Indexing items in the \meta{comma list} from~$1$ at the top (left), this
% function evaluates the \meta{integer expression} and leaves the
% appropriate item from the comma list in the input stream. If the
% \meta{integer expression} is negative, indexing occurs from the
% bottom (right) of the comma list. When the \meta{integer expression}
% is larger than the number of items in the \meta{comma list} (as
% calculated by \cs{clist_count:N}) then the function expands to
% nothing.
% \begin{texnote}
% The result is returned within the \tn{unexpanded}
% primitive (\cs{exp_not:n}), which means that the \meta{item}
% does not expand further when appearing in an \texttt{x}-type
% or \texttt{e}-type argument expansion.
% \end{texnote}
% \end{function}
%
% \begin{function}[EXP, added = 2016-12-06]
% {\clist_rand_item:N, \clist_rand_item:n, \clist_rand_item:c}
% \begin{syntax}
% \cs{clist_rand_item:N} \meta{clist~var}
% \cs{clist_rand_item:n} \Arg{comma list}
% \end{syntax}
% Selects a pseudo-random item of the \meta{comma list}. If the
% \meta{comma list} has no item, the result is empty.
% \begin{texnote}
% The result is returned within the \tn{unexpanded}
% primitive (\cs{exp_not:n}), which means that the \meta{item}
% does not expand further when appearing in an \texttt{x}-type
% or \texttt{e}-type argument expansion.
% \end{texnote}
% \end{function}
%
% \section{Viewing comma lists}
%
% \begin{function}[updated = 2021-04-29]{\clist_show:N, \clist_show:c}
% \begin{syntax}
% \cs{clist_show:N} \meta{comma list}
% \end{syntax}
% Displays the entries in the \meta{comma list} in the terminal.
% \end{function}
%
% \begin{function}[updated = 2013-08-03]{\clist_show:n}
% \begin{syntax}
% \cs{clist_show:n} \Arg{tokens}
% \end{syntax}
% Displays the entries in the comma list in the terminal.
% \end{function}
%
% \begin{function}[added = 2014-08-22, updated = 2021-04-29]{\clist_log:N, \clist_log:c}
% \begin{syntax}
% \cs{clist_log:N} \meta{comma list}
% \end{syntax}
% Writes the entries in the \meta{comma list} in the log file. See
% also \cs{clist_show:N} which displays the result in the terminal.
% \end{function}
%
% \begin{function}[added = 2014-08-22]{\clist_log:n}
% \begin{syntax}
% \cs{clist_log:n} \Arg{tokens}
% \end{syntax}
% Writes the entries in the comma list in the log file. See also
% \cs{clist_show:n} which displays the result in the terminal.
% \end{function}
%
% \section{Constant and scratch comma lists}
%
% \begin{variable}[added = 2012-07-02]{\c_empty_clist}
% Constant that is always empty.
% \end{variable}
%
% \begin{variable}[added = 2011-09-06]{\l_tmpa_clist, \l_tmpb_clist}
% Scratch comma lists for local assignment. These are never used by
% the kernel code, and so are safe for use with any \LaTeX3-defined
% function. However, they may be overwritten by other non-kernel
% code and so should only be used for short-term storage.
% \end{variable}
%
% \begin{variable}[added = 2011-09-06]{\g_tmpa_clist, \g_tmpb_clist}
% Scratch comma lists for global assignment. These are never used by
% the kernel code, and so are safe for use with any \LaTeX3-defined
% function. However, they may be overwritten by other non-kernel
% code and so should only be used for short-term storage.
% \end{variable}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3clist} implementation}
%
% \TestFiles{m3clist002}
%
% \begin{macrocode}
%<*package>
% \end{macrocode}
%
% \begin{macrocode}
%<@@=clist>
% \end{macrocode}
%
% \begin{variable}{\c_empty_clist}
% An empty comma list is simply an empty token list.
% \begin{macrocode}
\cs_new_eq:NN \c_empty_clist \c_empty_tl
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\l_@@_internal_clist}
% Scratch space for various internal uses. This comma list variable
% cannot be declared as such because it comes before \cs{clist_new:N}
% \begin{macrocode}
\tl_new:N \l_@@_internal_clist
% \end{macrocode}
% \end{variable}
%
% \begin{variable}{\s_@@_mark,\s_@@_stop}
% Internal scan marks.
% \begin{macrocode}
\scan_new:N \s_@@_mark
\scan_new:N \s_@@_stop
% \end{macrocode}
% \end{variable}
%
% \begin{macro}[EXP]{
% \@@_use_none_delimit_by_s_mark:w,
% \@@_use_none_delimit_by_s_stop:w,
% \@@_use_i_delimit_by_s_stop:nw
% }
% Functions to gobble up to a scan mark.
% \begin{macrocode}
\cs_new:Npn \@@_use_none_delimit_by_s_mark:w #1 \s_@@_mark { }
\cs_new:Npn \@@_use_none_delimit_by_s_stop:w #1 \s_@@_stop { }
\cs_new:Npn \@@_use_i_delimit_by_s_stop:nw #1 #2 \s_@@_stop {#1}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\@@_tmp:w}
% A temporary function for various purposes.
% \begin{macrocode}
\cs_new_protected:Npn \@@_tmp:w { }
% \end{macrocode}
% \end{macro}
%
% \subsection{Removing spaces around items}
%
% \begin{macro}{\@@_trim_next:w}
% Called as \cs{exp:w} \cs{@@_trim_next:w} \cs{prg_do_nothing:}
% \meta{comma list} \ldots{} it expands to \Arg{trimmed item} where
% the \meta{trimmed item} is the first non-empty result from removing
% spaces from both ends of comma-delimited items in the \meta{comma
% list}. The \cs{prg_do_nothing:} marker avoids losing braces. The
% test for blank items is a somewhat optimized \cs{tl_if_empty:oTF}
% construction; if blank, another item is sought, otherwise trim
% spaces.
% \begin{macrocode}
\cs_new:Npn \@@_trim_next:w #1 ,
{
\tl_if_empty:oTF { \use_none:nn #1 ? }
{ \@@_trim_next:w \prg_do_nothing: }
{ \tl_trim_spaces_apply:oN {#1} \exp_end: }
}
% \end{macrocode}
% \end{macro}
%
% \begin{macro}[rEXP]{\@@_sanitize:n}
% \begin{macro}{\@@_sanitize:Nn}
% The auxiliary \cs{@@_sanitize:Nn} receives a delimiter
% (\cs{c_empty_tl} the first time, afterwards a comma) and that item
% as arguments. Unless we are done with the loop it calls
% \cs{@@_wrap_item:w} to unbrace the item (using a comma delimiter is
% safe since |#2| came from removing spaces from an argument delimited
% by a comma) and possibly re-brace it if needed.
% \begin{macrocode}
\cs_new:Npn \@@_sanitize:n #1
{
\exp_after:wN \@@_sanitize:Nn \exp_after:wN \c_empty_tl
\exp:w \@@_trim_next:w \prg_do_nothing:
#1 , \s_@@_stop \prg_break: , \prg_break_point:
}
\cs_new:Npn \@@_sanitize:Nn #1#2
{
\@@_use_none_delimit_by_s_stop:w #2 \s_@@_stop
#1 \@@_wrap_item:w #2 ,
\exp_after:wN \@@_sanitize:Nn \exp_after:wN ,
\exp:w \@@_trim_next:w \prg_do_nothing:
}
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[TF]{\@@_if_wrap:n}
% \begin{macro}{\@@_if_wrap:w}
% True if the argument must be wrapped to avoid getting altered by some
% clist operations. That is the case whenever the argument
% \begin{itemize}
% \item starts or end with a space or contains a comma,
% \item is empty, or
% \item consists of a single braced group.
% \end{itemize}
% If the argument starts or ends with a space or contains
% a comma then one of the three arguments of \cs{@@_if_wrap:w} will
% have its end delimiter (partly) in one of the three copies of |#1|
% in \cs{@@_if_wrap:nTF}; this has a knock-on effect meaning that the
% result of the expansion is not empty; in that case, wrap.
% Otherwise, the argument is safe unless it starts with a brace group
% (or is empty) and it is empty or consists of a single
% \texttt{n}-type argument.
% \begin{macrocode}
\prg_new_conditional:Npnn \@@_if_wrap:n #1 { TF }
{
\tl_if_empty:oTF
{
\@@_if_wrap:w
\s_@@_mark ? #1 ~ \s_@@_mark ? ~ #1
\s_@@_mark , ~ \s_@@_mark #1 ,
}
{
\tl_if_head_is_group:nTF { #1 { } }
{
\tl_if_empty:nTF {#1}
{ \prg_return_true: }
{
\tl_if_empty:oTF { \use_none:n #1}
{ \prg_return_true: }
{ \prg_return_false: }
}
}
{ \prg_return_false: }
}
{ \prg_return_true: }
}
\cs_new:Npn \@@_if_wrap:w #1 \s_@@_mark ? ~ #2 ~ \s_@@_mark #3 , { }
% \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@@_wrap_item:w}
% Safe items are put in \cs{exp_not:n}, otherwise we put an extra set
% of braces.
% \begin{macrocode}
\cs_new:Npn \@@_wrap_item:w #1 ,
{ \@@_if_wrap:nTF {#1} { \exp_not:n { {#1} } } { \exp_not:n {#1} } }
% \end{macrocode}
% \end{macro}
%
% \subsection{Allocation and initialisation}
%
% \begin{macro}{\clist_new:N, \clist_new:c}
% \UnitTested
% Internally, comma lists are just token lists.
% \begin{macrocode}
\cs_new_eq:NN \clist_new:N \tl_new:N
\cs_new_eq:NN \clist_new:c \tl_new:c
% \end{macrocode}
% \end{macro}
%
% \begin{macro}
% {
% \clist_const:Nn, \clist_const:cn,
% \clist_const:Nx, \clist_const:cx
% }
% Creating and initializing a constant comma list is done by
% sanitizing all items (stripping spaces and braces).
% \begin{macrocode}
\cs_new_protected:Npn \clist_const:Nn #1#2
{ \tl_const:Nx #1 { \@@_sanitize:n {#2} } }
\cs_generate_variant:Nn \clist_const:Nn { c , Nx , cx }
% \end{macrocode}
% \end{macro}
%
% \begin{macro}{\clist_clear:N, \clist_clear:c}
% \UnitTested
% \begin{macro}{\clist_gclear:N, \clist_gclear:c}
% \UnitTested
% Clearing comma lists is just the same as clearing token lists.
% \begin{macrocode}
\cs_new_eq:NN \clist_clear:N \tl_clear:N
\cs_new_eq:NN \clist_clear:c \tl_clear:c
\cs_new_eq:NN \clist_gclear:N \tl_gclear:N
\cs_new_eq:NN \clist_gclear:c \tl_gclear:c