Newsgroups: fj.comp.texhax
Path: galaxy.trc.rwcp.or.jp!coconuts.jaist!wnoc-tyo-news!wnoc-sfc-news!wnoc-kyo-news!kuis-news!kuee-news!kuee!tsato
From: tsato@ibuki.kuee.kyoto-u.ac.jp (Toru Sato)
Subject: Re: replacing characters
In-Reply-To: mtoyo@Lgjp0.Lit.hokudai.ac.jp's message of 07 Jan 1995 15:11:05 JST
Message-ID: <TSATO.95Jan8183806@ibuki.kuee.kyoto-u.ac.jp>
Lines: 144
Sender: news@kuee.kuee.kyoto-u.ac.jp (KUEE News Administrator)
Nntp-Posting-Host: ibuki
Organization: Dept. of Electr. Eng. II, Kyoto Univ., Kyoto, Japan
References: <3elb9p$ij0@nameserv.sys.hokudai.ac.jp>
Distribution: fj
Date: Sun, 8 Jan 1995 09:38:06 GMT
Xref: galaxy.trc.rwcp.or.jp fj.comp.texhax:5637
X-originally-archived-at: http://galaxy.rwcp.or.jp/text/cgi-bin/newsarticle2?ng=fj.comp.texhax&nb=5637&hd=a
X-reformat-date: Mon, 18 Oct 2004 15:18:22 +0900
X-reformat-comment: Tabs were expanded into 4 column tabstops by the Galaxy's archiver. See http://katsu.watanabe.name/ancientfj/galaxy-format.html for more info.

