1 /* An abstract string datatype.
2 Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc.
3 Contributed by Mark Mitchell (mark@markmitchell.com).
4
5 This file is part of GNU CC.
6
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)
10 any later version.
11
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
19 executable.)
20
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.
25
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. */
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 /* If this file is being compiled for inclusion in the C++ runtime
49 library, as part of the demangler implementation, we don't want to
50 abort if an allocation fails. Instead, percolate an error code up
51 through the call chain. */
52
53 #if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
54 #define RETURN_ON_ALLOCATION_FAILURE
55 #endif
56
57 /* Performs in-place initialization of a dyn_string struct. This
58 function can be used with a dyn_string struct on the stack or
59 embedded in another object. The contents of of the string itself
60 are still dynamically allocated. The string initially is capable
61 of holding at least SPACE characeters, including the terminating
62 NUL. If SPACE is 0, it will silently be increated to 1.
63
64 If RETURN_ON_ALLOCATION_FAILURE is defined and memory allocation
65 fails, returns 0. Otherwise returns 1. */
66
67 int
dyn_string_init(struct dyn_string * ds_struct_ptr,int space)68 dyn_string_init (struct dyn_string *ds_struct_ptr, int space)
69 {
70 /* We need at least one byte in which to store the terminating NUL. */
71 if (space == 0)
72 space = 1;
73
74 #ifdef RETURN_ON_ALLOCATION_FAILURE
75 ds_struct_ptr->s = (char *) malloc (space);
76 if (ds_struct_ptr->s == NULL)
77 return 0;
78 #else
79 ds_struct_ptr->s = XNEWVEC (char, space);
80 #endif
81 ds_struct_ptr->allocated = space;
82 ds_struct_ptr->length = 0;
83 ds_struct_ptr->s[0] = '\0';
84
85 return 1;
86 }
87
88 /* Create a new dynamic string capable of holding at least SPACE
89 characters, including the terminating NUL. If SPACE is 0, it will
90 be silently increased to 1. If RETURN_ON_ALLOCATION_FAILURE is
91 defined and memory allocation fails, returns NULL. Otherwise
92 returns the newly allocated string. */
93
94 dyn_string_t
dyn_string_new(int space)95 dyn_string_new (int space)
96 {
97 dyn_string_t result;
98 #ifdef RETURN_ON_ALLOCATION_FAILURE
99 result = (dyn_string_t) malloc (sizeof (struct dyn_string));
100 if (result == NULL)
101 return NULL;
102 if (!dyn_string_init (result, space))
103 {
104 free (result);
105 return NULL;
106 }
107 #else
108 result = XNEW (struct dyn_string);
109 dyn_string_init (result, space);
110 #endif
111 return result;
112 }
113
114 /* Free the memory used by DS. */
115
116 void
dyn_string_delete(dyn_string_t ds)117 dyn_string_delete (dyn_string_t ds)
118 {
119 free (ds->s);
120 free (ds);
121 }
122
123 /* Returns the contents of DS in a buffer allocated with malloc. It
124 is the caller's responsibility to deallocate the buffer using free.
125 DS is then set to the empty string. Deletes DS itself. */
126
127 char*
dyn_string_release(dyn_string_t ds)128 dyn_string_release (dyn_string_t ds)
129 {
130 /* Store the old buffer. */
131 char* result = ds->s;
132 /* The buffer is no longer owned by DS. */
133 ds->s = NULL;
134 /* Delete DS. */
135 free (ds);
136 /* Return the old buffer. */
137 return result;
138 }
139
140 /* Increase the capacity of DS so it can hold at least SPACE
141 characters, plus the terminating NUL. This function will not (at
142 present) reduce the capacity of DS. Returns DS on success.
143
144 If RETURN_ON_ALLOCATION_FAILURE is defined and a memory allocation
145 operation fails, deletes DS and returns NULL. */
146
147 dyn_string_t
dyn_string_resize(dyn_string_t ds,int space)148 dyn_string_resize (dyn_string_t ds, int space)
149 {
150 int new_allocated = ds->allocated;
151
152 /* Increase SPACE to hold the NUL termination. */
153 ++space;
154
155 /* Increase allocation by factors of two. */
156 while (space > new_allocated)
157 new_allocated *= 2;
158
159 if (new_allocated != ds->allocated)
160 {
161 ds->allocated = new_allocated;
162 /* We actually need more space. */
163 #ifdef RETURN_ON_ALLOCATION_FAILURE
164 ds->s = (char *) realloc (ds->s, ds->allocated);
165 if (ds->s == NULL)
166 {
167 free (ds);
168 return NULL;
169 }
170 #else
171 ds->s = XRESIZEVEC (char, ds->s, ds->allocated);
172 #endif
173 }
174
175 return ds;
176 }
177
178 /* Sets the contents of DS to the empty string. */
179
180 void
dyn_string_clear(dyn_string_t ds)181 dyn_string_clear (dyn_string_t ds)
182 {
183 /* A dyn_string always has room for at least the NUL terminator. */
184 ds->s[0] = '\0';
185 ds->length = 0;
186 }
187
188 /* Makes the contents of DEST the same as the contents of SRC. DEST
189 and SRC must be distinct. Returns 1 on success. On failure, if
190 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
191
192 int
dyn_string_copy(dyn_string_t dest,dyn_string_t src)193 dyn_string_copy (dyn_string_t dest, dyn_string_t src)
194 {
195 if (dest == src)
196 abort ();
197
198 /* Make room in DEST. */
199 if (dyn_string_resize (dest, src->length) == NULL)
200 return 0;
201 /* Copy DEST into SRC. */
202 strlcpy (dest->s, src->s, dest->allocated);
203 /* Update the size of DEST. */
204 dest->length = src->length;
205 return 1;
206 }
207
208 /* Copies SRC, a NUL-terminated string, into DEST. Returns 1 on
209 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
210 and returns 0. */
211
212 int
dyn_string_copy_cstr(dyn_string_t dest,const char * src)213 dyn_string_copy_cstr (dyn_string_t dest, const char *src)
214 {
215 int length = strlen (src);
216 /* Make room in DEST. */
217 if (dyn_string_resize (dest, length) == NULL)
218 return 0;
219 /* Copy DEST into SRC. */
220 strlcpy (dest->s, src, dest->allocated);
221 /* Update the size of DEST. */
222 dest->length = length;
223 return 1;
224 }
225
226 /* Inserts SRC at the beginning of DEST. DEST is expanded as
227 necessary. SRC and DEST must be distinct. Returns 1 on success.
228 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
229 returns 0. */
230
231 int
dyn_string_prepend(dyn_string_t dest,dyn_string_t src)232 dyn_string_prepend (dyn_string_t dest, dyn_string_t src)
233 {
234 return dyn_string_insert (dest, 0, src);
235 }
236
237 /* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
238 DEST is expanded as necessary. Returns 1 on success. On failure,
239 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
240
241 int
dyn_string_prepend_cstr(dyn_string_t dest,const char * src)242 dyn_string_prepend_cstr (dyn_string_t dest, const char *src)
243 {
244 return dyn_string_insert_cstr (dest, 0, src);
245 }
246
247 /* Inserts SRC into DEST starting at position POS. DEST is expanded
248 as necessary. SRC and DEST must be distinct. Returns 1 on
249 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
250 and returns 0. */
251
252 int
dyn_string_insert(dyn_string_t dest,int pos,dyn_string_t src)253 dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src)
254 {
255 int i;
256
257 if (src == dest)
258 abort ();
259
260 if (dyn_string_resize (dest, dest->length + src->length) == NULL)
261 return 0;
262 /* Make room for the insertion. Be sure to copy the NUL. */
263 for (i = dest->length; i >= pos; --i)
264 dest->s[i + src->length] = dest->s[i];
265 /* Splice in the new stuff. */
266 strncpy (dest->s + pos, src->s, src->length);
267 /* Compute the new length. */
268 dest->length += src->length;
269 return 1;
270 }
271
272 /* Inserts SRC, a NUL-terminated string, into DEST starting at
273 position POS. DEST is expanded as necessary. Returns 1 on
274 success. On failure, RETURN_ON_ALLOCATION_FAILURE, deletes DEST
275 and returns 0. */
276
277 int
dyn_string_insert_cstr(dyn_string_t dest,int pos,const char * src)278 dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src)
279 {
280 int i;
281 int length = strlen (src);
282
283 if (dyn_string_resize (dest, dest->length + length) == NULL)
284 return 0;
285 /* Make room for the insertion. Be sure to copy the NUL. */
286 for (i = dest->length; i >= pos; --i)
287 dest->s[i + length] = dest->s[i];
288 /* Splice in the new stuff. */
289 strncpy (dest->s + pos, src, length);
290 /* Compute the new length. */
291 dest->length += length;
292 return 1;
293 }
294
295 /* Inserts character C into DEST starting at position POS. DEST is
296 expanded as necessary. Returns 1 on success. On failure,
297 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
298
299 int
dyn_string_insert_char(dyn_string_t dest,int pos,int c)300 dyn_string_insert_char (dyn_string_t dest, int pos, int c)
301 {
302 int i;
303
304 if (dyn_string_resize (dest, dest->length + 1) == NULL)
305 return 0;
306 /* Make room for the insertion. Be sure to copy the NUL. */
307 for (i = dest->length; i >= pos; --i)
308 dest->s[i + 1] = dest->s[i];
309 /* Add the new character. */
310 dest->s[pos] = c;
311 /* Compute the new length. */
312 ++dest->length;
313 return 1;
314 }
315
316 /* Append S to DS, resizing DS if necessary. Returns 1 on success.
317 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
318 returns 0. */
319
320 int
dyn_string_append(dyn_string_t dest,dyn_string_t s)321 dyn_string_append (dyn_string_t dest, dyn_string_t s)
322 {
323 if (dyn_string_resize (dest, dest->length + s->length) == 0)
324 return 0;
325 strlcpy (dest->s + dest->length, s->s, dest->allocated - dest->length);
326 dest->length += s->length;
327 return 1;
328 }
329
330 /* Append the NUL-terminated string S to DS, resizing DS if necessary.
331 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
332 deletes DEST and returns 0. */
333
334 int
dyn_string_append_cstr(dyn_string_t dest,const char * s)335 dyn_string_append_cstr (dyn_string_t dest, const char *s)
336 {
337 int len = strlen (s);
338
339 /* The new length is the old length plus the size of our string, plus
340 one for the null at the end. */
341 if (dyn_string_resize (dest, dest->length + len) == NULL)
342 return 0;
343 strlcpy (dest->s + dest->length, s, dest->allocated - dest->length);
344 dest->length += len;
345 return 1;
346 }
347
348 /* Appends C to the end of DEST. Returns 1 on success. On failiure,
349 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
350
351 int
dyn_string_append_char(dyn_string_t dest,int c)352 dyn_string_append_char (dyn_string_t dest, int c)
353 {
354 /* Make room for the extra character. */
355 if (dyn_string_resize (dest, dest->length + 1) == NULL)
356 return 0;
357 /* Append the character; it will overwrite the old NUL. */
358 dest->s[dest->length] = c;
359 /* Add a new NUL at the end. */
360 dest->s[dest->length + 1] = '\0';
361 /* Update the length. */
362 ++(dest->length);
363 return 1;
364 }
365
366 /* Sets the contents of DEST to the substring of SRC starting at START
367 and ending before END. START must be less than or equal to END,
368 and both must be between zero and the length of SRC, inclusive.
369 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
370 deletes DEST and returns 0. */
371
372 int
dyn_string_substring(dyn_string_t dest,dyn_string_t src,int start,int end)373 dyn_string_substring (dyn_string_t dest, dyn_string_t src,
374 int start, int end)
375 {
376 int i;
377 int length = end - start;
378
379 if (start > end || start > src->length || end > src->length)
380 abort ();
381
382 /* Make room for the substring. */
383 if (dyn_string_resize (dest, length) == NULL)
384 return 0;
385 /* Copy the characters in the substring, */
386 for (i = length; --i >= 0; )
387 dest->s[i] = src->s[start + i];
388 /* NUL-terimate the result. */
389 dest->s[length] = '\0';
390 /* Record the length of the substring. */
391 dest->length = length;
392
393 return 1;
394 }
395
396 /* Returns non-zero if DS1 and DS2 have the same contents. */
397
398 int
dyn_string_eq(dyn_string_t ds1,dyn_string_t ds2)399 dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2)
400 {
401 /* If DS1 and DS2 have different lengths, they must not be the same. */
402 if (ds1->length != ds2->length)
403 return 0;
404 else
405 return !strcmp (ds1->s, ds2->s);
406 }
407