| 1 |
(* Copyright (C) 2005-2025 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 |
DEFINITION MODULE Strings2; |
| 18 |
|
| 19 |
(* Facilities for manipulating strings *) |
| 20 |
|
| 21 |
TYPE |
| 22 |
String1 = ARRAY [0..0] OF CHAR; |
| 23 |
(* String1 is provided for constructing a value of a single-character string |
| 24 |
type from a single character value in order to pass CHAR values to |
| 25 |
ARRAY OF CHAR parameters. |
| 26 |
*) |
| 27 |
|
| 28 |
PROCEDURE Length (stringVal: ARRAY OF CHAR): CARDINAL; |
| 29 |
(* Returns the length of stringVal (the same value as would be returned by the |
| 30 |
pervasive function LENGTH). |
| 31 |
*) |
| 32 |
|
| 33 |
|
| 34 |
(* The following seven procedures construct a string value, and attempt to |
| 35 |
assign it to a variable parameter. They all have the property that if |
| 36 |
the length of the constructed string value exceeds the capacity of the |
| 37 |
variable parameter, a truncated value is assigned, while if the length |
| 38 |
of the constructed string value is less than the capacity of the variable |
| 39 |
parameter, a string terminator is appended before assignment is performed. |
| 40 |
*) |
| 41 |
|
| 42 |
|
| 43 |
PROCEDURE Assign (source: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR); |
| 44 |
(* Copies source to destination *) |
| 45 |
|
| 46 |
PROCEDURE Extract (source: ARRAY OF CHAR; startIndex, numberToExtract: CARDINAL; |
| 47 |
VAR destination: ARRAY OF CHAR); |
| 48 |
(* Copies at most numberToExtract characters from source to destination, |
| 49 |
starting at position startIndex in source. |
| 50 |
*) |
| 51 |
|
| 52 |
PROCEDURE Delete (VAR string: ARRAY OF CHAR; startIndex, numberToDelete: CARDINAL); |
| 53 |
(* Deletes at most numberToDelete characters from stringVar, starting at position |
| 54 |
startIndex. |
| 55 |
*) |
| 56 |
|
| 57 |
PROCEDURE Insert (source: ARRAY OF CHAR; startIndex: CARDINAL; |
| 58 |
VAR destination: ARRAY OF CHAR); |
| 59 |
(* Inserts source into destination at position startIndex *) |
| 60 |
|
| 61 |
PROCEDURE Replace (source: ARRAY OF CHAR; startIndex: CARDINAL; |
| 62 |
VAR destination: ARRAY OF CHAR); |
| 63 |
(* Copies source into destination, starting at position startIndex. |
| 64 |
Copying stops when all of source has been copied, or when the last |
| 65 |
character of the string value in destination has been replaced. |
| 66 |
*) |
| 67 |
|
| 68 |
PROCEDURE Append (source: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR); |
| 69 |
(* Appends source to destination. *) |
| 70 |
|
| 71 |
PROCEDURE Concat (source1, source2: ARRAY OF CHAR; VAR destination: ARRAY OF CHAR); |
| 72 |
(* Concatenates source2 onto source1 and copies the result into destination. *) |
| 73 |
|
| 74 |
|
| 75 |
(* The following predicates provide for pre-testing of the operation-completion |
| 76 |
conditions for the procedures above. |
| 77 |
*) |
| 78 |
|
| 79 |
PROCEDURE CanAssignAll (sourceLength: CARDINAL; VAR destination: ARRAY OF CHAR): BOOLEAN; |
| 80 |
(* Returns TRUE if a number of characters, indicated by sourceLength, will fit |
| 81 |
into destination; otherwise returns FALSE. |
| 82 |
*) |
| 83 |
|
| 84 |
PROCEDURE CanExtractAll (sourceLength, startIndex, numberToExtract: CARDINAL; |
| 85 |
VAR destination: ARRAY OF CHAR): BOOLEAN; |
| 86 |
(* Returns TRUE if there are numberToExtract characters starting at startIndex |
| 87 |
and within the sourceLength of some string, and if the capacity of destination |
| 88 |
is sufficient to hold numberToExtract characters; otherwise returns FALSE. |
| 89 |
*) |
| 90 |
|
| 91 |
PROCEDURE CanDeleteAll (stringLength, startIndex, numberToDelete: CARDINAL): BOOLEAN; |
| 92 |
(* Returns TRUE if there are numberToDelete characters starting at startIndex |
| 93 |
and within the stringLength of some string; otherwise returns FALSE. |
| 94 |
*) |
| 95 |
|
| 96 |
PROCEDURE CanInsertAll (sourceLength, startIndex: CARDINAL; |
| 97 |
VAR destination: ARRAY OF CHAR): BOOLEAN; |
| 98 |
(* Returns TRUE if there is room for the insertion of sourceLength characters from |
| 99 |
some string into destination starting at startIndex; otherwise returns FALSE. |
| 100 |
*) |
| 101 |
|
| 102 |
PROCEDURE CanReplaceAll (sourceLength, startIndex: CARDINAL; |
| 103 |
VAR destination: ARRAY OF CHAR): BOOLEAN; |
| 104 |
(* Returns TRUE if there is room for the replacement of sourceLength characters in |
| 105 |
destination starting at startIndex; otherwise returns FALSE. |
| 106 |
*) |
| 107 |
|
| 108 |
PROCEDURE CanAppendAll (sourceLength: CARDINAL; VAR destination: ARRAY OF CHAR): BOOLEAN; |
| 109 |
(* Returns TRUE if there is sufficient room in destination to append a string of |
| 110 |
length sourceLength to the string in destination; otherwise returns FALSE. |
| 111 |
*) |
| 112 |
|
| 113 |
PROCEDURE CanConcatAll (source1Length, source2Length: CARDINAL; |
| 114 |
VAR destination: ARRAY OF CHAR): BOOLEAN; |
| 115 |
(* Returns TRUE if there is sufficient room in destination for a two strings of |
| 116 |
lengths source1Length and source2Length; otherwise returns FALSE. |
| 117 |
*) |
| 118 |
|
| 119 |
|
| 120 |
(* The following type and procedures provide for the comparison of string values, |
| 121 |
and for the location of substrings within strings. |
| 122 |
*) |
| 123 |
|
| 124 |
TYPE |
| 125 |
CompareResults = (less, equal, greater); |
| 126 |
|
| 127 |
|
| 128 |
PROCEDURE Compare (stringVal1, stringVal2: ARRAY OF CHAR): CompareResults; |
| 129 |
(* Returns less, equal, or greater, according as stringVal1 is lexically |
| 130 |
less than, equal to, or greater than stringVal2. |
| 131 |
*) |
| 132 |
|
| 133 |
PROCEDURE Equal (stringVal1, stringVal2: ARRAY OF CHAR): BOOLEAN; |
| 134 |
(* Returns Strings.Compare(stringVal1, stringVal2) = Strings.equal *) |
| 135 |
|
| 136 |
PROCEDURE FindNext (pattern, stringToSearch: ARRAY OF CHAR; startIndex: CARDINAL; |
| 137 |
VAR patternFound: BOOLEAN; VAR posOfPattern: CARDINAL); |
| 138 |
(* Looks forward for next occurrence of pattern in stringToSearch, starting |
| 139 |
the search at position startIndex. If startIndex < LENGTH(stringToSearch) |
| 140 |
and pattern is found, patternFound is returned as TRUE, and posOfPattern |
| 141 |
contains the start position in stringToSearch of pattern. Otherwise |
| 142 |
patternFound is returned as FALSE, and posOfPattern is unchanged. |
| 143 |
*) |
| 144 |
|
| 145 |
PROCEDURE FindPrev (pattern, stringToSearch: ARRAY OF CHAR; startIndex: CARDINAL; |
| 146 |
VAR patternFound: BOOLEAN; VAR posOfPattern: CARDINAL); |
| 147 |
(* Looks backward for the previous occurrence of pattern in stringToSearch and |
| 148 |
returns the position of the first character of the pattern if found. |
| 149 |
The search for the pattern begins at startIndex. If pattern is found, |
| 150 |
patternFound is returned as TRUE, and posOfPattern contains the start |
| 151 |
position in stringToSearch of pattern in the range [0..startIndex]. |
| 152 |
Otherwise patternFound is returned as FALSE, and posOfPattern is unchanged. |
| 153 |
*) |
| 154 |
|
| 155 |
PROCEDURE FindDiff (stringVal1, stringVal2: ARRAY OF CHAR; |
| 156 |
VAR differenceFound: BOOLEAN; VAR posOfDifference: CARDINAL); |
| 157 |
(* Compares the string values in stringVal1 and stringVal2 for differences. |
| 158 |
If they are equal, differenceFound is returned as FALSE, and TRUE otherwise. |
| 159 |
If differenceFound is TRUE, posOfDifference is set to the position of the |
| 160 |
first difference; otherwise posOfDifference is unchanged. |
| 161 |
*) |
| 162 |
|
| 163 |
PROCEDURE Capitalize (VAR stringVar: ARRAY OF CHAR); |
| 164 |
(* Applies the function CAP to each character of the string value in stringVar *) |
| 165 |
|
| 166 |
END Strings2. |