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)
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/>. */
31 # if !defined (PROC_D)
33 typedef void (*PROC_t
) (void);
34 typedef struct { PROC_t proc
; } PROC
;
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
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
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
)
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
);
125 StrLib_StrCopy ((const char *) a
, _a_high
, (char *) c
, _c_high
);
126 i
= StrLib_StrLen ((const char *) c
, _c_high
);
128 while ((j
< Highb
) && (i
<= Highc
))
130 const_cast<char *>(c
)[i
] = b
[j
];
136 const_cast<char *>(c
)[i
] = ASCII_nul
;
142 StrLess - returns TRUE if string, a, alphabetically occurs before
146 extern "C" bool StrLib_StrLess (const char *a_
, unsigned int _a_high
, const char *b_
, unsigned int _b_high
)
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
);
161 while ((i
< Higha
) && (i
< Highb
))
167 else if (a
[i
] > b
[i
])
169 /* avoid dangling else. */
172 /* must be equal, move on to next character */
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
)
188 /* make a local copy of each unbounded array. */
189 memcpy (a
, a_
, _a_high
+1);
190 memcpy (b
, b_
, _b_high
+1);
195 while ((((i
<= higha
) && (i
<= highb
)) && (a
[i
] != ASCII_nul
)) && (b
[i
] != ASCII_nul
))
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
)
214 /* make a local copy of each unbounded array. */
215 memcpy (a
, a_
, _a_high
+1);
219 while ((Len
<= High
) && (a
[Len
] != ASCII_nul
))
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
;
240 char src
[_src_high
+1];
242 /* make a local copy of each unbounded array. */
243 memcpy (src
, src_
, _src_high
+1);
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
];
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
)
268 unsigned int LengthA
;
269 unsigned int LengthB
;
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
);
280 if (LengthA
> LengthB
)
282 while (i
<= (LengthA
-LengthB
))
285 while ((j
< LengthB
) && (a
[i
+j
] == b
[j
]))
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
310 extern "C" void StrLib_StrRemoveWhitePrefix (const char *a_
, unsigned int _a_high
, char *b
, unsigned int _b_high
)
318 /* make a local copy of each unbounded array. */
319 memcpy (a
, a_
, _a_high
+1);
323 higha
= StrLib_StrLen ((const char *) a
, _a_high
);
325 while ((i
< higha
) && (IsWhite (a
[i
])))
329 while ((i
< higha
) && (j
<= highb
))
331 const_cast<char *>(b
)[j
] = a
[i
];
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
[])