Debian Codegraph @

gcc/m2/gm2-libs-iso/Strings.mod

raw
1 (* Strings.mod implement the ISO Strings specification.
2
3 Copyright (C) 2008-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE Strings ;
28
29 IMPORT ASCII ;
30 FROM libc IMPORT printf ;
31
32 CONST
33 Debugging = FALSE ;
34
35
36 (*
37 Length - Returns the length of stringVal (the same value as would be returned by the
38 pervasive function LENGTH).
39 *)
40
41 PROCEDURE Length (stringVal: ARRAY OF CHAR) : CARDINAL;
42 BEGIN
43 RETURN( LENGTH(stringVal) )
44 END Length ;
45
46
47 (* The following seven procedures construct a string value, and
48 attempt to assign it to a variable parameter. They all have
49 the property that if the length of the constructed string
50 value exceeds the capacity of the variable parameter, a
51 truncated value is assigned, while if the length of the
52 constructed string value is less than the capacity of the
53 variable parameter, a string terminator is appended before
54 assignment is performed.
55 *)
56
57 (*
58 Assign - Copies source to destination.
59 *)
60
61 PROCEDURE Assign (source: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR) ;
62 VAR
63 i,
64 sh, dh: CARDINAL ;
65 BEGIN
66 sh := Length(source) ;
67 dh := HIGH(destination) ;
68 i := 0 ;
69 WHILE (i<sh) AND (i<=dh) DO
70 destination[i] := source[i] ;
71 INC(i)
72 END ;
73 IF i<=dh
74 THEN
75 destination[i] := ASCII.nul
76 END
77 END Assign ;
78
79
80 (* Copies at most numberToExtract characters from source to destination,
81 starting at position startIndex in source.
82 *)
83
84 PROCEDURE Extract (source: ARRAY OF CHAR;
85 startIndex, numberToExtract: CARDINAL;
86 VAR destination: ARRAY OF CHAR) ;
87 VAR
88 sh, dh,
89 i : CARDINAL ;
90 BEGIN
91 sh := Length(source) ;
92 dh := HIGH(destination) ;
93 i := 0 ;
94 WHILE (i<numberToExtract) AND (startIndex<sh) AND (i<=dh) DO
95 destination[i] := source[startIndex] ;
96 INC(i) ;
97 INC(startIndex)
98 END ;
99 IF i<=dh
100 THEN
101 destination[i] := ASCII.nul
102 END
103 END Extract ;
104
105
106 PROCEDURE MinCard (a, b: CARDINAL) : CARDINAL ;
107 BEGIN
108 IF a < b
109 THEN
110 RETURN a
111 ELSE
112 RETURN b
113 END
114 END MinCard ;
115
116
117 (* Deletes at most numberToDelete characters from stringVar, starting at position
118 startIndex.
119 *)
120
121 PROCEDURE Delete (VAR stringVar: ARRAY OF CHAR;
122 startIndex, numberToDelete: CARDINAL) ;
123 CONST
124 Debugging = FALSE ;
125 VAR
126 length,
127 high,
128 last : CARDINAL ;
129 BEGIN
130 IF numberToDelete > 0
131 THEN
132 length := Length (stringVar) ;
133 IF startIndex < length
134 THEN
135 high := HIGH (stringVar) ;
136 (* Calculate the number of characters to delete. *)
137 last := MinCard (high, length-1) ;
138 IF last - startIndex < numberToDelete
139 THEN
140 numberToDelete := last - startIndex + 1
141 END ;
142 IF numberToDelete > 0
143 THEN
144 IF Debugging
145 THEN
146 printf ("startIndex = %d, numberToDelete = %d, last = %d\n",
147 startIndex, numberToDelete, last)
148 END ;
149 WHILE startIndex + numberToDelete <= last DO
150 IF Debugging
151 THEN
152 printf ("strVar[%d] is %c\n", startIndex, stringVar[startIndex]) ;
153 printf (" overwriting with strVar[%d] <- %c\n",
154 startIndex + numberToDelete, stringVar[startIndex + numberToDelete])
155 END ;
156 stringVar[startIndex] := stringVar[startIndex + numberToDelete] ;
157 INC (startIndex) ;
158 END
159 END ;
160 IF startIndex <= high
161 THEN
162 stringVar[startIndex] := ASCII.nul
163 END
164 END
165 END
166 END Delete ;
167
168
169 (* Inserts source into destination at position startIndex *)
170
171 PROCEDURE Insert (source: ARRAY OF CHAR;
172 startIndex: CARDINAL;
173 VAR destination: ARRAY OF CHAR) ;
174 VAR
175 newEnd, endCopy,
176 i, j, sh, dh, dl: CARDINAL ;
177 BEGIN
178 sh := Length(source) ;
179 dh := HIGH(destination) ;
180 dl := Length(destination) ;
181 (* make space for source *)
182 IF Debugging
183 THEN
184 printf("sh = %d dh = %d dl = %d\n",
185 sh, dh, dl);
186 END ;
187 newEnd := dl+sh ;
188 IF newEnd>dh
189 THEN
190 (* insert will truncate destination *)
191 newEnd := dh
192 END ;
193 IF newEnd>sh
194 THEN
195 endCopy := newEnd-sh
196 ELSE
197 endCopy := 0
198 END ;
199 IF Debugging
200 THEN
201 printf("\ndestination contains\n%s\nnewEnd = %d endCopy = %d\n", destination, newEnd, endCopy) ;
202 printf("newEnd = %d\n", newEnd) ;
203 printf("endCopy = %d\n", endCopy) ;
204 END ;
205 INC(newEnd) ;
206 INC(endCopy) ;
207 WHILE endCopy>startIndex DO
208 DEC(newEnd) ;
209 DEC(endCopy) ;
210 IF Debugging
211 THEN
212 printf("copying dest %d to %d (%c) (startIndex=%d)\n",
213 endCopy, newEnd, destination[newEnd], startIndex)
214 END ;
215 destination[newEnd] := destination[endCopy]
216 END ;
217 IF Debugging
218 THEN
219 printf("destination now contains %s\n", destination)
220 END ;
221 (* copy source into destination *)
222 j := startIndex ;
223 i := 0 ;
224 WHILE (i<sh) AND (j<=dh) DO
225 destination[j] := source[i] ;
226 INC(i) ;
227 INC(j)
228 END
229 END Insert ;
230
231
232 (* Copies source into destination, starting at position startIndex. Copying stops when
233 all of source has been copied, or when the last character of the string value in
234 destination has been replaced. *)
235
236 PROCEDURE Replace (source: ARRAY OF CHAR;
237 startIndex: CARDINAL;
238 VAR destination: ARRAY OF CHAR) ;
239 VAR
240 i, sh, dh: CARDINAL ;
241 BEGIN
242 i := 0 ;
243 sh := Length(source) ;
244 dh := Length(destination) ;
245 WHILE (i<sh) AND (startIndex<dh) DO
246 destination[startIndex] := source[i] ;
247 INC(i) ;
248 INC(startIndex)
249 END ;
250 IF startIndex<HIGH(destination)
251 THEN
252 destination[startIndex] := ASCII.nul
253 END
254 END Replace ;
255
256
257 PROCEDURE Append (source: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR) ;
258 (* Appends source to destination. *)
259 VAR
260 i, j, sh, dh: CARDINAL ;
261 BEGIN
262 j := Length(destination) ;
263 dh := HIGH(destination) ;
264 sh := Length(source) ;
265 i := 0 ;
266 WHILE (i<sh) AND (j<=dh) DO
267 destination[j] := source[i] ;
268 INC(i) ;
269 INC(j)
270 END ;
271 IF j<=dh
272 THEN
273 destination[j] := ASCII.nul
274 END
275 END Append ;
276
277
278 PROCEDURE Concat (source1, source2: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR);
279 (* Concatenates source2 onto source1 and copies the result into destination. *)
280 BEGIN
281 Assign(source1, destination) ;
282 Append(source2, destination)
283 END Concat ;
284
285
286 (* The following predicates provide for pre-testing of the operation-completion
287 conditions for the procedures above.
288 *)
289
290 PROCEDURE CanAssignAll (sourceLength: CARDINAL; VAR destination: ARRAY OF CHAR) : BOOLEAN;
291 (* Returns TRUE if a number of characters, indicated by sourceLength, will fit into
292 destination; otherwise returns FALSE.
293 *)
294 BEGIN
295 RETURN( sourceLength<=HIGH(destination) )
296 END CanAssignAll ;
297
298
299 PROCEDURE CanExtractAll (sourceLength,
300 startIndex,
301 numberToExtract: CARDINAL;
302 VAR destination: ARRAY OF CHAR) : BOOLEAN ;
303 (* Returns TRUE if there are numberToExtract characters starting at startIndex and
304 within the sourceLength of some string, and if the capacity of destination is
305 sufficient to hold numberToExtract characters; otherwise returns FALSE.
306 *)
307 BEGIN
308 RETURN( (numberToExtract+startIndex<=sourceLength) AND
309 (HIGH(destination)>=numberToExtract) )
310 END CanExtractAll ;
311
312 PROCEDURE CanDeleteAll (stringLength, startIndex, numberToDelete: CARDINAL) : BOOLEAN ;
313 (* Returns TRUE if there are numberToDelete characters starting at startIndex and
314 within the stringLength of some string; otherwise returns FALSE.
315 *)
316 BEGIN
317 RETURN( startIndex+numberToDelete<=stringLength )
318 END CanDeleteAll ;
319
320 PROCEDURE CanInsertAll (sourceLength, startIndex: CARDINAL;
321 VAR destination: ARRAY OF CHAR) : BOOLEAN ;
322 (* Returns TRUE if there is room for the insertion of sourceLength characters
323 from some string into destination starting at startIndex; otherwise returns
324 FALSE.
325 *)
326 BEGIN
327 RETURN( (HIGH(destination)-Length(destination)<sourceLength) AND
328 (HIGH(destination)-startIndex<sourceLength) )
329 END CanInsertAll ;
330
331 PROCEDURE CanReplaceAll (sourceLength, startIndex: CARDINAL;
332 VAR destination: ARRAY OF CHAR) : BOOLEAN;
333 (* Returns TRUE if there is room for the replacement of sourceLength
334 characters in destination starting at startIndex; otherwise returns
335 FALSE.
336 *)
337 BEGIN
338 RETURN( sourceLength<=Length(destination)-startIndex )
339 END CanReplaceAll ;
340
341 PROCEDURE CanAppendAll (sourceLength: CARDINAL; VAR destination: ARRAY OF CHAR) : BOOLEAN;
342 (* Returns TRUE if there is sufficient room in destination to append a string of
343 length sourceLength to the string in destination; otherwise returns FALSE.
344 *)
345 BEGIN
346 RETURN( HIGH(destination)-Length(destination)>=sourceLength )
347 END CanAppendAll ;
348
349 PROCEDURE CanConcatAll (source1Length, source2Length: CARDINAL;
350 VAR destination: ARRAY OF CHAR) : BOOLEAN;
351 (* Returns TRUE if there is sufficient room in destination for a two strings of
352 lengths source1Length and source2Length; otherwise returns FALSE.
353 *)
354 BEGIN
355 RETURN( HIGH(destination)-Length(destination)>=source1Length+source2Length )
356 END CanConcatAll ;
357
358
359 (* The following type and procedures provide for the comparison of string values, and for the
360 location of substrings within strings.
361 *)
362
363 PROCEDURE Compare (stringVal1, stringVal2: ARRAY OF CHAR) : CompareResults ;
364 (* Returns less, equal, or greater, according as stringVal1 is lexically less than,
365 equal to, or greater than stringVal2.
366 *)
367 VAR
368 i, l1, l2: CARDINAL ;
369 BEGIN
370 l1 := Length(stringVal1) ;
371 l2 := Length(stringVal2) ;
372 i := 0 ;
373 WHILE (i<l1) AND (i<l2) DO
374 IF stringVal1[i]<stringVal2[i]
375 THEN
376 RETURN less
377 ELSIF stringVal1[i]>stringVal2[i]
378 THEN
379 RETURN greater
380 ELSE
381 INC(i)
382 END
383 END ;
384 IF l1<l2
385 THEN
386 RETURN less
387 ELSIF l1>l2
388 THEN
389 RETURN greater
390 ELSE
391 RETURN equal
392 END
393 END Compare ;
394
395 PROCEDURE Equal (stringVal1, stringVal2: ARRAY OF CHAR) : BOOLEAN ;
396 (* Returns Strings.Compare(stringVal1, stringVal2) = Strings.equal *)
397 VAR
398 h1, h2, i: CARDINAL ;
399 c1, c2 : CHAR ;
400 BEGIN
401 i := 0 ;
402 h1 := HIGH(stringVal1) ;
403 h2 := HIGH(stringVal2) ;
404 IF h1=h2
405 THEN
406 REPEAT
407 c1 := stringVal1[i] ;
408 c2 := stringVal2[i] ;
409 IF c1#c2
410 THEN
411 RETURN FALSE
412 END ;
413 IF c1=ASCII.nul
414 THEN
415 RETURN TRUE
416 END ;
417 INC(i) ;
418 UNTIL i>h1 ;
419 RETURN TRUE
420 ELSE
421 c1 := stringVal1[0] ;
422 c2 := stringVal2[0] ;
423 WHILE c1=c2 DO
424 IF c1=ASCII.nul
425 THEN
426 RETURN TRUE
427 END ;
428 INC(i) ;
429 IF i<=h1
430 THEN
431 c1 := stringVal1[i] ;
432 IF i<=h2
433 THEN
434 c2 := stringVal2[i]
435 ELSE
436 RETURN c1=ASCII.nul
437 END
438 ELSIF i<=h2
439 THEN
440 c2 := stringVal2[i] ;
441 RETURN c2=ASCII.nul
442 END
443 END ;
444 RETURN FALSE
445 END
446 END Equal ;
447
448 PROCEDURE FindNext (pattern, stringToSearch: ARRAY OF CHAR; startIndex: CARDINAL;
449 VAR patternFound: BOOLEAN; VAR posOfPattern: CARDINAL) ;
450 (* Looks forward for next occurrence of pattern in stringToSearch, starting the search at
451 position startIndex. If startIndex < LENGTH(stringToSearch) and pattern is found,
452 patternFound is returned as TRUE, and posOfPattern contains the start position in
453 stringToSearch of pattern. Otherwise patternFound is returned as FALSE, and posOfPattern
454 is unchanged.
455 *)
456 VAR
457 i, j, hp, hs: CARDINAL ;
458 BEGIN
459 i := startIndex ;
460 hp := Length(pattern) ;
461 hs := Length(stringToSearch) ;
462 IF hp<=hs
463 THEN
464 WHILE (i<=hs-hp) DO
465 j := 0 ;
466 WHILE (j<hp) AND (pattern[j]=stringToSearch[i+j]) DO
467 INC(j) ;
468 IF j=hp
469 THEN
470 posOfPattern := i ;
471 patternFound := TRUE ;
472 RETURN
473 END
474 END ;
475 INC(i)
476 END
477 END ;
478 patternFound := FALSE
479 END FindNext ;
480
481 PROCEDURE FindPrev (pattern, stringToSearch: ARRAY OF CHAR; startIndex: CARDINAL;
482 VAR patternFound: BOOLEAN; VAR posOfPattern: CARDINAL);
483 (* Looks backward for the previous occurrence of pattern in stringToSearch and returns the
484 position of the first character of the pattern if found. The search for the pattern
485 begins at startIndex. If pattern is found, patternFound is returned as TRUE, and
486 posOfPattern contains the start position in stringToSearch of pattern in the range
487 [0..startIndex]. Otherwise patternFound is returned as FALSE, and
488 posOfPattern is unchanged.
489 *)
490 VAR
491 i, j, hp, hs: CARDINAL ;
492 BEGIN
493 hp := Length(pattern) ;
494 hs := Length(stringToSearch) ;
495 IF hp<=hs
496 THEN
497 i := hs-hp+1 ;
498 WHILE i>0 DO
499 DEC(i) ;
500 j := 0 ;
501 WHILE (j<hp) AND (pattern[j]=stringToSearch[i+j]) DO
502 INC(j) ;
503 IF j=hp
504 THEN
505 posOfPattern := i ;
506 patternFound := TRUE ;
507 RETURN
508 END
509 END
510 END
511 END ;
512 patternFound := FALSE
513 END FindPrev ;
514
515 PROCEDURE FindDiff (stringVal1, stringVal2: ARRAY OF CHAR;
516 VAR differenceFound: BOOLEAN; VAR posOfDifference: CARDINAL) ;
517 (* Compares the string values in stringVal1 and stringVal2 for differences. If they
518 are equal, differenceFound is returned as FALSE, and TRUE otherwise. If
519 differenceFound is TRUE, posOfDifference is set to the position of the first
520 difference; otherwise posOfDifference is unchanged.
521 *)
522 VAR
523 i,
524 s1h, s2h: CARDINAL ;
525 BEGIN
526 s1h := Length(stringVal1) ;
527 s2h := Length(stringVal2) ;
528 i := 0 ;
529 WHILE (i<s1h) AND (i<s2h) DO
530 IF stringVal1[i]=stringVal2[i]
531 THEN
532 INC(i)
533 ELSE
534 differenceFound := TRUE ;
535 posOfDifference := i ;
536 RETURN
537 END
538 END ;
539 IF s1h=s2h
540 THEN
541 differenceFound := FALSE ;
542 ELSE
543 differenceFound := TRUE ;
544 posOfDifference := i
545 END
546 END FindDiff ;
547
548 PROCEDURE Capitalize (VAR stringVar: ARRAY OF CHAR) ;
549 (* Applies the function CAP to each character of the string value in stringVar. *)
550 VAR
551 i, h: CARDINAL ;
552 BEGIN
553 i := 0 ;
554 h := Length(stringVar) ;
555 WHILE i<h DO
556 stringVar[i] := CAP(stringVar[i]) ;
557 INC(i)
558 END
559 END Capitalize ;
560
561
562 END Strings.