gdb: add target_ops::supports_displaced_step
[deliverable/binutils-gdb.git] / libiberty / dyn-string.c
CommitLineData
eb383413 1/* An abstract string datatype.
533da483 2 Copyright (C) 1998-2020 Free Software Foundation, Inc.
eb383413
L
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
38e57a35
DD
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
eb383413
L
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
979c05d3
NC
28the Free Software Foundation, 51 Franklin Street - Fifth Floor,
29Boston, MA 02110-1301, USA. */
eb383413
L
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <stdio.h>
36
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
03d5f569 53 NUL. If SPACE is 0, it will silently be increated to 1.
eb383413 54
03d5f569
JM
55 If RETURN_ON_ALLOCATION_FAILURE is defined and memory allocation
56 fails, returns 0. Otherwise returns 1. */
57
58int
49b1fae4 59dyn_string_init (struct dyn_string *ds_struct_ptr, int space)
eb383413
L
60{
61 /* We need at least one byte in which to store the terminating NUL. */
62 if (space == 0)
63 space = 1;
64
03d5f569
JM
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
abf6a75b 70 ds_struct_ptr->s = XNEWVEC (char, space);
03d5f569
JM
71#endif
72 ds_struct_ptr->allocated = space;
eb383413
L
73 ds_struct_ptr->length = 0;
74 ds_struct_ptr->s[0] = '\0';
eb383413 75
03d5f569
JM
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. */
eb383413
L
84
85dyn_string_t
49b1fae4 86dyn_string_new (int space)
eb383413 87{
03d5f569
JM
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
abf6a75b 99 result = XNEW (struct dyn_string);
eb383413 100 dyn_string_init (result, space);
03d5f569 101#endif
eb383413
L
102 return result;
103}
104
105/* Free the memory used by DS. */
106
107void
49b1fae4 108dyn_string_delete (dyn_string_t ds)
eb383413
L
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.
03d5f569 116 DS is then set to the empty string. Deletes DS itself. */
eb383413
L
117
118char*
49b1fae4 119dyn_string_release (dyn_string_t ds)
eb383413
L
120{
121 /* Store the old buffer. */
122 char* result = ds->s;
123 /* The buffer is no longer owned by DS. */
124 ds->s = NULL;
03d5f569
JM
125 /* Delete DS. */
126 free (ds);
eb383413
L
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
03d5f569
JM
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. */
eb383413
L
137
138dyn_string_t
49b1fae4 139dyn_string_resize (dyn_string_t ds, int space)
eb383413
L
140{
141 int new_allocated = ds->allocated;
142
143 /* Increase SPACE to hold the NUL termination. */
144 ++space;
145
03d5f569 146 /* Increase allocation by factors of two. */
eb383413
L
147 while (space > new_allocated)
148 new_allocated *= 2;
149
150 if (new_allocated != ds->allocated)
151 {
eb383413 152 ds->allocated = new_allocated;
03d5f569
JM
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
abf6a75b 162 ds->s = XRESIZEVEC (char, ds->s, ds->allocated);
03d5f569 163#endif
eb383413
L
164 }
165
166 return ds;
167}
168
169/* Sets the contents of DS to the empty string. */
170
171void
49b1fae4 172dyn_string_clear (dyn_string_t ds)
eb383413
L
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
03d5f569
JM
180 and SRC must be distinct. Returns 1 on success. On failure, if
181 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
eb383413 182
03d5f569 183int
49b1fae4 184dyn_string_copy (dyn_string_t dest, dyn_string_t src)
eb383413
L
185{
186 if (dest == src)
187 abort ();
188
189 /* Make room in DEST. */
03d5f569
JM
190 if (dyn_string_resize (dest, src->length) == NULL)
191 return 0;
eb383413
L
192 /* Copy DEST into SRC. */
193 strcpy (dest->s, src->s);
194 /* Update the size of DEST. */
195 dest->length = src->length;
03d5f569 196 return 1;
eb383413
L
197}
198
03d5f569
JM
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. */
eb383413 202
03d5f569 203int
49b1fae4 204dyn_string_copy_cstr (dyn_string_t dest, const char *src)
eb383413
L
205{
206 int length = strlen (src);
207 /* Make room in DEST. */
03d5f569
JM
208 if (dyn_string_resize (dest, length) == NULL)
209 return 0;
eb383413
L
210 /* Copy DEST into SRC. */
211 strcpy (dest->s, src);
212 /* Update the size of DEST. */
213 dest->length = length;
03d5f569 214 return 1;
eb383413
L
215}
216
217/* Inserts SRC at the beginning of DEST. DEST is expanded as
03d5f569
JM
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. */
eb383413 221
03d5f569 222int
49b1fae4 223dyn_string_prepend (dyn_string_t dest, dyn_string_t src)
eb383413 224{
03d5f569 225 return dyn_string_insert (dest, 0, src);
eb383413
L
226}
227
228/* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
03d5f569
JM
229 DEST is expanded as necessary. Returns 1 on success. On failure,
230 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
eb383413 231
03d5f569 232int
49b1fae4 233dyn_string_prepend_cstr (dyn_string_t dest, const char *src)
eb383413 234{
03d5f569 235 return dyn_string_insert_cstr (dest, 0, src);
eb383413
L
236}
237
03d5f569
JM
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. */
eb383413 242
03d5f569 243int
49b1fae4 244dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src)
eb383413
L
245{
246 int i;
247
248 if (src == dest)
249 abort ();
250
03d5f569
JM
251 if (dyn_string_resize (dest, dest->length + src->length) == NULL)
252 return 0;
eb383413
L
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;
03d5f569 260 return 1;
eb383413
L
261}
262
263/* Inserts SRC, a NUL-terminated string, into DEST starting at
03d5f569
JM
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. */
eb383413 267
03d5f569 268int
49b1fae4 269dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src)
eb383413
L
270{
271 int i;
272 int length = strlen (src);
273
03d5f569
JM
274 if (dyn_string_resize (dest, dest->length + length) == NULL)
275 return 0;
eb383413
L
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;
03d5f569 283 return 1;
eb383413
L
284}
285
74bcd529
DD
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
49b1fae4 291dyn_string_insert_char (dyn_string_t dest, int pos, int c)
74bcd529
DD
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
03d5f569
JM
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. */
eb383413 310
03d5f569 311int
49b1fae4 312dyn_string_append (dyn_string_t dest, dyn_string_t s)
eb383413 313{
03d5f569
JM
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;
eb383413
L
319}
320
321/* Append the NUL-terminated string S to DS, resizing DS if necessary.
03d5f569
JM
322 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
323 deletes DEST and returns 0. */
eb383413 324
03d5f569 325int
49b1fae4 326dyn_string_append_cstr (dyn_string_t dest, const char *s)
eb383413
L
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. */
03d5f569
JM
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;
eb383413
L
337}
338
ca938790 339/* Appends C to the end of DEST. Returns 1 on success. On failure,
03d5f569 340 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
eb383413 341
03d5f569 342int
49b1fae4 343dyn_string_append_char (dyn_string_t dest, int c)
eb383413
L
344{
345 /* Make room for the extra character. */
03d5f569
JM
346 if (dyn_string_resize (dest, dest->length + 1) == NULL)
347 return 0;
eb383413 348 /* Append the character; it will overwrite the old NUL. */
03d5f569 349 dest->s[dest->length] = c;
eb383413 350 /* Add a new NUL at the end. */
03d5f569 351 dest->s[dest->length + 1] = '\0';
eb383413 352 /* Update the length. */
03d5f569
JM
353 ++(dest->length);
354 return 1;
eb383413
L
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,
03d5f569
JM
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. */
eb383413 362
03d5f569 363int
49b1fae4
DD
364dyn_string_substring (dyn_string_t dest, dyn_string_t src,
365 int start, int end)
eb383413
L
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. */
03d5f569
JM
374 if (dyn_string_resize (dest, length) == NULL)
375 return 0;
eb383413
L
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;
03d5f569
JM
383
384 return 1;
eb383413
L
385}
386
387/* Returns non-zero if DS1 and DS2 have the same contents. */
388
389int
49b1fae4 390dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2)
eb383413
L
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}
This page took 0.864299 seconds and 4 git commands to generate.