\ProvidesPackage{cu-util}[2016/03/31 v1.0 CU Util]

\RequirePackage{etoolbox}
%
% Some ideas taken from: 
% http://tex.stackexchange.com/questions/33197/parsing-a-macro-argument-character-by-character-for-conditional-execution
%
\def\cu@declare@accent#1{%
    \def\cu@@declare@accent##1##2\relax{%
        \@namedef{cu@accent@\detokenize{##2}}{\relax}%
    }%
    \cu@@declare@accent#1\relax
}%
%
\newcommand\cu@tokenizeletter[1]{%
    \bgroup
    \def\@accum{}%
    \def\cu@collectworker{%
        \cu@ifnextaccent{%
            \cu@collectworker@one
        }{%
            \expandafter \egroup \expandafter #1\expandafter {\@accum}%
        }%
    }%
    \def\cu@collectworker@one##1{%
        \edef\@accum{\@accum ##1}%
        \cu@collectworker
    }%
    \def\cu@collectworker@two##1##2{%
        \edef\@accum{\@accum ##1##2}%
        \cu@collectworker
    }%
    \cu@ifnextbgroup{\egroup #1}{%
        \cu@ifnextdigraph{\cu@collectworker@two}{\cu@collectworker@one}%
    }%
}%
%
\newrobustcmd\cu@ifnextletter[2]{%
    \bgroup
    \def\cu@worker{%
        \ifcat\noexpand\@let@token a%
            \expandafter\egroup\expandafter\@firstoftwo
        \else
            \expandafter\egroup\expandafter\@secondoftwo
        \fi
        {#1}{#2}%
    }%
    \futurelet\@let@token\cu@worker
}
%
\newrobustcmd\cu@ifnextaccent[2]{%
    \bgroup
    \def\cu@letter@i{%
        \ifdefequal{\@let@token}{ }{%
            \egroup #2% false
        }{%
            \cu@letter@ii
        }%
    }%
    \def\cu@letter@ii##1{%
        \ifcsdef{cu@accent@\detokenize{##1}}{%
            \egroup #1##1% true
        }{%
            \egroup #2##1% false
        }%
    }%
    \futurelet\@let@token\cu@letter@i
}%
%
% Detect Oy and oy digraphs
% kv command colors just one letter with all accents if any
\def\cu@declare@digraph#1#2\relax{%
    \@namedef{cu@digraph@start@\detokenize{#1}}{\relax}%
    \@namedef{cu@digraph@end@\detokenize{#2}}{\relax}%
}%
%
\cu@declare@digraph оу\relax
\cu@declare@digraph Оу\relax
\cu@declare@digraph ᲂу\relax
%
\newrobustcmd{\cu@ifnextdigraph}[2]{%
    \bgroup
    \def\cu@digraph@i{%
        \ifdefequal{\@let@token}{ }{%
            \egroup #2% false!
        }{%
            \cu@digraph@ii
        }%
    }%
    \def\cu@digraph@ii##1{%
        \ifcsdef{cu@digraph@start@\detokenize{##1}}{%
            \edef\@accum{\@accum##1}%
            \futurelet\@let@token\cu@digraph@iii
        }{%
            \egroup #2##1% false!
        }%
    }%
    \def\cu@digraph@iii##1{%
        \ifcsdef{cu@digraph@end@\detokenize{##1}}{%
            \expandafter \egroup \expandafter #1\@accum ##1% true!
        }{%
            \expandafter \egroup \expandafter #2\@accum ##1% false!
        }%
    }%
    \def\@accum{}%
    \futurelet\@let@token\cu@digraph@i
}%
%
\newrobustcmd{\cu@ifnextbgroup}[2]{%
    \bgroup
    \def\cu@bgroup{%
        \ifdefequal{\@let@token}{ }{%
            \egroup #2% false!
        }{%
            \egroup\@ifnextchar\bgroup{#1}{#2}%
        }%
    }%
    \futurelet\@let@token\cu@bgroup
}%
%
\def\cu@test@tokenizeletter#1#2{%
    \bgroup
        \def\cu@testframe##1##2\@nnil{%
            \edef\@aa{##1-##2}%
        }%
        \cu@tokenizeletter\cu@testframe #1\@nnil
        \def\@bb{#2}%
        \ifx\@aa\@bb\else
            \errmessage{TEST FAILED: #1: "\@aa" expected "\@bb"}%
        \fi
    \egroup
}%
%
% List all accents
%
%% declare all possible combineres (generated by make_declare_accents)
\cu@declare@accent{◌̀}
\cu@declare@accent{◌́}
\cu@declare@accent{◌̑}
\cu@declare@accent{◌̈}
\cu@declare@accent{◌҆}
\cu@declare@accent{◌̆}
\cu@declare@accent{◌̾}
\cu@declare@accent{◌꙽}
\cu@declare@accent{◌꙼}
\cu@declare@accent{◌̇}
\cu@declare@accent{◌̏}
\cu@declare@accent{◌҃}
\cu@declare@accent{◌҇}
\cu@declare@accent{◌ⷠ}
\cu@declare@accent{◌ⷡ}
\cu@declare@accent{◌ⷢ}
\cu@declare@accent{◌ⷣ}
\cu@declare@accent{◌ⷤ}
\cu@declare@accent{◌ⷥ}
\cu@declare@accent{◌ⷦ}
\cu@declare@accent{◌ⷧ}
\cu@declare@accent{◌ⷨ}
\cu@declare@accent{◌ⷩ}
\cu@declare@accent{◌ⷪ}
\cu@declare@accent{◌ⷫ}
\cu@declare@accent{◌ⷬ}
\cu@declare@accent{◌ⷭ}
\cu@declare@accent{◌ⷮ}
\cu@declare@accent{◌ⷯ}
\cu@declare@accent{◌ⷰ}
\cu@declare@accent{◌ⷱ}
\cu@declare@accent{◌ⷲ}
\cu@declare@accent{◌ⷳ}
\cu@declare@accent{◌ⷴ}
\cu@declare@accent{◌ⷵ}
\cu@declare@accent{◌ⷶ}
\cu@declare@accent{◌ⷷ}
\cu@declare@accent{◌ⷸ}
\cu@declare@accent{◌ⷹ}
\cu@declare@accent{◌ⷺ}
\cu@declare@accent{◌ⷻ}
\cu@declare@accent{◌ⷼ}
\cu@declare@accent{◌ⷽ}
\cu@declare@accent{◌ⷾ}
\cu@declare@accent{◌ⷿ}
\cu@declare@accent{◌ꙴ}
\cu@declare@accent{◌ꙵ}
\cu@declare@accent{◌ꙶ}
\cu@declare@accent{◌ꙷ}
\cu@declare@accent{◌ꙸ}
\cu@declare@accent{◌ꙹ}
\cu@declare@accent{◌ꙺ}
\cu@declare@accent{◌ꙻ}
\cu@declare@accent{◌꙼}
\cu@declare@accent{◌꙽}
\cu@declare@accent{◌҆}
\cu@declare@accent{◌҅}
%% end of generated block

\endinput
