blob: 0dbb3ac8893ae4ec78c81627ab390faefad3b687 [file] [log] [blame]
sewardjde4a1d02002-03-22 01:27:54 +00001/* An abstract string datatype.
sewardj4f2683a2008-10-26 11:53:30 +00002 Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc.
sewardjde4a1d02002-03-22 01:27:54 +00003 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
sewardj4f2683a2008-10-26 11:53:30 +000012In 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
sewardjde4a1d02002-03-22 01:27:54 +000021GNU 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
sewardj4f2683a2008-10-26 11:53:30 +000028the Free Software Foundation, 51 Franklin Street - Fifth Floor,
29Boston, MA 02110-1301, USA. */
sewardjde4a1d02002-03-22 01:27:54 +000030
sewardj9a2ce3d2008-10-27 09:54:14 +000031#if 0 /* in valgrind */
sewardjde4a1d02002-03-22 01:27:54 +000032#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
sewardj9a2ce3d2008-10-27 09:54:14 +000035#endif /* ! in valgrind */
sewardjde4a1d02002-03-22 01:27:54 +000036
sewardj9a2ce3d2008-10-27 09:54:14 +000037#if 0 /* in valgrind */
sewardj4f2683a2008-10-26 11:53:30 +000038#include <stdio.h>
sewardj9a2ce3d2008-10-27 09:54:14 +000039#endif /* ! in valgrind */
sewardj4f2683a2008-10-26 11:53:30 +000040
sewardj9a2ce3d2008-10-27 09:54:14 +000041#if 0 /* in valgrind */
sewardj4f2683a2008-10-26 11:53:30 +000042#ifdef HAVE_STRING_H
sewardjde4a1d02002-03-22 01:27:54 +000043#include <string.h>
sewardj4f2683a2008-10-26 11:53:30 +000044#endif
sewardj9a2ce3d2008-10-27 09:54:14 +000045#endif /* ! in valgrind */
sewardjde4a1d02002-03-22 01:27:54 +000046
sewardj9a2ce3d2008-10-27 09:54:14 +000047#if 0 /* in valgrind */
sewardj4f2683a2008-10-26 11:53:30 +000048#ifdef HAVE_STDLIB_H
sewardjde4a1d02002-03-22 01:27:54 +000049#include <stdlib.h>
sewardj4f2683a2008-10-26 11:53:30 +000050#endif
sewardj9a2ce3d2008-10-27 09:54:14 +000051#endif /* ! in valgrind */
sewardjde4a1d02002-03-22 01:27:54 +000052
sewardj9a2ce3d2008-10-27 09:54:14 +000053#if 0 /* in valgrind */
sewardj4f2683a2008-10-26 11:53:30 +000054#include "libiberty.h"
sewardj9a2ce3d2008-10-27 09:54:14 +000055#endif /* ! in valgrind */
56
57#include "vg_libciface.h"
58
sewardjde4a1d02002-03-22 01:27:54 +000059#include "dyn-string.h"
60
sewardjde4a1d02002-03-22 01:27:54 +000061/* 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 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.
67
68 If RETURN_ON_ALLOCATION_FAILURE is defined and memory allocation
69 fails, returns 0. Otherwise returns 1. */
70
71int
sewardj4f2683a2008-10-26 11:53:30 +000072dyn_string_init (struct dyn_string *ds_struct_ptr, int space)
sewardjde4a1d02002-03-22 01:27:54 +000073{
74 /* We need at least one byte in which to store the terminating NUL. */
75 if (space == 0)
76 space = 1;
77
78#ifdef RETURN_ON_ALLOCATION_FAILURE
79 ds_struct_ptr->s = (char *) malloc (space);
80 if (ds_struct_ptr->s == NULL)
81 return 0;
82#else
sewardj4f2683a2008-10-26 11:53:30 +000083 ds_struct_ptr->s = XNEWVEC (char, space);
sewardjde4a1d02002-03-22 01:27:54 +000084#endif
85 ds_struct_ptr->allocated = space;
86 ds_struct_ptr->length = 0;
87 ds_struct_ptr->s[0] = '\0';
88
89 return 1;
90}
91
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. */
97
98dyn_string_t
sewardj4f2683a2008-10-26 11:53:30 +000099dyn_string_new (int space)
sewardjde4a1d02002-03-22 01:27:54 +0000100{
101 dyn_string_t result;
102#ifdef RETURN_ON_ALLOCATION_FAILURE
sewardj4f2683a2008-10-26 11:53:30 +0000103 result = (dyn_string_t) malloc (sizeof (struct dyn_string));
sewardjde4a1d02002-03-22 01:27:54 +0000104 if (result == NULL)
105 return NULL;
106 if (!dyn_string_init (result, space))
107 {
108 free (result);
109 return NULL;
110 }
111#else
sewardj4f2683a2008-10-26 11:53:30 +0000112 result = XNEW (struct dyn_string);
sewardjde4a1d02002-03-22 01:27:54 +0000113 dyn_string_init (result, space);
114#endif
115 return result;
116}
117
118/* Free the memory used by DS. */
119
120void
sewardj4f2683a2008-10-26 11:53:30 +0000121dyn_string_delete (dyn_string_t ds)
sewardjde4a1d02002-03-22 01:27:54 +0000122{
123 free (ds->s);
124 free (ds);
125}
126
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. */
130
131char*
sewardj4f2683a2008-10-26 11:53:30 +0000132dyn_string_release (dyn_string_t ds)
sewardjde4a1d02002-03-22 01:27:54 +0000133{
134 /* Store the old buffer. */
135 char* result = ds->s;
136 /* The buffer is no longer owned by DS. */
137 ds->s = NULL;
138 /* Delete DS. */
139 free (ds);
140 /* Return the old buffer. */
141 return result;
142}
143
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.
147
148 If RETURN_ON_ALLOCATION_FAILURE is defined and a memory allocation
149 operation fails, deletes DS and returns NULL. */
150
151dyn_string_t
sewardj4f2683a2008-10-26 11:53:30 +0000152dyn_string_resize (dyn_string_t ds, int space)
sewardjde4a1d02002-03-22 01:27:54 +0000153{
154 int new_allocated = ds->allocated;
155
156 /* Increase SPACE to hold the NUL termination. */
157 ++space;
158
159 /* Increase allocation by factors of two. */
160 while (space > new_allocated)
161 new_allocated *= 2;
162
163 if (new_allocated != ds->allocated)
164 {
165 ds->allocated = new_allocated;
166 /* We actually need more space. */
167#ifdef RETURN_ON_ALLOCATION_FAILURE
sewardj4f2683a2008-10-26 11:53:30 +0000168 ds->s = (char *) realloc (ds->s, ds->allocated);
sewardjde4a1d02002-03-22 01:27:54 +0000169 if (ds->s == NULL)
170 {
171 free (ds);
172 return NULL;
173 }
174#else
sewardj4f2683a2008-10-26 11:53:30 +0000175 ds->s = XRESIZEVEC (char, ds->s, ds->allocated);
sewardjde4a1d02002-03-22 01:27:54 +0000176#endif
177 }
178
179 return ds;
180}
181
182/* Sets the contents of DS to the empty string. */
183
184void
sewardj4f2683a2008-10-26 11:53:30 +0000185dyn_string_clear (dyn_string_t ds)
sewardjde4a1d02002-03-22 01:27:54 +0000186{
187 /* A dyn_string always has room for at least the NUL terminator. */
188 ds->s[0] = '\0';
189 ds->length = 0;
190}
191
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. */
195
196int
sewardj4f2683a2008-10-26 11:53:30 +0000197dyn_string_copy (dyn_string_t dest, dyn_string_t src)
sewardjde4a1d02002-03-22 01:27:54 +0000198{
199 if (dest == src)
sewardj4f2683a2008-10-26 11:53:30 +0000200 abort ();
sewardjde4a1d02002-03-22 01:27:54 +0000201
202 /* Make room in DEST. */
203 if (dyn_string_resize (dest, src->length) == NULL)
204 return 0;
205 /* Copy DEST into SRC. */
sewardj4f2683a2008-10-26 11:53:30 +0000206 strcpy (dest->s, src->s);
sewardjde4a1d02002-03-22 01:27:54 +0000207 /* Update the size of DEST. */
208 dest->length = src->length;
209 return 1;
210}
211
212/* Copies SRC, a NUL-terminated string, into DEST. Returns 1 on
213 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
214 and returns 0. */
215
216int
sewardj4f2683a2008-10-26 11:53:30 +0000217dyn_string_copy_cstr (dyn_string_t dest, const char *src)
sewardjde4a1d02002-03-22 01:27:54 +0000218{
sewardj4f2683a2008-10-26 11:53:30 +0000219 int length = strlen (src);
sewardjde4a1d02002-03-22 01:27:54 +0000220 /* Make room in DEST. */
221 if (dyn_string_resize (dest, length) == NULL)
222 return 0;
223 /* Copy DEST into SRC. */
sewardj4f2683a2008-10-26 11:53:30 +0000224 strcpy (dest->s, src);
sewardjde4a1d02002-03-22 01:27:54 +0000225 /* Update the size of DEST. */
226 dest->length = length;
227 return 1;
228}
229
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
233 returns 0. */
234
235int
sewardj4f2683a2008-10-26 11:53:30 +0000236dyn_string_prepend (dyn_string_t dest, dyn_string_t src)
sewardjde4a1d02002-03-22 01:27:54 +0000237{
238 return dyn_string_insert (dest, 0, src);
239}
240
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. */
244
245int
sewardj4f2683a2008-10-26 11:53:30 +0000246dyn_string_prepend_cstr (dyn_string_t dest, const char *src)
sewardjde4a1d02002-03-22 01:27:54 +0000247{
248 return dyn_string_insert_cstr (dest, 0, src);
249}
250
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
254 and returns 0. */
255
256int
sewardj4f2683a2008-10-26 11:53:30 +0000257dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src)
sewardjde4a1d02002-03-22 01:27:54 +0000258{
259 int i;
260
261 if (src == dest)
sewardj4f2683a2008-10-26 11:53:30 +0000262 abort ();
sewardjde4a1d02002-03-22 01:27:54 +0000263
264 if (dyn_string_resize (dest, dest->length + src->length) == NULL)
265 return 0;
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. */
sewardj4f2683a2008-10-26 11:53:30 +0000270 strncpy (dest->s + pos, src->s, src->length);
sewardjde4a1d02002-03-22 01:27:54 +0000271 /* Compute the new length. */
272 dest->length += src->length;
273 return 1;
274}
275
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
279 and returns 0. */
280
281int
sewardj4f2683a2008-10-26 11:53:30 +0000282dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src)
sewardjde4a1d02002-03-22 01:27:54 +0000283{
284 int i;
sewardj4f2683a2008-10-26 11:53:30 +0000285 int length = strlen (src);
sewardjde4a1d02002-03-22 01:27:54 +0000286
287 if (dyn_string_resize (dest, dest->length + length) == NULL)
288 return 0;
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. */
sewardj4f2683a2008-10-26 11:53:30 +0000293 strncpy (dest->s + pos, src, length);
sewardjde4a1d02002-03-22 01:27:54 +0000294 /* Compute the new length. */
295 dest->length += length;
296 return 1;
297}
298
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. */
302
303int
sewardj4f2683a2008-10-26 11:53:30 +0000304dyn_string_insert_char (dyn_string_t dest, int pos, int c)
sewardjde4a1d02002-03-22 01:27:54 +0000305{
306 int i;
307
308 if (dyn_string_resize (dest, dest->length + 1) == NULL)
309 return 0;
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. */
314 dest->s[pos] = c;
315 /* Compute the new length. */
316 ++dest->length;
317 return 1;
318}
319
320/* Append S to DS, resizing DS if necessary. Returns 1 on success.
321 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
322 returns 0. */
323
324int
sewardj4f2683a2008-10-26 11:53:30 +0000325dyn_string_append (dyn_string_t dest, dyn_string_t s)
sewardjde4a1d02002-03-22 01:27:54 +0000326{
327 if (dyn_string_resize (dest, dest->length + s->length) == 0)
328 return 0;
sewardj4f2683a2008-10-26 11:53:30 +0000329 strcpy (dest->s + dest->length, s->s);
sewardjde4a1d02002-03-22 01:27:54 +0000330 dest->length += s->length;
331 return 1;
332}
333
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. */
337
338int
sewardj4f2683a2008-10-26 11:53:30 +0000339dyn_string_append_cstr (dyn_string_t dest, const char *s)
sewardjde4a1d02002-03-22 01:27:54 +0000340{
sewardj4f2683a2008-10-26 11:53:30 +0000341 int len = strlen (s);
sewardjde4a1d02002-03-22 01:27:54 +0000342
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)
346 return 0;
sewardj4f2683a2008-10-26 11:53:30 +0000347 strcpy (dest->s + dest->length, s);
sewardjde4a1d02002-03-22 01:27:54 +0000348 dest->length += len;
349 return 1;
350}
351
florian8dc79ce2011-12-10 16:00:25 +0000352/* Appends C to the end of DEST. Returns 1 on success. On failure,
sewardjde4a1d02002-03-22 01:27:54 +0000353 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
354
355int
sewardj4f2683a2008-10-26 11:53:30 +0000356dyn_string_append_char (dyn_string_t dest, int c)
sewardjde4a1d02002-03-22 01:27:54 +0000357{
358 /* Make room for the extra character. */
359 if (dyn_string_resize (dest, dest->length + 1) == NULL)
360 return 0;
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. */
366 ++(dest->length);
367 return 1;
368}
369
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. */
375
376int
sewardj4f2683a2008-10-26 11:53:30 +0000377dyn_string_substring (dyn_string_t dest, dyn_string_t src,
378 int start, int end)
sewardjde4a1d02002-03-22 01:27:54 +0000379{
380 int i;
381 int length = end - start;
382
sewardj4f2683a2008-10-26 11:53:30 +0000383 if (start > end || start > src->length || end > src->length)
384 abort ();
sewardjde4a1d02002-03-22 01:27:54 +0000385
386 /* Make room for the substring. */
387 if (dyn_string_resize (dest, length) == NULL)
388 return 0;
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;
396
397 return 1;
398}
399
400/* Returns non-zero if DS1 and DS2 have the same contents. */
401
402int
sewardj4f2683a2008-10-26 11:53:30 +0000403dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2)
sewardjde4a1d02002-03-22 01:27:54 +0000404{
405 /* If DS1 and DS2 have different lengths, they must not be the same. */
406 if (ds1->length != ds2->length)
407 return 0;
408 else
sewardj4f2683a2008-10-26 11:53:30 +0000409 return !strcmp (ds1->s, ds2->s);
sewardjde4a1d02002-03-22 01:27:54 +0000410}