File: coolstr.plt

package info (click to toggle)
tralics 2.14.4-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 17,152 kB
  • ctags: 8,260
  • sloc: cpp: 49,403; xml: 18,145; sh: 140; makefile: 118
file content (290 lines) | stat: -rw-r--r-- 7,836 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
%%% -*- latex -*-
\ProvidesPackage{coolstr}[2008/04/24 v1.0  COntent Oriented LaTeX Strings]
%% based on a package by nsetzer
%% This file may be distributed and/or modified under the
%% conditions of the Limited General Product License


%% $Id: coolstr.plt,v 2.1 2009/01/15 18:10:58 grimm Exp $
%% TRALICS, copyright (C) INRIA/apics (Jose' Grimm) 2007
%% Licensed under the CeCILL free software license 
%% (See the file COPYING in the main directory for details)

%% 
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\RequirePackage{ifthen}

\newcommand{\setstrEnd}[1]{} % useless
\newcount\COOL@str@index
\newcount\COOL@str@start
\newcount\COOL@str@end

%%%%%%% testing types of numbers etc
\newboolean{COOL@decimalfound}
\newboolean{COOL@decimal}

%% Set a user boolean from \ifCOOL@decimal
\def\COOL@set@bool#1{%
  \ifCOOL@decimal\setboolean{#1}{true}%
  \else \setboolean{#1}{false}\fi}


%% Read all chars until finding \@nil; increment \@tempcntb
%% Emit #1 if counter has the same value as \@tempcnta
\def\COOL@strgobble#1{%
  \ifx\@nil#1% done
  \else
  \advance\@tempcntb by 1
  \ifnum \@tempcntb=\@tempcnta #1\fi
  \expandafter\COOL@strgobble\fi
}

%% Read all chars until finding \@nil; increment \@tempcntb
%% Emit #1 if counter is between \COOL@str@start and \COOL@str@end
\def\COOL@substrgobbler#1{%
  \ifx\@nil#1\else
  \advance\@tempcntb by 1
  \ifnum\@tempcntb < \COOL@str@start
  \else
    \ifnum\@tempcntb > \COOL@str@end
     \else #1\fi\fi
  \expandafter\COOL@substrgobbler\fi
}

%% Read all chars until finding \@nil; increment \@tempcntb
%% Set boolean to true if position is \@tempcnta and char is #1
\def\COOL@strcomparegobble#1#2{%
 \ifx#2\@nil
 \else
    \advance\@tempcntb by1
    \ifnum\@tempcnta=\@tempcntb 
      \ifx #1#2\@tempswatrue\fi
    \fi
    \expandafter\COOL@strcomparegobble\expandafter#1\fi
}

