I've recently learned how to create a macro to iterate over a token sequence using \afterassignment
and \let
in TeX.
Currently, I'm trying to apply this to a tokenization macro (where "tokenization" means splitting a character sequence around a delimiting character/ character-sequence); that is, an algorithm stringTokenize
which does the following:
- Iterate over
character
in the string - if
character
is not the delimiter (for example,,
): addcharacter
tobuffer
; ifcharacter
is the delimiter, expandbuffer
and do something with it (that is, pass to a given macro) and clearbuffer
.
This is not as simple as I thought however, because apparently you can't expand a character which is \let
to a command into another command (in order to create a "buffer" structure), for example,
\edef\buffer{\buffer\character}
will (after n
iterations) expand to
{\character\character(...)character}
which is n
times the character last \let
to \character
.
How do I do this?
Here's what I have so far:
\documentclass{minimal}
\makeatletter
\def\structures@stringIterate#1#2{%
\newcount\@@index%
\def\@@stop{§}
\def\@@next{\afterassignment\@@each\let\@@current= }
\def\@@each{%
\ifx\@@current\@@stop%
\let\@@next=\relax
\else%
\advance\@@index1\relax
#2{\the\@@index}{\@@current}\let\@@next=\@@next
\fi%
\@@next % macro expansion must be delayed until the condition has been evaluated
}
\@@next#1\@@stop
}
\def\structures@stringTokenize#1#2#3{%
\newcount\@@index
\@@index 0\relax
\def\@@buffer{}
\def\@@test##1##2{%
\ifx#2##2
\advance\@@index 1\relax
#3{\the\@@index}{\@@buffer}
\def\@@buffer{} % reset the buffer
\else
\edef\@@buffer{\@@buffer##2} % gather character
\fi
}
\structures@stringIterate{#1}{\@@test}
}
\makeatother
\begin{document}
\makeatletter
\def\@@print#1#2{token (#1): ``#2''\par}
\structures@stringTokenize{Humpty Dumpty sat on a wall, Humpty Dumpty had a great fall, All the King's horses and all the King's men, Couldn't put Humpty together again.}{,}{\@@print}
\makeatother
\end{document}
\def\foo#1<token>{<do stuff>}
.\futurelet
!\futurelet
in this context?