Skip to content

Instantly share code, notes, and snippets.

@jfbu
Created September 11, 2018 09:02
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/1057afd0fb95e2f1b273ac539e4cfb47 to your computer and use it in GitHub Desktop.
Save jfbu/1057afd0fb95e2f1b273ac539e4cfb47 to your computer and use it in GitHub Desktop.
CheckWhetherOnlySpaces.tex
% Time-stamp: <11-09-2018 10:59:40 CEST>
% 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}
%%%%%%%%%%%%%%%%
% \JFB@CheckWhetherOnlySpaces
% (branches according to whether #1 consists only of space tokens,
% possibly none)
\long\def\JFB@CheckWhetherOnlySpaces#1{%
\romannumeral
\expandafter\JFB@CheckWhetherOnlySpaces@a\string{#1{}}%
}%
\long\def\JFB@CheckWhetherOnlySpaces@a#1#2{%
\JFB@CheckWhetherNull{#2}%
{\expandafter\JFB@CheckWhetherOnlySpaces@b}%
{\expandafter\JFB@stop@thirdofthree}%
\expandafter{\string}%
}
\long\def\JFB@CheckWhetherOnlySpaces@b#1{%
\expandafter\JFB@CheckWhetherNull\expandafter{\@gobble#1}%
\JFB@stop@firstoftwo\JFB@stop@secondoftwo
}
testing checkwhetheronlyspaces
\JFB@CheckWhetherOnlySpaces{}{OK}{WRONG}
\JFB@CheckWhetherOnlySpaces{{}}{WRONG}{OK}
\JFB@CheckWhetherOnlySpaces{ }{OK}{WRONG}
\JFB@CheckWhetherOnlySpaces{ {}}{WRONG}{OK}
\JFB@CheckWhetherOnlySpaces{{} }{WRONG}{OK}
\JFB@CheckWhetherOnlySpaces{{ }}{WRONG}{OK}
\JFB@CheckWhetherOnlySpaces{ { }}{WRONG}{OK}
\JFB@CheckWhetherOnlySpaces{a}{WRONG}{OK}
\JFB@CheckWhetherOnlySpaces{\error}{WRONG}{OK}
\JFB@CheckWhetherOnlySpaces{ \error}{WRONG}{OK}
\edef\foo{\noexpand\JFB@CheckWhetherOnlySpaces{\space\space\space}{OK}{WRONG}}
\foo
\edef\foo{\noexpand\JFB@CheckWhetherOnlySpaces{\space\space\space{}}{WRONG}{OK}}
\foo
\bye
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment