]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/dyn-string.c
Remove unnecessary string literals from static_assert in C++17 tests
[thirdparty/gcc.git] / libiberty / dyn-string.c
CommitLineData
168d63e5 1/* An abstract string datatype.
fbd26352 2 Copyright (C) 1998-2019 Free Software Foundation, Inc.
168d63e5 3 Contributed by Mark Mitchell (mark@markmitchell.com).
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
baf716a5 12In addition to the permissions in the GNU General Public License, the
13Free Software Foundation gives you unlimited permission to link the
14compiled version of this file into combinations with other programs,
15and to distribute those combinations without any restriction coming
16from the use of this file. (The General Public License restrictions
17do apply in other respects; for example, they cover modification of
18the file, and distribution when not linked into a combined
19executable.)
20
168d63e5 21GNU CC is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with GNU CC; see the file COPYING. If not, write to
95b8d1bc 28the Free Software Foundation, 51 Franklin Street - Fifth Floor,
29Boston, MA 02110-1301, USA. */
168d63e5 30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
e5d6924a 35#include <stdio.h>
36
168d63e5 37#ifdef HAVE_STRING_H
38#include <string.h>
39#endif
40
41#ifdef HAVE_STDLIB_H
42#include <stdlib.h>
43#endif
44
45#include "libiberty.h"
46#include "dyn-string.h"
47
48/* Performs in-place initialization of a dyn_string struct. This
49 function can be used with a dyn_string struct on the stack or
50 embedded in another object. The contents of of the string itself
51 are still dynamically allocated. The string initially is capable
52 of holding at least SPACE characeters, including the terminating
140d75d7 53 NUL. If SPACE is 0, it will silently be increated to 1.
168d63e5 54
140d75d7 55 If RETURN_ON_ALLOCATION_FAILURE is defined and memory allocation
56 fails, returns 0. Otherwise returns 1. */
57
58int
8858115e 59dyn_string_init (struct dyn_string *ds_struct_ptr, int space)
168d63e5 60{
61 /* We need at least one byte in which to store the terminating NUL. */
62 if (space == 0)
63 space = 1;
64
140d75d7 65#ifdef RETURN_ON_ALLOCATION_FAILURE
66 ds_struct_ptr->s = (char *) malloc (space);
67 if (ds_struct_ptr->s == NULL)
68 return 0;
69#else
f2d737fc 70 ds_struct_ptr->s = XNEWVEC (char, space);
140d75d7 71#endif
72 ds_struct_ptr->allocated = space;
168d63e5 73 ds_struct_ptr->length = 0;
74 ds_struct_ptr->s[0] = '\0';
168d63e5 75
140d75d7 76 return 1;
77}
78
79/* Create a new dynamic string capable of holding at least SPACE
80 characters, including the terminating NUL. If SPACE is 0, it will
81 be silently increased to 1. If RETURN_ON_ALLOCATION_FAILURE is
82 defined and memory allocation fails, returns NULL. Otherwise
83 returns the newly allocated string. */
168d63e5 84
85dyn_string_t
8858115e 86dyn_string_new (int space)
168d63e5 87{
140d75d7 88 dyn_string_t result;
89#ifdef RETURN_ON_ALLOCATION_FAILURE
90 result = (dyn_string_t) malloc (sizeof (struct dyn_string));
91 if (result == NULL)
92 return NULL;
93 if (!dyn_string_init (result, space))
94 {
95 free (result);
96 return NULL;
97 }
98#else
f2d737fc 99 result = XNEW (struct dyn_string);
168d63e5 100 dyn_string_init (result, space);
140d75d7 101#endif
168d63e5 102 return result;
103}
104
105/* Free the memory used by DS. */
106
107void
8858115e 108dyn_string_delete (dyn_string_t ds)
168d63e5 109{
110 free (ds->s);
111 free (ds);
112}
113
114/* Returns the contents of DS in a buffer allocated with malloc. It
115 is the caller's responsibility to deallocate the buffer using free.
140d75d7 116 DS is then set to the empty string. Deletes DS itself. */
168d63e5 117
118char*
8858115e 119dyn_string_release (dyn_string_t ds)
168d63e5 120{
121 /* Store the old buffer. */
122 char* result = ds->s;
123 /* The buffer is no longer owned by DS. */
124 ds->s = NULL;
140d75d7 125 /* Delete DS. */
126 free (ds);
168d63e5 127 /* Return the old buffer. */
128 return result;
129}
130
131/* Increase the capacity of DS so it can hold at least SPACE
132 characters, plus the terminating NUL. This function will not (at
140d75d7 133 present) reduce the capacity of DS. Returns DS on success.
134
135 If RETURN_ON_ALLOCATION_FAILURE is defined and a memory allocation
136 operation fails, deletes DS and returns NULL. */
168d63e5 137
138dyn_string_t
8858115e 139dyn_string_resize (dyn_string_t ds, int space)
168d63e5 140{
141 int new_allocated = ds->allocated;
142
143 /* Increase SPACE to hold the NUL termination. */
144 ++space;
145
140d75d7 146 /* Increase allocation by factors of two. */
168d63e5 147 while (space > new_allocated)
148 new_allocated *= 2;
149
150 if (new_allocated != ds->allocated)
151 {
168d63e5 152 ds->allocated = new_allocated;
140d75d7 153 /* We actually need more space. */
154#ifdef RETURN_ON_ALLOCATION_FAILURE
155 ds->s = (char *) realloc (ds->s, ds->allocated);
156 if (ds->s == NULL)
157 {
158 free (ds);
159 return NULL;
160 }
161#else
f2d737fc 162 ds->s = XRESIZEVEC (char, ds->s, ds->allocated);
140d75d7 163#endif
168d63e5 164 }
165
166 return ds;
167}
168
169/* Sets the contents of DS to the empty string. */
170
171void
8858115e 172dyn_string_clear (dyn_string_t ds)
168d63e5 173{
174 /* A dyn_string always has room for at least the NUL terminator. */
175 ds->s[0] = '\0';
176 ds->length = 0;
177}
178
179/* Makes the contents of DEST the same as the contents of SRC. DEST
140d75d7 180 and SRC must be distinct. Returns 1 on success. On failure, if
181 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
168d63e5 182
140d75d7 183int
8858115e 184dyn_string_copy (dyn_string_t dest, dyn_string_t src)
168d63e5 185{
186 if (dest == src)
187 abort ();
188
189 /* Make room in DEST. */
140d75d7 190 if (dyn_string_resize (dest, src->length) == NULL)
191 return 0;
168d63e5 192 /* Copy DEST into SRC. */
193 strcpy (dest->s, src->s);
194 /* Update the size of DEST. */
195 dest->length = src->length;
140d75d7 196 return 1;
168d63e5 197}
198
140d75d7 199/* Copies SRC, a NUL-terminated string, into DEST. Returns 1 on
200 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
201 and returns 0. */
168d63e5 202
140d75d7 203int
8858115e 204dyn_string_copy_cstr (dyn_string_t dest, const char *src)
168d63e5 205{
206 int length = strlen (src);
207 /* Make room in DEST. */
140d75d7 208 if (dyn_string_resize (dest, length) == NULL)
209 return 0;
168d63e5 210 /* Copy DEST into SRC. */
211 strcpy (dest->s, src);
212 /* Update the size of DEST. */
213 dest->length = length;
140d75d7 214 return 1;
168d63e5 215}
216
217/* Inserts SRC at the beginning of DEST. DEST is expanded as
140d75d7 218 necessary. SRC and DEST must be distinct. Returns 1 on success.
219 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
220 returns 0. */
168d63e5 221
140d75d7 222int
8858115e 223dyn_string_prepend (dyn_string_t dest, dyn_string_t src)
168d63e5 224{
140d75d7 225 return dyn_string_insert (dest, 0, src);
168d63e5 226}
227
228/* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
140d75d7 229 DEST is expanded as necessary. Returns 1 on success. On failure,
230 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
168d63e5 231
140d75d7 232int
8858115e 233dyn_string_prepend_cstr (dyn_string_t dest, const char *src)
168d63e5 234{
140d75d7 235 return dyn_string_insert_cstr (dest, 0, src);
168d63e5 236}
237
140d75d7 238/* Inserts SRC into DEST starting at position POS. DEST is expanded
239 as necessary. SRC and DEST must be distinct. Returns 1 on
240 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
241 and returns 0. */
168d63e5 242
140d75d7 243int
8858115e 244dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src)
168d63e5 245{
246 int i;
247
248 if (src == dest)
249 abort ();
250
140d75d7 251 if (dyn_string_resize (dest, dest->length + src->length) == NULL)
252 return 0;
168d63e5 253 /* Make room for the insertion. Be sure to copy the NUL. */
254 for (i = dest->length; i >= pos; --i)
255 dest->s[i + src->length] = dest->s[i];
256 /* Splice in the new stuff. */
257 strncpy (dest->s + pos, src->s, src->length);
258 /* Compute the new length. */
259 dest->length += src->length;
140d75d7 260 return 1;
168d63e5 261}
262
263/* Inserts SRC, a NUL-terminated string, into DEST starting at
140d75d7 264 position POS. DEST is expanded as necessary. Returns 1 on
265 success. On failure, RETURN_ON_ALLOCATION_FAILURE, deletes DEST
266 and returns 0. */
168d63e5 267
140d75d7 268int
8858115e 269dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src)
168d63e5 270{
271 int i;
272 int length = strlen (src);
273
140d75d7 274 if (dyn_string_resize (dest, dest->length + length) == NULL)
275 return 0;
168d63e5 276 /* Make room for the insertion. Be sure to copy the NUL. */
277 for (i = dest->length; i >= pos; --i)
278 dest->s[i + length] = dest->s[i];
279 /* Splice in the new stuff. */
280 strncpy (dest->s + pos, src, length);
281 /* Compute the new length. */
282 dest->length += length;
140d75d7 283 return 1;
168d63e5 284}
285
40e00cb0 286/* Inserts character C into DEST starting at position POS. DEST is
287 expanded as necessary. Returns 1 on success. On failure,
288 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
289
290int
8858115e 291dyn_string_insert_char (dyn_string_t dest, int pos, int c)
40e00cb0 292{
293 int i;
294
295 if (dyn_string_resize (dest, dest->length + 1) == NULL)
296 return 0;
297 /* Make room for the insertion. Be sure to copy the NUL. */
298 for (i = dest->length; i >= pos; --i)
299 dest->s[i + 1] = dest->s[i];
300 /* Add the new character. */
301 dest->s[pos] = c;
302 /* Compute the new length. */
303 ++dest->length;
304 return 1;
305}
306
140d75d7 307/* Append S to DS, resizing DS if necessary. Returns 1 on success.
308 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
309 returns 0. */
168d63e5 310
140d75d7 311int
8858115e 312dyn_string_append (dyn_string_t dest, dyn_string_t s)
168d63e5 313{
140d75d7 314 if (dyn_string_resize (dest, dest->length + s->length) == 0)
315 return 0;
316 strcpy (dest->s + dest->length, s->s);
317 dest->length += s->length;
318 return 1;
168d63e5 319}
320
321/* Append the NUL-terminated string S to DS, resizing DS if necessary.
140d75d7 322 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
323 deletes DEST and returns 0. */
168d63e5 324
140d75d7 325int
8858115e 326dyn_string_append_cstr (dyn_string_t dest, const char *s)
168d63e5 327{
328 int len = strlen (s);
329
330 /* The new length is the old length plus the size of our string, plus
331 one for the null at the end. */
140d75d7 332 if (dyn_string_resize (dest, dest->length + len) == NULL)
333 return 0;
334 strcpy (dest->s + dest->length, s);
335 dest->length += len;
336 return 1;
168d63e5 337}
338
7e46109d 339/* Appends C to the end of DEST. Returns 1 on success. On failure,
140d75d7 340 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
168d63e5 341
140d75d7 342int
8858115e 343dyn_string_append_char (dyn_string_t dest, int c)
168d63e5 344{
345 /* Make room for the extra character. */
140d75d7 346 if (dyn_string_resize (dest, dest->length + 1) == NULL)
347 return 0;
168d63e5 348 /* Append the character; it will overwrite the old NUL. */
140d75d7 349 dest->s[dest->length] = c;
168d63e5 350 /* Add a new NUL at the end. */
140d75d7 351 dest->s[dest->length + 1] = '\0';
168d63e5 352 /* Update the length. */
140d75d7 353 ++(dest->length);
354 return 1;
168d63e5 355}
356
357/* Sets the contents of DEST to the substring of SRC starting at START
358 and ending before END. START must be less than or equal to END,
140d75d7 359 and both must be between zero and the length of SRC, inclusive.
360 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
361 deletes DEST and returns 0. */
168d63e5 362
140d75d7 363int
8858115e 364dyn_string_substring (dyn_string_t dest, dyn_string_t src,
365 int start, int end)
168d63e5 366{
367 int i;
368 int length = end - start;
369
370 if (start > end || start > src->length || end > src->length)
371 abort ();
372
373 /* Make room for the substring. */
140d75d7 374 if (dyn_string_resize (dest, length) == NULL)
375 return 0;
168d63e5 376 /* Copy the characters in the substring, */
377 for (i = length; --i >= 0; )
378 dest->s[i] = src->s[start + i];
379 /* NUL-terimate the result. */
380 dest->s[length] = '\0';
381 /* Record the length of the substring. */
382 dest->length = length;
140d75d7 383
384 return 1;
168d63e5 385}
386
387/* Returns non-zero if DS1 and DS2 have the same contents. */
388
389int
8858115e 390dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2)
168d63e5 391{
392 /* If DS1 and DS2 have different lengths, they must not be the same. */
393 if (ds1->length != ds2->length)
394 return 0;
395 else
396 return !strcmp (ds1->s, ds2->s);
397}