| 1 |
/* do not edit automatically generated by mc from StrLib. */ |
| 2 |
/* StrLib.mod provides string manipulation procedures. |
| 3 |
|
| 4 |
Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 5 |
Contributed by Gaius Mulley <[email protected]>. |
| 6 |
|
| 7 |
This file is part of GNU Modula-2. |
| 8 |
|
| 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. |
| 13 |
|
| 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. |
| 18 |
|
| 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. |
| 22 |
|
| 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/>. */ |
| 27 |
|
| 28 |
#include <stdbool.h> |
| 29 |
# if !defined (PROC_D) |
| 30 |
# define PROC_D |
| 31 |
typedef void (*PROC_t) (void); |
| 32 |
typedef struct { PROC_t proc; } PROC; |
| 33 |
# endif |
| 34 |
|
| 35 |
# if !defined (TRUE) |
| 36 |
# define TRUE (1==1) |
| 37 |
# endif |
| 38 |
|
| 39 |
# if !defined (FALSE) |
| 40 |
# define FALSE (1==0) |
| 41 |
# endif |
| 42 |
|
| 43 |
#include <string.h> |
| 44 |
#include <limits.h> |
| 45 |
#define _StrLib_C |
| 46 |
|
| 47 |
#include "GStrLib.h" |
| 48 |
# include "GASCII.h" |
| 49 |
|
| 50 |
|
| 51 |
/* |
| 52 |
StrConCat - combines a and b into c. |
| 53 |
*/ |
| 54 |
|
| 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); |
| 56 |
|
| 57 |
/* |
| 58 |
StrLess - returns TRUE if string, a, alphabetically occurs before |
| 59 |
string, b. |
| 60 |
*/ |
| 61 |
|
| 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); |
| 65 |
|
| 66 |
/* |
| 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. |
| 70 |
*/ |
| 71 |
|
| 72 |
extern "C" void StrLib_StrCopy (const char *src_, unsigned int _src_high, char *dest, unsigned int _dest_high); |
| 73 |
|
| 74 |
/* |
| 75 |
IsSubString - returns true if b is a subcomponent of a. |
| 76 |
*/ |
| 77 |
|
| 78 |
extern "C" bool StrLib_IsSubString (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high); |
| 79 |
|
| 80 |
/* |
| 81 |
StrRemoveWhitePrefix - copies string, into string, b, excluding any white |
| 82 |
space infront of a. |
| 83 |
*/ |
| 84 |
|
| 85 |
extern "C" void StrLib_StrRemoveWhitePrefix (const char *a_, unsigned int _a_high, char *b, unsigned int _b_high); |
| 86 |
|
| 87 |
/* |
| 88 |
IsWhite - returns TRUE if, ch, is a space or a tab. |
| 89 |
*/ |
| 90 |
|
| 91 |
static bool IsWhite (char ch); |
| 92 |
|
| 93 |
|
| 94 |
/* |
| 95 |
IsWhite - returns TRUE if, ch, is a space or a tab. |
| 96 |
*/ |
| 97 |
|
| 98 |
static bool IsWhite (char ch) |
| 99 |
{ |
| 100 |
return (ch == ' ') || (ch == ASCII_tab); |
| 101 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 102 |
__builtin_unreachable (); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/* |
| 107 |
StrConCat - combines a and b into c. |
| 108 |
*/ |
| 109 |
|
| 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) |
| 111 |
{ |
| 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]; |
| 118 |
|
| 119 |
/* make a local copy of each unbounded array. */ |
| 120 |
memcpy (a, a_, _a_high+1); |
| 121 |
memcpy (b, b_, _b_high+1); |
| 122 |
|
| 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)) |
| 129 |
{ |
| 130 |
const_cast<char *>(c)[i] = b[j]; |
| 131 |
i += 1; |
| 132 |
j += 1; |
| 133 |
} |
| 134 |
if (i <= Highc) |
| 135 |
{ |
| 136 |
const_cast<char *>(c)[i] = ASCII_nul; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/* |
| 142 |
StrLess - returns TRUE if string, a, alphabetically occurs before |
| 143 |
string, b. |
| 144 |
*/ |
| 145 |
|
| 146 |
extern "C" bool StrLib_StrLess (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high) |
| 147 |
{ |
| 148 |
unsigned int Higha; |
| 149 |
unsigned int Highb; |
| 150 |
unsigned int i; |
| 151 |
char a[_a_high+1]; |
| 152 |
char b[_b_high+1]; |
| 153 |
|
| 154 |
/* make a local copy of each unbounded array. */ |
| 155 |
memcpy (a, a_, _a_high+1); |
| 156 |
memcpy (b, b_, _b_high+1); |
| 157 |
|
| 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)) |
| 162 |
{ |
| 163 |
if (a[i] < b[i]) |
| 164 |
{ |
| 165 |
return true; |
| 166 |
} |
| 167 |
else if (a[i] > b[i]) |
| 168 |
{ |
| 169 |
/* avoid dangling else. */ |
| 170 |
return false; |
| 171 |
} |
| 172 |
/* must be equal, move on to next character */ |
| 173 |
i += 1; |
| 174 |
} |
| 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 (); |
| 178 |
} |
| 179 |
|
| 180 |
extern "C" bool StrLib_StrEqual (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high) |
| 181 |
{ |
| 182 |
unsigned int i; |
| 183 |
unsigned int higha; |
| 184 |
unsigned int highb; |
| 185 |
char a[_a_high+1]; |
| 186 |
char b[_b_high+1]; |
| 187 |
|
| 188 |
/* make a local copy of each unbounded array. */ |
| 189 |
memcpy (a, a_, _a_high+1); |
| 190 |
memcpy (b, b_, _b_high+1); |
| 191 |
|
| 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)) |
| 196 |
{ |
| 197 |
if (a[i] != b[i]) |
| 198 |
{ |
| 199 |
return false; |
| 200 |
} |
| 201 |
i += 1; |
| 202 |
} |
| 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 (); |
| 206 |
} |
| 207 |
|
| 208 |
extern "C" unsigned int StrLib_StrLen (const char *a_, unsigned int _a_high) |
| 209 |
{ |
| 210 |
unsigned int High; |
| 211 |
unsigned int Len; |
| 212 |
char a[_a_high+1]; |
| 213 |
|
| 214 |
/* make a local copy of each unbounded array. */ |
| 215 |
memcpy (a, a_, _a_high+1); |
| 216 |
|
| 217 |
Len = 0; |
| 218 |
High = _a_high; |
| 219 |
while ((Len <= High) && (a[Len] != ASCII_nul)) |
| 220 |
{ |
| 221 |
Len += 1; |
| 222 |
} |
| 223 |
return Len; |
| 224 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 225 |
__builtin_unreachable (); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
/* |
| 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. |
| 233 |
*/ |
| 234 |
|
| 235 |
extern "C" void StrLib_StrCopy (const char *src_, unsigned int _src_high, char *dest, unsigned int _dest_high) |
| 236 |
{ |
| 237 |
unsigned int HighSrc; |
| 238 |
unsigned int HighDest; |
| 239 |
unsigned int n; |
| 240 |
char src[_src_high+1]; |
| 241 |
|
| 242 |
/* make a local copy of each unbounded array. */ |
| 243 |
memcpy (src, src_, _src_high+1); |
| 244 |
|
| 245 |
n = 0; |
| 246 |
HighSrc = StrLib_StrLen ((const char *) src, _src_high); |
| 247 |
HighDest = _dest_high; |
| 248 |
while ((n < HighSrc) && (n <= HighDest)) |
| 249 |
{ |
| 250 |
const_cast<char *>(dest)[n] = src[n]; |
| 251 |
n += 1; |
| 252 |
} |
| 253 |
if (n <= HighDest) |
| 254 |
{ |
| 255 |
const_cast<char *>(dest)[n] = ASCII_nul; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
/* |
| 261 |
IsSubString - returns true if b is a subcomponent of a. |
| 262 |
*/ |
| 263 |
|
| 264 |
extern "C" bool StrLib_IsSubString (const char *a_, unsigned int _a_high, const char *b_, unsigned int _b_high) |
| 265 |
{ |
| 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]; |
| 272 |
|
| 273 |
/* make a local copy of each unbounded array. */ |
| 274 |
memcpy (a, a_, _a_high+1); |
| 275 |
memcpy (b, b_, _b_high+1); |
| 276 |
|
| 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) |
| 281 |
{ |
| 282 |
while (i <= (LengthA-LengthB)) |
| 283 |
{ |
| 284 |
j = 0; |
| 285 |
while ((j < LengthB) && (a[i+j] == b[j])) |
| 286 |
{ |
| 287 |
j += 1; |
| 288 |
} |
| 289 |
if (j == LengthB) |
| 290 |
{ |
| 291 |
return true; |
| 292 |
} |
| 293 |
else |
| 294 |
{ |
| 295 |
i += 1; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
return false; |
| 300 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 301 |
__builtin_unreachable (); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
/* |
| 306 |
StrRemoveWhitePrefix - copies string, into string, b, excluding any white |
| 307 |
space infront of a. |
| 308 |
*/ |
| 309 |
|
| 310 |
extern "C" void StrLib_StrRemoveWhitePrefix (const char *a_, unsigned int _a_high, char *b, unsigned int _b_high) |
| 311 |
{ |
| 312 |
unsigned int i; |
| 313 |
unsigned int j; |
| 314 |
unsigned int higha; |
| 315 |
unsigned int highb; |
| 316 |
char a[_a_high+1]; |
| 317 |
|
| 318 |
/* make a local copy of each unbounded array. */ |
| 319 |
memcpy (a, a_, _a_high+1); |
| 320 |
|
| 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]))) |
| 326 |
{ |
| 327 |
i += 1; |
| 328 |
} |
| 329 |
while ((i < higha) && (j <= highb)) |
| 330 |
{ |
| 331 |
const_cast<char *>(b)[j] = a[i]; |
| 332 |
i += 1; |
| 333 |
j += 1; |
| 334 |
} |
| 335 |
if (j <= highb) |
| 336 |
{ |
| 337 |
const_cast<char *>(b)[j] = ASCII_nul; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
extern "C" void _M2_StrLib_init (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[]) |
| 342 |
{ |
| 343 |
} |
| 344 |
|
| 345 |
extern "C" void _M2_StrLib_fini (__attribute__((unused)) int argc,__attribute__((unused)) char *argv[],__attribute__((unused)) char *envp[]) |
| 346 |
{ |
| 347 |
} |