% Copyright 2016 Slavonic Computing Initialive
% http://sci.ponomar.net
% 
\ProvidesPackage{cu-num}[2016/03/03 v1.1 CU Numerics]

\@ifundefined{detokenize}{%
  \PackageError{cu-num}%
    {You must upgrade your TeX distribution to be able to use this package}%
    {This package depends on eTeX-based engine}%
}{}

%
% Tools
%
\def\cu@ifempty#1{%
  \if\relax\detokenize{#1}\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}%
% 
\def\cu@firstoffour\cu@repeat #1\stop@ #2#3#4#5{#2}%
\def\cu@firstofthree\cu@repeat@i #1\stop@ #2#3#4{#2}%
\def\cu@switch #1{%
  \if\relax\detokenize{#1}\relax
    \expandafter\cu@firstoffour
  \fi
  \cu@repeat #1\stop@
}%
\def\cu@repeat #1#2\stop@ #3{%
  \if\relax\detokenize{#2}\relax
    \expandafter\cu@firstofthree
  \fi
  \cu@repeat@i #2\stop@
}%
\def\cu@repeat@i #1#2\stop@ #3{%
  \if\relax\detokenize{#2}\relax
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}%
%
% Slavonic numbering system. Internals.
%
\def\cu@format@small@number#1#2#3{%
  \ifcase#1 \or
    \expandafter р\or
    \expandafter с\or
    \expandafter т\or
    \expandafter у\or
    \expandafter ф\or
    \expandafter х\or
    \expandafter ѱ\or
    \expandafter ѿ\or
    \expandafter ц\else
    \errmessage{cu-num: Internal error}%
  \fi
  \if 1#2%
    \ifcase#3 \or
      \expandafter а\or
      \expandafter в\or
      \expandafter г\or
      \expandafter д\or
      \expandafter є\or
      \expandafter ѕ\or
      \expandafter з\or
      \expandafter и\or
      \expandafter ѳ\else
      \errmessage{cu-num: Internal error}%
    \fi
    і%
  \else
    \ifcase#2 \or
      \expandafter і\or
      \expandafter к\or
      \expandafter л\or
      \expandafter м\or
      \expandafter н\or
      \expandafter ѯ\or
      \expandafter ѻ\or
      \expandafter п\or
      \expandafter ч\else
      \errmessage{cu-num: Internal error}%
    \fi
    \ifcase#3 \or
      \expandafter а\or
      \expandafter в\or
      \expandafter г\or
      \expandafter д\or
      \expandafter є\or
      \expandafter ѕ\or
      \expandafter з\or
      \expandafter и\or
      \expandafter ѳ\else
      \errmessage{cu-num: Internal error}%
    \fi
  \fi
}%
%
%
\protected\def\cu@titlo{\char"0483}%
\def\cu@thousand{҂}%
\protected\def\cu@spacer{\,}%
%
\protected\def\stop@{}%
\protected\def\divider@{DIVIDER}%
%
\def\cu@format#1{%
  \ifnum#1<0%
    \errmessage{cu-num: negative numbers not supported}%
  \else\ifnum#1=0%
    0\cu@titlo
  \else\ifnum#1<10%
    {}%
    {}%
    \cu@format@xxx 00#1\stop@
  \else\ifnum#1<100%
    {}%
    {}%
    \cu@format@xxx 0#1\stop@
  \else\ifnum#1<1000%
    {}%
    {}%
    \cu@format@xxx #1\stop@
  \else\ifnum#1<10000%
    {}%
    \cu@format@xxxxxx 00#1\stop@
  \else\ifnum#1<100000%
    {}%
    \cu@format@xxxxxx 0#1\stop@
  \else\ifnum#1<1000000%
    {}%
    \cu@format@xxxxxx #1\stop@
  \else\ifnum#1<10000000%
    \cu@format@xxxxxxxxx 00#1\stop@
  \else\ifnum#1<100000000%
    \cu@format@xxxxxxxxx 0#1\stop@
  \else\ifnum#1<1000000000%
    \cu@format@xxxxxxxxx #1\stop@
  \else
    \errmessage{cu-num: number too big "#1"}%
  \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi
}%
%
\def\cu@format@xxx#1#2#3\stop@{%
  {%
  \cu@format@small@number#1#2#3%
  }%
}%
%
\def\cu@format@xxxxxx#1#2#3#4#5#6\stop@{%
  {\cu@format@small@number#1#2#3}%
  {\cu@format@small@number#4#5#6}%
}%
%
\def\cu@format@xxxxxxxxx#1#2#3#4#5#6#7#8#9\stop@{%
  {\cu@format@small@number#1#2#3}%
  \cu@format@xxxxxx#4#5#6#7#8#9\stop@
}%
%
\def\cu@place@titlo #1{%
  \cu@switch{#1}{%
    {}%
  }{% 
    {\cu@place@titlo@i #1\stop@}%
  }{%
    {\cu@place@titlo@ii #1\stop@}%
  }{%
    {\cu@place@titlo@iii #1\stop@}%
  }%
}%
\def\cu@place@titlo@iii #1#2#3#4\stop@{%
  \cu@ifempty{#4}{%
    #1\cu@place@titlo@ii #2#3\stop@
  }{%
    #1\cu@place@titlo@iii #2#3#4\stop@
  }%
}%
\def\cu@place@titlo@ii #1#2\stop@{%
  \if #1ѿ%
    #1#2\cu@titlo\else #1\cu@titlo #2\fi
}%
\def\cu@place@titlo@i #1\stop@{%
  \if #10%
    0\else #1\cu@titlo \fi
}%
%
\def\cu@merge@historic #1#2#3{%
  \cu@switch{#2}{%
    {#1}{#2}{#3}%
  }{%
    {#1}{#2#3}{0}%
  }{%
    {#1}{#2}{#3}%
  }{%
    {#1}{#2}{#3}%
  }%
}%
%
\def\cu@place@thousands#1#2#3{%
  \cu@ifempty{#1}{%
    {}%
  }{%
    {\cu@thousand\cu@thousand #1}%
  }%
  \cu@ifempty{#2}{%
    {}%
  }{%
    \if#30%
      {\cu@thousand #2}%
    \else
      \cu@ifempty{#3}{%
        {\cu@interleave #2\stop@}%
      }{%
        {\cu@thousand #2}%
      }%
    \fi
  }%
  \if#30%
    {}%
  \else
    {#3}%
  \fi
}%
%
\def\cu@interleave#1#2\stop@{%
  \ifx#1\cu@titlo
    \cu@ifempty{#2}{#1}{%
       #1\cu@interleave #2\stop@
     }%
  \else
    \cu@ifempty{#2}{%
      \cu@thousand #1%
    }{%
      \cu@thousand #1\cu@interleave #2\stop@
    }%
  \fi
}%
%
\def\cu@place@all@titlo#1#2#3{%
  \cu@place@titlo{#1}%
  \cu@place@titlo{#2}%
  \cu@place@titlo{#3}%
}%
\def\cu@place#1{%
  \if\relax\detokenize{#1}\relax
    {}%
  \else
    \cu@place@i #1\stop@
  \fi
}%
\def\cu@place@i#1#2\stop@{%
  \if\relax\detokenize{#2}\relax
    {#1\cu@titlo}%
  \else
    \cu@place@ii #1#2\stop@
  \fi 
}%
\def\cu@place@ii #1#2#3\stop@{%
  \if\relax\detokenize{#3}\relax
    \if#1ѿ{#1#2\cu@titlo}\else
      {#1\cu@titlo #2}\fi
  \else
    \if#2ѿ{#1#2#3\cu@titlo}\else
      {#1#2\cu@titlo #3}\fi
  \fi
}%
%
\def\cu@compose#1#2#3{%
  \cu@ifempty{#1}{%
    \cu@compose@ii{#2}{#3}%
  }{%
    \cu@ifempty{#2}{%
      \cu@compose@ii{#1}{#3}%
    }{%
      \cu@ifempty{#3}{%
        \cu@compose@ii{#1}{#2}%
      }{%
        #1\cu@spacer #2\cu@spacer #3%
      }%
    }%
  }%
}%
%
\def\cu@compose@ii#1#2{%
  \cu@ifempty{#1}{%
    \cu@ifempty{#2}{}{#2}%
  }{%
    \cu@ifempty{#2}{#1}{#1\cu@spacer #2}%
  }%
}%
%
\def\cu@fmt@#1{%
  %\tracingmacros=1%
  %\tracingonline=1%
  \edef\@x{\number #1}%
  \ifnum\@x=0\relax
    \edef\@x{0\cu@titlo}%
  \else\ifnum\@x<0%
    \edef\@x{\number-\@x}%
    \edef\@x{\noexpand\cu@format{\@x}}%
    \edef\@x{\@x}%
    \edef\@x{\noexpand\cu@merge@historic\@x}%
    \edef\@x{\@x}%
    \edef\@x{\noexpand\cu@place@all@titlo\@x}%
    \edef\@x{\@x}%
    \edef\@x{\noexpand\cu@place@thousands\@x}%
    \edef\@x{\@x}%
    \edef\@x{\noexpand\cu@compose\@x}%
    \edef\@x{\@x}%
    \edef\@x{-\@x}%
  \else
    \edef\@x{\noexpand\cu@format{\@x}}%
    \edef\@x{\@x}%
    %\show\@x
    \edef\@x{\noexpand\cu@merge@historic\@x}%
    \edef\@x{\@x}%
    %\show\@x
    \edef\@x{\noexpand\cu@place@all@titlo\@x}%
    \edef\@x{\@x}%
    %\show\@x
    \edef\@x{\noexpand\cu@place@thousands\@x}%
    \edef\@x{\@x}%
    %\show\@x
    \edef\@x{\noexpand\cu@compose\@x}%
    \edef\@x{\@x}%
    %\show\@x
  \fi\fi
  %\tracingmacros=0%
}%
%
% User API
%
\protected\def\cuNum#1{%
  \bgroup
    \cu@fmt@{#1}%
    \@x
  \egroup
}%
