summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/l3kernel/l3text-case.dtx
blob: 3c608eacb9e2ff22ac19d0a5586ab057648b149c (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
% \iffalse meta-comment
%
%% File: l3text-case.dtx
%
% Copyright (C) 2020 The LaTeX3 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 \textsf{l3text-case} package: text processing (case changing)^^A
% }
%
% \author{^^A
%  The \LaTeX3 Project\thanks
%    {^^A
%      E-mail:
%        \href{mailto:latex-team@latex-project.org}
%          {latex-team@latex-project.org}^^A
%    }^^A
% }
%
% \date{Released 2020-01-12}
%
% \maketitle
%
% \begin{documentation}
%
% \end{documentation}
%
% \begin{implementation}
%
% \section{\pkg{l3text-case} implementation}
%
%    \begin{macrocode}
%<*initex|package>
%    \end{macrocode}
%
%    \begin{macrocode}
%<@@=text>
%    \end{macrocode}
%
% \subsection{Case changing}
%
% \begin{macro}[EXP]
%   {
%     \text_lowercase:n,
%     \text_uppercase:n,
%     \text_titlecase:n,
%     \text_titlecase_first:n
%   }
% \begin{macro}[EXP]
%   {
%     \text_lowercase:nn,
%     \text_uppercase:nn,
%     \text_titlecase:nn,
%     \text_titlecase_first:nn
%   }
%   The user level functions here are all wrappers around the internal
%   functions for case changing.
%    \begin{macrocode}
\cs_new:Npn \text_lowercase:n #1
  { \@@_change_case:nnn { lower } { } {#1} }
\cs_new:Npn \text_uppercase:n #1
  { \@@_change_case:nnn { upper } { } {#1} }
\cs_new:Npn \text_titlecase:n #1
  { \@@_change_case:nnn { title } { } {#1} }
\cs_new:Npn \text_titlecase_first:n #1
  { \@@_change_case:nnn { titleonly } { } {#1} }
\cs_new:Npn \text_lowercase:nn #1#2
  { \@@_change_case:nnn { lower } {#1} {#2} }
\cs_new:Npn \text_uppercase:nn #1#2
  { \@@_change_case:nnn { upper } {#1} {#2} }
\cs_new:Npn \text_titlecase:nn #1#2
  { \@@_change_case:nnn { title } {#1} {#2} }
\cs_new:Npn \text_titlecase_first:nn #1#2
  { \@@_change_case:nnn { titleonly } {#1} {#2} }
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_change_case:nnn, \@@_change_case_aux:nnn}
% \begin{macro}[EXP]{\@@_change_case_store:n}
% \begin{macro}[EXP]{\@@_change_case_store:nw}
% \begin{macro}[EXP]{\@@_change_case_result:n} 
% \begin{macro}[EXP]{\@@_change_case_end:w}
% \begin{macro}[EXP]{\@@_change_case_loop:nnw}
% \begin{macro}[EXP]{\@@_change_case_break:w}
% \begin{macro}[EXP]
%   {
%     \@@_change_case_group_lower:nnn     ,
%     \@@_change_case_group_upper:nnn     ,
%     \@@_change_case_group_title:nnn     ,
%     \@@_change_case_group_titleonly:nnn
%   }
% \begin{macro}[EXP]{\@@_change_case_space:nnw}
% \begin{macro}[EXP]{\@@_change_case_N_type:nnN, \@@_change_case_N_type_aux:nnN}
% \begin{macro}[EXP]{\@@_change_case_N_type:nnnN}
% \begin{macro}[EXP]{\@@_change_case_math_search:nnNNN}
% \begin{macro}[EXP]{\@@_change_case_math_loop:nnNw}
% \begin{macro}[EXP]{\@@_change_case_math_N_type:nnNN}
% \begin{macro}[EXP]{\@@_change_case_math_group:nnNn}
% \begin{macro}[EXP]{\@@_change_case_math_space:nnNw}
% \begin{macro}[EXP]{\@@_change_case_cs_check:nnN}
% \begin{macro}[EXP]{\@@_change_case_exclude:nnN}
% \begin{macro}[EXP]{\@@_change_case_exclude:nnnN}
% \begin{macro}[EXP]{\@@_change_case_exclude:nnNN}
% \begin{macro}[EXP]{\@@_change_case_exclude:nnNn}
% \begin{macro}[EXP]
%   {
%     \@@_change_case_letterlike_lower:nnN ,
%     \@@_change_case_letterlike_upper:nnN ,
%     \@@_change_case_letterlike_title:nnN ,
%     \@@_change_case_letterlike_titleonly:nnN
%   }
% \begin{macro}[EXP]{\@@_change_case_letterlike:nnnnN}
% \begin{macro}[EXP]
%   {\@@_change_case_char_lower:nnN, \@@_change_case_char_upper:nnN}
% \begin{macro}[EXP]{\@@_change_case_lower_sigma:nnnN}
% \begin{macro}[EXP]{\@@_change_case_lower_sigma:nnNw}
% \begin{macro}[EXP]{\@@_change_case_lower_sigma:NnnN}
% \begin{macro}[EXP]
%   {\@@_change_case_char_title:nnN, \@@_change_case_char_titleonly:nnN}
% \begin{macro}[EXP]
%   {\@@_change_case_char_title:nN, \@@_change_case_char_titleonly:nN}
% \begin{macro}[EXP]{\@@_change_case_char_title:nnnN}
% \begin{macro}[EXP]{\@@_change_case_char_char:nnnN}
% \begin{macro}[EXP]{\@@_change_case_char_UTFviii:nnnNN}
% \begin{macro}[EXP]{\@@_change_case_char_UTFviii:nnnNNN}
% \begin{macro}[EXP]{\@@_change_case_char_UTFviii:nnnNNNNN}
% \begin{macro}[EXP]{\@@_change_case_char_UTFviii:nnnn}
% \begin{macro}[EXP]
%   {
%   \@@_change_case_char_next_lower:nn     ,
%   \@@_change_case_char_next_upper:nn     ,
%   \@@_change_case_char_next_title:nn     ,
%   \@@_change_case_char_next_titleonly:nn
%   }
% \begin{macro}[EXP]{\@@_change_case_char_next_end:nn}
%   As for the expansion code, the business end of case changing is the
%   handling of \texttt{N}-type tokens. First, we expand the input fully
%   (so the loops here don't need to worry about awkward look-aheads and the
%   like). Then we split into the different paths.
%
%   The code here needs to be \texttt{f}-type expandable to deal with the
%   situation where case changing is applied in running text. There, we
%   might have case changing as a document command and the text containing
%   other non-expandable document commands.
%   \begin{verbatim}
%     \cs_set_eq:NN \MakeLowercase \text_lowercase
%     ...
%     \MakeLowercase{\enquote*{A} text}
%   \end{verbatim}
%   If we use an \texttt{e}-type expansion and wrap each token in
%   \cs{exp_not:n}, that would explode: the document command grabs
%   \cs{exp_not:n} as an argument, and things go badly wrong. So we have to
%   wrap the entire result in exactly one \cs{exp_not:n}, or rather in the
%   kernel version.
%    \begin{macrocode}
\cs_new:Npn \@@_change_case:nnn #1#2#3
  {
     \__kernel_exp_not:w \exp_after:wN
      {
        \exp:w
        \exp_args:Ne \@@_change_case_aux:nnn
          { \text_expand:n {#3} }
          {#1} {#2}
      }
  }
\cs_new:Npn \@@_change_case_aux:nnn #1#2#3
  {
    \group_align_safe_begin:
    \@@_change_case_loop:nnw {#2} {#3} #1
      \q_recursion_tail \q_recursion_stop
    \@@_change_case_result:n { }
  }
%    \end{macrocode}
%   As for expansion, collect up the tokens for future use.
%    \begin{macrocode}
\cs_new:Npn \@@_change_case_store:n #1
  { \@@_change_case_store:nw {#1} }
\cs_generate_variant:Nn \@@_change_case_store:n { o , e , V , v }
\cs_new:Npn \@@_change_case_store:nw #1#2 \@@_change_case_result:n #3
  { #2 \@@_change_case_result:n { #3 #1 } }
\cs_new:Npn \@@_change_case_end:w #1 \@@_change_case_result:n #2
  {
    \group_align_safe_end:
    \exp_end:
    #2
  }
%    \end{macrocode}
%   The main loop is the standard \texttt{tl action} type.
%    \begin{macrocode}
\cs_new:Npn \@@_change_case_loop:nnw #1#2#3 \q_recursion_stop
  {
    \tl_if_head_is_N_type:nTF {#3}
      { \@@_change_case_N_type:nnN }
      {
        \tl_if_head_is_group:nTF {#3}
          { \use:c { @@_change_case_group_ #1 :nnn } }
          { \@@_change_case_space:nnw }
      }
    {#1} {#2} #3 \q_recursion_stop
  }
\cs_new:Npn \@@_change_case_break:w #1 \q_recursion_tail \q_recursion_stop
  {
    \@@_change_case_store:n {#1}
    \@@_change_case_end:w
  }
%    \end{macrocode}
%   For a group, we \emph{could} worry about whether this contains a character
%   or not. However, that would make life very complex for little gain: exactly
%   what a first character is is rather weakly-defined anyway. So if there is
%   a group, we simply assume that a character has been seen, and for title
%   case we switch to the \enquote{rest of the tokens} situation. To avoid
%   having too much testing, we use a two-step process here to allow the
%   titlecase functions to be separate.
%    \begin{macrocode}
\cs_new:Npn \@@_change_case_group_lower:nnn #1#2#3
  {
    \@@_change_case_store:o
      {
        \exp_after:wN
          {
            \exp:w
            \@@_change_case_aux:nnn {#3} {#1} {#2}
          }
      }
    \@@_change_case_loop:nnw {#1} {#2}
  }
\cs_new_eq:NN \@@_change_case_group_upper:nnn
  \@@_change_case_group_lower:nnn
\cs_new:Npn \@@_change_case_group_title:nnn #1#2#3
  {
    \@@_change_case_store:o
      {
        \exp_after:wN
          {
            \exp:w
            \@@_change_case_aux:nnn {#3} {#1} {#2}
          }
      }
    \@@_change_case_loop:nnw { lower } {#2}
  }
\cs_new:Npn \@@_change_case_group_titleonly:nnn #1#2#3
  {
    \@@_change_case_store:o
      {
        \exp_after:wN
          {
            \exp:w
            \@@_change_case_aux:nnn {#3} {#1} {#2}
          }
      }
    \@@_change_case_break:w
  }
\use:x
  {
    \cs_new:Npn \exp_not:N \@@_change_case_space:nnw ##1##2 \c_space_tl
  }
  {
    \@@_change_case_store:n { ~ }
    \@@_change_case_loop:nnw {#1} {#2}
  }
%    \end{macrocode}
%   The first step of handling \texttt{N}-type tokens is to filter out the
%   end-of-loop. That has to be done separately from the first real step
%   as otherwise we pick up the wrong delimiter. The loop here is the same
%   as the \texttt{expand} one, just passing the additional data long. If no
%   close-math token is found then the final clean-up is forced
%   (i.e.~there is no assumption of \enquote{well-behaved} input in terms of
%   math mode).
%    \begin{macrocode}
\cs_new:Npn \@@_change_case_N_type:nnN #1#2#3
  {
    \quark_if_recursion_tail_stop_do:Nn #3
      { \@@_change_case_end:w }
    \@@_change_case_N_type_aux:nnN {#1} {#2} #3
  }
\cs_new:Npn \@@_change_case_N_type_aux:nnN #1#2#3
  {
    \exp_args:NV \@@_change_case_N_type:nnnN
      \l_text_math_delims_tl {#1} {#2} #3
  }
\cs_new:Npn \@@_change_case_N_type:nnnN #1#2#3#4
  {
    \@@_change_case_math_search:nnNNN {#2} {#3} #4 #1
      \q_recursion_tail \q_recursion_tail
      \q_recursion_stop
  }
\cs_new:Npn \@@_change_case_math_search:nnNNN #1#2#3#4#5
  {
    \quark_if_recursion_tail_stop_do:Nn #4
      { \@@_change_case_cs_check:nnN {#1} {#2} #3 }
    \token_if_eq_meaning:NNTF #3 #4
      {
        \use_i_delimit_by_q_recursion_stop:nw
           {
             \@@_change_case_store:n {#3}
             \@@_change_case_math_loop:nnNw {#1} {#2} #5
           }
      }
      { \@@_change_case_math_search:nnNNN {#1} {#2} #3 }
  }
\cs_new:Npn \@@_change_case_math_loop:nnNw #1#2#3#4 \q_recursion_stop
  {
    \tl_if_head_is_N_type:nTF {#4}
      { \@@_change_case_math_N_type:nnNN }
      {
        \tl_if_head_is_group:nTF {#4}
          { \@@_change_case_math_group:nnNn }
          { \@@_change_case_math_space:nnNw }
      }
    {#1} {#2} #3 #4 \q_recursion_stop
  }
\cs_new:Npn \@@_change_case_math_N_type:nnNN #1#2#3#4
  {
    \quark_if_recursion_tail_stop_do:Nn #4
      { \@@_change_case_end:w }
    \@@_change_case_store:n {#4}
    \token_if_eq_meaning:NNTF #4 #3
      { \@@_change_case_loop:nnw {#1} {#2} }
      { \@@_change_case_math_loop:nnNw {#1} {#2} #3 }
  }
\cs_new:Npn \@@_change_case_math_group:nnNn #1#2#3#4
  {
    \@@_change_case_store:n { {#4} }
    \@@_change_case_math_loop:nnNw {#1} {#2} #3
  }
\use:x
  {
    \cs_new:Npn \exp_not:N \@@_change_case_math_space:nnNw ##1##2##3
      \c_space_tl
  }
  {
    \@@_change_case_store:n { ~ }
    \@@_change_case_math_loop:nnNw {#1} {#2} #3
  }
%    \end{macrocode}
%   Once potential math-mode cases are filtered out the next stage is to
%   test if the token grabbed is a control sequence: the two routes the code
%   may take are then very different.
%    \begin{macrocode}
\cs_new:Npn \@@_change_case_cs_check:nnN #1#2#3
  {
    \token_if_cs:NTF #3
      { \@@_change_case_exclude:nnN }
      { \use:c { @@_change_case_char_ #1 :nnN } }
        {#1} {#2} #3
  }
%    \end{macrocode}
%   To deal with a control sequence there is first a need to test if it is
%   on the list which indicate that case changing should be skipped. That's
%   done using a loop as for the other special cases. If a hit is found then
%   the argument is grabbed and passed through as-is.
%    \begin{macrocode}
\cs_new:Npn \@@_change_case_exclude:nnN #1#2#3
  {
    \exp_args:Ne \@@_change_case_exclude:nnnN
      {
        \exp_not:V \l_text_math_arg_tl
        \exp_not:V \l_text_case_exclude_arg_tl
      }
      {#1} {#2} #3
  }
\cs_new:Npn \@@_change_case_exclude:nnnN #1#2#3#4
  {
    \@@_change_case_exclude:nnNN {#2} {#3} #4 #1 
      \q_recursion_tail \q_recursion_stop
  }
\cs_new:Npn \@@_change_case_exclude:nnNN #1#2#3#4
  {
    \quark_if_recursion_tail_stop_do:Nn #4
      { \use:c { @@_change_case_letterlike_ #1 :nnN } {#1} {#2} #3 }
    \cs_if_eq:NNTF #3 #4
      {
        \use_i_delimit_by_q_recursion_stop:nw
          { \@@_change_case_exclude:nnNn {#1} {#2} #3 }
      }
      { \@@_change_case_exclude:nnNN {#1} {#2} #3 }
  }
\cs_new:Npn \@@_change_case_exclude:nnNn #1#2#3#4
  {
    \@@_change_case_store:n { #3 {#4} }
    \@@_change_case_loop:nnw {#1} {#2}
  }
%    \end{macrocode}
%  Letter-like commands may still be present: they are set up using a simple
%  lookup approach, so can easily be handled with no loop. If there is no
%  hit, we are at the end of the process: we loop around. Letter-like chars
%  are all available only in upper- and lowercase, so titlecasing maps to the
%  uppercase version.
%    \begin{macrocode}
\cs_new:Npn \@@_change_case_letterlike_lower:nnN #1#2#3
  { \@@_change_case_letterlike:nnnnN {#1} {#1} {#1} {#2} #3 }
\cs_new_eq:NN \@@_change_case_letterlike_upper:nnN
  \@@_change_case_letterlike_lower:nnN
\cs_new:Npn \@@_change_case_letterlike_title:nnN #1#2#3
  { \@@_change_case_letterlike:nnnnN { upper } { lower } {#1} {#2} #3 }
\cs_new:Npn \@@_change_case_letterlike_titleonly:nnN #1#2#3
  { \@@_change_case_letterlike:nnnnN { upper } { end } {#1} {#2} #3 }
\cs_new:Npn \@@_change_case_letterlike:nnnnN #1#2#3#4#5
  {
    \cs_if_exist:cTF { c_@@_ #1 case_ \token_to_str:N #5 _tl }
      {
        \@@_change_case_store:v
          { c_@@_ #1 case_ \token_to_str:N #5 _tl }
         \use:c { @@_change_case_char_next_ #2 :nn } {#2} {#4}
      }
      {
        \@@_change_case_store:n {#5}
        \cs_if_exist:cTF
          {
            c_@@_
            \str_if_eq:nnTF {#1} { lower } { upper } { lower }
            case_ \token_to_str:N #5 _tl
          }
          { \use:c { @@_change_case_char_next_ #2 :nn } {#2} {#4} }
          { \@@_change_case_loop:nnw {#3} {#4} }
      }
  }
%    \end{macrocode}
%   For upper- and lowercase changes, once we get to this stage there are only
%   a couple of questions remaining: is there a language-specific mapping and
%   is there the special case of a terminal sigma. If not, then we pass to
%   a simple character mapping.
%    \begin{macrocode}
\cs_new:Npx \@@_change_case_char_lower:nnN #1#2#3
  {
    \exp_not:N \cs_if_exist_use:cF { @@_change_case_lower_ #2 :nnnN }
      {
        \bool_lazy_or:nnTF
          { \sys_if_engine_luatex_p: }
          { \sys_if_engine_xetex_p: }
          { \exp_not:N \@@_change_case_lower_sigma:nnnN }
          { \exp_not:N \@@_change_case_char:nnnN }
       }
        {#1} {#1} {#2} #3
  }
\cs_new:Npn \@@_change_case_char_upper:nnN #1#2#3
  {
    \cs_if_exist_use:cF { @@_change_case_upper_ #2 :nnnN }
      { \@@_change_case_char:nnnN }
        {#1} {#1} {#2} #3
  }
%    \end{macrocode}
%   If the current character is an uppercase sigma, the a check is made on the
%   next item in the input.  If it is \texttt{N}-type and not a control sequence
%   then there is a look-ahead phase: the logic here is simply based on letters.
%   The one exception is Dutch: see below.
%    \begin{macrocode}
\bool_lazy_or:nnT
  { \sys_if_engine_luatex_p: }
  { \sys_if_engine_xetex_p: }
  {
    \cs_new:Npn \@@_change_case_lower_sigma:nnnN #1#2#3#4
      {
        \int_compare:nNnTF { `#4 } = { "03A3 }
          { \@@_change_case_lower_sigma:nnNw {#2} {#3} #4 }
          { \@@_change_case_char:nnnN {#1} {#2} {#3} #4 }
      }
    \cs_new:Npn \@@_change_case_lower_sigma:nnNw #1#2#3#4 \q_recursion_stop
      {
        \tl_if_head_is_N_type:nTF {#4}
          { \@@_change_case_lower_sigma:NnnN #3 }
          {
            \@@_change_case_store:e
              { \char_generate:nn { "03C2 } { \@@_char_catcode:N #3 } }
            \@@_change_case_loop:nnw
          }
            {#1} {#2} #4 \q_recursion_stop
      }
    \cs_new:Npn \@@_change_case_lower_sigma:NnnN #1#2#3#4
      {
        \@@_change_case_store:e
          {
            \token_if_letter:NTF #4
              { \char_generate:nn { "03C3 } { \@@_char_catcode:N #1 } }
              { \char_generate:nn { "03C2 } { \@@_char_catcode:N #1 } }
          }
        \@@_change_case_loop:nnw {#2} {#3} #4
      }
  }
%    \end{macrocode}
%   For titlecasing, we need to fully expand the new character to see if it
%   is a letter (or active) But that means looking ahead in the $8$-bit case, so
%   we have to grab the required tokens up-front. Life is a lot easier for Unicode
%   \TeX{}'s, where we just have one token to worry about. The one wrinkle here
%   is that for look-ahead we'd get into trouble: luckily, only Dutch has that
%   issue.
%    \begin{macrocode}
\cs_new:Npx \@@_change_case_char_title:nnN #1#2#3
  {
    \bool_lazy_or:nnTF
      { \sys_if_engine_luatex_p: }
      { \sys_if_engine_xetex_p: }
      { \exp_not:N \token_if_letter:NTF #3 }
      {
        \exp_not:N \bool_lazy_or:nnTF
          { \exp_not:N \token_if_letter_p:N #3 }
          { \exp_not:N \token_if_active_p:N #3 }
      }
      { \exp_not:N \use:c { @@_change_case_char_ #1 :nN } }
      { \exp_not:N \@@_change_case_char_title:nnnN { title } {#1} }
        {#2} #3
  }
\cs_new_eq:NN \@@_change_case_char_titleonly:nnN
  \@@_change_case_char_title:nnN
\cs_new:Npn \@@_change_case_char_title:nN #1#2
  { \@@_change_case_char_title:nnnN { title } { lower } {#1} #2 }
\cs_new:Npn \@@_change_case_char_titleonly:nN #1#2
  { \@@_change_case_char_title:nnnN { title } { end } {#1} #2 }
\cs_new:Npn \@@_change_case_char_title:nnnN #1#2#3#4
  {
    \cs_if_exist_use:cF { @@_change_case_title_ #3 :nnnN }
      {
        \cs_if_exist_use:cF { @@_change_case_upper_ #3 :nnnN }
          { \@@_change_case_char:nnnN }
      }
        {#1} {#2} {#3} #4
  }
%    \end{macrocode}
%   For Unicode engines we can handle all characters directly. However, for
%   the $8$-bit engines the aim is to deal with (a subset of) Unicode (UTF-8)
%   input. They deal with that by making the upper half of the range active,
%   so we look for that and if found work out how many UTF-8 octets there
%   are to deal with. Those can then be grabbed to reconstruct the full
%   Unicode character, which is then used in a lookup. (As will become
%   obvious below, there is no intention here of covering all of Unicode.)
%    \begin{macrocode}
\bool_lazy_or:nnTF
  { \sys_if_engine_luatex_p: }
  { \sys_if_engine_xetex_p: }
  {
    \cs_new:Npn \@@_change_case_char:nnnN #1#2#3#4
      {
        \@@_change_case_store:e
          { \use:c { char_ #1 case :N } #4 }
        \use:c { @@_change_case_char_next_ #2 :nn } {#2} {#3}
      }
  }
  {
    \cs_new:Npn \@@_change_case_char:nnnN #1#2#3#4
      {
        \int_compare:nNnTF { `#4 } > { "80 }
          {
            \int_compare:nNnTF { `#4 } < { "E0 }
              { \@@_change_case_char_UTFviii:nnnNN }
              {
                \int_compare:nNnTF { `#4 } < { "F0 }
                  { \@@_change_case_char_UTFviii:nnnNNN }
                  { \@@_change_case_char_UTFviii:nnnNNNN }
              }
                {#1} {#2} {#3} #4
          }
          {
            \@@_change_case_store:e{ \use:c { char_ #1 case :N } #4 }
            \use:c { @@_change_case_char_next_ #2 :nn } {#2} {#3}
          }
       }
    \cs_new:Npn \@@_change_case_char_UTFviii:nnnNN #1#2#3#4#5
      { \@@_change_case_char_UTFviii:nnnn {#1} {#2} {#3} {#4#5} }
    \cs_new:Npn \@@_change_case_char_UTFviii:nnnNNN #1#2#3#4#5#6
      { \@@_change_case_char_UTFviii:nnnn {#1} {#2} {#3} {#4#5#6} }
    \cs_new:Npn \@@_change_case_char_UTFviii:nnnNNNNN #1#2#3#4#5#6#7
      { \@@_change_case_char_UTFviii:nnnn {#1} {#2} {#3} {#4#5#6#7} }
    \cs_new:Npn \@@_change_case_char_UTFviii:nnnn #1#2#3#4
      {
        \cs_if_exist:cTF { c_@@_ #1 case_ \tl_to_str:n {#4} _tl }
          {
            \@@_change_case_store:v
              { c_@@_ #1 case_ \tl_to_str:n {#4} _tl }
          }
          { \@@_change_case_store:n {#4} }
        \use:c { @@_change_case_char_next_ #2 :nn } {#2} {#3}
      }
  }
\cs_new:Npn \@@_change_case_char_next_lower:nn #1#2
  { \@@_change_case_loop:nnw {#1} {#2} }
\cs_new_eq:NN \@@_change_case_char_next_upper:nn
  \@@_change_case_char_next_lower:nn
\cs_new_eq:NN \@@_change_case_char_next_title:nn
  \@@_change_case_char_next_lower:nn
\cs_new_eq:NN \@@_change_case_char_next_titleonly:nn
  \@@_change_case_char_next_lower:nn
\cs_new:Npn \@@_change_case_char_next_end:nn #1#2
  { \@@_change_case_break:w }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_change_case_upper_de-alt:nnnN}
%   A simple alternative version for German.
%    \begin{macrocode}
\bool_lazy_or:nnT
  { \sys_if_engine_luatex_p: }
  { \sys_if_engine_xetex_p: }
  {
    \cs_new:cpn { @@_change_case_upper_de-alt:nnnN } #1#2#3#4
      {
        \int_compare:nNnTF { `#4 } = { "00DF }
          {
            \@@_change_case_store:e
              { \char_generate:nn { "1E9E } { \@@_char_catcode:N #4 } }
            \use:c { @@_change_case_char_next_ #2 :nn }
              {#2} {#3}
          }
          { \@@_change_case_char:nnnN {#1} {#2} {#3} #4 }
      }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_change_case_upper_el:nnnN}
% \begin{macro}[EXP]{\@@_change_case_upper_el:nnnn}
% \begin{macro}[EXP]{\@@_change_case_upper_el_aux:nnnN}
% \begin{macro}[EXP]{\@@_change_case_upper_el_loop:nnw}
% \begin{macro}[EXP]{\@@_change_case_upper_el:nnN}
% \begin{macro}[EXP]{\@@_change_case_if_greek:nTF}
%   For Greek uppercasing, we need to know if characters \emph{in the Greek
%   range} have accents. That means doing a \textsc{nfd} conversion first, then
%   starting a search. As described by the Unicode \textsc{cldr}, Greek accents
%   need to be found \emph{after} any U+0308 (diaeresis) and are done in two
%   groups to allow for the canonical ordering.
%    \begin{macrocode}
\bool_lazy_or:nnT
  { \sys_if_engine_luatex_p: }
  { \sys_if_engine_xetex_p: }
  {
    \cs_new:Npn \@@_change_case_upper_el:nnnN #1#2#3#4
      {
        \@@_change_case_if_greek:nTF { `#4 }
          {
            \exp_args:Ne \@@_change_case_upper_el:nnnn
              { \char_to_nfd:N #4 } {#1} {#2} {#3}
          }
          { \@@_change_case_char:nnnN {#1} {#2} {#3} #4 }
      }
    \cs_new:Npn \@@_change_case_upper_el:nnnn #1#2#3#4
      { \@@_change_case_upper_el_aux:nnnN {#2} {#3} {#4} #1 }
    \cs_new:Npn \@@_change_case_upper_el_aux:nnnN #1#2#3#4
      {
        \@@_change_case_store:e { \use:c { char_ #1 case:N } #4 }
        \@@_change_case_upper_el_loop:nnw {#2} {#3}
      }
    \cs_new:Npn \@@_change_case_upper_el_loop:nnw
      #1#2#3 \q_recursion_stop
      {
        \tl_if_head_is_N_type:nTF {#3}
          { \@@_change_case_upper_el:nnN }
          { \@@_change_case_loop:nnw }
            {#1} {#2} #3 \q_recursion_stop
      }
%    \end{macrocode}
%   In addition to the Greek accents, we list three cases here where an
%   accent outside the Greek range has a \text{nfd} that would make it
%   equivalent. That includes U+0344, which has to insert U+0308.
%    \begin{macrocode}
    \cs_new:Npn \@@_change_case_upper_el:nnN #1#2#3
      {
        \token_if_cs:NTF #3
          { \@@_change_case_loop:nnw {#1} {#2} #3 }
          {
            \int_compare:nNnTF { `#3 } = { "0308 }
              {
                \@@_change_case_store:n {#3}
                \@@_change_case_upper_el_loop:nnw {#1} {#2}
              }
              {
                \bool_lazy_any:nTF
                  {
                    { \int_compare_p:nNn { `#3 } = { "0300 } }
                    { \int_compare_p:nNn { `#3 } = { "0301 } }
                    { \int_compare_p:nNn { `#3 } = { "0304 } }
                    { \int_compare_p:nNn { `#3 } = { "0306 } }
                    { \int_compare_p:nNn { `#3 } = { "0308 } }
                    { \int_compare_p:nNn { `#3 } = { "0313 } }
                    { \int_compare_p:nNn { `#3 } = { "0314 } }
                    { \int_compare_p:nNn { `#3 } = { "0342 } }
                    { \int_compare_p:nNn { `#3 } = { "0340 } }
                    { \int_compare_p:nNn { `#3 } = { "0341 } }
                    { \int_compare_p:nNn { `#3 } = { "0343 } }
                  }
                  { \@@_change_case_upper_el_loop:nnw {#1} {#2} }
                  {
                    \int_compare:nNnTF { `#3 } = { "0344 }
                      {
                        \@@_change_case_store:e
                          {
                            \char_generate:nn { "0308 }
                              { \@@_char_catcode:N #3 }
                          }
                        \@@_change_case_upper_el_loop:nnw {#1} {#2}
                      }
                      {
                        \int_compare:nNnTF { `#3 } = { "0345 }
                          { \@@_change_case_loop:nnw {#1} {#2} }
                          { \@@_change_case_loop:nnw {#1} {#2} #3 }
                      }
                  }
              }
          }
      }
    \prg_new_conditional:Npnn \@@_change_case_if_greek:n #1 { TF }
      {
        \if_int_compare:w #1 < "0370 \exp_stop_f:
          \prg_return_false:
        \else:
          \if_int_compare:w #1 > "03FF \exp_stop_f:
            \if_int_compare:w #1 < "1F00 \exp_stop_f:
              \prg_return_false:
            \else:
              \if_int_compare:w #1 > "1FFF \exp_stop_f:
                \prg_return_false:
              \else:
                \prg_return_true:
              \fi:
            \fi:
          \else:
            \prg_return_true:
          \fi:
        \fi:
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}[EXP]{\@@_change_case_title_el:nnnN}
%   Titlecasing retains accents, but to prevent the uppercasing code
%   from kicking in, there has to be an explicit function here.
%    \begin{macrocode}
\bool_lazy_or:nnT
  { \sys_if_engine_luatex_p: }
  { \sys_if_engine_xetex_p: }
  {
    \cs_new:Npn \@@_change_case_title_el:nnnN #1#2#3#4
      { \@@_change_case_char:nnnN {#1} {#2} {#3} #4 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]
%   {
%     \@@_change_cases_lower_lt:nnnN      ,
%     \@@_change_cases_lower_lt_auxi:nnnN ,
%     \@@_change_cases_lower_lt_auxii:nnnN
%   }
% \begin{macro}[rEXP]{\@@_change_case_lower_lt:nnw}
% \begin{macro}[rEXP]{\@@_change_case_lower_lt:nnN}
%   For  Lithuanian, the issue to be dealt with is dots over lower case
%   letters: these should be present if there is another accent. The first step
%   is a simple match attempt: look for the three uppercase accented letters
%   which should gain a dot-above char in their lowercase form.
%    \begin{macrocode}
\bool_lazy_or:nnT
  { \sys_if_engine_luatex_p: }
  { \sys_if_engine_xetex_p: }
  {
   \cs_new:Npn \@@_change_case_lower_lt:nnnN #1#2#3#4
     {
        \exp_args:Ne \@@_change_case_lower_lt_auxi:nnnN
          {
            \int_case:nn { `#4 }
              {
                { "00CC } { "0300 }
                { "00CD } { "0301 }
                { "0128 } { "0303 }
              }  
          }
            {#2} {#3} #4
      }
%    \end{macrocode}
%   If there was a hit, output the result with the dot-above and move on.
%   Otherwise, look for one of the three letters that can take a combining
%   accent: I, J and I-ogonek. 
%    \begin{macrocode}
    \cs_new:Npn \@@_change_case_lower_lt_auxi:nnnN #1#2#3#4
      {
        \tl_if_blank:nTF {#1}
          {
            \exp_args:Ne \@@_change_case_lower_lt_auxii:nnnN
              {
                \int_case:nn { `#4 }
                  {
                    { "0049 } { "0069 }
                    { "004A } { "006A }
                    { "012E } { "012F }
                  }  
              }
              {#2} {#3} #4
          }
          {
            \@@_change_case_store:e
              {
                \char_generate:nn { "0069 } { \@@_char_catcode:N #4 }
                \char_generate:nn { "0307 } { \@@_char_catcode:N #4 }
                \char_generate:nn {#1} { \@@_char_catcode:N #4 }
              }
            \@@_change_case_loop:nnw {#2} {#3}
          }
      }
%    \end{macrocode}
%   Again, branch depending on a hit. If there is one, we output the character
%   then need to look for a combining accent: as usual, we need to be aware of
%   the loop situation.
%    \begin{macrocode}
    \cs_new:Npn \@@_change_case_lower_lt_auxii:nnnN #1#2#3#4
      {
        \tl_if_blank:nTF {#1}
          { \@@_change_case_lower_sigma:nnnN {#2} {#2} {#3} #4 }
          {
            \@@_change_case_store:e
              { \char_generate:nn {#1} { \@@_char_catcode:N #4 } }
            \@@_change_case_lower_lt:nnw {#2} {#3}
          }
      }
    \cs_new:Npn \@@_change_case_lower_lt:nnw #1#2#3 \q_recursion_stop
      {
        \tl_if_head_is_N_type:nTF {#3}
          { \@@_change_case_lower_lt:nnN }
          { \@@_change_case_loop:nnw }
           {#1} {#2} #3 \q_recursion_stop
      }
    \cs_new:Npn \@@_change_case_lower_lt:nnN #1#2#3
      {
        \bool_lazy_and:nnT
          { ! \token_if_cs_p:N #3 }
          {
            \bool_lazy_any_p:n
              {
                { \int_compare_p:nNn { `#3 } = { "0300 } }
                { \int_compare_p:nNn { `#3 } = { "0301 } }
                { \int_compare_p:nNn { `#3 } = { "0303 } }
              }
          }
          {
            \@@_change_case_store:e
              { \char_generate:nn { "0307 } { \@@_char_catcode:N #3 } }
          }
        \@@_change_case_loop:nnw {#1} {#2} #3
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}[EXP]
%   {
%     \@@_change_cases_upper_lt:nnnN     ,
%     \@@_change_cases_upper_lt_aux:nnnN
%   }
% \begin{macro}[rEXP]{\@@_change_case_upper_lt:nnw}
% \begin{macro}[rEXP]{\@@_change_case_upper_lt:nnN}
%   The uppercasing version: first find i/j/i-ogonek, then look for the
%   combining char: drop it if present.
%    \begin{macrocode}
\bool_lazy_or:nnT
  { \sys_if_engine_luatex_p: }
  { \sys_if_engine_xetex_p: }
  {
   \cs_new:Npn \@@_change_case_upper_lt:nnnN #1#2#3#4
     {
        \exp_args:Ne \@@_change_case_upper_lt_aux:nnnN
          {
            \int_case:nn { `#4 }
              {
                { "0069 } { "0049 }
                { "006A } { "004A }
                { "012F } { "012E }
              }  
          }
            {#2} {#3} #4
      }
   \cs_new:Npn \@@_change_case_upper_lt_aux:nnnN #1#2#3#4
     {
       \tl_if_blank:nTF {#1}
         { \@@_change_case_char:nnnN { upper } {#2} {#3} #4 }
         {
           \@@_change_case_store:e
             { \char_generate:nn {#1} { \@@_char_catcode:N #4 } }
           \@@_change_case_upper_lt:nnw {#2} {#3}
         }
     }
    \cs_new:Npn \@@_change_case_upper_lt:nnw #1#2#3 \q_recursion_stop
      {
        \tl_if_head_is_N_type:nTF {#3}
          { \@@_change_case_upper_lt:nnN }
          { \use:c { @@_change_case_char_next_ #1 :nn } }
            {#1} {#2} #3 \q_recursion_stop
      }
    \cs_new:Npn \@@_change_case_upper_lt:nnN #1#2#3
      {
        \bool_lazy_and:nnTF
          { ! \token_if_cs_p:N #3 }
          { \int_compare_p:nNn { `#3 } = { "0307 } }
          { \use:c { @@_change_case_char_next_ #1 :nn } {#1} {#2} }
          { \use:c { @@_change_case_char_next_ #1 :nn } {#1} {#2} #3 }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_change_case_title_nl:nnnN}
% \begin{macro}[EXP]{\@@_change_case_title_nl:nnw}
% \begin{macro}[EXP]{\@@_change_case_title_nl:nnN}
%   For Dutch, there is a single look-ahead test for \texttt{ij} when
%   title casing. If the appropriate letters are found, produce \texttt{IJ}
%   and gobble the \texttt{j}/\texttt{J}.
%    \begin{macrocode}
\cs_new:Npn \@@_change_case_title_nl:nnnN #1#2#3#4
  {
    \bool_lazy_or:nnTF
      { \int_compare_p:nNn { `#4 } = { "0049 } }
      { \int_compare_p:nNn { `#4 } = { "0069 } }
      {
        \@@_change_case_store:e
          { \char_generate:nn { "0049 } { \@@_char_catcode:N #4 } }
        \@@_change_case_title_nl:nnw {#2} {#3}
      }
      { \@@_change_case_char:nnnN {#1} {#2} {#3} #4 }
  }
\cs_new:Npn \@@_change_case_title_nl:nnw #1#2#3 \q_recursion_stop
  {
    \tl_if_head_is_N_type:nTF {#3}
      { \@@_change_case_title_nl:nnN }
      { \use:c { @@_change_case_char_next_ #1 :nn } }
        {#1} {#2} #3 \q_recursion_stop
  }
\cs_new:Npn \@@_change_case_title_nl:nnN #1#2#3
  {
    \bool_lazy_and:nnTF
      { ! \token_if_cs_p:N #3 }
      {
        \bool_lazy_or_p:nn
          { \int_compare_p:nNn { `#3 } = { "004A } }
          { \int_compare_p:nNn { `#3 } = { "006A } }
      }
      {
        \@@_change_case_store:e
          { \char_generate:nn { "004A } { \@@_char_catcode:N #3 } }
        \use:c { @@_change_case_char_next_ #1 :nn } {#1} {#2}
      }
      { \use:c { @@_change_case_char_next_ #1 :nn } {#1} {#2} #3 }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}[EXP]{\@@_change_case_lower_tr:nnnN}
% \begin{macro}[EXP]{\@@_change_case_lower_tr:nnNw}
% \begin{macro}[EXP]{\@@_change_case_lower_tr:nnN}
% \begin{macro}[EXP]{\@@_change_case_lower_tr:nnnNN}
%   The Turkic languages need special treatment for dotted-i and dotless-i.
%   The lower casing rule can be expressed in terms of searching first for
%   either a dotless-I or a dotted-I. In the latter case the mapping is
%   easy, but in the former there is a second stage search.
%    \begin{macrocode}
\bool_lazy_or:nnTF
  { \sys_if_engine_luatex_p: }
  { \sys_if_engine_xetex_p: }
  {
    \cs_new:Npn \@@_change_case_lower_tr:nnnN #1#2#3#4
      {
        \int_compare:nNnTF { `#4 } = { "0049 }
          { \@@_change_case_lower_tr:nnNw {#1} {#3} #4 }
          {
            \int_compare:nNnTF { `#4 } = { "0130 }
              {
                \@@_change_case_store:e
                  { \char_generate:nn { "0069 } { \@@_char_catcode:N #4 } }
                \@@_change_case_loop:nnw {#1} {#3}
              }
              { \@@_change_case_lower_sigma:nnnN {#1} {#2} {#3} #4 }
          }
      }
%    \end{macrocode}
%   After a dotless-I there may be a dot-above character. If there is then
%   a dotted-i should be produced, otherwise output a dotless-i. When the
%   combination is found both the dotless-I and the dot-above char have to
%   be removed from the input.
%    \begin{macrocode}
    \cs_new:Npn \@@_change_case_lower_tr:nnNw #1#2#3#4 \q_recursion_stop
      {
        \tl_if_head_is_N_type:nTF {#4}
          { \@@_change_case_lower_tr:nnN }
          {
            \@@_change_case_store:e
              { \char_generate:nn { "0131 } { \@@_char_catcode:N #3 } }
            \@@_change_case_loop:nnw
          }
            {#1} {#2} #4 \q_recursion_stop
      }
    \cs_new:Npn \@@_change_case_lower_tr:nnN #1#2#3
      {
        \bool_lazy_or:nnTF
          { \token_if_cs_p:N #3 }
          { ! \int_compare_p:nNn { `#3 } = { "0307 } }
          {
            \@@_change_case_store:e 
              { \char_generate:nn { "0131 } { \@@_char_catcode:N #3 } }
            \@@_change_case_loop:nnw {#1} {#2} #3
          }
          {
            \@@_change_case_store:e
              { \char_generate:nn { "0069 } { \@@_char_catcode:N #3 } }
            \@@_change_case_loop:nnw {#1} {#2}
          }
      }
  }
%    \end{macrocode}
%   For $8$-bit engines, dot-above is not available so there is a simple
%   test for an upper-case I. Then we can look for the UTF-8 representation of
%   an upper case dotted-I without the combining char. If it's not there,
%   preserve the UTF-8 sequence as-is. With $8$bit engines, we cannot
%   completely preserve category codes, so we have to make some assumptions:
%   output a \enquote{normal} \texttt{i} for the dotted case. As the original
%   character here is catcode-$13$, we have to make a choice about handling
%   of |i|: generate a \enquote{normal} one.
%    \begin{macrocode}
  {
    \cs_new:Npn \@@_change_case_lower_tr:nnnN #1#2#3#4
      {
        \int_compare:nNnTF { `#4 } = { "0049 }
          {
            \@@_change_case_store:V \c_@@_dotless_i_tl
            \@@_change_case_loop:nnw {#1} {#3}
          }
          {
            \int_compare:nNnTF { `#4 } = { "00C4 }
              { \@@_change_case_lower_tr:nnnNN {#1} {#2} {#3} #4 }
              { \@@_change_case_char:nnnN {#1} {#2} {#3} #4 }
          }
      }
    \cs_new:Npn \@@_change_case_lower_tr:nnnNN #1#2#3#4#5
      {
        \int_compare:nNnTF { `#5 } = { "00B0 }
          {
            \@@_change_case_store:e
              {
                \char_generate:nn { "0069 }
                  { \char_value_catcode:n { "0069 } }
              }
            \@@_change_case_loop:nnw {#1} {#3}
          }
          { \@@_change_case_char:nnnN {#1} {#2} {#3} #4#5 }
      }
  }
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \begin{macro}[EXP]{\@@_change_case_upper_tr:nnnN}
%   Uppercasing is easier: just one exception with no context.
%    \begin{macrocode}
\cs_new:Npx \@@_change_case_upper_tr:nnnN #1#2#3#4
  {
    \exp_not:N \int_compare:nNnTF { `#4 } = { "0069 }
      {
        \bool_lazy_or:nnTF
          { \sys_if_engine_luatex_p: }
          { \sys_if_engine_xetex_p: }
          {
            \exp_not:N \@@_change_case_store:e
              {
                \exp_not:N \char_generate:nn { "0130 }
                  { \exp_not:N \@@_char_catcode:N #4 }
              }
          }
          {
            \exp_not:N \@@_change_case_store:V
            \exp_not:N \c_@@_dotted_I_tl
          }
        \exp_not:N \use:c { @@_change_case_char_next_ #2 :nn } {#2} {#3}
      }
      { \exp_not:N \@@_change_case_char:nnnN {#1} {#2} {#3} #4 }
  }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}[EXP]
%   {\@@_change_case_lower_az:nnnN, \@@_change_case_upper_az:nnnN}
%   Straight copies.
%    \begin{macrocode}
\cs_new_eq:NN \@@_change_case_lower_az:nnnN
  \@@_change_case_lower_tr:nnnN
\cs_new_eq:NN \@@_change_case_upper_az:nnnN
  \@@_change_case_upper_tr:nnnN
%    \end{macrocode}
% \end{macro}
%
% \subsection{Case changing data for $8$-bit engines}
%
% \begin{variable}
%   {
%     \c_@@_dotless_i_tl ,
%     \c_@@_dotted_I_tl  ,
%     \c_@@_i_ogonek_tl  ,
%     \c_@@_I_ogonek_tl
%   }
%  For cases where there is an $8$-bit option in the |T1| font set up,
%  a variant is provided in both cases.
%    \begin{macrocode}
\group_begin:
  \bool_lazy_or:nnF
    { \sys_if_engine_luatex_p: }
    { \sys_if_engine_xetex_p: }
    {
      \cs_set_protected:Npn \@@_tmp:w #1#2
        {
          \group_begin:
            \cs_set_protected:Npn \@@_tmp:w ##1##2##3##4
              {
                \tl_const:Nx #1
                  {
                    \exp_after:wN \exp_after:wN \exp_after:wN
                      \exp_not:N \char_generate:nn {##1} { 13 }
                    \exp_after:wN \exp_after:wN \exp_after:wN
                      \exp_not:N \char_generate:nn {##2} { 13 }
                  }
              }
            \use:x
              { \@@_tmp:w \char_to_utfviii_bytes:n { "#2 } }  
          \group_end:
        }
      \@@_tmp:w \c_@@_dotless_i_tl { 0131 }
      \@@_tmp:w \c_@@_dotted_I_tl  { 0130 }
      \@@_tmp:w \c_@@_i_ogonek_tl  { 012F }
      \@@_tmp:w \c_@@_I_ogonek_tl  { 012E }
    }
\group_end:
%    \end{macrocode}
% \end{variable}
%
% For $8$-bit engines we now need to define the case-change data for
% the multi-octet mappings. These need a list of what code points are
% doable in |T1| so the list is hard coded (there's no saving in loading
% the mappings dynamically). All of the straight-forward ones have two
% octets, so that is taken as read.
%    \begin{macrocode}
\group_begin:
  \bool_lazy_or:nnF
    { \sys_if_engine_luatex_p: }
    { \sys_if_engine_xetex_p: }
    {
      \cs_set_protected:Npn \@@_loop:nn #1#2
        {
          \quark_if_recursion_tail_stop:n {#1}
          \use:x
            {
              \@@_tmp:w
                \char_to_utfviii_bytes:n { "#1 }
                \char_to_utfviii_bytes:n { "#2 }
            }
          \@@_loop:nn
        }
      \cs_set_protected:Npn \@@_tmp:nnnn #1#2#3#4#5
        {
          \tl_const:cx
            {
              c_@@_ #1 case_
              \char_generate:nn {#2} { 12 }
              \char_generate:nn {#3} { 12 }
              _tl
            }
            {
              \exp_after:wN \exp_after:wN \exp_after:wN
                \exp_not:N \char_generate:nn {#4} { 13 }
              \exp_after:wN \exp_after:wN \exp_after:wN
                \exp_not:N \char_generate:nn {#5} { 13 }
            }
        }
      \cs_set_protected:Npn \@@_tmp:w #1#2#3#4#5#6#7#8
        {
          \tl_const:cx
            {
              c_@@_lowercase_
              \char_generate:nn {#1} { 12 }
              \char_generate:nn {#2} { 12 }
              _tl
            }
            {
              \exp_after:wN \exp_after:wN \exp_after:wN
                \exp_not:N \char_generate:nn {#5} { 13 }
              \exp_after:wN \exp_after:wN \exp_after:wN
                \exp_not:N \char_generate:nn {#6} { 13 }
            }
          \@@_tmp:nnnn { upper } {#5} {#6} {#1} {#2}
          \@@_tmp:nnnn { title } {#5} {#6} {#1} {#2}
        }
      \@@_loop:nn
        { 00C0 } { 00E0 }
        { 00C1 } { 00E1 }
        { 00C2 } { 00E2 }
        { 00C3 } { 00E3 }
        { 00C4 } { 00E4 }
        { 00C5 } { 00E5 }
        { 00C6 } { 00E6 }
        { 00C7 } { 00E7 }
        { 00C8 } { 00E8 }
        { 00C9 } { 00E9 }
        { 00CA } { 00EA }
        { 00CB } { 00EB }
        { 00CC } { 00EC }
        { 00CD } { 00ED }
        { 00CE } { 00EE }
        { 00CF } { 00EF }
        { 00D0 } { 00F0 }
        { 00D1 } { 00F1 }
        { 00D2 } { 00F2 }
        { 00D3 } { 00F3 }
        { 00D4 } { 00F4 }
        { 00D5 } { 00F5 }
        { 00D6 } { 00F6 }
        { 00D8 } { 00F8 }
        { 00D9 } { 00F9 }
        { 00DA } { 00FA }
        { 00DB } { 00FB }
        { 00DC } { 00FC }
        { 00DD } { 00FD }
        { 00DE } { 00FE }
        { 0100 } { 0101 }
        { 0102 } { 0103 }
        { 0104 } { 0105 }
        { 0106 } { 0107 }
        { 0108 } { 0109 }
        { 010A } { 010B }
        { 010C } { 010D }
        { 010E } { 010F }
        { 0110 } { 0111 }
        { 0112 } { 0113 }
        { 0114 } { 0115 }
        { 0116 } { 0117 }
        { 0118 } { 0119 }
        { 011A } { 011B }
        { 011C } { 011D }
        { 011E } { 011F }
        { 0120 } { 0121 }
        { 0122 } { 0123 }
        { 0124 } { 0125 }
        { 0128 } { 0129 }
        { 012A } { 012B }
        { 012C } { 012D }
        { 012E } { 012F }
        { 0132 } { 0133 }
        { 0134 } { 0135 }
        { 0136 } { 0137 }
        { 0139 } { 013A }
        { 013B } { 013C }
        { 013E } { 013F }
        { 0141 } { 0142 }
        { 0143 } { 0144 }
        { 0145 } { 0146 }
        { 0147 } { 0148 }
        { 014A } { 014B }
        { 014C } { 014D }
        { 014E } { 014F }
        { 0150 } { 0151 }
        { 0152 } { 0153 }
        { 0154 } { 0155 }
        { 0156 } { 0157 }
        { 0158 } { 0159 }
        { 015A } { 015B }
        { 015C } { 015D }
        { 015E } { 015F }
        { 0160 } { 0161 }
        { 0162 } { 0163 }
        { 0164 } { 0165 }
        { 0168 } { 0169 }
        { 016A } { 016B }
        { 016C } { 016D }
        { 016E } { 016F }
        { 0170 } { 0171 }
        { 0172 } { 0173 }
        { 0174 } { 0175 }
        { 0176 } { 0177 }
        { 0178 } { 00FF }
        { 0179 } { 017A }
        { 017B } { 017C }
        { 017D } { 017E }
        { 01CD } { 01CE }
        { 01CF } { 01D0 }
        { 01D1 } { 01D2 }
        { 01D3 } { 01D4 }
        { 01E2 } { 01E3 }
        { 01E6 } { 01E7 }
        { 01E8 } { 01E9 }
        { 01EA } { 01EB }
        { 01F4 } { 01F5 }
        { 0218 } { 0219 }
        { 021A } { 021B }
        \q_recursion_tail ?
        \q_recursion_stop
      \cs_set_protected:Npn \@@_tmp:w #1#2#3
        {
          \group_begin:
            \cs_set_protected:Npn \@@_tmp:w ##1##2##3##4
              {
                \tl_const:cx
                  {
                    c_@@_ #3 case_
                    \char_generate:nn {##1} { 12 }
                    \char_generate:nn {##2} { 12 }
                    _tl
                  }
                    {#2}
              }
            \use:x
              { \@@_tmp:w \char_to_utfviii_bytes:n { "#1 } }
          \group_end:
        }
      \@@_tmp:w { 00DF } { SS } { upper }
      \@@_tmp:w { 00DF } { Ss } { title }
      \@@_tmp:w { 0131 } { I }  { upper }
    }
  \group_end:
%    \end{macrocode}
%
% The (fixed) look-up mappings for letter-like control sequences.
%    \begin{macrocode}
\group_begin:
  \cs_set_protected:Npn \@@_change_case_setup:NN #1#2
    {
      \quark_if_recursion_tail_stop:N #1
      \tl_const:cn { c_@@_lowercase_ \token_to_str:N #1 _tl }
        { #2 }
      \tl_const:cn { c_@@_uppercase_ \token_to_str:N #2 _tl }
        { #1 }
      \@@_change_case_setup:NN
    }
  \@@_change_case_setup:NN
  \AA \aa
  \AE \ae
  \DH \dh
  \DJ \dj
  \IJ \ij
  \L  \l
  \NG \ng
  \O  \o
  \OE \oe
  \SS \ss
  \TH \th
  \q_recursion_tail ?
  \q_recursion_stop
  \tl_const:cn { c_@@_uppercase_ \token_to_str:N \i _tl } { I }
  \tl_const:cn { c_@@_uppercase_ \token_to_str:N \j _tl } { J }
\group_end:
%    \end{macrocode}
%
%    \begin{macrocode}
%</initex|package>
%    \end{macrocode}
%
% \end{implementation}
%
% \PrintIndex