1 /* An abstract string datatype.
2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
3 Contributed by Mark Mitchell (mark@markmitchell.com).
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combined
21 GNU CC is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with GNU CC; see the file COPYING. If not, write to
28 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #if 0 /* in valgrind */
35 #endif /* ! in valgrind */
37 #if 0 /* in valgrind */
39 #endif /* ! in valgrind */
41 #if 0 /* in valgrind */
45 #endif /* ! in valgrind */
47 #if 0 /* in valgrind */
51 #endif /* ! in valgrind */
53 #if 0 /* in valgrind */
54 #include "libiberty.h"
55 #endif /* ! in valgrind */
57 #include "vg_libciface.h"
59 #include "dyn-string.h"
61 /* Performs in-place initialization of a dyn_string struct. This
62 function can be used with a dyn_string struct on the stack or
63 embedded in another object. The contents of the string itself
64 are still dynamically allocated. The string initially is capable
65 of holding at least SPACE characeters, including the terminating
66 NUL. If SPACE is 0, it will silently be increated to 1.
68 If RETURN_ON_ALLOCATION_FAILURE is defined and memory allocation
69 fails, returns 0. Otherwise returns 1. */
72 dyn_string_init (struct dyn_string
*ds_struct_ptr
, int space
)
74 /* We need at least one byte in which to store the terminating NUL. */
78 #ifdef RETURN_ON_ALLOCATION_FAILURE
79 ds_struct_ptr
->s
= (char *) malloc (space
);
80 if (ds_struct_ptr
->s
== NULL
)
83 ds_struct_ptr
->s
= XNEWVEC (char, space
);
85 ds_struct_ptr
->allocated
= space
;
86 ds_struct_ptr
->length
= 0;
87 ds_struct_ptr
->s
[0] = '\0';
92 /* Create a new dynamic string capable of holding at least SPACE
93 characters, including the terminating NUL. If SPACE is 0, it will
94 be silently increased to 1. If RETURN_ON_ALLOCATION_FAILURE is
95 defined and memory allocation fails, returns NULL. Otherwise
96 returns the newly allocated string. */
99 dyn_string_new (int space
)
102 #ifdef RETURN_ON_ALLOCATION_FAILURE
103 result
= (dyn_string_t
) malloc (sizeof (struct dyn_string
));
106 if (!dyn_string_init (result
, space
))
112 result
= XNEW (struct dyn_string
);
113 dyn_string_init (result
, space
);
118 /* Free the memory used by DS. */
121 dyn_string_delete (dyn_string_t ds
)
127 /* Returns the contents of DS in a buffer allocated with malloc. It
128 is the caller's responsibility to deallocate the buffer using free.
129 DS is then set to the empty string. Deletes DS itself. */
132 dyn_string_release (dyn_string_t ds
)
134 /* Store the old buffer. */
135 char* result
= ds
->s
;
136 /* The buffer is no longer owned by DS. */
140 /* Return the old buffer. */
144 /* Increase the capacity of DS so it can hold at least SPACE
145 characters, plus the terminating NUL. This function will not (at
146 present) reduce the capacity of DS. Returns DS on success.
148 If RETURN_ON_ALLOCATION_FAILURE is defined and a memory allocation
149 operation fails, deletes DS and returns NULL. */
152 dyn_string_resize (dyn_string_t ds
, int space
)
154 int new_allocated
= ds
->allocated
;
156 /* Increase SPACE to hold the NUL termination. */
159 /* Increase allocation by factors of two. */
160 while (space
> new_allocated
)
163 if (new_allocated
!= ds
->allocated
)
165 ds
->allocated
= new_allocated
;
166 /* We actually need more space. */
167 #ifdef RETURN_ON_ALLOCATION_FAILURE
168 ds
->s
= (char *) realloc (ds
->s
, ds
->allocated
);
175 ds
->s
= XRESIZEVEC (char, ds
->s
, ds
->allocated
);
182 /* Sets the contents of DS to the empty string. */
185 dyn_string_clear (dyn_string_t ds
)
187 /* A dyn_string always has room for at least the NUL terminator. */
192 /* Makes the contents of DEST the same as the contents of SRC. DEST
193 and SRC must be distinct. Returns 1 on success. On failure, if
194 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
197 dyn_string_copy (dyn_string_t dest
, dyn_string_t src
)
202 /* Make room in DEST. */
203 if (dyn_string_resize (dest
, src
->length
) == NULL
)
205 /* Copy DEST into SRC. */
206 strcpy (dest
->s
, src
->s
);
207 /* Update the size of DEST. */
208 dest
->length
= src
->length
;
212 /* Copies SRC, a NUL-terminated string, into DEST. Returns 1 on
213 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
217 dyn_string_copy_cstr (dyn_string_t dest
, const char *src
)
219 int length
= strlen (src
);
220 /* Make room in DEST. */
221 if (dyn_string_resize (dest
, length
) == NULL
)
223 /* Copy DEST into SRC. */
224 strcpy (dest
->s
, src
);
225 /* Update the size of DEST. */
226 dest
->length
= length
;
230 /* Inserts SRC at the beginning of DEST. DEST is expanded as
231 necessary. SRC and DEST must be distinct. Returns 1 on success.
232 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
236 dyn_string_prepend (dyn_string_t dest
, dyn_string_t src
)
238 return dyn_string_insert (dest
, 0, src
);
241 /* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
242 DEST is expanded as necessary. Returns 1 on success. On failure,
243 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
246 dyn_string_prepend_cstr (dyn_string_t dest
, const char *src
)
248 return dyn_string_insert_cstr (dest
, 0, src
);
251 /* Inserts SRC into DEST starting at position POS. DEST is expanded
252 as necessary. SRC and DEST must be distinct. Returns 1 on
253 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
257 dyn_string_insert (dyn_string_t dest
, int pos
, dyn_string_t src
)
264 if (dyn_string_resize (dest
, dest
->length
+ src
->length
) == NULL
)
266 /* Make room for the insertion. Be sure to copy the NUL. */
267 for (i
= dest
->length
; i
>= pos
; --i
)
268 dest
->s
[i
+ src
->length
] = dest
->s
[i
];
269 /* Splice in the new stuff. */
270 strncpy (dest
->s
+ pos
, src
->s
, src
->length
);
271 /* Compute the new length. */
272 dest
->length
+= src
->length
;
276 /* Inserts SRC, a NUL-terminated string, into DEST starting at
277 position POS. DEST is expanded as necessary. Returns 1 on
278 success. On failure, RETURN_ON_ALLOCATION_FAILURE, deletes DEST
282 dyn_string_insert_cstr (dyn_string_t dest
, int pos
, const char *src
)
285 int length
= strlen (src
);
287 if (dyn_string_resize (dest
, dest
->length
+ length
) == NULL
)
289 /* Make room for the insertion. Be sure to copy the NUL. */
290 for (i
= dest
->length
; i
>= pos
; --i
)
291 dest
->s
[i
+ length
] = dest
->s
[i
];
292 /* Splice in the new stuff. */
293 memcpy (dest
->s
+ pos
, src
, length
);
294 /* Compute the new length. */
295 dest
->length
+= length
;
299 /* Inserts character C into DEST starting at position POS. DEST is
300 expanded as necessary. Returns 1 on success. On failure,
301 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
304 dyn_string_insert_char (dyn_string_t dest
, int pos
, int c
)
308 if (dyn_string_resize (dest
, dest
->length
+ 1) == NULL
)
310 /* Make room for the insertion. Be sure to copy the NUL. */
311 for (i
= dest
->length
; i
>= pos
; --i
)
312 dest
->s
[i
+ 1] = dest
->s
[i
];
313 /* Add the new character. */
315 /* Compute the new length. */
320 /* Append S to DS, resizing DS if necessary. Returns 1 on success.
321 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
325 dyn_string_append (dyn_string_t dest
, dyn_string_t s
)
327 if (dyn_string_resize (dest
, dest
->length
+ s
->length
) == 0)
329 strcpy (dest
->s
+ dest
->length
, s
->s
);
330 dest
->length
+= s
->length
;
334 /* Append the NUL-terminated string S to DS, resizing DS if necessary.
335 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
336 deletes DEST and returns 0. */
339 dyn_string_append_cstr (dyn_string_t dest
, const char *s
)
341 int len
= strlen (s
);
343 /* The new length is the old length plus the size of our string, plus
344 one for the null at the end. */
345 if (dyn_string_resize (dest
, dest
->length
+ len
) == NULL
)
347 strcpy (dest
->s
+ dest
->length
, s
);
352 /* Appends C to the end of DEST. Returns 1 on success. On failure,
353 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
356 dyn_string_append_char (dyn_string_t dest
, int c
)
358 /* Make room for the extra character. */
359 if (dyn_string_resize (dest
, dest
->length
+ 1) == NULL
)
361 /* Append the character; it will overwrite the old NUL. */
362 dest
->s
[dest
->length
] = c
;
363 /* Add a new NUL at the end. */
364 dest
->s
[dest
->length
+ 1] = '\0';
365 /* Update the length. */
370 /* Sets the contents of DEST to the substring of SRC starting at START
371 and ending before END. START must be less than or equal to END,
372 and both must be between zero and the length of SRC, inclusive.
373 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
374 deletes DEST and returns 0. */
377 dyn_string_substring (dyn_string_t dest
, dyn_string_t src
,
381 int length
= end
- start
;
383 if (start
> end
|| start
> src
->length
|| end
> src
->length
)
386 /* Make room for the substring. */
387 if (dyn_string_resize (dest
, length
) == NULL
)
389 /* Copy the characters in the substring, */
390 for (i
= length
; --i
>= 0; )
391 dest
->s
[i
] = src
->s
[start
+ i
];
392 /* NUL-terimate the result. */
393 dest
->s
[length
] = '\0';
394 /* Record the length of the substring. */
395 dest
->length
= length
;
400 /* Returns non-zero if DS1 and DS2 have the same contents. */
403 dyn_string_eq (dyn_string_t ds1
, dyn_string_t ds2
)
405 /* If DS1 and DS2 have different lengths, they must not be the same. */
406 if (ds1
->length
!= ds2
->length
)
409 return !strcmp (ds1
->s
, ds2
->s
);