%D \module
%D   [       file=syst-str,
%D        version=2006.09.18,
%D          title=\CONTEXT\ System Macros,
%D       subtitle=String Processing,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\unprotect

%D We need a convenient way to change lccode vectors.

\dostepwiserecurse{0}{255}{1}
  {\@EA\chardef\csname-\recurselevel\endcsname\recurselevel}

\newtoks\withminorcharacters
\newtoks\withlowercharacters
\newtoks\withuppercharacters

\dostepwiserecurse{0}{31}{1}
  {\expanded
     {\appendtoks\noexpand\withcharacter\csname-\recurselevel\endcsname
        \noexpand\to\withminorcharacters}}

\dostepwiserecurse{32}{127}{1}
  {\expanded
     {\appendtoks\noexpand\withcharacter\csname-\recurselevel\endcsname
        \noexpand\to\withlowercharacters}}

\dostepwiserecurse{128}{255}{1}
  {\expanded
     {\appendtoks\noexpand\withcharacter\csname-\recurselevel\endcsname
        \noexpand\to\withuppercharacters}}

\chardef\obeyedlccode=`. % so <32 and >127 chars become .

%D \macros
%D   {retainlccodes}
%D
%D One way of manipulating characters is changing the their
%D \type{\lccode} and applying \type{\lowcase}. An example of
%D this can be found in \type{spec-mis}.

\def\setownlccode   #1{\lccode#1#1}
\def\setobeyedlccode#1{\lccode#1\obeyedlccode}

\def\obeylccodes
  {\let\withcharacter\setownlccode
   \the\withlowercharacters
   \let\withcharacter\setobeyedlccode
   \the\withminorcharacters
   \the\withuppercharacters
   \relax}

\def\retainlccodes
  {\let\withcharacter\setownlccode
   \the\withlowercharacters
   \the\withminorcharacters
   \the\withuppercharacters
   \relax}

%D I got tired of making dedicated clean up macros using the
%D same mechanism again and again, so now we have:
%D
%D \starttyping
%D \def\xxxx{abc.d} \replacecharacters\xxxx{a.}{-} \xxxx
%D \stoptyping

\def\replacecharacters#1#2#3% macro characters replacement
  {\bgroup
   \edef\ascii{#1}%
   \retainlccodes
   \def\docommand##1{\lccode\expandafter`\csname##1\endcsname=\expandafter`\csname#3\endcsname}%
   \handletokens#2\with\docommand
   \lowercase\@EA{\@EA\xdef\@EA\globalascii\@EA{\ascii}}%
   \egroup
   \dodoglobal\let#1\globalascii}

% \bgroup
% \catcode`\ =\@@other % needed in order to get it visible
% \lccode`\ =`\-
% \lowercase{\xdef\globalascii{aap noot}}%
% \egroup
%
% [\globalascii] {\tttf [\globalascii]}
%
% interesting:
%
% \setvalue{xxx}{oeps}
% \setvalue{test-test}{\getvalue{xxx}oeps}
% \bgroup\lccode`\ =`\-\lowercase{\egroup\csname test test\endcsname}
%
% \setvalue{test\string~test}{\getvalue{xxx}oeps}
% \bgroup\lccode`\ =`\~\lowercase{\egroup\csname test test\endcsname}
%
% \bgroup\catcode`\~=\@@letter\lowercase{\egroup\csname test~test\endcsname}

\def\separatestring#1\to#2%
  {\bgroup
   \retainlccodes
   \lccode `\ =`\,%
   % when typesetting: \catcode`\ =\@@other
   \edef\ascii{#1}%
   \lowercase\@EA{\@EA\xdef\@EA\globalascii\@EA{\ascii}}%
   \egroup
   \let#2\globalascii}

\def\unspacefilename#1\to#2%
  {\bgroup
   \def\obeyedspace{-}%
   \sanitizefilename#1\to\ascii
   \retainlccodes
   \lccode `\ =`\-%
   % when typesetting: \catcode`\ =\@@other
   \lowercase\@EA{\@EA\xdef\@EA\globalascii\@EA{\ascii}}%
   \egroup
   \let#2\globalascii}

\protect \endinput
