Debian Codegraph @

gcc/testsuite/gm2/switches/pedantic-params/pass/Strings.mod

raw
1 (* Copyright (C) 2005, 2006 Free Software Foundation, Inc. *)
2 (* This file is part of GNU Modula-2.
3
4 GNU Modula-2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with gm2; see the file COPYING. If not, write to the Free Software
16 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *)
17 (* ---------------------------------------------------------------------
18 * This program is copyright (c) 1996 Faculty of Information Technology,
19 * Queensland University of Technology, Brisbane, Australia.
20 * The program may be freely distributed in source or compiled form,
21 * provided this copyright notice remains intact in the sources.
22 * --------------------------------------------------------------------- *)
23
24 (****************************************************************
25 $Log: Strings.mod,v $
26 Revision 1.3 2006/01/11 00:12:52 gaius
27 added 2006 to all Copyright dates
28
29 Revision 1.2 2005/11/18 21:54:50 gaius
30 fixed Copyright dates and added GPL notices to all files.
31
32 Revision 1.1 2004/10/26 14:40:19 gaius
33 added switch pass and fail tests
34
35 Revision 1.1 2003/11/04 21:34:13 iztokk
36 added library sources and some document stuff
37
38 Revision 1.1 1996/09/06 07:51:32 lederman
39 Initial revision
40
41 *)
42
43 IMPLEMENTATION MODULE Strings;
44 (*
45 * Purpose:
46 * Facilities for manipulating strings
47 *
48 * Log:
49 * 25/08/96 JL Initial Release
50 *
51 * Acknowledgments:
52 * All procedures are substantially based on the existing
53 * GPM module StdStrings
54 *
55 * Notes:
56 * Complies with ISO/IEC 10514-1:1996
57 *
58 *)
59
60 IMPORT ASCII;
61
62
63 PROCEDURE Length (str : ARRAY OF CHAR) : CARDINAL;
64 BEGIN
65 RETURN LENGTH(str);
66 END Length;
67
68
69 PROCEDURE CanAssignAll (sourceLength: CARDINAL;
70 VAR destination: ARRAY OF CHAR): BOOLEAN;
71 BEGIN
72 RETURN sourceLength <= HIGH(destination) + 1;
73 END CanAssignAll;
74
75
76 PROCEDURE Assign (source: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR);
77 VAR ix, smaller : CARDINAL;
78 ch : CHAR;
79 BEGIN
80 IF HIGH(source) < HIGH(destination) THEN
81 smaller := HIGH(source);
82 ELSE
83 smaller := HIGH(destination);
84 END;
85 FOR ix := 0 TO smaller DO
86 ch := source[ix];
87 destination[ix] := ch;
88 IF ch = ASCII.nul THEN RETURN END;
89 END;
90 IF smaller < HIGH(destination) THEN
91 destination[smaller + 1] := ASCII.nul;
92 END;
93 END Assign;
94
95
96 PROCEDURE CanExtractAll (sourceLength, startIndex, numberToExtract: CARDINAL;
97 VAR destination: ARRAY OF CHAR): BOOLEAN;
98 BEGIN
99 RETURN (startIndex + numberToExtract <= sourceLength) AND
100 (numberToExtract <= HIGH(destination) + 1);
101 END CanExtractAll;
102
103
104 PROCEDURE Extract (source: ARRAY OF CHAR; startIndex, numberToExtract: CARDINAL;
105 VAR destination: ARRAY OF CHAR);
106 VAR dIx, lim : CARDINAL;
107 BEGIN
108 dIx := 0;
109 IF numberToExtract > 0 THEN (* else special case *)
110 DEC(numberToExtract);
111 IF numberToExtract > HIGH(destination) THEN
112 numberToExtract := HIGH(destination);
113 END;
114 lim := LENGTH(source);
115 IF startIndex + numberToExtract >= lim THEN DEC(lim);
116 ELSE lim := startIndex + numberToExtract;
117 END;
118 (* lim is last index to copy *)
119 WHILE startIndex <= lim DO
120 destination[dIx] := source[startIndex]; INC(startIndex); INC(dIx);
121 END;
122 END;
123 IF dIx <= HIGH(destination) THEN destination[dIx] := ASCII.nul END;
124 END Extract;
125
126
127 PROCEDURE CanDeleteAll (stringLength, startIndex, numberToDelete: CARDINAL): BOOLEAN;
128 BEGIN
129 RETURN startIndex + numberToDelete <= stringLength;
130 END CanDeleteAll;
131
132
133 PROCEDURE Delete (VAR string: ARRAY OF CHAR; startIndex, numberToDelete: CARDINAL);
134 VAR lim, mIx : CARDINAL;
135 BEGIN
136 lim := LENGTH(string);
137 IF startIndex < lim THEN (* else do nothing *)
138 IF startIndex + numberToDelete <= lim THEN (* else startIndex is unchanged *)
139 mIx := startIndex + numberToDelete;
140 WHILE mIx < lim DO
141 string[startIndex] := string[mIx]; INC(startIndex); INC(mIx);
142 END;
143 END;
144 IF startIndex <= HIGH(string) THEN string[startIndex] := ASCII.nul END;
145 END;
146 END Delete;
147
148
149 PROCEDURE CanInsertAll (sourceLength, startIndex: CARDINAL;
150 VAR destination: ARRAY OF CHAR): BOOLEAN;
151 VAR dLen : CARDINAL;
152 BEGIN
153 dLen := LENGTH(destination);
154 RETURN (startIndex <= dLen) AND (sourceLength + dLen <= HIGH(destination) +1);
155 END CanInsertAll;
156
157
158 PROCEDURE Insert (source: ARRAY OF CHAR; startIndex: CARDINAL;
159 VAR destination: ARRAY OF CHAR);
160 VAR dLen, sLen, rIx, ix : CARDINAL;
161 BEGIN
162 sLen := LENGTH(source);
163 dLen := LENGTH(destination);
164 IF (sLen = 0) OR (dLen <= startIndex) THEN RETURN END; (* trivial cases *)
165
166 (* copy excess characters up *)
167 rIx := dLen + sLen;
168 IF rIx > HIGH(destination) THEN
169 rIx := HIGH(destination);
170 IF rIx >= (sLen + startIndex) THEN
171 FOR ix := rIx - sLen TO startIndex BY -1 DO
172 destination[rIx] := destination[ix]; DEC(rIx);
173 END;
174 END;
175 ELSE
176 destination[rIx] := ASCII.nul; DEC(rIx);
177 FOR ix := dLen - 1 TO startIndex BY -1 DO
178 destination[rIx] := destination[ix]; DEC(rIx);
179 END;
180 END;
181
182 (* now copy in source string *)
183 DEC(sLen); (* assert : was not zero previously *)
184 IF sLen + startIndex > HIGH(destination) THEN
185 sLen := HIGH(destination) - startIndex;
186 END;
187 FOR ix := 0 TO sLen DO
188 destination[startIndex] := source[ix]; INC(startIndex);
189 END;
190 END Insert;
191
192
193 PROCEDURE CanReplaceAll (sourceLength, startIndex: CARDINAL;
194 VAR destination: ARRAY OF CHAR): BOOLEAN;
195 BEGIN
196 RETURN sourceLength + startIndex <= LENGTH(destination);
197 END CanReplaceAll;
198
199
200 PROCEDURE Replace (source: ARRAY OF CHAR; startIndex: CARDINAL;
201 VAR destination: ARRAY OF CHAR);
202 VAR dLen, ix : CARDINAL;
203 BEGIN
204 dLen := LENGTH(destination);
205 ix := 0;
206 WHILE (startIndex < dLen) AND
207 (ix <= HIGH(source)) AND (source[ix] <> ASCII.nul) DO
208 destination[startIndex] := source[ix]; INC(ix); INC(startIndex);
209 END;
210 END Replace;
211
212
213 PROCEDURE CanAppendAll (sourceLength: CARDINAL;
214 VAR destination: ARRAY OF CHAR): BOOLEAN;
215 BEGIN
216 RETURN LENGTH(destination) + sourceLength <= HIGH(destination) + 1;
217 END CanAppendAll;
218
219
220 PROCEDURE Append (source: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR);
221 VAR sIx, dIx, lim : CARDINAL;
222 BEGIN
223 dIx := LENGTH(destination);
224 IF HIGH(source) + dIx < HIGH(destination) THEN
225 lim := HIGH(source) + dIx;
226 ELSE
227 lim := HIGH(destination);
228 END;
229 sIx := 0;
230 WHILE dIx <= lim DO
231 destination[dIx] := source[sIx];
232 IF source[sIx] = ASCII.nul THEN RETURN END;
233 INC(sIx); INC(dIx);
234 END;
235 IF dIx <= HIGH(destination) THEN destination[dIx] := ASCII.nul END;
236 END Append;
237
238
239 PROCEDURE CanConcatAll (source1Length, source2Length: CARDINAL;
240 VAR destination: ARRAY OF CHAR): BOOLEAN;
241 BEGIN
242 RETURN source1Length + source2Length <= HIGH(destination) + 1;
243 END CanConcatAll;
244
245
246 PROCEDURE Concat (source1, source2: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR);
247 VAR dIx, sIx, lim : CARDINAL;
248 BEGIN
249 IF HIGH(source1) < HIGH(destination) THEN
250 lim := HIGH(source1);
251 ELSE
252 lim := HIGH(destination);
253 END;
254 dIx := 0;
255 WHILE (dIx <= lim) AND (source1[dIx] <> ASCII.nul) DO
256 destination[dIx] := source1[dIx]; INC(dIx);
257 END;
258
259 IF HIGH(source2) + lim < HIGH(destination) THEN
260 lim := HIGH(source2) + lim;
261 ELSE
262 lim := HIGH(destination);
263 END;
264 sIx := 0;
265 WHILE dIx <= lim DO
266 destination[dIx] := source2[sIx];
267 IF source2[sIx] = ASCII.nul THEN RETURN END;
268 INC(sIx); INC(dIx);
269 END;
270 IF dIx <= HIGH(destination) THEN destination[dIx] := ASCII.nul END;
271 END Concat;
272
273
274 PROCEDURE Capitalize(VAR str : ARRAY OF CHAR);
275 VAR ix : CARDINAL;
276 BEGIN
277 ix := 0;
278 WHILE (ix <= HIGH(str)) AND (str[ix] <> ASCII.nul) DO
279 str[ix] := CAP(str[ix]);
280 INC(ix);
281 END;
282 END Capitalize;
283
284
285 (*
286 * There are two different versions of Compare here.
287 * They use different algorithms which have been
288 * tuned for statistically good behaviour. The first
289 * one is usually faster, but neither is as good
290 * as the libc's strncmp.
291 *
292 * Is it worthwhile to define an interface module to strncmp?
293 *
294 *)
295 PROCEDURE Compare (str1 : ARRAY OF CHAR;
296 str2 : ARRAY OF CHAR) : CompareResults;
297 VAR s1, s2 : CHAR;
298 ix, lim : CARDINAL;
299 BEGIN
300 s1 := str1[0]; s2 := str2[0];
301 IF s1 < s2 THEN RETURN less;
302 ELSIF s1 > s2 THEN RETURN greater;
303 ELSE
304 lim := HIGH(str1);
305 IF lim > HIGH(str2) THEN lim := HIGH(str2) END;
306 ix := 0;
307 IF str1[lim] <> str2[lim] THEN (* loop terminates before smaller high *)
308 LOOP
309 IF s1 = s2 THEN
310 IF s2 = ASCII.nul THEN RETURN equal END;
311 INC(ix); s1:= str1[ix]; s2 := str2[ix];
312 ELSIF s1 < s2 THEN RETURN less;
313 ELSE RETURN greater;
314 END;
315 END;
316 END;
317 WHILE ix < lim DO
318 IF s1 = s2 THEN
319 IF s2 = ASCII.nul THEN RETURN equal END;
320 INC(ix); s1:= str1[ix]; s2 := str2[ix];
321 ELSIF s1 < s2 THEN RETURN less;
322 ELSE RETURN greater;
323 END;
324 END;
325 (* Assert((s1 = s2) AND (ix = lim)); *)
326 IF s2 = ASCII.nul THEN RETURN equal;
327 ELSIF HIGH(str2) > lim THEN
328 IF str2[lim + 1] = ASCII.nul THEN RETURN equal ELSE RETURN less END;
329 ELSIF HIGH(str1) > lim THEN
330 IF str1[lim + 1] = ASCII.nul THEN RETURN equal ELSE RETURN greater END;
331 END;
332 RETURN equal;
333 END;
334 END Compare;
335
336 (* --------------------------------------------------------
337 PROCEDURE Compare (str1 : ARRAY OF CHAR;
338 str2 : ARRAY OF CHAR) : CompareResults;
339 VAR s1, s2 : CHAR; ix, lim, lim1, lim2 : CARDINAL;
340 BEGIN
341 IF str1[0] <> str2[0] THEN
342 IF str1[0] > str2[0] THEN RETURN less;
343 ELSE RETURN greater;
344 END;
345 ELSE
346 IF str1[0] = ASCII.nul THEN RETURN equal END;
347 lim1 := HIGH(str1) + 1;
348 lim2 := HIGH(str2) + 1;
349 s1 := str1[1]; s2 := str2[1]; ix := 1;
350 IF str2[lim2] = ASCII.nul THEN (* simple is safe *)
351 WHILE s1 = s2 DO
352 IF s1 = ASCII.nul THEN RETURN equal END;
353 INC(ix); s1 := str1[ix]; s2 := str2[ix];
354 END;
355 (* chars are different *)
356 IF ix >= lim1 THEN
357 IF s2 = ASCII.nul THEN RETURN equal;
358 ELSE RETURN less;
359 END;
360 ELSIF s1 < s2 THEN RETURN less;
361 ELSE RETURN greater;
362 END;
363 ELSIF str1[lim1] = ASCII.nul THEN (* simple is safe *)
364 WHILE s1 = s2 DO
365 IF s1 = ASCII.nul THEN RETURN equal END;
366 INC(ix); s1 := str1[ix]; s2 := str2[ix];
367 END;
368 (* chars are different *)
369 IF ix >= lim2 THEN
370 IF s1 = ASCII.nul THEN RETURN equal;
371 ELSE RETURN greater;
372 END;
373 ELSIF s1 < s2 THEN RETURN less;
374 ELSE RETURN greater;
375 END;
376 ELSE (* must do full test *)
377 IF lim1 <= lim2 THEN lim := lim1 ELSE lim := lim2 END;
378 WHILE ix < lim DO
379 IF s1 = s2 THEN
380 IF s2 = ASCII.nul THEN RETURN equal;
381 ELSE INC(ix); s1 := str1[ix]; s2 := str2[ix];
382 END;
383 ELSIF s1 < s2 THEN RETURN less;
384 ELSE RETURN greater;
385 END;
386 END;
387 IF lim2 > lim THEN (* only lim1 is known to be ended *)
388 IF s2 <> ASCII.nul THEN RETURN less;
389 ELSE RETURN equal;
390 END;
391 ELSIF lim1 > lim THEN
392 IF s1 <> ASCII.nul THEN RETURN greater;
393 ELSE RETURN equal;
394 END;
395 ELSE RETURN equal;
396 END;
397 END;
398 END;
399 END Compare;
400 -------------------------------------------------------- *)
401
402
403 PROCEDURE Equal (string1, string2: ARRAY OF CHAR): BOOLEAN;
404 BEGIN
405 RETURN Compare(string1, string2) = equal;
406 END Equal;
407
408
409 PROCEDURE FindNext (pattern, stringToSearch: ARRAY OF CHAR; startIndex: CARDINAL;
410 VAR patternFound: BOOLEAN; VAR posOfPattern: CARDINAL);
411 VAR sLen, pLen, px, sx : CARDINAL;
412 BEGIN
413 sLen := LENGTH(stringToSearch);
414 pLen := LENGTH(pattern);
415 IF pLen > sLen THEN patternFound := FALSE; RETURN END;
416
417 IF pLen = 0 THEN (* What are the semantics here? *)
418 patternFound := startIndex < sLen;
419 posOfPattern := startIndex; (* Martin says "" matches any character *)
420 RETURN;
421 END;
422
423 WHILE startIndex <= sLen - pLen DO (* find potential starting points *)
424 IF stringToSearch[startIndex] = pattern[0] THEN
425 px := 0; sx := startIndex; (* now compare strings *)
426 LOOP
427 INC(px); INC(sx);
428 IF px = pLen THEN
429 patternFound := TRUE;
430 posOfPattern := startIndex;
431 RETURN;
432 ELSIF pattern[px] <> stringToSearch[sx] THEN
433 EXIT;
434 END;
435 END; (* loop *)
436 END;
437 INC(startIndex);
438 END;
439 patternFound := FALSE;
440 END FindNext;
441
442
443 PROCEDURE FindPrev (pattern, stringToSearch: ARRAY OF CHAR; startIndex: CARDINAL;
444 VAR patternFound: BOOLEAN; VAR posOfPattern: CARDINAL);
445 VAR sLen, pLen, sx, px : CARDINAL;
446 BEGIN
447 sLen := LENGTH(stringToSearch);
448 pLen := LENGTH(pattern);
449 IF pLen > sLen THEN patternFound := FALSE; RETURN END;
450
451 IF pLen = 0 THEN (* What are the semantics here? *)
452 patternFound := TRUE;
453 IF startIndex >= sLen THEN
454 posOfPattern := sLen - 1;
455 ELSE
456 posOfPattern := startIndex;
457 END;
458 RETURN;
459 END;
460
461 (* if startIndex > sLen - pLen the whole string is searched *)
462 IF startIndex > sLen - pLen THEN startIndex := sLen - pLen END;
463
464 (* now find potential starting points *)
465 INC(startIndex); (* so it doesn't go negative *)
466 WHILE startIndex > 0 DO
467 DEC(startIndex);
468 IF stringToSearch[startIndex] = pattern[0] THEN
469 px := 0; sx := startIndex; (* now compare strings *)
470 LOOP
471 INC(px); INC(sx);
472 IF px = pLen THEN
473 patternFound := TRUE;
474 posOfPattern := startIndex;
475 RETURN;
476 ELSIF pattern[px] <> stringToSearch[sx] THEN
477 EXIT;
478 END;
479 END; (* loop *)
480 END;
481 END;
482 patternFound := FALSE;
483 END FindPrev;
484
485
486 PROCEDURE FindDiff (string1, string2: ARRAY OF CHAR;
487 VAR differenceFound: BOOLEAN; VAR posOfDifference: CARDINAL);
488 VAR ix : CARDINAL;
489 BEGIN
490 ix := 0;
491 IF string1[0] = string2[0] THEN
492 IF string1[0] = ASCII.nul THEN (* both strings empty *)
493 differenceFound := FALSE; RETURN;
494 END;
495 LOOP
496 INC(ix);
497 IF (ix > HIGH(string1)) OR (string1[ix] = ASCII.nul) THEN (* 1 ended *)
498 IF (ix > HIGH(string2)) OR (string2[ix] = ASCII.nul) THEN (* 2 also *)
499 differenceFound := FALSE; RETURN; (* both ended, and equal *)
500 ELSE
501 EXIT; (* only 1 ended *)
502 END;
503 ELSIF (ix > HIGH(string2)) OR (string2[ix] = ASCII.nul) THEN
504 EXIT; (* only 2 ended *)
505 ELSIF string1[ix] <> string2[ix] THEN
506 EXIT; (* strings differ *)
507 (* else go around the loop once more *)
508 END;
509 END; (* loop *)
510 END;
511 differenceFound := TRUE;
512 posOfDifference := ix;
513 END FindDiff;
514
515 END Strings.