In article <3elb9p$ij0@nameserv.sys.hokudai.ac.jp> mtoyo@Lgjp0.Lit.hokudai.ac.jp (Masayuki TOYOSHIMA) writes:

 |(j)(La)TeX$B$G!"J8;z$rCV$-49$($D$D=PNO$9$k$K$O!"$I$&$9$l$P$h$$$G$7$g$&$+!#(B
 |$BNc$($P(B
 |  { \ReplaceLefttoRight{a}{X} abc } abc
 |$B$H$9$k$H(B
 |  Xbc abc
$B$&$&$`!"$3$s$J$3$H9M$($k?M$O$$$J$$$N$+$H;W$C$F$^$7$?$,!"$$$k$s$G$9$M$'!#(B
$B;EMM$b$h$/;w$F$$$k$N$G6C$-$^$7$?!#(B

$B<B$O!"A0$K$=$l$r$d$m$&$H$7$F9T$-5M$^$j$^$7$?!#$"$kDxEY$O$G$-$?$s$G$9$,(B
$B!J$b$&$G$-$?$H;W$C$F;H$C$F$$$?$7!";d$NMQES$K$OLdBj$J$$$N$G!":#$G$b(B
$B;H$C$F$$$^$9$,!K$@$a$J>l9g$,$"$j$^$9!#%P%0$H$$$&$K$O$"$^$j$K$R$I$$$N$G!"(B
$B$[$&$C$F$"$C$?$N$G$9$,!"$I$J$?$+$h$$CN7C$r$*B_$72<$5$$(B _o_

$B;H$$J}$O(B
  \replace{abc}{xxx}{This is abc.}
$B$H$$$&46$8$G!"(B
  This is xxx.
$B$H=PNO$5$l$^$9!#7k2L$O(B \Rstring $B$H$$$&%^%/%m$K$b3JG<$5$l$k$N$G!"(B
$B$5$i$KCV49$r$9$k$3$H$b$G$-$^$9!#(B\Rstring $B$X$N3JG<$@$1$7$F=PNO$7$J$$(B
\Replace $B$H$$$&$N$b$"$j$^$9!#$3$l$i$OC1=cCV49$G$9!#(B
\greplace $B!J$*$h$S(B \Greplace$B!K$H$$$&7+$jJV$7CV49$r$9$k$N$b$"$j$^$9!#(B
$BJ8Kv$NJ8;zNs$rCV49$9$k$K$O!"(B
  \replace{abc.\END}{xxx.}{This is abc, and that is abc.}
$B$H$7$^$9!#(B
  This is abc, and that is xxx.
$B$H$J$j$^$9!#(B
$B>/$J$/$H$b(BASCII-jTeX$B$G$OF|K\8l!J#2%P%$%HJ8;z!K$b(BOK$B$G$9!#(B

$B!t(B $B$H!"$3$3$^$G$O$J$+$J$+$$$$$G$7$g(B ;_;

$BBgLdBj$O!"(B
  \replace{bba}{xxx}{This is bbba.}
$B$H$$$&$h$&$J>l9g$KCV49$7$=$3$J$&$3$H$G$9!#(B

$B8-L@$JFI<T=t;a$K$O$9$G$K$*$o$+$j$N$h$&$K!"ESCf$^$G$N$_(Bmatch$B$7$?J8;zNs$O(B
$BCV49$;$:$K=PNO$7$F$7$^$&$N$G!"(Bbbb $B$HMh$?;~E@$G$=$3$^$G=q$-=P$7$F$7$^$$$^$9!#(B
$B<!$K(B a $B$+$i%A%'%C%/$r;O$a$k$N$G$9$,!"$b$&CY$$!"$H$$$&$o$1$G$9!#(B

$B860x$O$o$+$j$^$7$?$,!"$3$l$r2r7h$9$k$N$O!J;d$N$d$jJ}$G$O!K$"$^$j$KLLE](B
$B$K;W$($^$9!#$=$l0J8e$R$^$b$J$$$N$G9M$($F$$$^$;$s!#(B

 |\catcode $B$,(B 11,12 $B$NJ8;z$r<+M3$KCV49$7$?$$$N$G!"(B
$B$3$l$OB?J,Bg>fIW$G$9!#$?$@$7!"J8Cf$K%^%/%m$,$"$C$F$bCV49;~$K$OE83+$7$^$;$s!#(B

$B$G$O!"$h$m$7$/!#(B
$B!!!!!!!!!!!!!!!!!!!!!!!!:4F#!!5|!wEE5$9)3XBhFs65<<!%5~ETBg3X(B
$B!!!!!!!!!!!!!!!!!!!!!!!!(Btsato@kuee.kyoto-u.ac.jp
%------------------------------------------------------------------
% \find{from}{text}
%    Find the position of `from' in `text' and set it to \Cposition.
%    It is set to 0 if not found.
% \replace{from}{to}{text}
%    Replace the first string `from' in `text' by `to'.
%    String at the end of `text' can be replaced by saying `from\END'.
%    A counter \Cposition is set to the position of the string 'from'
%    in `text', or to 0 if not found.
% \Replace{from}{to}{text}
%    Same as \replace, but the replaced string is not displayed, but
%    set to \Rstring, which can be used as `text' for further editing.
\long\def\find#1#2{\@Recursfalse\@Replace{#1}{}{#2}}
\long\def\replace#1#2#3{\@Recursfalse\@Replace{#1}{#2}{#3}\Rstring}
\long\def\Replace#1#2#3{\@Recursfalse\@Replace{#1}{#2}{#3}}
\long\def\greplace#1#2#3{\@Recurstrue\@Replace{#1}{#2}{#3}\Rstring}
\long\def\Greplace#1#2#3{\@Recurstrue\@Replace{#1}{#2}{#3}}
%
\long\def\@Replace#1#2#3{{%
 \global\Cposition=0\relax%
 \@tempcnta=-1% length of `from' string
 \expandafter\Fgeteachchar{}#1\end%
 \def\Substring{#2}\def\@tempb{}\def\@tempc{}%
 \@tempcntb=1% current position in `from' string
 \expandafter\Sgeteachchar#3\END\end%
 \ifnum\Cposition<0\global\Cposition=0\fi%
 \xdef\Rstring{\@tempb\@tempc}%
}}

\newif\if@Recurs
\newif\if@Rmatch
\def\Rwild{\?}   %  wild character. Change this if you need to use \?
\newcount\Cposition
\def\LETBLANK:{\let\letblank= }\LETBLANK: 
\def\END{:end:}
\long\def\Fgeteachchar{\futurelet\nextletchar\Fnextdefchar}
\long\def\Fnextdefchar#1{% store `from' string into \R@i, \R@ii, ...
\ifx #1\end \let\next=\relax%
\else%
  \ifx \nextletchar\letblank% a blank exists before the current char
    \advance\@tempcnta 1\relax%
    \expandafter\def\csname R@\@roman{\@tempcnta}\endcsname{\ }\relax%
  \fi%
  \advance\@tempcnta 1\relax%
  \expandafter\def\csname R@\@roman{\@tempcnta}\endcsname{#1}\relax%
  \let\next=\Fgeteachchar%
\fi\next}

\long\def\Sgeteachchar{\futurelet\nextletchar\Snextdefchar}
\long\def\Snextdefchar#1{% scan `text'
\ifx #1\end \let\next=\relax%
\else%
  \ifx \nextletchar\letblank% a blank exists before the current char
    \Scheck{\ }%
  \fi%
  \Scheck{#1}%
  \let\next=\Sgeteachchar%
\fi\next}

\long\def\Scheck#1{% find `from' string in `text' string
  \def\@tempa{#1}%
  \ifnum\Cposition>0\else\global\advance\Cposition -1\relax\fi%
  \ifnum\@tempcntb>\@tempcnta%    already finished. copy input string
    \ifx #1\END\let\@tempa=\empty\fi%
    \let\@tempd=\@tempb\edef\@tempb{\@tempd\@tempa}%
  \else% not yet. check the charcater
    \expandafter\ifx\csname R@\@roman{\@tempcntb}\endcsname\@tempa%
      \@Rmatchtrue\else\@Rmatchfalse\fi%
    \expandafter\ifx\csname R@\@roman{\@tempcntb}\endcsname\Rwild%
      \ifx #1\END\@Rmatchfalse\else\@Rmatchtrue\fi\fi%
    \if@Rmatch%
      \ifnum\@tempcntb=\@tempcnta%     found the entire string
        \let\@tempd=\@tempb\edef\@tempb{\@tempd\Substring}%
        \def\@tempc{}%
        \global\multiply\Cposition-1\global\advance\Cposition-\@tempcnta%
        \global\advance\Cposition 1\relax%
        \if@Recurs%      `greplace' mode, start again
          \@tempcntb=1%
        \else%
          \advance\@tempcntb 1%    finished.
        \fi%
      \else%     found a non-last-character, keep searching
        \ifx #1\END\let\@tempa=\empty\fi%
        \let\@tempd=\@tempc\edef\@tempc{\@tempd\@tempa}%   save the input
        \advance\@tempcntb 1%    find the next character in `from'
      \fi%
    \else%     no match, restore the saved string
      \@tempcntb=1%
      \ifx #1\END\let\@tempa=\empty\fi%
      \let\@tempd=\@tempb\edef\@tempb{\@tempd\@tempc\@tempa}%
      \def\@tempc{}%
    \fi%
  \fi%
}
%-------------------------------------------
