Daily bump.
[official-gcc.git] / gcc / m2 / mc-boot / GStrLib.cc
blobdbde45c912262b939526da0fda106fcef310b976
1 /* do not edit automatically generated by mc from StrLib. */
2 /* StrLib.mod provides string manipulation procedures.
4 Copyright (C) 2001-2025 Free Software Foundation, Inc.
5 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
7 This file is part of GNU Modula-2.
9 GNU Modula-2 is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GNU Modula-2 is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
28 #include "config.h"
29 #include "system.h"
30 #include <stdbool.h>
31 # if !defined (PROC_D)
32 # define PROC_D
33 typedef void (*PROC_t) (void);
34 typedef struct { PROC_t proc; } PROC;
35 # endif
37 # if !defined (TRUE)
38 # define TRUE (1==1)
39 # endif
41 # if !defined (FALSE)
42 # define FALSE (1==0)
43 # endif
45 #define _StrLib_C
47 #include "GStrLib.h"
48 # include "GASCII.h"
52 StrConCat - combines a and b into c.
55 extern "C" void StrLib_StrConCat (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high, char *c, unsigned int _c_high);
58 StrLess - returns TRUE if string, a, alphabetically occurs before
59 string, b.
62 extern "C" bool StrLib_StrLess (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high);
63 extern "C" bool StrLib_StrEqual (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high);
64 extern "C" unsigned int StrLib_StrLen (const char *a_, unsigned int _a_high);
67 StrCopy - copy string src into string dest providing dest is large enough.
68 If dest is smaller than a then src then the string is truncated when
69 dest is full. Add a nul character if there is room in dest.
72 extern "C" void StrLib_StrCopy (const char *src_, unsigned int _src_high, char *dest, unsigned int _dest_high);
75 IsSubString - returns true if b is a subcomponent of a.
78 extern "C" bool StrLib_IsSubString (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high);
81 StrRemoveWhitePrefix - copies string, into string, b, excluding any white
82 space infront of a.
85 extern "C" void StrLib_StrRemoveWhitePrefix (const char *a_, unsigned int _a_high, char *b, unsigned int _b_high);
88 IsWhite - returns TRUE if, ch, is a space or a tab.
91 static bool IsWhite (char ch);
95 IsWhite - returns TRUE if, ch, is a space or a tab.
98 static bool IsWhite (char ch)
100 return (ch == ' ') || (ch == ASCII_tab);
101 /* static analysis guarentees a RETURN statement will be used before here. */
102 __builtin_unreachable ();
107 StrConCat - combines a and b into c.
110 extern "C" void StrLib_StrConCat (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high, char *c, unsigned int _c_high)
112 unsigned int Highb;
113 unsigned int Highc;
114 unsigned int i;
115 unsigned int j;
116 char a[_a_high+1];
117 char b[_b_high+1];
119 /* make a local copy of each unbounded array. */
120 memcpy (a, a_, _a_high+1);
121 memcpy (b, b_, _b_high+1);
123 Highb = StrLib_StrLen ((const char *) b, _b_high);
124 Highc = _c_high;
125 StrLib_StrCopy ((const char *) a, _a_high, (char *) c, _c_high);
126 i = StrLib_StrLen ((const char *) c, _c_high);
127 j = 0;
128 while ((j < Highb) && (i <= Highc))
130 const_cast<char *>(c)[i] = b[j];
131 i += 1;
132 j += 1;
134 if (i <= Highc)
136 const_cast<char *>(c)[i] = ASCII_nul;
142 StrLess - returns TRUE if string, a, alphabetically occurs before
143 string, b.
146 extern "C" bool StrLib_StrLess (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high)
148 unsigned int Higha;
149 unsigned int Highb;
150 unsigned int i;
151 char a[_a_high+1];
152 char b[_b_high+1];
154 /* make a local copy of each unbounded array. */
155 memcpy (a, a_, _a_high+1);
156 memcpy (b, b_, _b_high+1);
158 Higha = StrLib_StrLen ((const char *) a, _a_high);
159 Highb = StrLib_StrLen ((const char *) b, _b_high);
160 i = 0;
161 while ((i < Higha) && (i < Highb))
163 if (a[i] < b[i])
165 return true;
167 else if (a[i] > b[i])
169 /* avoid dangling else. */
170 return false;
172 /* must be equal, move on to next character */
173 i += 1;
175 return Higha < Highb; /* substrings are equal so we go on length */
176 /* static analysis guarentees a RETURN statement will be used before here. */
177 __builtin_unreachable ();
180 extern "C" bool StrLib_StrEqual (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high)
182 unsigned int i;
183 unsigned int higha;
184 unsigned int highb;
185 char a[_a_high+1];
186 char b[_b_high+1];
188 /* make a local copy of each unbounded array. */
189 memcpy (a, a_, _a_high+1);
190 memcpy (b, b_, _b_high+1);
192 higha = _a_high;
193 highb = _b_high;
194 i = 0;
195 while ((((i <= higha) && (i <= highb)) && (a[i] != ASCII_nul)) && (b[i] != ASCII_nul))
197 if (a[i] != b[i])
199 return false;
201 i += 1;
203 return ! (((i <= higha) && (a[i] != ASCII_nul)) || ((i <= highb) && (b[i] != ASCII_nul)));
204 /* static analysis guarentees a RETURN statement will be used before here. */
205 __builtin_unreachable ();
208 extern "C" unsigned int StrLib_StrLen (const char *a_, unsigned int _a_high)
210 unsigned int High;
211 unsigned int Len;
212 char a[_a_high+1];
214 /* make a local copy of each unbounded array. */
215 memcpy (a, a_, _a_high+1);
217 Len = 0;
218 High = _a_high;
219 while ((Len <= High) && (a[Len] != ASCII_nul))
221 Len += 1;
223 return Len;
224 /* static analysis guarentees a RETURN statement will be used before here. */
225 __builtin_unreachable ();
230 StrCopy - copy string src into string dest providing dest is large enough.
231 If dest is smaller than a then src then the string is truncated when
232 dest is full. Add a nul character if there is room in dest.
235 extern "C" void StrLib_StrCopy (const char *src_, unsigned int _src_high, char *dest, unsigned int _dest_high)
237 unsigned int HighSrc;
238 unsigned int HighDest;
239 unsigned int n;
240 char src[_src_high+1];
242 /* make a local copy of each unbounded array. */
243 memcpy (src, src_, _src_high+1);
245 n = 0;
246 HighSrc = StrLib_StrLen ((const char *) src, _src_high);
247 HighDest = _dest_high;
248 while ((n < HighSrc) && (n <= HighDest))
250 const_cast<char *>(dest)[n] = src[n];
251 n += 1;
253 if (n <= HighDest)
255 const_cast<char *>(dest)[n] = ASCII_nul;
261 IsSubString - returns true if b is a subcomponent of a.
264 extern "C" bool StrLib_IsSubString (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high)
266 unsigned int i;
267 unsigned int j;
268 unsigned int LengthA;
269 unsigned int LengthB;
270 char a[_a_high+1];
271 char b[_b_high+1];
273 /* make a local copy of each unbounded array. */
274 memcpy (a, a_, _a_high+1);
275 memcpy (b, b_, _b_high+1);
277 LengthA = StrLib_StrLen ((const char *) a, _a_high);
278 LengthB = StrLib_StrLen ((const char *) b, _b_high);
279 i = 0;
280 if (LengthA > LengthB)
282 while (i <= (LengthA-LengthB))
284 j = 0;
285 while ((j < LengthB) && (a[i+j] == b[j]))
287 j += 1;
289 if (j == LengthB)
291 return true;
293 else
295 i += 1;
299 return false;
300 /* static analysis guarentees a RETURN statement will be used before here. */
301 __builtin_unreachable ();
306 StrRemoveWhitePrefix - copies string, into string, b, excluding any white
307 space infront of a.
310 extern "C" void StrLib_StrRemoveWhitePrefix (const char *a_, unsigned int _a_high, char *b, unsigned int _b_high)
312 unsigned int i;
313 unsigned int j;
314 unsigned int higha;
315 unsigned int highb;
316 char a[_a_high+1];
318 /* make a local copy of each unbounded array. */
319 memcpy (a, a_, _a_high+1);
321 i = 0;
322 j = 0;
323 higha = StrLib_StrLen ((const char *) a, _a_high);
324 highb = _b_high;
325 while ((i < higha) && (IsWhite (a[i])))
327 i += 1;
329 while ((i < higha) && (j <= highb))
331 const_cast<char *>(b)[j] = a[i];
332 i += 1;
333 j += 1;
335 if (j <= highb)
337 const_cast<char *>(b)[j] = ASCII_nul;
341 extern "C" void _M2_StrLib_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
345 extern "C" void _M2_StrLib_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])