Debian Codegraph @

gcc/testsuite/gm2/pimlib/base/run/pass/StrLib.mod

raw
1 (* StrLib.mod provides string manipulation procedures.
2
3 Copyright (C) 2001-2023 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 StrLib ;
28
29 FROM ASCII IMPORT nul, tab ;
30
31
32 (*
33 StrConCat - combines a and b into c.
34 *)
35
36 PROCEDURE StrConCat (a: ARRAY OF CHAR; b: ARRAY OF CHAR; VAR c: ARRAY OF CHAR) ;
37 VAR
38 Highb,
39 Highc,
40 i, j : CARDINAL ;
41 BEGIN
42 Highb := StrLen(b) ;
43 Highc := HIGH(c) ;
44 StrCopy(a, c) ;
45 i := StrLen(c) ;
46 j := 0 ;
47 WHILE (j<Highb) AND (i<=Highc) DO
48 c[i] := b[j] ;
49 INC(i) ;
50 INC(j)
51 END ;
52 IF i<=Highc
53 THEN
54 c[i] := nul
55 END
56 END StrConCat ;
57
58
59 (*
60 StrLess - returns TRUE if string, a, alphabetically occurs before
61 string, b.
62 *)
63
64 PROCEDURE StrLess (a, b: ARRAY OF CHAR) : BOOLEAN ;
65 VAR
66 Higha,
67 Highb,
68 i : CARDINAL ;
69 BEGIN
70 Higha := StrLen(a) ;
71 Highb := StrLen(b) ;
72 i := 0 ;
73 WHILE (i<Higha) AND (i<Highb) DO
74 IF a[i]<b[i]
75 THEN
76 RETURN( TRUE )
77 ELSIF a[i]>b[i]
78 THEN
79 RETURN( FALSE )
80 END ;
81 (* must be equal, move on to next character *)
82 INC(i)
83 END ;
84 RETURN( Higha<Highb ) (* substrings are equal so we go on length *)
85 END StrLess ;
86
87
88 PROCEDURE StrEqual (a, b: ARRAY OF CHAR) : BOOLEAN ;
89 VAR
90 i,
91 higha,
92 highb: CARDINAL ;
93 BEGIN
94 higha := HIGH(a) ;
95 highb := HIGH(b) ;
96 i := 0 ;
97 WHILE (i<=higha) AND (i<=highb) AND (a[i]#nul) AND (b[i]#nul) DO
98 IF a[i]#b[i]
99 THEN
100 RETURN( FALSE )
101 END ;
102 INC(i)
103 END ;
104 RETURN NOT (((i<=higha) AND (a[i]#nul)) OR
105 ((i<=highb) AND (b[i]#nul)))
106 END StrEqual ;
107
108
109 PROCEDURE StrLen (a: ARRAY OF CHAR) : CARDINAL ;
110 VAR
111 High,
112 Len : CARDINAL ;
113 BEGIN
114 Len := 0 ;
115 High := HIGH(a) ;
116 WHILE (Len<=High) AND (a[Len]#nul) DO
117 INC(Len)
118 END ;
119 RETURN( Len )
120 END StrLen ;
121
122
123 (*
124 StrCopy - copy string src into string dest providing dest is large enough.
125 If dest is smaller than a then src then the string is truncated when
126 dest is full. Add a nul character if there is room in dest.
127 *)
128
129 PROCEDURE StrCopy (src: ARRAY OF CHAR ; VAR dest: ARRAY OF CHAR) ;
130 VAR
131 HighSrc,
132 HighDest,
133 n : CARDINAL ;
134 BEGIN
135 n := 0 ;
136 HighSrc := StrLen (src) ;
137 HighDest := HIGH (dest) ;
138 WHILE (n < HighSrc) AND (n <= HighDest) DO
139 dest[n] := src[n] ;
140 INC (n)
141 END ;
142 IF n <= HighDest
143 THEN
144 dest[n] := nul
145 END
146 END StrCopy ;
147
148
149 (*
150 IsSubString - returns true if b is a subcomponent of a.
151 *)
152
153 PROCEDURE IsSubString (a, b: ARRAY OF CHAR) : BOOLEAN ;
154 VAR
155 i, j,
156 LengthA,
157 LengthB: CARDINAL ;
158 BEGIN
159 LengthA := StrLen(a) ;
160 LengthB := StrLen(b) ;
161 i := 0 ;
162 IF LengthA>LengthB
163 THEN
164 WHILE i<=LengthA-LengthB DO
165 j := 0 ;
166 WHILE (j<LengthB) AND (a[i+j]=b[j]) DO
167 INC(j)
168 END ;
169 IF j=LengthB
170 THEN
171 RETURN( TRUE )
172 ELSE
173 INC(i)
174 END
175 END
176 END ;
177 RETURN( FALSE )
178 END IsSubString ;
179
180
181 (*
182 IsWhite - returns TRUE if, ch, is a space or a tab.
183 *)
184
185 PROCEDURE IsWhite (ch: CHAR) : BOOLEAN ;
186 BEGIN
187 RETURN( (ch=' ') OR (ch=tab) )
188 END IsWhite ;
189
190
191 (*
192 StrRemoveWhitePrefix - copies string, into string, b, excluding any white
193 space infront of a.
194 *)
195
196 PROCEDURE StrRemoveWhitePrefix (a: ARRAY OF CHAR; VAR b: ARRAY OF CHAR) ;
197 VAR
198 i, j,
199 higha, highb: CARDINAL ;
200 BEGIN
201 i := 0 ;
202 j := 0 ;
203 higha := StrLen(a) ;
204 highb := HIGH(b) ;
205 WHILE (i<higha) AND IsWhite(a[i]) DO
206 INC(i)
207 END ;
208 WHILE (i<higha) AND (j<=highb) DO
209 b[j] := a[i] ;
210 INC(i) ;
211 INC(j)
212 END ;
213 IF j<=highb
214 THEN
215 b[j] := nul
216 END
217 END StrRemoveWhitePrefix ;
218
219
220 END StrLib.