Skip to content

Instantly share code, notes, and snippets.

@jfbu
Last active September 10, 2018 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfbu/4e17bddf28581b9a00ef34e68d00b04b to your computer and use it in GitHub Desktop.
Save jfbu/4e17bddf28581b9a00ef34e68d00b04b to your computer and use it in GitHub Desktop.
CheckWhetherLeadingSpace.tex
% Time-stamp: <10-09-2018 à 17:03:05>
% cf https://tex.stackexchange.com/a/449979/4686
\catcode`@ 11
\long\def\@gobble#1{}
\long\def\@gobbletwo#1#2{}
%\long\def\JFB@stop@firstoftwo#1#2{\z@#1}
\long\def\JFB@stop@secondoftwo#1#2{\z@#2}
\long\def\JFB@stop@thirdoffour#1#2#3#4{\z@#3}
%\long\def\JFB@stop@firstofthree#1#2#3{\z@#1}
\long\def\JFB@stop@secondofthree#1#2#3{\z@#2}
\long\def\JFB@stop@thirdofthree#1#2#3{\z@#3}
%%%%%%%%%%%%%%%%
% \JFB@CheckWhetherBrace
% (branches according to whether #1 starts or not with explicit
% character token of catcode 1)
\long\def\JFB@CheckWhetherBrace#1{%
\romannumeral
\expandafter\@gobble\expandafter
{\expandafter{\string#1.}\expandafter\JFB@stop@thirdoffour\string}%
\JFB@stop@secondoftwo
}%
testing checkwhetherbrace
\JFB@CheckWhetherBrace{cccc{aaaa}bbbb}{WRONG}{OK}
\JFB@CheckWhetherBrace{{aaaa}bbbb}{OK}{WRONG}
%%%%%%%%%%%%%%%%
% \JFB@CheckWhetherNull
% (branches according to whether #1 is or not empty)
\long\def\JFB@CheckWhetherNull#1{%
\romannumeral
\expandafter\expandafter\expandafter\@gobbletwo
\expandafter\string\expandafter{\expandafter{%
\expandafter{\string#1}\expandafter\@gobble\string}%
\iffalse}\JFB@stop@thirdoffour\fi
\JFB@stop@secondoftwo
}%
testing checkwhethernull
\JFB@CheckWhetherNull{}{OK}{WRONG}
\JFB@CheckWhetherNull{{}}{WRONG}{OK}
\JFB@CheckWhetherNull{ }{WRONG}{OK}
\JFB@CheckWhetherNull{ {}}{WRONG}{OK}
\JFB@CheckWhetherNull{{} }{WRONG}{OK}
\JFB@CheckWhetherNull{{ }}{WRONG}{OK}
\JFB@CheckWhetherNull{a}{WRONG}{OK}
\JFB@CheckWhetherNull{\error}{WRONG}{OK}
%%%%%%%%%%%%%%%%
% \JFB@CheckWhetherLeadingSpace
% (branches according to whether #1 starts or not
% with character code 32 space token)
% as in the other macros we could simplify with "delimiter" tokens
% but this would limit generality as they would be forbidden
% from argument
\long\def\JFB@CheckWhetherLeadingSpace#1{%
\romannumeral
\expandafter\JFB@CheckWhetherLeadingSpace@a\string{{}#1. }%
}%
\long\def\JFB@CheckWhetherLeadingSpace@a#1#2 {%
\JFB@CheckWhetherNull{#2}%
{\expandafter\JFB@stop@secondofthree}%
{\expandafter\JFB@stop@thirdofthree}%
\expandafter{\string}%
}
testing checkwhetherleadingspace
\JFB@CheckWhetherLeadingSpace{}{WRONG}{OK}
\JFB@CheckWhetherLeadingSpace{{}}{WRONG}{OK}
\JFB@CheckWhetherLeadingSpace{ }{OK}{WRONG}
\JFB@CheckWhetherLeadingSpace{ {}}{OK}{WRONG}
\JFB@CheckWhetherLeadingSpace{{} }{WRONG}{OK}
\JFB@CheckWhetherLeadingSpace{{ }}{WRONG}{OK}
\JFB@CheckWhetherLeadingSpace{a}{WRONG}{OK}
\JFB@CheckWhetherLeadingSpace{\error}{WRONG}{OK}
\JFB@CheckWhetherLeadingSpace{ \error}{OK}{WRONG}
\bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment