[Tex/LaTex] Implementing switch cases

conditionals

I'd like to have command built around a switch-case environment in LaTeX, much like the example below:

\documentclass{article}

\usepackage{xstring}

\newcommand{\dothis}[1]{%
\IfStrEqCase{#1}{{a}{so you typed a}
    {b}{now this is b}
    {c}{you want me to do c?}}
    [nada]
}

\begin{document}

\dothis{a}

\dothis{b}

\dothis{c}

\dothis{e}

\end{document}

My problem with it is that it requires the xstring package. Is there any other way to do this? Preferably without loading additional packages and avoiding disgraceful thickets of \if-\else-\fi statements?

Best Answer

The question asks to avoid packages, so while this is the method used by expl3 in \str_case:nnF I have recoded it with minimal support. The only package I've used is pdftexcmds, which is needed as the \pdfstrcmp primitive from pdfTeX is called \strcmp by XeTeX and has to be implemented in Lua for LuaTeX. This is easy enough to do without the package, but obscures the method: ask a separate question if required!

The general idea here is to set up a comparison loop in which the test is done expandably by \pdfstrcmp. The test string is passed every time, with the 'true' string inserted if there is a match by the 'tidy up' code. If there is no match at all then the 'else' code is inserted. The \romannumeral business means that it always requires exactly two expansions to do the work here:

\documentclass{article}
\usepackage{pdftexcmds}
\makeatletter
\newcommand*{\dothis}[1]{%
  \stringcases
    {#1}%
    {%
      {a}{so you typed a}%
      {b}{now this is b}%
      {c}{you want me to do c?}%
    }%
    {[nada]}%
}
\newcommand{\stringcases}[3]{%
  \romannumeral
    \str@case{#1}#2{#1}{#3}\q@stop
}
\newcommand{\str@case}[3]{%
  \ifnum\pdf@strcmp{\unexpanded{#1}}{\unexpanded{#2}}=\z@
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
    {\str@case@end{#3}}
    {\str@case{#1}}%
}
\newcommand{\str@case@end}{}
\long\def\str@case@end#1#2\q@stop{\z@#1}
\makeatother

\begin{document}

\dothis{a}

\dothis{b}

\dothis{c}

\dothis{e}

\end{document}