]>
Commit | Line | Data |
---|---|---|
1eee94d3 GM |
1 | (* Strings.mod provides a Logitech-3.0 compatible library. |
2 | ||
83ffe9cd | 3 | Copyright (C) 2005-2023 Free Software Foundation, Inc. |
1eee94d3 GM |
4 | Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>. |
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 | Pos - return the first position of, substr, in, str. | |
87 | *) | |
88 | ||
89 | PROCEDURE Pos (substr, str: ARRAY OF CHAR) : CARDINAL ; | |
90 | VAR | |
91 | i, k, l : INTEGER ; | |
92 | s1, s2, s3: DynamicStrings.String ; | |
93 | BEGIN | |
94 | s1 := DynamicStrings.InitString(str) ; | |
95 | s2 := DynamicStrings.InitString(substr) ; | |
96 | k := DynamicStrings.Length(s1) ; | |
97 | l := DynamicStrings.Length(s2) ; | |
98 | i := 0 ; | |
99 | REPEAT | |
100 | i := DynamicStrings.Index(s1, DynamicStrings.char(s2, 0), i) ; | |
101 | IF i>=0 | |
102 | THEN | |
103 | s3 := DynamicStrings.Slice(s1, i, l) ; | |
104 | IF DynamicStrings.Equal(s3, s2) | |
105 | THEN | |
106 | s1 := DynamicStrings.KillString(s1) ; | |
107 | s2 := DynamicStrings.KillString(s2) ; | |
108 | s3 := DynamicStrings.KillString(s3) ; | |
109 | RETURN( i ) | |
110 | END ; | |
111 | s3 := DynamicStrings.KillString(s3) | |
112 | END ; | |
113 | INC(i) | |
114 | UNTIL i>=k ; | |
115 | s1 := DynamicStrings.KillString(s1) ; | |
116 | s2 := DynamicStrings.KillString(s2) ; | |
117 | s3 := DynamicStrings.KillString(s3) ; | |
118 | RETURN( HIGH(str)+1 ) | |
119 | END Pos ; | |
120 | ||
121 | ||
122 | (* | |
123 | Copy - copy at most, length, characters in, substr, to, str, | |
124 | starting at position, index. | |
125 | *) | |
126 | ||
127 | PROCEDURE Copy (str: ARRAY OF CHAR; | |
128 | index, length: CARDINAL; VAR result: ARRAY OF CHAR) ; | |
129 | VAR | |
130 | s1, s2: DynamicStrings.String ; | |
131 | BEGIN | |
132 | s1 := DynamicStrings.InitString(str) ; | |
133 | s2 := DynamicStrings.Slice(s1, index, index+length) ; | |
134 | DynamicStrings.CopyOut(result, s2) ; | |
135 | s1 := DynamicStrings.KillString(s1) ; | |
136 | s2 := DynamicStrings.KillString(s2) | |
137 | END Copy ; | |
138 | ||
139 | ||
140 | (* | |
141 | ConCat - concatenates two strings, s1, and, s2 | |
142 | and places the result into, dest. | |
143 | *) | |
144 | ||
145 | PROCEDURE ConCat (s1, s2: ARRAY OF CHAR; VAR dest: ARRAY OF CHAR) ; | |
146 | BEGIN | |
147 | StrLib.StrConCat(s1, s2, dest) | |
148 | END ConCat ; | |
149 | ||
150 | ||
151 | (* | |
152 | Length - return the length of string, s. | |
153 | *) | |
154 | ||
155 | PROCEDURE Length (s: ARRAY OF CHAR) : CARDINAL ; | |
156 | BEGIN | |
157 | RETURN( StrLib.StrLen(s) ) | |
158 | END Length ; | |
159 | ||
160 | ||
161 | (* | |
162 | CompareStr - compare two strings, left, and, right. | |
163 | *) | |
164 | ||
165 | PROCEDURE CompareStr (left, right: ARRAY OF CHAR) : INTEGER ; | |
166 | BEGIN | |
167 | IF StrLib.StrLess(left, right) | |
168 | THEN | |
169 | RETURN( -1 ) | |
170 | ELSIF StrLib.StrEqual(left, right) | |
171 | THEN | |
172 | RETURN( 0 ) | |
173 | ELSE | |
174 | RETURN( 1 ) | |
175 | END | |
176 | END CompareStr ; | |
177 | ||
178 | ||
179 | END Strings. |