diff options
author | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2019-09-02 13:46:59 +0900 |
commit | e0c6872cf40896c7be36b11dcc744620f10adf1d (patch) | |
tree | 60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/latex/contrib/substr |
Initial commit
Diffstat (limited to 'macros/latex/contrib/substr')
-rw-r--r-- | macros/latex/contrib/substr/ChangeLog | 5 | ||||
-rw-r--r-- | macros/latex/contrib/substr/README | 36 | ||||
-rw-r--r-- | macros/latex/contrib/substr/substr.sty | 194 | ||||
-rw-r--r-- | macros/latex/contrib/substr/testsubstr.pdf | bin | 0 -> 18413 bytes | |||
-rw-r--r-- | macros/latex/contrib/substr/testsubstr.tex | 63 |
5 files changed, 298 insertions, 0 deletions
diff --git a/macros/latex/contrib/substr/ChangeLog b/macros/latex/contrib/substr/ChangeLog new file mode 100644 index 0000000000..c57dd3e456 --- /dev/null +++ b/macros/latex/contrib/substr/ChangeLog @@ -0,0 +1,5 @@ +2009/10/20 v1.2 Uwe Lück + - Fix \IfSubStringInString which did not work if the string was contained in the substring + +2005/11/29 v1.1 Harald Harders + - Add \IfBeforeSubStringEmpty and \IfBehindSubStringEmpty. diff --git a/macros/latex/contrib/substr/README b/macros/latex/contrib/substr/README new file mode 100644 index 0000000000..95fef13824 --- /dev/null +++ b/macros/latex/contrib/substr/README @@ -0,0 +1,36 @@ +substr.sty + +This package provides commands to deal with substrings in strings: +Determine if a string contains a substring, count appearances of a +substring in a string. + + +The Description of the commands can be found in the file substr.sty. + + + +History of this package: + +The package arises from a posting of me in the newsgroup +de.comp.text.tex in which I asked how to find out if a substring +is included in a string. Heiko Oberdiek +<oberdiek@ruf.uni-freiburg.de> posted the commands +\IfSubStringInString and \IfCharInString and suggested to write a +command which counts the appearances in a string. So, I wrote the +commands \CountSubStrings and \setcountertoSubStrings. +After I wrote this package I sent it to Heiko Oberdiek +who improved and rewrote many parts of it. + + + +Copyright 2000, 2005, 2009 Harald Harders + +This program can be redistributed and/or modified under the terms +of the LaTeX Project Public License Distributed from CTAN +archives in directory macros/latex/base/lppl.txt; either +version 1 of the License, or any later version. + + +2009-10-20 +Harald Harders +h.harders@tu-bs.de diff --git a/macros/latex/contrib/substr/substr.sty b/macros/latex/contrib/substr/substr.sty new file mode 100644 index 0000000000..0f5a74762b --- /dev/null +++ b/macros/latex/contrib/substr/substr.sty @@ -0,0 +1,194 @@ +%% +%% substr.sty +%% +%% This package provides commands to deal with substrings in strings: +%% Determine if a string contains a substring, count appearances of a +%% substring in a string. +%% +%% +%% Commands: +%% +%% \IfSubStringInString{substring}{string}{true part}{false part} +%% This command searches <substring> in <string> and executes the +%% <true part> if it is and else the <else part> +%% +%% \IfCharInString{char}{string}{true part}{false part} +%% Actualy the same as \IfSubStringInString. +%% +%% \BehindSubString{substring}{string} +%% Returns the part of <string> that is on the behind +%% <substring>. Always the first appearance of <substring> is taken. +%% +%% \BeforeSubString{substring}{string} +%% Returns the part of <string> that is on the before +%% <substring>. Always the first appearance of <substring> is taken. +%% +%% \CountSubStrings{substring}{string} +%% Counts the number of appearances of <substring> in <string> and +%% returns it as text. +%% +%% \SubStringsToCounter{counter}{substring}{string} +%% Counts the number of appearances of <substring> in <string> and +%% sets the counter <counter> to that value. +%% +%% \IfBeforeSubStringEmpty{substring}{string}{true part}{false part} +%% Calls <true part> if <substring> is equal to the beginning of <string>. +%% Else call <false part>. +%% +%% \IfBehindSubStringEmpty{substring}{string}{true part}{false part} +%% Calls <true part> if <substring> is equal to the end of <string>. +%% Else call <false part>. +%% +%% +%% History of this package: +%% +%% The package arises from a posting of me in the newsgroup +%% de.comp.text.tex in which I asked how to find out if a substring +%% is included in a string. Heiko Oberdiek +%% <oberdiek@ruf.uni-freiburg.de> posted the commands +%% \IfSubStringInString and \IfCharInString and suggested to write a +%% command which counts the appearances in a string. So, I wrote the +%% commands \CountSubStrings and \SubStringsToCounter. +%% After I wrote this package I sent it to Heiko Oberdiek +%% who improved and rewrote many parts of it. +%% +%% +%% ChangeLog +%% +%% 2009/10/20 v1.2 Uwe Lück +%% - Fix \IfSubStringInString which did not work if the string was contained in the substring +%% +%% 2005/11/29 v1.1 Harald Harders +%% - Add \IfBeforeSubStringEmpty and \IfBehindSubStringEmpty. +%% +%% +%% +%% Copyright 2000, 2005, 2009 Harald Harders +%% +%% This program can be redistributed and/or modified under the terms +%% of the LaTeX Project Public License Distributed from CTAN +%% archives in directory macros/latex/base/lppl.txt; either +%% version 1 of the License, or any later version. +%% +%% +%% 2009-10-20 +%% Harald Harders +%% h.harders@tu-bs.de +%% +\ProvidesPackage{substr}[2009/10/20 v1.2 Handle substrings] +%% +% expands the first and second argument with +% \protected@edef and calls #3 with them: +\newcommand\su@ExpandTwoArgs[3]{% + \protected@edef\su@SubString{#1}% + \protected@edef\su@String{#2}% + \expandafter\expandafter\expandafter#3% + \expandafter\expandafter\expandafter{% + \expandafter\su@SubString\expandafter + }\expandafter{\su@String}% +} +%% +%% tests if #1 in #2. If yes execute #3, else #4 +\newcommand*\IfSubStringInString[2]{% + \su@ExpandTwoArgs{#1}{#2}\su@IfSubStringInString +} +%% +\newcommand*\su@IfSubStringInString[2]{% + \def\su@compare##1#1##2\@nil{% + \def\su@param{##2}% + \ifx\su@param\@empty + \expandafter\@secondoftwo + \else + \expandafter\@firstoftwo + \fi + }% + \su@compare#2\@nnil#1\@nil +} +%% +%% tests if #1 in #2. If yes execute #3, else #4 +\newcommand\IfCharInString{} +\let\IfCharInString\IfSubStringInString +%% +%% returns the part of the string behind the found substring +\newcommand*\BehindSubString[2]{% + \su@ExpandTwoArgs{#1}{#2}\su@BehindSubString +} +\newcommand*\su@BehindSubString[2]{% + \def\su@rest##1#1##2\@nil{##2}% + \IfSubStringInString{#1}{#2}{\su@rest#2\@nil}{}% +} +%% +%% returns the part of the string before the found substring +\newcommand*\BeforeSubString[2]{% + \su@ExpandTwoArgs{#1}{#2}\su@BeforeSubString +} +\newcommand*\su@BeforeSubString[2]{% + \def\su@rest##1#1##2\@nil{##1}% + \IfSubStringInString{#1}{#2}{\su@rest#2\@nil}{#2}% +} +%% +%% calls #3 if part of string before substring is empty, otherwise calls #4. +\newcommand*\IfBeforeSubStringEmpty[2]{% + \su@ExpandTwoArgs{#1}{#2}\su@IfBeforeSubStringEmpty +} +%% +\newcommand*\su@IfBeforeSubStringEmpty[4]{% + \def\su@rest##1#1##2\@nil{##1}% + \IfSubStringInString{#1}{#2}{% + \edef\su@resta{\su@rest#2\@nil}% + \ifx\@empty\su@resta #3\else #4\fi + }{#4}% +} +%% +%% calls #3 if part of string after substring is empty, otherwise calls #4. +\newcommand*\IfBehindSubStringEmpty[2]{% + \su@ExpandTwoArgs{#1}{#2}\su@IfBehindSubStringEmpty +} +%% +\newcommand*\su@IfBehindSubStringEmpty[4]{% + \def\su@rest##1#1##2\@nil{##2}% + \IfSubStringInString{#1}{#2}{% + \edef\su@resta{\su@rest#2\@nil}% + \ifx\@empty\su@resta #3\else #4\fi + }{#4}% +} +%% +%% counter for counting appearances +\newcounter{su@anzahl} +%% +% #1: String +% #2: Substring +% #3: Counter +\newcommand*\su@StringSubstringCounter[3]{% + \su@IfSubStringInString{#2}{#1}{% + \stepcounter{#3}% + \def\su@rest##1#2##2\@nil{##2}% + \expandafter\su@StringSubstringCounter\expandafter + {\su@rest#1\@nil}{#2}{#3}% + }{}% +} +%% +\newcommand*\CountSubStrings[2]{% + \su@ExpandTwoArgs{#1}{#2}\su@CountSubStrings +} +\newcommand*\su@CountSubStrings[2]{% + \setcounter{su@anzahl}{0}% + \su@StringSubstringCounter{#2}{#1}{su@anzahl}% + \thesu@anzahl +} +% #1: counter +% #2: substring +% #3: string +\newcommand*\SubStringsToCounter[3]{% + \su@ExpandTwoArgs{#2}{#3}\su@SubStringsToCounter{#1}% +} +% #1: substring +% #2: string +% #3: counter +\newcommand*\su@SubStringsToCounter[3]{% + \setcounter{#3}{0}% + \su@StringSubstringCounter{#2}{#1}{#3}% +} +%% +\endinput +%% EOF diff --git a/macros/latex/contrib/substr/testsubstr.pdf b/macros/latex/contrib/substr/testsubstr.pdf Binary files differnew file mode 100644 index 0000000000..2d94070fb9 --- /dev/null +++ b/macros/latex/contrib/substr/testsubstr.pdf diff --git a/macros/latex/contrib/substr/testsubstr.tex b/macros/latex/contrib/substr/testsubstr.tex new file mode 100644 index 0000000000..1429b7515a --- /dev/null +++ b/macros/latex/contrib/substr/testsubstr.tex @@ -0,0 +1,63 @@ +\documentclass{minimal} +\usepackage{substr} +\def\str{hellohello} +\def\longstr{hellohellohellohello} +\def\medstr{hellohellohello} +\def\substr{ell} +\def\subSTR{ELL} + +\newcounter{appearances} +\begin{document} + +\bigskip{\bfseries IfSubStringInString} + +\IfSubStringInString{\substr}{\str}{``\subSTR'' is in ``\str''}{Error} + +\IfSubStringInString{\subSTR}{\str}{Error}{No ``\subSTR'' in ``\str''} + +\IfSubStringInString{day after }{day after day}{JA}{NEIN} +\IfSubStringInString{AMSTERD}{AMSTERDAM}{JA}{NEIN} +\IfSubStringInString{bon}{bonbon}{JA}{NEIN} +\IfSubStringInString{bono}{bonobo}{JA}{NEIN} +\IfSubStringInString{ionizat}{ionization}{JA}{NEIN} + +\IfSubStringInString{day after day}{day after }{JA}{NEIN} +\IfSubStringInString{AMSTERDAM}{AMSTERD}{JA}{NEIN} +\IfSubStringInString{bonbon}{bon}{JA}{NEIN} +\IfSubStringInString{bonobo}{bono}{JA}{NEIN} +\IfSubStringInString{ionization}{ionizat}{JA}{NEIN} + + + +\bigskip{\bfseries BeforeSubString and BehindSubString} + +Text befor ``\substr'' in ``\str'': +\BeforeSubString{\substr}{\str} + +Text after ``\substr'' in ``\str'': +\BehindSubString{\substr}{\str} + +\BeforeSubString{\substr}{\str}% +\emph{\bfseries\substr}% +\BehindSubString{\substr}{\str} + + +\bigskip{\bfseries CountSubStrings} + +Count of ``\substr'' in ``\str'': +\CountSubStrings{\substr}{\str} + +Count of ``\substr'' in ``\medstr'': +\CountSubStrings{\substr}{\medstr} + +Count of ``\substr'' in ``\longstr'': +\CountSubStrings{\substr}{\longstr} + + +\bigskip{\bfseries SubStringsToCounter} + +\SubStringsToCounter{appearances}{\substr}{\str}% +Count of ``\substr'' in \str'' (counter): +\theappearances + +\end{document} |