I am trying to make a fully expandable function that uses two arguments. The first one is the tokenlist
to test against the second argument another tokenlist
.
The test is a character by character comparison (I know regex is a better solution, and as a matter of fact maybe plain TeX as well), but I am trying to familiarize myself with LaTeX3.
How would you improve the code? The code with some more tests can be useful to check for numbers, vowels, valid character input etc.
\documentclass{article}
\usepackage{expl3,xcolor}
\begin{document}
\ExplSyntaxOn
\def\PASS{\par{\bfseries\textcolor{green!80!blue}{PASS\ }}}
\def\FAIL{\par{\bfseries\textcolor{red!70!black}{FAIL\ }}}
\cs_new:Npn \test_two:nn #1#2 {
% syntactic cyanide for expandafters
\cs_generate_variant:Nn \tl_if_in:NnTF {ff }
\cs_generate_variant:Nn \cs_gset:Npn {Npf}
\cs_generate_variant:Nn \tl_set_eq:NN {Nn}
\cs_generate_variant:Nn \int_step_inline:nnnn {nnfn, nnVn}
\cs_generate_variant:Nn \int_set_eq:NN {No,Nf}
\cs_generate_variant:Nn \tl_gset:Nn {No,Nf}
% set the lists
\tl_gset:Nx \temp {#2}
\tl_gset:Nx \temp_needle_tl {#1}
\tl_gset:Nn \head_tl {\tl_head:V \temp_needle_tl }
\tl_gset:Nn \start_tl {\temp_needle_tl }
% print some values
head~at~start~ \head_tl\par
tail~at~ start~ \start_tl\ par
% set iteration limit
\int_set_eq:Nf \g_tmpa_int {\tl_count:V\temp_needle_tl}
Number~of~items~to~test \int_use:N \g_tmpa_int\par
% iteration
\int_step_inline:nnnn {1} {1} {\g_tmpa_int}{
% test value
\tl_if_in:ffTF {\temp} {\head_tl}
{
\PASS \head_tl\ ~~\start_tl \par
}
{
\FAIL \head_tl\ ~~\start_tl \par
}
% Swap and go
\tl_gset:Nn \head_tl {\tl_head:f\start_tl }
\tl_gset:Nf \oldtail_tl {\start_tl }
\tl_gset:Nn \start_tl {\tl_tail:f \oldtail_tl}
}
}
\test_two:nn {1234567890AAA}{-1234567890)(}
\test_two:nn {apple}{aeiouAEIOU)(}
\end{document}
Edit: Originally I asked for some error correction, which I fixed. I am now asking for improvement to the code.
\cs_generate_variant:Nn
inside a macro? Why all those_set
and_gset
? If you want an expandable function, you definitely have to leave those out. I'm not sure what you want to do, but you have\tl_item:nn
at your disposition.generate_variant functions
out. I also changed all_set
toNx
these are\edefs
, so it should be closer to fully expandable.\edef
is an assignment, so it's not expandable.\tl_gset:Nn \head_tl{\tl_head:f \start_tl}
is completely wrong, because you probably want\tl_gset:Nf \head_tl {\tl_head:V \start_tl}
; instead of\tl_gset:Nf \oldtail_tl {\start_tl }
you should say\tl_gset_eq:NN \oldtail_tl \start_tl
. But it's unclear why global setting is used.