%% Emit char at position #2 in #1 if index positive; set size in \@tempcntb
\newcommand{\strchar}[2]{%
  \@tempcntb=\z@
  \@tempcnta=#2\relax
  \COOL@strgobble#1\@nil}

%% set size in \@tempcntb
\newcommand{\strlen}[1]{%
  \strchar{#1}{0}%
  \number\@tempcntb}

%% set size of #1 in #2
\newcommand{\strlenstore}[2]{%
  \strchar{#1}{0}%
  \setcounter{#2}{\@tempcntb}}

%%\ifstrchareq{str}{pos}[char}{true-code}{false-code}
%% Executes one of true-code false-code depending on whether character
%% at position pos if str is char.
\newcommand{\ifstrchareq}[3]{
 \@tempcntb=\z@
 \@tempcnta=#2
 \@tempswafalse
 \COOL@strcomparegobble#3#1\@nil
 \if@tempswa
     \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

%%\ifstrleneq{str}{len}{true-code}{false-code}
%% Executes one of true-code false-code depending on whether size of str is len
\newcommand{\ifstrleneq}[2]{%
  \strchar{#1}{0}%
  \ifnum\@tempcntb=#2\relax
   \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

%% let A=\@tempcntA, B=\@tempcntb; we set #1 into A, then COOL@str@index
%% B+A+1 if A<0 and to A to A >0
%% is size of string, we set starting index to #1 or B+#1
\def\subtr@checkstart#1{%
  \@tempcnta=#1\relax
  \ifnum\@tempcnta<\z@
    \ifnum\@tempcnta<-\@tempcntb
      \COOL@str@index=0 % too big
    \else
      \COOL@str@index=\@tempcntb
      \advance\COOL@str@index \@tempcnta
      \advance\COOL@str@index 1
    \fi
  \else 
    \ifnum\@tempcnta>\@tempcntb
       \COOL@str@index=\z@ %too big
    \else \COOL@str@index\@tempcnta\fi
  \fi
}

%% Argument is the number of characters
%% This sets \COOL@str@start and \COOL@str@end 
\def\subtr@checksize#1{%
  \def\@tempa{beg}%
  \def\@tempb{#1}%
  %% if len=beg, use [1,I]
  \ifx\@tempa\@tempb
    \COOL@str@start=1
    \COOL@str@end=\COOL@str@index
    \else
      \def\@tempa{end}%
       % len=end use [I,max]
      \ifx\@tempa\@tempb
      \COOL@str@start=\COOL@str@index
      \COOL@str@end=\@tempcntb
      \else \ifnum #1<\z@
        %% size negative [I+size+1,I]
        \COOL@str@start=\COOL@str@index
        \advance\COOL@str@start #1%
        \advance\COOL@str@start 1 
        \ifnum\COOL@str@start > 0\else\COOL@str@start=1\fi
        \COOL@str@end=\COOL@str@index
      \else \ifnum #1 > \z@
         %% normal case [I, I+len-1]
          \COOL@str@start=\COOL@str@index 
          \COOL@str@end=\COOL@str@index
          \advance\COOL@str@end #1\relax
          \advance\COOL@str@end by -1
          \ifnum\COOL@str@end > \@tempcntb \COOL@str@end\@tempcntb \fi
        \else
          \COOL@str@index=\z@
        \fi\fi\fi\fi}

%% Emits a particur substring of #1 (starting at #2 with #3 characters).
\newcommand{\substr}[3]{%
  \strchar{#1}{0}%
  \subtr@checkstart{#2}%
  \subtr@checksize{#3}%
  \ifnum \COOL@str@index>\z@ \@afterfi
    \@tempcntb=0
    \COOL@substrgobbler#1\@nil%
  \fi
}



%% Checking numbers 
%% Skip initial sign
\def\COOL@decimalgobbler#1{%
  \ifx\@nil#1\let\next\COOL@decimalfalse
  \else \ifnum`#1=`+ \let\next\COOL@decimalgobblerA
  \else \ifnum`#1=`- \let\next\COOL@decimalgobblerA
  \else \def\next{\COOL@decimalgobblerA#1}\fi\fi\fi
  \next
}

%% We have read initial sign, must check for a single dot
\def\COOL@decimalgobblerA#1{%
   \ifx\@nil#1\let\next\COOL@decimalfalse
   \else \ifnum`#1=`. \let\next\COOL@decimalgobblerB
   \else \def\next{\COOL@decimalgobblerC#1}\fi\fi\next}

%% We have see optional sign and dot
\def\COOL@decimalgobblerB#1{%
  \ifx\@nil#1\let\next\COOL@decimalfalse 
  \else \COOL@decimalfoundtrue\let\next\COOL@decimalgobblerC\fi\next}

%% Generic 
\def\COOL@decimalgobblerC#1{%
  \ifx\@nil#1%
  \else 
   \@tempswafalse % false char ok
   \ifnum`#1 < `0 \@tempswatrue \fi
   \ifnum `#1 > `9 \@tempswatrue \fi
   \ifnum `#1 = `. 
      \ifCOOL@decimalfound \else \COOL@decimalfoundtrue\@tempswafalse \fi
   \fi
   \if@tempswa\COOL@decimalfalse\fi
   \expandafter\COOL@decimalgobblerC\fi}

%% Sets \COOL@decimal if #1 is a decimal number
\def\COOL@isdecimal#1{%
  \COOL@decimalfoundfalse
  \COOL@decimaltrue
  \expandafter\COOL@decimalgobbler#1\@nil}

%% Split AeB or AEB in two parts
% \COOL@num@magnitude and \COOL@num@exponent
\def\COOL@split@number#1{%
  \COOL@eparser#1e\@nil%
  \ifx\empty\COOL@num@exponent\empty
    \COOL@Eparser#1E\@nil%
    \ifx\empty\COOL@num@exponent\empty
      \def\COOL@num@exponent{0}%
    \else 
      \expandafter\COOL@Ecorrector\COOL@num@exponent%
    \fi
  \else
    \expandafter\COOL@ecorrector\COOL@num@exponent%
  \fi
}

\def\COOL@eparser#1e#2\@nil{%
  \def\COOL@num@magnitude{#1}%
  \def\COOL@num@exponent{#2}%
}
\def\COOL@ecorrector#1e{%
  \def\COOL@num@exponent{#1}%
}
\def\COOL@Eparser#1E#2\@nil{%
  \def\COOL@num@magnitude{#1}%
  \def\COOL@num@exponent{#2}%
}
\def\COOL@Ecorrector#1E{%
  \def\COOL@num@exponent{#1}%
}


%% Set \COOL@decimalfalse if arg not int
%% First check initial sign
\def\COOL@intgobblerA#1{%
  \ifx\@nil#1\let\next\COOL@decimalfalse
  \else \ifnum`#1=`+ \let\next\COOL@intgobblerB
  \else \ifnum`#1=`- \let\next\COOL@intgobblerB
  \else \def\next{\COOL@intgobblerB#1}\fi\fi\fi
  \next
}

%% Check if the number is empty
\def\COOL@intgobblerB#1{%
  \ifx\@nil#1\let\next\COOL@decimalfalse \fi
  \COOL@intgobbler#1}

\def\COOL@intgobbler#1{%
  \ifx\@nil#1%
  \else 
   \ifcat#11\else\COOL@decimalfalse\fi
   \@tempswafalse % false char ok
   \ifnum`#1 < `0 \@tempswatrue \fi
   \ifnum `#1 > `9 \@tempswatrue \fi
   \if@tempswa\COOL@decimalfalse\fi
   \expandafter\COOL@intgobbler\fi}




%% Sets a user boolean #2 if #1 is a decimal number
\newcommand{\isdecimal}[2]{%
  \COOL@isdecimal{#1}%
  \COOL@set@bool{#2}}

%% Sets a user boolean #2 if #1 is integer
\newcommand{\isint}[2]{%
  \COOL@decimaltrue
  \expandafter\COOL@intgobblerA#1\@nil%
  \COOL@set@bool{#2}}

%% Sets a user boolean #2 if #1 is a number
\newcommand{\isnumeric}[2]{%
  \COOL@split@number{#1}%
  \COOL@isdecimal{\COOL@num@magnitude}%
  \ifCOOL@decimal\expandafter\COOL@intgobblerA\COOL@num@exponent\@nil\fi
  \COOL@set@bool{#2}}

\endinput