1 /* do not edit automatically generated by mc from DynamicStrings. */
2 /* DynamicStrings.mod provides a dynamic string type and procedures.
4 Copyright (C) 2001-2024 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
;
45 # include "GStorage.h"
46 #if defined(__cplusplus)
50 #define _DynamicStrings_H
51 #define _DynamicStrings_C
55 # include "GStorage.h"
56 # include "GAssertion.h"
62 # define PoisonOn false
63 # define DebugOn false
64 # define CheckOn false
65 # define TraceOn false
66 typedef struct DynamicStrings_Contents_r DynamicStrings_Contents
;
68 typedef struct DynamicStrings_DebugInfo_r DynamicStrings_DebugInfo
;
70 typedef struct DynamicStrings_stringRecord_r DynamicStrings_stringRecord
;
72 typedef struct DynamicStrings_descriptor_r DynamicStrings_descriptor
;
74 typedef DynamicStrings_descriptor
*DynamicStrings_Descriptor
;
76 typedef struct DynamicStrings_frameRec_r DynamicStrings_frameRec
;
78 typedef DynamicStrings_frameRec
*DynamicStrings_frame
;
80 typedef struct DynamicStrings__T3_a DynamicStrings__T3
;
82 typedef enum {DynamicStrings_inuse
, DynamicStrings_marked
, DynamicStrings_onlist
, DynamicStrings_poisoned
} DynamicStrings_desState
;
84 typedef DynamicStrings_stringRecord
*DynamicStrings_String
;
86 struct DynamicStrings_DebugInfo_r
{
87 DynamicStrings_String next
;
93 struct DynamicStrings_descriptor_r
{
96 unsigned int charStarSize
;
98 DynamicStrings_desState state
;
99 DynamicStrings_String garbage
;
102 struct DynamicStrings_frameRec_r
{
103 DynamicStrings_String alloc
;
104 DynamicStrings_String dealloc
;
105 DynamicStrings_frame next
;
108 struct DynamicStrings__T3_a
{ char array
[(MaxBuf
-1)+1]; };
109 struct DynamicStrings_Contents_r
{
110 DynamicStrings__T3 buf
;
112 DynamicStrings_String next
;
115 struct DynamicStrings_stringRecord_r
{
116 DynamicStrings_Contents contents
;
117 DynamicStrings_Descriptor head
;
118 DynamicStrings_DebugInfo debug
;
121 static bool Initialized
;
122 static DynamicStrings_frame frameHead
;
123 static DynamicStrings_String captured
;
126 InitString - creates and returns a String type object.
127 Initial contents are, a.
130 extern "C" DynamicStrings_String
DynamicStrings_InitString (const char *a_
, unsigned int _a_high
);
133 KillString - frees String, s, and its contents.
137 extern "C" DynamicStrings_String
DynamicStrings_KillString (DynamicStrings_String s
);
140 Fin - finishes with a string, it calls KillString with, s.
141 The purpose of the procedure is to provide a short cut
142 to calling KillString and then testing the return result.
145 extern "C" void DynamicStrings_Fin (DynamicStrings_String s
);
148 InitStringCharStar - initializes and returns a String to contain the C string.
151 extern "C" DynamicStrings_String
DynamicStrings_InitStringCharStar (void * a
);
154 InitStringChar - initializes and returns a String to contain the single character, ch.
157 extern "C" DynamicStrings_String
DynamicStrings_InitStringChar (char ch
);
160 Mark - marks String, s, ready for garbage collection.
163 extern "C" DynamicStrings_String
DynamicStrings_Mark (DynamicStrings_String s
);
166 Length - returns the length of the String, s.
169 extern "C" unsigned int DynamicStrings_Length (DynamicStrings_String s
);
172 ConCat - returns String, a, after the contents of, b, have been appended.
175 extern "C" DynamicStrings_String
DynamicStrings_ConCat (DynamicStrings_String a
, DynamicStrings_String b
);
178 ConCatChar - returns String, a, after character, ch, has been appended.
181 extern "C" DynamicStrings_String
DynamicStrings_ConCatChar (DynamicStrings_String a
, char ch
);
184 Assign - assigns the contents of, b, into, a.
185 String, a, is returned.
188 extern "C" DynamicStrings_String
DynamicStrings_Assign (DynamicStrings_String a
, DynamicStrings_String b
);
191 ReplaceChar - returns string s after it has changed all occurances of from to to.
194 extern "C" DynamicStrings_String
DynamicStrings_ReplaceChar (DynamicStrings_String s
, char from
, char to
);
197 Dup - duplicate a String, s, returning the copy of s.
200 extern "C" DynamicStrings_String
DynamicStrings_Dup (DynamicStrings_String s
);
203 Add - returns a new String which contains the contents of a and b.
206 extern "C" DynamicStrings_String
DynamicStrings_Add (DynamicStrings_String a
, DynamicStrings_String b
);
209 Equal - returns TRUE if String, a, and, b, are equal.
212 extern "C" bool DynamicStrings_Equal (DynamicStrings_String a
, DynamicStrings_String b
);
215 EqualCharStar - returns TRUE if contents of String, s, is the same as the
219 extern "C" bool DynamicStrings_EqualCharStar (DynamicStrings_String s
, void * a
);
222 EqualArray - returns TRUE if contents of String, s, is the same as the
226 extern "C" bool DynamicStrings_EqualArray (DynamicStrings_String s
, const char *a_
, unsigned int _a_high
);
229 Mult - returns a new string which is n concatenations of String, s.
232 extern "C" DynamicStrings_String
DynamicStrings_Mult (DynamicStrings_String s
, unsigned int n
);
235 Slice - returns a new string which contains the elements
238 strings start at element 0
239 Slice(s, 0, 2) will return elements 0, 1 but not 2
240 Slice(s, 1, 3) will return elements 1, 2 but not 3
241 Slice(s, 2, 0) will return elements 2..max
242 Slice(s, 3, -1) will return elements 3..max-1
243 Slice(s, 4, -2) will return elements 4..max-2
246 extern "C" DynamicStrings_String
DynamicStrings_Slice (DynamicStrings_String s
, int low
, int high
);
249 Index - returns the indice of the first occurance of, ch, in
250 String, s. -1 is returned if, ch, does not exist.
251 The search starts at position, o.
254 extern "C" int DynamicStrings_Index (DynamicStrings_String s
, char ch
, unsigned int o
);
257 RIndex - returns the indice of the last occurance of, ch,
258 in String, s. The search starts at position, o.
259 -1 is returned if, ch, is not found.
262 extern "C" int DynamicStrings_RIndex (DynamicStrings_String s
, char ch
, unsigned int o
);
265 RemoveComment - assuming that, comment, is a comment delimiter
266 which indicates anything to its right is a comment
267 then strip off the comment and also any white space
268 on the remaining right hand side.
269 It leaves any white space on the left hand side alone.
272 extern "C" DynamicStrings_String
DynamicStrings_RemoveComment (DynamicStrings_String s
, char comment
);
275 RemoveWhitePrefix - removes any leading white space from String, s.
276 A new string is returned.
279 extern "C" DynamicStrings_String
DynamicStrings_RemoveWhitePrefix (DynamicStrings_String s
);
282 RemoveWhitePostfix - removes any leading white space from String, s.
283 A new string is returned.
286 extern "C" DynamicStrings_String
DynamicStrings_RemoveWhitePostfix (DynamicStrings_String s
);
289 ToUpper - returns string, s, after it has had its lower case characters
290 replaced by upper case characters.
291 The string, s, is not duplicated.
294 extern "C" DynamicStrings_String
DynamicStrings_ToUpper (DynamicStrings_String s
);
297 ToLower - returns string, s, after it has had its upper case characters
298 replaced by lower case characters.
299 The string, s, is not duplicated.
302 extern "C" DynamicStrings_String
DynamicStrings_ToLower (DynamicStrings_String s
);
305 CopyOut - copies string, s, to a.
308 extern "C" void DynamicStrings_CopyOut (char *a
, unsigned int _a_high
, DynamicStrings_String s
);
311 char - returns the character, ch, at position, i, in String, s.
314 extern "C" char DynamicStrings_char (DynamicStrings_String s
, int i
);
317 string - returns the C style char * of String, s.
320 extern "C" void * DynamicStrings_string (DynamicStrings_String s
);
323 InitStringDB - the debug version of InitString.
326 extern "C" DynamicStrings_String
DynamicStrings_InitStringDB (const char *a_
, unsigned int _a_high
, const char *file_
, unsigned int _file_high
, unsigned int line
);
329 InitStringCharStarDB - the debug version of InitStringCharStar.
332 extern "C" DynamicStrings_String
DynamicStrings_InitStringCharStarDB (void * a
, const char *file_
, unsigned int _file_high
, unsigned int line
);
335 InitStringCharDB - the debug version of InitStringChar.
338 extern "C" DynamicStrings_String
DynamicStrings_InitStringCharDB (char ch
, const char *file_
, unsigned int _file_high
, unsigned int line
);
341 MultDB - the debug version of MultDB.
344 extern "C" DynamicStrings_String
DynamicStrings_MultDB (DynamicStrings_String s
, unsigned int n
, const char *file_
, unsigned int _file_high
, unsigned int line
);
347 DupDB - the debug version of Dup.
350 extern "C" DynamicStrings_String
DynamicStrings_DupDB (DynamicStrings_String s
, const char *file_
, unsigned int _file_high
, unsigned int line
);
353 SliceDB - debug version of Slice.
356 extern "C" DynamicStrings_String
DynamicStrings_SliceDB (DynamicStrings_String s
, int low
, int high
, const char *file_
, unsigned int _file_high
, unsigned int line
);
359 PushAllocation - pushes the current allocation/deallocation lists.
362 extern "C" void DynamicStrings_PushAllocation (void);
365 PopAllocation - test to see that all strings are deallocated since
366 the last push. Then it pops to the previous
367 allocation/deallocation lists.
369 If halt is true then the application terminates
370 with an exit code of 1.
373 extern "C" void DynamicStrings_PopAllocation (bool halt
);
376 PopAllocationExemption - test to see that all strings are deallocated, except
377 string e since the last push.
378 Post-condition: it pops to the previous allocation/deallocation
381 If halt is true then the application terminates
382 with an exit code of 1.
385 extern "C" DynamicStrings_String
DynamicStrings_PopAllocationExemption (bool halt
, DynamicStrings_String e
);
388 writeStringDesc write out debugging information about string, s. */
390 static void writeStringDesc (DynamicStrings_String s
);
396 static void writeNspace (unsigned int n
);
402 static void DumpStringInfo (DynamicStrings_String s
, unsigned int i
);
408 static void stop (void);
414 static void doDSdbEnter (void);
420 static void doDSdbExit (DynamicStrings_String s
);
426 static void DSdbEnter (void);
432 static void DSdbExit (DynamicStrings_String s
);
433 static unsigned int Capture (DynamicStrings_String s
);
439 static unsigned int Min (unsigned int a
, unsigned int b
);
445 static unsigned int Max (unsigned int a
, unsigned int b
);
448 writeString - writes a string to stdout.
451 static void writeString (const char *a_
, unsigned int _a_high
);
454 writeCstring - writes a C string to stdout.
457 static void writeCstring (void * a
);
463 static void writeCard (unsigned int c
);
469 static void writeLongcard (long unsigned int l
);
472 writeAddress - writes out the address of a with a C style hex prefix.
475 static void writeAddress (void * a
);
478 writeLn - writes a newline.
481 static void writeLn (void);
484 AssignDebug - assigns, file, and, line, information to string, s.
487 static DynamicStrings_String
AssignDebug (DynamicStrings_String s
, const char *file_
, unsigned int _file_high
, unsigned int line
, const char *proc_
, unsigned int _proc_high
);
490 IsOn - returns TRUE if, s, is on one of the debug lists.
493 static bool IsOn (DynamicStrings_String list
, DynamicStrings_String s
);
496 AddTo - adds string, s, to, list.
499 static void AddTo (DynamicStrings_String
*list
, DynamicStrings_String s
);
502 SubFrom - removes string, s, from, list.
505 static void SubFrom (DynamicStrings_String
*list
, DynamicStrings_String s
);
508 AddAllocated - adds string, s, to the head of the allocated list.
511 static void AddAllocated (DynamicStrings_String s
);
514 AddDeallocated - adds string, s, to the head of the deallocated list.
517 static void AddDeallocated (DynamicStrings_String s
);
520 IsOnAllocated - returns TRUE if the string, s, has ever been allocated.
523 static bool IsOnAllocated (DynamicStrings_String s
);
526 IsOnDeallocated - returns TRUE if the string, s, has ever been deallocated.
529 static bool IsOnDeallocated (DynamicStrings_String s
);
532 SubAllocated - removes string, s, from the list of allocated strings.
535 static void SubAllocated (DynamicStrings_String s
);
538 SubDeallocated - removes string, s, from the list of deallocated strings.
541 static void SubDeallocated (DynamicStrings_String s
);
544 SubDebugInfo - removes string, s, from the list of allocated strings.
547 static void SubDebugInfo (DynamicStrings_String s
);
550 AddDebugInfo - adds string, s, to the list of allocated strings.
553 static void AddDebugInfo (DynamicStrings_String s
);
556 ConcatContents - add the contents of string, a, where, h, is the
557 total length of, a. The offset is in, o.
560 static void ConcatContents (DynamicStrings_Contents
*c
, const char *a_
, unsigned int _a_high
, unsigned int h
, unsigned int o
);
563 DeallocateCharStar - deallocates any charStar.
566 static void DeallocateCharStar (DynamicStrings_String s
);
569 CheckPoisoned - checks for a poisoned string, s.
572 static DynamicStrings_String
CheckPoisoned (DynamicStrings_String s
);
575 MarkInvalid - marks the char * version of String, s, as invalid.
578 static void MarkInvalid (DynamicStrings_String s
);
581 ConcatContentsAddress - concatenate the string, a, where, h, is the
585 static void ConcatContentsAddress (DynamicStrings_Contents
*c
, void * a
, unsigned int h
);
588 AddToGarbage - adds String, b, onto the garbage list of, a. Providing
589 the state of b is marked. The state is then altered to
590 onlist. String, a, is returned.
593 static DynamicStrings_String
AddToGarbage (DynamicStrings_String a
, DynamicStrings_String b
);
596 IsOnGarbage - returns TRUE if, s, is on string, e, garbage list.
599 static bool IsOnGarbage (DynamicStrings_String e
, DynamicStrings_String s
);
602 IsWhite - returns TRUE if, ch, is a space or a tab.
605 static bool IsWhite (char ch
);
611 static void DumpState (DynamicStrings_String s
);
617 static void DumpStringSynopsis (DynamicStrings_String s
);
620 DumpString - displays the contents of string, s.
623 static void DumpString (DynamicStrings_String s
);
626 Init - initialize the module.
629 static void Init (void);
633 writeStringDesc write out debugging information about string, s. */
635 static void writeStringDesc (DynamicStrings_String s
)
637 writeCstring (s
->debug
.file
);
638 writeString ((const char *) ":", 1);
639 writeCard (s
->debug
.line
);
640 writeString ((const char *) ":", 1);
641 writeCstring (s
->debug
.proc
);
642 writeString ((const char *) " ", 1);
643 writeAddress (reinterpret_cast<void *> (s
));
644 writeString ((const char *) " ", 1);
645 switch (s
->head
->state
)
647 case DynamicStrings_inuse
:
648 writeString ((const char *) "still in use (", 14);
649 writeCard (s
->contents
.len
);
650 writeString ((const char *) ") characters", 12);
653 case DynamicStrings_marked
:
654 writeString ((const char *) "marked", 6);
657 case DynamicStrings_onlist
:
658 writeString ((const char *) "on a (lost) garbage list", 24);
661 case DynamicStrings_poisoned
:
662 writeString ((const char *) "poisoned", 8);
667 writeString ((const char *) "unknown state", 13);
677 static void writeNspace (unsigned int n
)
681 writeString ((const char *) " ", 1);
691 static void DumpStringInfo (DynamicStrings_String s
, unsigned int i
)
698 if (s
->head
->garbage
!= NULL
)
701 writeString ((const char *) "garbage list:", 13);
704 s
= s
->head
->garbage
;
705 DumpStringInfo (s
, i
+1);
707 } while (! (s
== NULL
));
717 static void stop (void)
726 static void doDSdbEnter (void)
730 DynamicStrings_PushAllocation ();
739 static void doDSdbExit (DynamicStrings_String s
)
743 s
= DynamicStrings_PopAllocationExemption (true, s
);
752 static void DSdbEnter (void)
761 static void DSdbExit (DynamicStrings_String s
)
765 static unsigned int Capture (DynamicStrings_String s
)
768 * #undef GM2_DEBUG_DYNAMICSTINGS
769 * #if defined(GM2_DEBUG_DYNAMICSTINGS)
770 * # define DSdbEnter doDSdbEnter
771 * # define DSdbExit doDSdbExit
772 * # define CheckOn TRUE
773 * # define TraceOn TRUE
778 /* static analysis guarentees a RETURN statement will be used before here. */
779 __builtin_unreachable ();
787 static unsigned int Min (unsigned int a
, unsigned int b
)
797 /* static analysis guarentees a RETURN statement will be used before here. */
798 __builtin_unreachable ();
806 static unsigned int Max (unsigned int a
, unsigned int b
)
816 /* static analysis guarentees a RETURN statement will be used before here. */
817 __builtin_unreachable ();
822 writeString - writes a string to stdout.
825 static void writeString (const char *a_
, unsigned int _a_high
)
830 /* make a local copy of each unbounded array. */
831 memcpy (a
, a_
, _a_high
+1);
833 i
= static_cast<int> (libc_write (1, &a
, static_cast<size_t> (StrLib_StrLen ((const char *) a
, _a_high
))));
838 writeCstring - writes a C string to stdout.
841 static void writeCstring (void * a
)
847 writeString ((const char *) "(null)", 6);
851 i
= static_cast<int> (libc_write (1, a
, libc_strlen (a
)));
860 static void writeCard (unsigned int c
)
872 ch
= ((char) ( ((unsigned int) ('0'))+c
));
873 i
= static_cast<int> (libc_write (1, &ch
, static_cast<size_t> (1)));
882 static void writeLongcard (long unsigned int l
)
889 writeLongcard (l
/ 16);
890 writeLongcard (l
% 16);
894 /* avoid dangling else. */
895 ch
= ((char) ( ((unsigned int) ('0'))+((unsigned int ) (l
))));
896 i
= static_cast<int> (libc_write (1, &ch
, static_cast<size_t> (1)));
900 /* avoid dangling else. */
901 ch
= ((char) (( ((unsigned int) ('a'))+((unsigned int ) (l
)))-10));
902 i
= static_cast<int> (libc_write (1, &ch
, static_cast<size_t> (1)));
908 writeAddress - writes out the address of a with a C style hex prefix.
911 static void writeAddress (void * a
)
913 typedef struct writeAddress__T4_a writeAddress__T4
;
915 struct writeAddress__T4_a
{ char array
[30+1]; };
916 writeAddress__T4 buffer
;
918 libc_snprintf (&buffer
, static_cast<size_t> (sizeof (buffer
)), (const char *) "0x%", 3, a
);
919 writeString ((const char *) &buffer
.array
[0], 30);
924 writeLn - writes a newline.
927 static void writeLn (void)
933 i
= static_cast<int> (libc_write (1, &ch
, static_cast<size_t> (1)));
938 AssignDebug - assigns, file, and, line, information to string, s.
941 static DynamicStrings_String
AssignDebug (DynamicStrings_String s
, const char *file_
, unsigned int _file_high
, unsigned int line
, const char *proc_
, unsigned int _proc_high
)
945 char file
[_file_high
+1];
946 char proc
[_proc_high
+1];
948 /* make a local copy of each unbounded array. */
949 memcpy (file
, file_
, _file_high
+1);
950 memcpy (proc
, proc_
, _proc_high
+1);
954 Storage_ALLOCATE (&s
->debug
.file
, (StrLib_StrLen ((const char *) file
, _file_high
))+1);
955 if ((libc_strncpy (s
->debug
.file
, f
, (StrLib_StrLen ((const char *) file
, _file_high
))+1)) == NULL
)
957 s
->debug
.line
= line
;
958 Storage_ALLOCATE (&s
->debug
.proc
, (StrLib_StrLen ((const char *) proc
, _proc_high
))+1);
959 if ((libc_strncpy (s
->debug
.proc
, p
, (StrLib_StrLen ((const char *) proc
, _proc_high
))+1)) == NULL
)
962 /* static analysis guarentees a RETURN statement will be used before here. */
963 __builtin_unreachable ();
968 IsOn - returns TRUE if, s, is on one of the debug lists.
971 static bool IsOn (DynamicStrings_String list
, DynamicStrings_String s
)
973 while ((list
!= s
) && (list
!= NULL
))
975 list
= list
->debug
.next
;
978 /* static analysis guarentees a RETURN statement will be used before here. */
979 __builtin_unreachable ();
984 AddTo - adds string, s, to, list.
987 static void AddTo (DynamicStrings_String
*list
, DynamicStrings_String s
)
992 s
->debug
.next
= NULL
;
996 s
->debug
.next
= (*list
);
1003 SubFrom - removes string, s, from, list.
1006 static void SubFrom (DynamicStrings_String
*list
, DynamicStrings_String s
)
1008 DynamicStrings_String p
;
1012 (*list
) = s
->debug
.next
;
1017 while ((p
->debug
.next
!= NULL
) && (p
->debug
.next
!= s
))
1021 if (p
->debug
.next
== s
)
1023 p
->debug
.next
= s
->debug
.next
;
1027 /* not found, quit */
1031 s
->debug
.next
= NULL
;
1036 AddAllocated - adds string, s, to the head of the allocated list.
1039 static void AddAllocated (DynamicStrings_String s
)
1042 AddTo (&frameHead
->alloc
, s
);
1047 AddDeallocated - adds string, s, to the head of the deallocated list.
1050 static void AddDeallocated (DynamicStrings_String s
)
1053 AddTo (&frameHead
->dealloc
, s
);
1058 IsOnAllocated - returns TRUE if the string, s, has ever been allocated.
1061 static bool IsOnAllocated (DynamicStrings_String s
)
1063 DynamicStrings_frame f
;
1068 if (IsOn (f
->alloc
, s
))
1076 } while (! (f
== NULL
));
1078 /* static analysis guarentees a RETURN statement will be used before here. */
1079 __builtin_unreachable ();
1084 IsOnDeallocated - returns TRUE if the string, s, has ever been deallocated.
1087 static bool IsOnDeallocated (DynamicStrings_String s
)
1089 DynamicStrings_frame f
;
1094 if (IsOn (f
->dealloc
, s
))
1102 } while (! (f
== NULL
));
1104 /* static analysis guarentees a RETURN statement will be used before here. */
1105 __builtin_unreachable ();
1110 SubAllocated - removes string, s, from the list of allocated strings.
1113 static void SubAllocated (DynamicStrings_String s
)
1115 DynamicStrings_frame f
;
1120 if (IsOn (f
->alloc
, s
))
1122 SubFrom (&f
->alloc
, s
);
1129 } while (! (f
== NULL
));
1134 SubDeallocated - removes string, s, from the list of deallocated strings.
1137 static void SubDeallocated (DynamicStrings_String s
)
1139 DynamicStrings_frame f
;
1144 if (IsOn (f
->dealloc
, s
))
1146 SubFrom (&f
->dealloc
, s
);
1153 } while (! (f
== NULL
));
1158 SubDebugInfo - removes string, s, from the list of allocated strings.
1161 static void SubDebugInfo (DynamicStrings_String s
)
1163 if (IsOnDeallocated (s
))
1165 Assertion_Assert (! DebugOn
);
1166 /* string has already been deallocated */
1169 if (IsOnAllocated (s
))
1176 /* string has not been allocated */
1177 Assertion_Assert (! DebugOn
);
1183 AddDebugInfo - adds string, s, to the list of allocated strings.
1186 static void AddDebugInfo (DynamicStrings_String s
)
1188 s
->debug
.next
= NULL
;
1189 s
->debug
.file
= NULL
;
1191 s
->debug
.proc
= NULL
;
1200 ConcatContents - add the contents of string, a, where, h, is the
1201 total length of, a. The offset is in, o.
1204 static void ConcatContents (DynamicStrings_Contents
*c
, const char *a_
, unsigned int _a_high
, unsigned int h
, unsigned int o
)
1209 /* make a local copy of each unbounded array. */
1210 memcpy (a
, a_
, _a_high
+1);
1213 while ((o
< h
) && (i
< MaxBuf
))
1215 (*c
).buf
.array
[i
] = a
[o
];
1222 Storage_ALLOCATE ((void **) &(*c
).next
, sizeof (DynamicStrings_stringRecord
));
1223 (*c
).next
->head
= NULL
;
1224 (*c
).next
->contents
.len
= 0;
1225 (*c
).next
->contents
.next
= NULL
;
1226 ConcatContents (&(*c
).next
->contents
, (const char *) a
, _a_high
, h
, o
);
1227 AddDebugInfo ((*c
).next
);
1228 (*c
).next
= AssignDebug ((*c
).next
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 722, (const char *) "ConcatContents", 14);
1238 DeallocateCharStar - deallocates any charStar.
1241 static void DeallocateCharStar (DynamicStrings_String s
)
1243 if ((s
!= NULL
) && (s
->head
!= NULL
))
1245 if (s
->head
->charStarUsed
&& (s
->head
->charStar
!= NULL
))
1247 Storage_DEALLOCATE (&s
->head
->charStar
, s
->head
->charStarSize
);
1249 s
->head
->charStarUsed
= false;
1250 s
->head
->charStar
= NULL
;
1251 s
->head
->charStarSize
= 0;
1252 s
->head
->charStarValid
= false;
1258 CheckPoisoned - checks for a poisoned string, s.
1261 static DynamicStrings_String
CheckPoisoned (DynamicStrings_String s
)
1263 if (((PoisonOn
&& (s
!= NULL
)) && (s
->head
!= NULL
)) && (s
->head
->state
== DynamicStrings_poisoned
))
1266 __builtin_unreachable ();
1269 /* static analysis guarentees a RETURN statement will be used before here. */
1270 __builtin_unreachable ();
1275 MarkInvalid - marks the char * version of String, s, as invalid.
1278 static void MarkInvalid (DynamicStrings_String s
)
1282 s
= CheckPoisoned (s
);
1284 if (s
->head
!= NULL
)
1286 s
->head
->charStarValid
= false;
1292 ConcatContentsAddress - concatenate the string, a, where, h, is the
1296 static void ConcatContentsAddress (DynamicStrings_Contents
*c
, void * a
, unsigned int h
)
1298 typedef char *ConcatContentsAddress__T1
;
1300 ConcatContentsAddress__T1 p
;
1306 p
= static_cast<ConcatContentsAddress__T1
> (a
);
1307 while ((j
< h
) && (i
< MaxBuf
))
1309 (*c
).buf
.array
[i
] = (*p
);
1316 /* avoid dangling else. */
1318 Storage_ALLOCATE ((void **) &(*c
).next
, sizeof (DynamicStrings_stringRecord
));
1319 (*c
).next
->head
= NULL
;
1320 (*c
).next
->contents
.len
= 0;
1321 (*c
).next
->contents
.next
= NULL
;
1322 ConcatContentsAddress (&(*c
).next
->contents
, reinterpret_cast<void *> (p
), h
-j
);
1323 AddDebugInfo ((*c
).next
);
1326 (*c
).next
= AssignDebug ((*c
).next
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 917, (const char *) "ConcatContentsAddress", 21);
1338 AddToGarbage - adds String, b, onto the garbage list of, a. Providing
1339 the state of b is marked. The state is then altered to
1340 onlist. String, a, is returned.
1343 static DynamicStrings_String
AddToGarbage (DynamicStrings_String a
, DynamicStrings_String b
)
1345 DynamicStrings_String c
;
1349 a
= CheckPoisoned (a
);
1350 b
= CheckPoisoned (b
);
1353 IF (a#NIL) AND (a#b) AND (a^.head^.state=marked)
1355 writeString('warning trying to add to a marked string') ; writeLn
1358 if (((((a
!= b
) && (a
!= NULL
)) && (b
!= NULL
)) && (b
->head
->state
== DynamicStrings_marked
)) && (a
->head
->state
== DynamicStrings_inuse
))
1361 while (c
->head
->garbage
!= NULL
)
1363 c
= c
->head
->garbage
;
1365 c
->head
->garbage
= b
;
1366 b
->head
->state
= DynamicStrings_onlist
;
1373 /* static analysis guarentees a RETURN statement will be used before here. */
1374 __builtin_unreachable ();
1379 IsOnGarbage - returns TRUE if, s, is on string, e, garbage list.
1382 static bool IsOnGarbage (DynamicStrings_String e
, DynamicStrings_String s
)
1384 if ((e
!= NULL
) && (s
!= NULL
))
1386 while (e
->head
->garbage
!= NULL
)
1388 if (e
->head
->garbage
== s
)
1394 e
= e
->head
->garbage
;
1399 /* static analysis guarentees a RETURN statement will be used before here. */
1400 __builtin_unreachable ();
1405 IsWhite - returns TRUE if, ch, is a space or a tab.
1408 static bool IsWhite (char ch
)
1410 return (ch
== ' ') || (ch
== ASCII_tab
);
1411 /* static analysis guarentees a RETURN statement will be used before here. */
1412 __builtin_unreachable ();
1420 static void DumpState (DynamicStrings_String s
)
1422 switch (s
->head
->state
)
1424 case DynamicStrings_inuse
:
1425 writeString ((const char *) "still in use (", 14);
1426 writeCard (s
->contents
.len
);
1427 writeString ((const char *) ") characters", 12);
1430 case DynamicStrings_marked
:
1431 writeString ((const char *) "marked", 6);
1434 case DynamicStrings_onlist
:
1435 writeString ((const char *) "on a garbage list", 17);
1438 case DynamicStrings_poisoned
:
1439 writeString ((const char *) "poisoned", 8);
1444 writeString ((const char *) "unknown state", 13);
1451 DumpStringSynopsis -
1454 static void DumpStringSynopsis (DynamicStrings_String s
)
1456 writeCstring (s
->debug
.file
);
1457 writeString ((const char *) ":", 1);
1458 writeCard (s
->debug
.line
);
1459 writeString ((const char *) ":", 1);
1460 writeCstring (s
->debug
.proc
);
1461 writeString ((const char *) " string ", 8);
1462 writeAddress (reinterpret_cast<void *> (s
));
1463 writeString ((const char *) " ", 1);
1465 if (IsOnAllocated (s
))
1467 writeString ((const char *) " globally allocated", 19);
1469 else if (IsOnDeallocated (s
))
1471 /* avoid dangling else. */
1472 writeString ((const char *) " globally deallocated", 21);
1476 /* avoid dangling else. */
1477 writeString ((const char *) " globally unknown", 17);
1484 DumpString - displays the contents of string, s.
1487 static void DumpString (DynamicStrings_String s
)
1489 DynamicStrings_String t
;
1493 DumpStringSynopsis (s
);
1494 if ((s
->head
!= NULL
) && (s
->head
->garbage
!= NULL
))
1496 writeString ((const char *) "display chained strings on the garbage list", 43);
1498 t
= s
->head
->garbage
;
1501 DumpStringSynopsis (t
);
1502 t
= t
->head
->garbage
;
1510 Init - initialize the module.
1513 static void Init (void)
1519 DynamicStrings_PushAllocation ();
1525 InitString - creates and returns a String type object.
1526 Initial contents are, a.
1529 extern "C" DynamicStrings_String
DynamicStrings_InitString (const char *a_
, unsigned int _a_high
)
1531 DynamicStrings_String s
;
1534 /* make a local copy of each unbounded array. */
1535 memcpy (a
, a_
, _a_high
+1);
1537 Storage_ALLOCATE ((void **) &s
, sizeof (DynamicStrings_stringRecord
));
1538 s
->contents
.len
= 0;
1539 s
->contents
.next
= NULL
;
1540 ConcatContents (&s
->contents
, (const char *) a
, _a_high
, StrLib_StrLen ((const char *) a
, _a_high
), 0);
1541 Storage_ALLOCATE ((void **) &s
->head
, sizeof (DynamicStrings_descriptor
));
1542 s
->head
->charStarUsed
= false;
1543 s
->head
->charStar
= NULL
;
1544 s
->head
->charStarSize
= 0;
1545 s
->head
->charStarValid
= false;
1546 s
->head
->garbage
= NULL
;
1547 s
->head
->state
= DynamicStrings_inuse
;
1551 s
= AssignDebug (s
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 758, (const char *) "InitString", 10);
1554 /* static analysis guarentees a RETURN statement will be used before here. */
1555 __builtin_unreachable ();
1560 KillString - frees String, s, and its contents.
1564 extern "C" DynamicStrings_String
DynamicStrings_KillString (DynamicStrings_String s
)
1566 DynamicStrings_String t
;
1570 s
= CheckPoisoned (s
);
1576 /* avoid gcc warning by using compound statement even if not strictly necessary. */
1577 if (IsOnAllocated (s
))
1581 else if (IsOnDeallocated (s
))
1583 /* avoid dangling else. */
1587 if (s
->head
!= NULL
)
1589 s
->head
->state
= DynamicStrings_poisoned
;
1590 s
->head
->garbage
= DynamicStrings_KillString (s
->head
->garbage
);
1593 DeallocateCharStar (s
);
1597 Storage_DEALLOCATE ((void **) &s
->head
, sizeof (DynamicStrings_descriptor
));
1601 t
= DynamicStrings_KillString (s
->contents
.next
);
1604 Storage_DEALLOCATE ((void **) &s
, sizeof (DynamicStrings_stringRecord
));
1608 /* static analysis guarentees a RETURN statement will be used before here. */
1609 __builtin_unreachable ();
1614 Fin - finishes with a string, it calls KillString with, s.
1615 The purpose of the procedure is to provide a short cut
1616 to calling KillString and then testing the return result.
1619 extern "C" void DynamicStrings_Fin (DynamicStrings_String s
)
1621 if ((DynamicStrings_KillString (s
)) != NULL
)
1624 __builtin_unreachable ();
1630 InitStringCharStar - initializes and returns a String to contain the C string.
1633 extern "C" DynamicStrings_String
DynamicStrings_InitStringCharStar (void * a
)
1635 DynamicStrings_String s
;
1637 Storage_ALLOCATE ((void **) &s
, sizeof (DynamicStrings_stringRecord
));
1638 s
->contents
.len
= 0;
1639 s
->contents
.next
= NULL
;
1642 ConcatContentsAddress (&s
->contents
, a
, static_cast<unsigned int> (libc_strlen (a
)));
1644 Storage_ALLOCATE ((void **) &s
->head
, sizeof (DynamicStrings_descriptor
));
1645 s
->head
->charStarUsed
= false;
1646 s
->head
->charStar
= NULL
;
1647 s
->head
->charStarSize
= 0;
1648 s
->head
->charStarValid
= false;
1649 s
->head
->garbage
= NULL
;
1650 s
->head
->state
= DynamicStrings_inuse
;
1654 s
= AssignDebug (s
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 957, (const char *) "InitStringCharStar", 18);
1657 /* static analysis guarentees a RETURN statement will be used before here. */
1658 __builtin_unreachable ();
1663 InitStringChar - initializes and returns a String to contain the single character, ch.
1666 extern "C" DynamicStrings_String
DynamicStrings_InitStringChar (char ch
)
1668 typedef struct InitStringChar__T5_a InitStringChar__T5
;
1670 struct InitStringChar__T5_a
{ char array
[1+1]; };
1671 InitStringChar__T5 a
;
1672 DynamicStrings_String s
;
1675 a
.array
[1] = ASCII_nul
;
1676 s
= DynamicStrings_InitString ((const char *) &a
.array
[0], 1);
1679 s
= AssignDebug (s
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 977, (const char *) "InitStringChar", 14);
1682 /* static analysis guarentees a RETURN statement will be used before here. */
1683 __builtin_unreachable ();
1688 Mark - marks String, s, ready for garbage collection.
1691 extern "C" DynamicStrings_String
DynamicStrings_Mark (DynamicStrings_String s
)
1695 s
= CheckPoisoned (s
);
1697 if ((s
!= NULL
) && (s
->head
->state
== DynamicStrings_inuse
))
1699 s
->head
->state
= DynamicStrings_marked
;
1702 /* static analysis guarentees a RETURN statement will be used before here. */
1703 __builtin_unreachable ();
1708 Length - returns the length of the String, s.
1711 extern "C" unsigned int DynamicStrings_Length (DynamicStrings_String s
)
1719 return s
->contents
.len
+(DynamicStrings_Length (s
->contents
.next
));
1721 /* static analysis guarentees a RETURN statement will be used before here. */
1722 __builtin_unreachable ();
1727 ConCat - returns String, a, after the contents of, b, have been appended.
1730 extern "C" DynamicStrings_String
DynamicStrings_ConCat (DynamicStrings_String a
, DynamicStrings_String b
)
1732 DynamicStrings_String t
;
1736 a
= CheckPoisoned (a
);
1737 b
= CheckPoisoned (b
);
1741 return DynamicStrings_ConCat (a
, DynamicStrings_Mark (DynamicStrings_Dup (b
)));
1745 /* avoid dangling else. */
1746 a
= AddToGarbage (a
, b
);
1751 while ((t
->contents
.len
== MaxBuf
) && (t
->contents
.next
!= NULL
))
1753 t
= t
->contents
.next
;
1755 ConcatContents (&t
->contents
, (const char *) &b
->contents
.buf
.array
[0], (MaxBuf
-1), b
->contents
.len
, 0);
1756 b
= b
->contents
.next
;
1759 if ((a
== NULL
) && (b
!= NULL
))
1762 __builtin_unreachable ();
1765 /* static analysis guarentees a RETURN statement will be used before here. */
1766 __builtin_unreachable ();
1771 ConCatChar - returns String, a, after character, ch, has been appended.
1774 extern "C" DynamicStrings_String
DynamicStrings_ConCatChar (DynamicStrings_String a
, char ch
)
1776 typedef struct ConCatChar__T6_a ConCatChar__T6
;
1778 struct ConCatChar__T6_a
{ char array
[1+1]; };
1780 DynamicStrings_String t
;
1784 a
= CheckPoisoned (a
);
1787 b
.array
[1] = ASCII_nul
;
1790 while ((t
->contents
.len
== MaxBuf
) && (t
->contents
.next
!= NULL
))
1792 t
= t
->contents
.next
;
1794 ConcatContents (&t
->contents
, (const char *) &b
.array
[0], 1, 1, 0);
1796 /* static analysis guarentees a RETURN statement will be used before here. */
1797 __builtin_unreachable ();
1802 Assign - assigns the contents of, b, into, a.
1803 String, a, is returned.
1806 extern "C" DynamicStrings_String
DynamicStrings_Assign (DynamicStrings_String a
, DynamicStrings_String b
)
1810 a
= CheckPoisoned (a
);
1811 b
= CheckPoisoned (b
);
1813 if ((a
!= NULL
) && (b
!= NULL
))
1815 a
->contents
.next
= DynamicStrings_KillString (a
->contents
.next
);
1816 a
->contents
.len
= 0;
1818 return DynamicStrings_ConCat (a
, b
);
1819 /* static analysis guarentees a RETURN statement will be used before here. */
1820 __builtin_unreachable ();
1825 ReplaceChar - returns string s after it has changed all occurances of from to to.
1828 extern "C" DynamicStrings_String
DynamicStrings_ReplaceChar (DynamicStrings_String s
, char from
, char to
)
1830 DynamicStrings_String t
;
1837 while (i
< t
->contents
.len
)
1839 if (t
->contents
.buf
.array
[i
] == from
)
1841 t
->contents
.buf
.array
[i
] = to
;
1845 t
= t
->contents
.next
;
1848 /* static analysis guarentees a RETURN statement will be used before here. */
1849 __builtin_unreachable ();
1854 Dup - duplicate a String, s, returning the copy of s.
1857 extern "C" DynamicStrings_String
DynamicStrings_Dup (DynamicStrings_String s
)
1861 s
= CheckPoisoned (s
);
1863 s
= DynamicStrings_Assign (DynamicStrings_InitString ((const char *) "", 0), s
);
1866 s
= AssignDebug (s
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 1198, (const char *) "Dup", 3);
1869 /* static analysis guarentees a RETURN statement will be used before here. */
1870 __builtin_unreachable ();
1875 Add - returns a new String which contains the contents of a and b.
1878 extern "C" DynamicStrings_String
DynamicStrings_Add (DynamicStrings_String a
, DynamicStrings_String b
)
1882 a
= CheckPoisoned (a
);
1883 b
= CheckPoisoned (b
);
1885 a
= DynamicStrings_ConCat (DynamicStrings_ConCat (DynamicStrings_InitString ((const char *) "", 0), a
), b
);
1888 a
= AssignDebug (a
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 1218, (const char *) "Add", 3);
1891 /* static analysis guarentees a RETURN statement will be used before here. */
1892 __builtin_unreachable ();
1897 Equal - returns TRUE if String, a, and, b, are equal.
1900 extern "C" bool DynamicStrings_Equal (DynamicStrings_String a
, DynamicStrings_String b
)
1906 a
= CheckPoisoned (a
);
1907 b
= CheckPoisoned (b
);
1909 if ((DynamicStrings_Length (a
)) == (DynamicStrings_Length (b
)))
1911 while ((a
!= NULL
) && (b
!= NULL
))
1914 Assertion_Assert (a
->contents
.len
== b
->contents
.len
);
1915 while (i
< a
->contents
.len
)
1917 if (a
->contents
.buf
.array
[i
] != b
->contents
.buf
.array
[i
])
1923 a
= a
->contents
.next
;
1924 b
= b
->contents
.next
;
1932 /* static analysis guarentees a RETURN statement will be used before here. */
1933 __builtin_unreachable ();
1938 EqualCharStar - returns TRUE if contents of String, s, is the same as the
1942 extern "C" bool DynamicStrings_EqualCharStar (DynamicStrings_String s
, void * a
)
1944 DynamicStrings_String t
;
1948 s
= CheckPoisoned (s
);
1950 t
= DynamicStrings_InitStringCharStar (a
);
1953 t
= AssignDebug (t
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 1275, (const char *) "EqualCharStar", 13);
1955 t
= AddToGarbage (t
, s
);
1956 if (DynamicStrings_Equal (t
, s
))
1958 t
= DynamicStrings_KillString (t
);
1963 t
= DynamicStrings_KillString (t
);
1966 /* static analysis guarentees a RETURN statement will be used before here. */
1967 __builtin_unreachable ();
1972 EqualArray - returns TRUE if contents of String, s, is the same as the
1976 extern "C" bool DynamicStrings_EqualArray (DynamicStrings_String s
, const char *a_
, unsigned int _a_high
)
1978 DynamicStrings_String t
;
1981 /* make a local copy of each unbounded array. */
1982 memcpy (a
, a_
, _a_high
+1);
1986 s
= CheckPoisoned (s
);
1988 t
= DynamicStrings_InitString ((const char *) a
, _a_high
);
1991 t
= AssignDebug (t
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 1305, (const char *) "EqualArray", 10);
1993 t
= AddToGarbage (t
, s
);
1994 if (DynamicStrings_Equal (t
, s
))
1996 t
= DynamicStrings_KillString (t
);
2001 t
= DynamicStrings_KillString (t
);
2004 /* static analysis guarentees a RETURN statement will be used before here. */
2005 __builtin_unreachable ();
2010 Mult - returns a new string which is n concatenations of String, s.
2013 extern "C" DynamicStrings_String
DynamicStrings_Mult (DynamicStrings_String s
, unsigned int n
)
2017 s
= CheckPoisoned (s
);
2021 s
= AddToGarbage (DynamicStrings_InitString ((const char *) "", 0), s
);
2025 s
= DynamicStrings_ConCat (DynamicStrings_Mult (s
, n
-1), s
);
2029 s
= AssignDebug (s
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 1337, (const char *) "Mult", 4);
2032 /* static analysis guarentees a RETURN statement will be used before here. */
2033 __builtin_unreachable ();
2038 Slice - returns a new string which contains the elements
2041 strings start at element 0
2042 Slice(s, 0, 2) will return elements 0, 1 but not 2
2043 Slice(s, 1, 3) will return elements 1, 2 but not 3
2044 Slice(s, 2, 0) will return elements 2..max
2045 Slice(s, 3, -1) will return elements 3..max-1
2046 Slice(s, 4, -2) will return elements 4..max-2
2049 extern "C" DynamicStrings_String
DynamicStrings_Slice (DynamicStrings_String s
, int low
, int high
)
2051 DynamicStrings_String d
;
2052 DynamicStrings_String t
;
2059 s
= CheckPoisoned (s
);
2063 low
= ((int ) (DynamicStrings_Length (s
)))+low
;
2067 high
= ((int ) (DynamicStrings_Length (s
)))+high
;
2071 /* make sure high is <= Length (s) */
2072 high
= Min (DynamicStrings_Length (s
), static_cast<unsigned int> (high
));
2074 d
= DynamicStrings_InitString ((const char *) "", 0);
2075 d
= AddToGarbage (d
, s
);
2080 if (low
< (o
+((int ) (s
->contents
.len
))))
2088 /* found sliceable unit */
2097 end
= Max (Min (MaxBuf
, static_cast<unsigned int> (high
-o
)), 0);
2098 while (t
->contents
.len
== MaxBuf
)
2100 if (t
->contents
.next
== NULL
)
2102 Storage_ALLOCATE ((void **) &t
->contents
.next
, sizeof (DynamicStrings_stringRecord
));
2103 t
->contents
.next
->head
= NULL
;
2104 t
->contents
.next
->contents
.len
= 0;
2105 AddDebugInfo (t
->contents
.next
);
2108 t
->contents
.next
= AssignDebug (t
->contents
.next
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 1405, (const char *) "Slice", 5);
2111 t
= t
->contents
.next
;
2113 ConcatContentsAddress (&t
->contents
, &s
->contents
.buf
.array
[start
], static_cast<unsigned int> (end
-start
));
2114 o
+= s
->contents
.len
;
2115 s
= s
->contents
.next
;
2120 o
+= s
->contents
.len
;
2121 s
= s
->contents
.next
;
2126 d
= AssignDebug (d
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 1422, (const char *) "Slice", 5);
2129 /* static analysis guarentees a RETURN statement will be used before here. */
2130 __builtin_unreachable ();
2135 Index - returns the indice of the first occurance of, ch, in
2136 String, s. -1 is returned if, ch, does not exist.
2137 The search starts at position, o.
2140 extern "C" int DynamicStrings_Index (DynamicStrings_String s
, char ch
, unsigned int o
)
2147 s
= CheckPoisoned (s
);
2152 if ((k
+s
->contents
.len
) < o
)
2154 k
+= s
->contents
.len
;
2159 while (i
< s
->contents
.len
)
2161 if (s
->contents
.buf
.array
[i
] == ch
)
2170 s
= s
->contents
.next
;
2173 /* static analysis guarentees a RETURN statement will be used before here. */
2174 __builtin_unreachable ();
2179 RIndex - returns the indice of the last occurance of, ch,
2180 in String, s. The search starts at position, o.
2181 -1 is returned if, ch, is not found.
2184 extern "C" int DynamicStrings_RIndex (DynamicStrings_String s
, char ch
, unsigned int o
)
2192 s
= CheckPoisoned (s
);
2198 if ((k
+s
->contents
.len
) < o
)
2200 k
+= s
->contents
.len
;
2212 while (i
< s
->contents
.len
)
2214 if (s
->contents
.buf
.array
[i
] == ch
)
2222 s
= s
->contents
.next
;
2225 /* static analysis guarentees a RETURN statement will be used before here. */
2226 __builtin_unreachable ();
2231 RemoveComment - assuming that, comment, is a comment delimiter
2232 which indicates anything to its right is a comment
2233 then strip off the comment and also any white space
2234 on the remaining right hand side.
2235 It leaves any white space on the left hand side alone.
2238 extern "C" DynamicStrings_String
DynamicStrings_RemoveComment (DynamicStrings_String s
, char comment
)
2242 i
= DynamicStrings_Index (s
, comment
, 0);
2245 s
= DynamicStrings_InitString ((const char *) "", 0);
2249 /* avoid dangling else. */
2250 s
= DynamicStrings_RemoveWhitePostfix (DynamicStrings_Slice (DynamicStrings_Mark (s
), 0, i
));
2254 s
= AssignDebug (s
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 1534, (const char *) "RemoveComment", 13);
2257 /* static analysis guarentees a RETURN statement will be used before here. */
2258 __builtin_unreachable ();
2263 RemoveWhitePrefix - removes any leading white space from String, s.
2264 A new string is returned.
2267 extern "C" DynamicStrings_String
DynamicStrings_RemoveWhitePrefix (DynamicStrings_String s
)
2272 while (IsWhite (DynamicStrings_char (s
, static_cast<int> (i
))))
2276 s
= DynamicStrings_Slice (s
, (int ) (i
), 0);
2279 s
= AssignDebug (s
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 1646, (const char *) "RemoveWhitePrefix", 17);
2282 /* static analysis guarentees a RETURN statement will be used before here. */
2283 __builtin_unreachable ();
2288 RemoveWhitePostfix - removes any leading white space from String, s.
2289 A new string is returned.
2292 extern "C" DynamicStrings_String
DynamicStrings_RemoveWhitePostfix (DynamicStrings_String s
)
2296 i
= ((int ) (DynamicStrings_Length (s
)))-1;
2297 while ((i
>= 0) && (IsWhite (DynamicStrings_char (s
, i
))))
2301 s
= DynamicStrings_Slice (s
, 0, i
+1);
2304 s
= AssignDebug (s
, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, 1668, (const char *) "RemoveWhitePostfix", 18);
2307 /* static analysis guarentees a RETURN statement will be used before here. */
2308 __builtin_unreachable ();
2313 ToUpper - returns string, s, after it has had its lower case characters
2314 replaced by upper case characters.
2315 The string, s, is not duplicated.
2318 extern "C" DynamicStrings_String
DynamicStrings_ToUpper (DynamicStrings_String s
)
2322 DynamicStrings_String t
;
2331 while (i
< t
->contents
.len
)
2333 ch
= t
->contents
.buf
.array
[i
];
2334 if ((ch
>= 'a') && (ch
<= 'z'))
2336 t
->contents
.buf
.array
[i
] = ((char) (( ((unsigned int) (ch
))- ((unsigned int) ('a')))+ ((unsigned int) ('A'))));
2340 t
= t
->contents
.next
;
2344 /* static analysis guarentees a RETURN statement will be used before here. */
2345 __builtin_unreachable ();
2350 ToLower - returns string, s, after it has had its upper case characters
2351 replaced by lower case characters.
2352 The string, s, is not duplicated.
2355 extern "C" DynamicStrings_String
DynamicStrings_ToLower (DynamicStrings_String s
)
2359 DynamicStrings_String t
;
2368 while (i
< t
->contents
.len
)
2370 ch
= t
->contents
.buf
.array
[i
];
2371 if ((ch
>= 'A') && (ch
<= 'Z'))
2373 t
->contents
.buf
.array
[i
] = ((char) (( ((unsigned int) (ch
))- ((unsigned int) ('A')))+ ((unsigned int) ('a'))));
2377 t
= t
->contents
.next
;
2381 /* static analysis guarentees a RETURN statement will be used before here. */
2382 __builtin_unreachable ();
2387 CopyOut - copies string, s, to a.
2390 extern "C" void DynamicStrings_CopyOut (char *a
, unsigned int _a_high
, DynamicStrings_String s
)
2395 l
= Min (_a_high
+1, DynamicStrings_Length (s
));
2399 a
[i
] = DynamicStrings_char (s
, static_cast<int> (i
));
2410 char - returns the character, ch, at position, i, in String, s.
2413 extern "C" char DynamicStrings_char (DynamicStrings_String s
, int i
)
2419 s
= CheckPoisoned (s
);
2423 c
= (unsigned int ) (((int ) (DynamicStrings_Length (s
)))+i
);
2429 while ((s
!= NULL
) && (c
>= s
->contents
.len
))
2431 c
-= s
->contents
.len
;
2432 s
= s
->contents
.next
;
2434 if ((s
== NULL
) || (c
>= s
->contents
.len
))
2440 return s
->contents
.buf
.array
[c
];
2442 /* static analysis guarentees a RETURN statement will be used before here. */
2443 __builtin_unreachable ();
2448 string - returns the C style char * of String, s.
2451 extern "C" void * DynamicStrings_string (DynamicStrings_String s
)
2453 typedef char *string__T2
;
2455 DynamicStrings_String a
;
2462 s
= CheckPoisoned (s
);
2470 if (! s
->head
->charStarValid
)
2472 l
= DynamicStrings_Length (s
);
2473 if (! (s
->head
->charStarUsed
&& (s
->head
->charStarSize
> l
)))
2475 DeallocateCharStar (s
);
2476 Storage_ALLOCATE (&s
->head
->charStar
, l
+1);
2477 s
->head
->charStarSize
= l
+1;
2478 s
->head
->charStarUsed
= true;
2480 p
= static_cast<string__T2
> (s
->head
->charStar
);
2485 while (i
< a
->contents
.len
)
2487 (*p
) = a
->contents
.buf
.array
[i
];
2491 a
= a
->contents
.next
;
2494 s
->head
->charStarValid
= true;
2496 return s
->head
->charStar
;
2498 /* static analysis guarentees a RETURN statement will be used before here. */
2499 __builtin_unreachable ();
2504 InitStringDB - the debug version of InitString.
2507 extern "C" DynamicStrings_String
DynamicStrings_InitStringDB (const char *a_
, unsigned int _a_high
, const char *file_
, unsigned int _file_high
, unsigned int line
)
2510 char file
[_file_high
+1];
2512 /* make a local copy of each unbounded array. */
2513 memcpy (a
, a_
, _a_high
+1);
2514 memcpy (file
, file_
, _file_high
+1);
2516 return AssignDebug (DynamicStrings_InitString ((const char *) a
, _a_high
), (const char *) file
, _file_high
, line
, (const char *) "InitString", 10);
2517 /* static analysis guarentees a RETURN statement will be used before here. */
2518 __builtin_unreachable ();
2523 InitStringCharStarDB - the debug version of InitStringCharStar.
2526 extern "C" DynamicStrings_String
DynamicStrings_InitStringCharStarDB (void * a
, const char *file_
, unsigned int _file_high
, unsigned int line
)
2528 char file
[_file_high
+1];
2530 /* make a local copy of each unbounded array. */
2531 memcpy (file
, file_
, _file_high
+1);
2533 return AssignDebug (DynamicStrings_InitStringCharStar (a
), (const char *) file
, _file_high
, line
, (const char *) "InitStringCharStar", 18);
2534 /* static analysis guarentees a RETURN statement will be used before here. */
2535 __builtin_unreachable ();
2540 InitStringCharDB - the debug version of InitStringChar.
2543 extern "C" DynamicStrings_String
DynamicStrings_InitStringCharDB (char ch
, const char *file_
, unsigned int _file_high
, unsigned int line
)
2545 char file
[_file_high
+1];
2547 /* make a local copy of each unbounded array. */
2548 memcpy (file
, file_
, _file_high
+1);
2550 return AssignDebug (DynamicStrings_InitStringChar (ch
), (const char *) file
, _file_high
, line
, (const char *) "InitStringChar", 14);
2551 /* static analysis guarentees a RETURN statement will be used before here. */
2552 __builtin_unreachable ();
2557 MultDB - the debug version of MultDB.
2560 extern "C" DynamicStrings_String
DynamicStrings_MultDB (DynamicStrings_String s
, unsigned int n
, const char *file_
, unsigned int _file_high
, unsigned int line
)
2562 char file
[_file_high
+1];
2564 /* make a local copy of each unbounded array. */
2565 memcpy (file
, file_
, _file_high
+1);
2567 return AssignDebug (DynamicStrings_Mult (s
, n
), (const char *) file
, _file_high
, line
, (const char *) "Mult", 4);
2568 /* static analysis guarentees a RETURN statement will be used before here. */
2569 __builtin_unreachable ();
2574 DupDB - the debug version of Dup.
2577 extern "C" DynamicStrings_String
DynamicStrings_DupDB (DynamicStrings_String s
, const char *file_
, unsigned int _file_high
, unsigned int line
)
2579 char file
[_file_high
+1];
2581 /* make a local copy of each unbounded array. */
2582 memcpy (file
, file_
, _file_high
+1);
2584 return AssignDebug (DynamicStrings_Dup (s
), (const char *) file
, _file_high
, line
, (const char *) "Dup", 3);
2585 /* static analysis guarentees a RETURN statement will be used before here. */
2586 __builtin_unreachable ();
2591 SliceDB - debug version of Slice.
2594 extern "C" DynamicStrings_String
DynamicStrings_SliceDB (DynamicStrings_String s
, int low
, int high
, const char *file_
, unsigned int _file_high
, unsigned int line
)
2596 char file
[_file_high
+1];
2598 /* make a local copy of each unbounded array. */
2599 memcpy (file
, file_
, _file_high
+1);
2602 s
= AssignDebug (DynamicStrings_Slice (s
, low
, high
), (const char *) file
, _file_high
, line
, (const char *) "Slice", 5);
2605 /* static analysis guarentees a RETURN statement will be used before here. */
2606 __builtin_unreachable ();
2611 PushAllocation - pushes the current allocation/deallocation lists.
2614 extern "C" void DynamicStrings_PushAllocation (void)
2616 DynamicStrings_frame f
;
2621 Storage_ALLOCATE ((void **) &f
, sizeof (DynamicStrings_frameRec
));
2622 f
->next
= frameHead
;
2631 PopAllocation - test to see that all strings are deallocated since
2632 the last push. Then it pops to the previous
2633 allocation/deallocation lists.
2635 If halt is true then the application terminates
2636 with an exit code of 1.
2639 extern "C" void DynamicStrings_PopAllocation (bool halt
)
2643 if ((DynamicStrings_PopAllocationExemption (halt
, NULL
)) == NULL
)
2650 PopAllocationExemption - test to see that all strings are deallocated, except
2651 string e since the last push.
2652 Post-condition: it pops to the previous allocation/deallocation
2655 If halt is true then the application terminates
2656 with an exit code of 1.
2659 extern "C" DynamicStrings_String
DynamicStrings_PopAllocationExemption (bool halt
, DynamicStrings_String e
)
2661 DynamicStrings_String s
;
2667 /* avoid gcc warning by using compound statement even if not strictly necessary. */
2668 if (frameHead
== NULL
)
2671 M2RTS_Halt ((const char *) "mismatched number of PopAllocation's compared to PushAllocation's", 65, (const char *) "../../gcc-read-write/gcc/m2/gm2-libs/DynamicStrings.mod", 55, (const char *) "PopAllocationExemption", 22, 174);
2675 /* writeString ("mismatched number of PopAllocation's compared to PushAllocation's") */
2676 if (frameHead
->alloc
!= NULL
)
2679 s
= frameHead
->alloc
;
2682 if (! (((e
== s
) || (IsOnGarbage (e
, s
))) || (IsOnGarbage (s
, e
))))
2686 writeString ((const char *) "the following strings have been lost", 36);
2690 DumpStringInfo (s
, 0);
2699 frameHead
= frameHead
->next
;
2703 /* static analysis guarentees a RETURN statement will be used before here. */
2704 __builtin_unreachable ();
2707 extern "C" void _M2_DynamicStrings_init (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])
2709 Initialized
= false;
2713 extern "C" void _M2_DynamicStrings_fini (__attribute__((unused
)) int argc
,__attribute__((unused
)) char *argv
[],__attribute__((unused
)) char *envp
[])