| 1 |
(* Strings.mod provides a Logitech-3.0 compatible library. |
| 2 |
|
| 3 |
Copyright (C) 2005-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 |
FROM ASCII IMPORT nul ; |
| 30 |
IMPORT StrLib ; |
| 31 |
IMPORT DynamicStrings ; |
| 32 |
|
| 33 |
|
| 34 |
(* |
| 35 |
Assign - source := dest. |
| 36 |
*) |
| 37 |
|
| 38 |
PROCEDURE Assign (VAR dest: ARRAY OF CHAR; source: ARRAY OF CHAR) ; |
| 39 |
BEGIN |
| 40 |
StrLib.StrCopy(source, dest) |
| 41 |
END Assign ; |
| 42 |
|
| 43 |
|
| 44 |
(* |
| 45 |
Insert - insert the string, substr, into str at position, index. |
| 46 |
substr, is added to the end of, str, if, index >= length(str) |
| 47 |
*) |
| 48 |
|
| 49 |
PROCEDURE Insert (substr: ARRAY OF CHAR; VAR str: ARRAY OF CHAR; |
| 50 |
index: CARDINAL) ; |
| 51 |
VAR |
| 52 |
s1, s2: DynamicStrings.String ; |
| 53 |
BEGIN |
| 54 |
IF index>Length(str) |
| 55 |
THEN |
| 56 |
ConCat(str, substr, str) |
| 57 |
ELSE |
| 58 |
s1 := DynamicStrings.InitString(str) ; |
| 59 |
s2 := DynamicStrings.ConCat(DynamicStrings.Slice(s1, 0, index), |
| 60 |
DynamicStrings.Mark(DynamicStrings.InitString(str))) ; |
| 61 |
s2 := DynamicStrings.ConCat(s2, DynamicStrings.Slice(s1, index, 0)) ; |
| 62 |
DynamicStrings.CopyOut(str, s2) ; |
| 63 |
s1 := DynamicStrings.KillString(s1) ; |
| 64 |
s2 := DynamicStrings.KillString(s2) |
| 65 |
END |
| 66 |
END Insert ; |
| 67 |
|
| 68 |
|
| 69 |
(* |
| 70 |
Delete - delete len characters from, str, starting at, index. |
| 71 |
*) |
| 72 |
|
| 73 |
PROCEDURE Delete (VAR str: ARRAY OF CHAR; index: CARDINAL; length: CARDINAL) ; |
| 74 |
VAR |
| 75 |
s: DynamicStrings.String ; |
| 76 |
BEGIN |
| 77 |
s := DynamicStrings.InitString(str) ; |
| 78 |
s := DynamicStrings.ConCat(DynamicStrings.Mark(DynamicStrings.Slice(s, 0, index)), |
| 79 |
DynamicStrings.Mark(DynamicStrings.Slice(s, index+length, 0))) ; |
| 80 |
DynamicStrings.CopyOut(str, s) ; |
| 81 |
s := DynamicStrings.KillString(s) |
| 82 |
END Delete ; |
| 83 |
|
| 84 |
|
| 85 |
(* |
| 86 |
PosLower - return the first position of substr in str. |
| 87 |
*) |
| 88 |
|
| 89 |
PROCEDURE PosLower (substr, str: ARRAY OF CHAR) : CARDINAL ; |
| 90 |
VAR |
| 91 |
i, strLen, substrLen : INTEGER ; |
| 92 |
strS, substrS, scratchS: DynamicStrings.String ; |
| 93 |
BEGIN |
| 94 |
strS := DynamicStrings.InitString (str) ; |
| 95 |
substrS := DynamicStrings.InitString (substr) ; |
| 96 |
strLen := DynamicStrings.Length (strS) ; |
| 97 |
substrLen := DynamicStrings.Length (substrS) ; |
| 98 |
i := 0 ; |
| 99 |
REPEAT |
| 100 |
i := DynamicStrings.Index (strS, DynamicStrings.char (substrS, 0), i) ; |
| 101 |
IF i < 0 |
| 102 |
THEN |
| 103 |
(* No match on first character therefore return now. *) |
| 104 |
strS := DynamicStrings.KillString (strS) ; |
| 105 |
substrS := DynamicStrings.KillString (substrS) ; |
| 106 |
scratchS := DynamicStrings.KillString (scratchS) ; |
| 107 |
RETURN( HIGH (str) + 1 ) |
| 108 |
ELSIF i + substrLen <= strLen |
| 109 |
THEN |
| 110 |
scratchS := DynamicStrings.Slice (strS, i, i + substrLen) ; |
| 111 |
IF DynamicStrings.Equal (scratchS, substrS) |
| 112 |
THEN |
| 113 |
strS := DynamicStrings.KillString (strS) ; |
| 114 |
substrS := DynamicStrings.KillString (substrS) ; |
| 115 |
scratchS := DynamicStrings.KillString (scratchS) ; |
| 116 |
RETURN( i ) |
| 117 |
END ; |
| 118 |
scratchS := DynamicStrings.KillString (scratchS) |
| 119 |
END ; |
| 120 |
INC (i) |
| 121 |
UNTIL i >= strLen ; |
| 122 |
strS := DynamicStrings.KillString (strS) ; |
| 123 |
substrS := DynamicStrings.KillString (substrS) ; |
| 124 |
scratchS := DynamicStrings.KillString (scratchS) ; |
| 125 |
RETURN( HIGH (str) + 1 ) |
| 126 |
END PosLower ; |
| 127 |
|
| 128 |
|
| 129 |
(* |
| 130 |
Pos - return the first position of substr in str. |
| 131 |
If substr is not found in str then it returns |
| 132 |
HIGH (str) + 1. |
| 133 |
*) |
| 134 |
|
| 135 |
PROCEDURE Pos (substr, str: ARRAY OF CHAR) : CARDINAL ; |
| 136 |
BEGIN |
| 137 |
IF Length (substr) <= Length (str) |
| 138 |
THEN |
| 139 |
RETURN PosLower (substr, str) |
| 140 |
END ; |
| 141 |
RETURN( HIGH (str) + 1 ) |
| 142 |
END Pos ; |
| 143 |
|
| 144 |
|
| 145 |
(* |
| 146 |
Copy - copy at most, length, characters in, substr, to, str, |
| 147 |
starting at position, index. |
| 148 |
*) |
| 149 |
|
| 150 |
PROCEDURE Copy (str: ARRAY OF CHAR; |
| 151 |
index, length: CARDINAL; VAR result: ARRAY OF CHAR) ; |
| 152 |
VAR |
| 153 |
s1, s2: DynamicStrings.String ; |
| 154 |
BEGIN |
| 155 |
s1 := DynamicStrings.InitString (str) ; |
| 156 |
s2 := DynamicStrings.Slice (s1, index, index+length) ; |
| 157 |
DynamicStrings.CopyOut (result, s2) ; |
| 158 |
s1 := DynamicStrings.KillString (s1) ; |
| 159 |
s2 := DynamicStrings.KillString (s2) |
| 160 |
END Copy ; |
| 161 |
|
| 162 |
|
| 163 |
(* |
| 164 |
ConCat - concatenates two strings, s1, and, s2 |
| 165 |
and places the result into, dest. |
| 166 |
*) |
| 167 |
|
| 168 |
PROCEDURE ConCat (s1, s2: ARRAY OF CHAR; VAR dest: ARRAY OF CHAR) ; |
| 169 |
BEGIN |
| 170 |
StrLib.StrConCat(s1, s2, dest) |
| 171 |
END ConCat ; |
| 172 |
|
| 173 |
|
| 174 |
(* |
| 175 |
Length - return the length of string, s. |
| 176 |
*) |
| 177 |
|
| 178 |
PROCEDURE Length (s: ARRAY OF CHAR) : CARDINAL ; |
| 179 |
BEGIN |
| 180 |
RETURN( StrLib.StrLen(s) ) |
| 181 |
END Length ; |
| 182 |
|
| 183 |
|
| 184 |
(* |
| 185 |
CompareStr - compare two strings, left, and, right. |
| 186 |
*) |
| 187 |
|
| 188 |
PROCEDURE CompareStr (left, right: ARRAY OF CHAR) : INTEGER ; |
| 189 |
BEGIN |
| 190 |
IF StrLib.StrLess(left, right) |
| 191 |
THEN |
| 192 |
RETURN( -1 ) |
| 193 |
ELSIF StrLib.StrEqual(left, right) |
| 194 |
THEN |
| 195 |
RETURN( 0 ) |
| 196 |
ELSE |
| 197 |
RETURN( 1 ) |
| 198 |
END |
| 199 |
END CompareStr ; |
| 200 |
|
| 201 |
|
| 202 |
END Strings. |