blob: 0836971d594f554916b661fc087a0285ec965313 [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
sewardj4f2683a2008-10-26 11:53:30 +000031/////////////////////////////
32#include <string.h>
33#include <stdlib.h>
34/////////////////////////////
sewardjde4a1d02002-03-22 01:27:54 +000035#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
sewardj4f2683a2008-10-26 11:53:30 +000039#include <stdio.h>
40
41#ifdef HAVE_STRING_H
sewardjde4a1d02002-03-22 01:27:54 +000042#include <string.h>
sewardj4f2683a2008-10-26 11:53:30 +000043#endif
sewardjde4a1d02002-03-22 01:27:54 +000044
sewardj4f2683a2008-10-26 11:53:30 +000045#ifdef HAVE_STDLIB_H
sewardjde4a1d02002-03-22 01:27:54 +000046#include <stdlib.h>
sewardj4f2683a2008-10-26 11:53:30 +000047#endif
sewardjde4a1d02002-03-22 01:27:54 +000048
sewardj4f2683a2008-10-26 11:53:30 +000049#include "libiberty.h"
sewardjde4a1d02002-03-22 01:27:54 +000050#include "dyn-string.h"
51
sewardjde4a1d02002-03-22 01:27:54 +000052/* Performs in-place initialization of a dyn_string struct. This
53 function can be used with a dyn_string struct on the stack or
54 embedded in another object. The contents of of the string itself
55 are still dynamically allocated. The string initially is capable
56 of holding at least SPACE characeters, including the terminating
57 NUL. If SPACE is 0, it will silently be increated to 1.
58
59 If RETURN_ON_ALLOCATION_FAILURE is defined and memory allocation
60 fails, returns 0. Otherwise returns 1. */
61
62int
sewardj4f2683a2008-10-26 11:53:30 +000063dyn_string_init (struct dyn_string *ds_struct_ptr, int space)
sewardjde4a1d02002-03-22 01:27:54 +000064{
65 /* We need at least one byte in which to store the terminating NUL. */
66 if (space == 0)
67 space = 1;
68
69#ifdef RETURN_ON_ALLOCATION_FAILURE
70 ds_struct_ptr->s = (char *) malloc (space);
71 if (ds_struct_ptr->s == NULL)
72 return 0;
73#else
sewardj4f2683a2008-10-26 11:53:30 +000074 ds_struct_ptr->s = XNEWVEC (char, space);
sewardjde4a1d02002-03-22 01:27:54 +000075#endif
76 ds_struct_ptr->allocated = space;
77 ds_struct_ptr->length = 0;
78 ds_struct_ptr->s[0] = '\0';
79
80 return 1;
81}
82
83/* Create a new dynamic string capable of holding at least SPACE
84 characters, including the terminating NUL. If SPACE is 0, it will
85 be silently increased to 1. If RETURN_ON_ALLOCATION_FAILURE is
86 defined and memory allocation fails, returns NULL. Otherwise
87 returns the newly allocated string. */
88
89dyn_string_t
sewardj4f2683a2008-10-26 11:53:30 +000090dyn_string_new (int space)
sewardjde4a1d02002-03-22 01:27:54 +000091{
92 dyn_string_t result;
93#ifdef RETURN_ON_ALLOCATION_FAILURE
sewardj4f2683a2008-10-26 11:53:30 +000094 result = (dyn_string_t) malloc (sizeof (struct dyn_string));
sewardjde4a1d02002-03-22 01:27:54 +000095 if (result == NULL)
96 return NULL;
97 if (!dyn_string_init (result, space))
98 {
99 free (result);
100 return NULL;
101 }
102#else
sewardj4f2683a2008-10-26 11:53:30 +0000103 result = XNEW (struct dyn_string);
sewardjde4a1d02002-03-22 01:27:54 +0000104 dyn_string_init (result, space);
105#endif
106 return result;
107}
108
109/* Free the memory used by DS. */
110
111void
sewardj4f2683a2008-10-26 11:53:30 +0000112dyn_string_delete (dyn_string_t ds)
sewardjde4a1d02002-03-22 01:27:54 +0000113{
114 free (ds->s);
115 free (ds);
116}
117
118/* Returns the contents of DS in a buffer allocated with malloc. It
119 is the caller's responsibility to deallocate the buffer using free.
120 DS is then set to the empty string. Deletes DS itself. */
121
122char*
sewardj4f2683a2008-10-26 11:53:30 +0000123dyn_string_release (dyn_string_t ds)
sewardjde4a1d02002-03-22 01:27:54 +0000124{
125 /* Store the old buffer. */
126 char* result = ds->s;
127 /* The buffer is no longer owned by DS. */
128 ds->s = NULL;
129 /* Delete DS. */
130 free (ds);
131 /* Return the old buffer. */
132 return result;
133}
134
135/* Increase the capacity of DS so it can hold at least SPACE
136 characters, plus the terminating NUL. This function will not (at
137 present) reduce the capacity of DS. Returns DS on success.
138
139 If RETURN_ON_ALLOCATION_FAILURE is defined and a memory allocation
140 operation fails, deletes DS and returns NULL. */
141
142dyn_string_t
sewardj4f2683a2008-10-26 11:53:30 +0000143dyn_string_resize (dyn_string_t ds, int space)
sewardjde4a1d02002-03-22 01:27:54 +0000144{
145 int new_allocated = ds->allocated;
146
147 /* Increase SPACE to hold the NUL termination. */
148 ++space;
149
150 /* Increase allocation by factors of two. */
151 while (space > new_allocated)
152 new_allocated *= 2;
153
154 if (new_allocated != ds->allocated)
155 {
156 ds->allocated = new_allocated;
157 /* We actually need more space. */
158#ifdef RETURN_ON_ALLOCATION_FAILURE
sewardj4f2683a2008-10-26 11:53:30 +0000159 ds->s = (char *) realloc (ds->s, ds->allocated);
sewardjde4a1d02002-03-22 01:27:54 +0000160 if (ds->s == NULL)
161 {
162 free (ds);
163 return NULL;
164 }
165#else
sewardj4f2683a2008-10-26 11:53:30 +0000166 ds->s = XRESIZEVEC (char, ds->s, ds->allocated);
sewardjde4a1d02002-03-22 01:27:54 +0000167#endif
168 }
169
170 return ds;
171}
172
173/* Sets the contents of DS to the empty string. */
174
175void
sewardj4f2683a2008-10-26 11:53:30 +0000176dyn_string_clear (dyn_string_t ds)
sewardjde4a1d02002-03-22 01:27:54 +0000177{
178 /* A dyn_string always has room for at least the NUL terminator. */
179 ds->s[0] = '\0';
180 ds->length = 0;
181}
182
183/* Makes the contents of DEST the same as the contents of SRC. DEST
184 and SRC must be distinct. Returns 1 on success. On failure, if
185 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
186
187int
sewardj4f2683a2008-10-26 11:53:30 +0000188dyn_string_copy (dyn_string_t dest, dyn_string_t src)
sewardjde4a1d02002-03-22 01:27:54 +0000189{
190 if (dest == src)
sewardj4f2683a2008-10-26 11:53:30 +0000191 abort ();
sewardjde4a1d02002-03-22 01:27:54 +0000192
193 /* Make room in DEST. */
194 if (dyn_string_resize (dest, src->length) == NULL)
195 return 0;
196 /* Copy DEST into SRC. */
sewardj4f2683a2008-10-26 11:53:30 +0000197 strcpy (dest->s, src->s);
sewardjde4a1d02002-03-22 01:27:54 +0000198 /* Update the size of DEST. */
199 dest->length = src->length;
200 return 1;
201}
202
203/* Copies SRC, a NUL-terminated string, into DEST. Returns 1 on
204 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
205 and returns 0. */
206
207int
sewardj4f2683a2008-10-26 11:53:30 +0000208dyn_string_copy_cstr (dyn_string_t dest, const char *src)
sewardjde4a1d02002-03-22 01:27:54 +0000209{
sewardj4f2683a2008-10-26 11:53:30 +0000210 int length = strlen (src);
sewardjde4a1d02002-03-22 01:27:54 +0000211 /* Make room in DEST. */
212 if (dyn_string_resize (dest, length) == NULL)
213 return 0;
214 /* Copy DEST into SRC. */
sewardj4f2683a2008-10-26 11:53:30 +0000215 strcpy (dest->s, src);
sewardjde4a1d02002-03-22 01:27:54 +0000216 /* Update the size of DEST. */
217 dest->length = length;
218 return 1;
219}
220
221/* Inserts SRC at the beginning of DEST. DEST is expanded as
222 necessary. SRC and DEST must be distinct. Returns 1 on success.
223 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
224 returns 0. */
225
226int
sewardj4f2683a2008-10-26 11:53:30 +0000227dyn_string_prepend (dyn_string_t dest, dyn_string_t src)
sewardjde4a1d02002-03-22 01:27:54 +0000228{
229 return dyn_string_insert (dest, 0, src);
230}
231
232/* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
233 DEST is expanded as necessary. Returns 1 on success. On failure,
234 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
235
236int
sewardj4f2683a2008-10-26 11:53:30 +0000237dyn_string_prepend_cstr (dyn_string_t dest, const char *src)
sewardjde4a1d02002-03-22 01:27:54 +0000238{
239 return dyn_string_insert_cstr (dest, 0, src);
240}
241
242/* Inserts SRC into DEST starting at position POS. DEST is expanded
243 as necessary. SRC and DEST must be distinct. Returns 1 on
244 success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
245 and returns 0. */
246
247int
sewardj4f2683a2008-10-26 11:53:30 +0000248dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src)
sewardjde4a1d02002-03-22 01:27:54 +0000249{
250 int i;
251
252 if (src == dest)
sewardj4f2683a2008-10-26 11:53:30 +0000253 abort ();
sewardjde4a1d02002-03-22 01:27:54 +0000254
255 if (dyn_string_resize (dest, dest->length + src->length) == NULL)
256 return 0;
257 /* Make room for the insertion. Be sure to copy the NUL. */
258 for (i = dest->length; i >= pos; --i)
259 dest->s[i + src->length] = dest->s[i];
260 /* Splice in the new stuff. */
sewardj4f2683a2008-10-26 11:53:30 +0000261 strncpy (dest->s + pos, src->s, src->length);
sewardjde4a1d02002-03-22 01:27:54 +0000262 /* Compute the new length. */
263 dest->length += src->length;
264 return 1;
265}
266
267/* Inserts SRC, a NUL-terminated string, into DEST starting at
268 position POS. DEST is expanded as necessary. Returns 1 on
269 success. On failure, RETURN_ON_ALLOCATION_FAILURE, deletes DEST
270 and returns 0. */
271
272int
sewardj4f2683a2008-10-26 11:53:30 +0000273dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src)
sewardjde4a1d02002-03-22 01:27:54 +0000274{
275 int i;
sewardj4f2683a2008-10-26 11:53:30 +0000276 int length = strlen (src);
sewardjde4a1d02002-03-22 01:27:54 +0000277
278 if (dyn_string_resize (dest, dest->length + length) == NULL)
279 return 0;
280 /* Make room for the insertion. Be sure to copy the NUL. */
281 for (i = dest->length; i >= pos; --i)
282 dest->s[i + length] = dest->s[i];
283 /* Splice in the new stuff. */
sewardj4f2683a2008-10-26 11:53:30 +0000284 strncpy (dest->s + pos, src, length);
sewardjde4a1d02002-03-22 01:27:54 +0000285 /* Compute the new length. */
286 dest->length += length;
287 return 1;
288}
289
290/* Inserts character C into DEST starting at position POS. DEST is
291 expanded as necessary. Returns 1 on success. On failure,
292 RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
293
294int
sewardj4f2683a2008-10-26 11:53:30 +0000295dyn_string_insert_char (dyn_string_t dest, int pos, int c)
sewardjde4a1d02002-03-22 01:27:54 +0000296{
297 int i;
298
299 if (dyn_string_resize (dest, dest->length + 1) == NULL)
300 return 0;
301 /* Make room for the insertion. Be sure to copy the NUL. */
302 for (i = dest->length; i >= pos; --i)
303 dest->s[i + 1] = dest->s[i];
304 /* Add the new character. */
305 dest->s[pos] = c;
306 /* Compute the new length. */
307 ++dest->length;
308 return 1;
309}
310
311/* Append S to DS, resizing DS if necessary. Returns 1 on success.
312 On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
313 returns 0. */
314
315int
sewardj4f2683a2008-10-26 11:53:30 +0000316dyn_string_append (dyn_string_t dest, dyn_string_t s)
sewardjde4a1d02002-03-22 01:27:54 +0000317{
318 if (dyn_string_resize (dest, dest->length + s->length) == 0)
319 return 0;
sewardj4f2683a2008-10-26 11:53:30 +0000320 strcpy (dest->s + dest->length, s->s);
sewardjde4a1d02002-03-22 01:27:54 +0000321 dest->length += s->length;
322 return 1;
323}
324
325/* Append the NUL-terminated string S to DS, resizing DS if necessary.
326 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
327 deletes DEST and returns 0. */
328
329int
sewardj4f2683a2008-10-26 11:53:30 +0000330dyn_string_append_cstr (dyn_string_t dest, const char *s)
sewardjde4a1d02002-03-22 01:27:54 +0000331{
sewardj4f2683a2008-10-26 11:53:30 +0000332 int len = strlen (s);
sewardjde4a1d02002-03-22 01:27:54 +0000333
334 /* The new length is the old length plus the size of our string, plus
335 one for the null at the end. */
336 if (dyn_string_resize (dest, dest->length + len) == NULL)
337 return 0;
sewardj4f2683a2008-10-26 11:53:30 +0000338 strcpy (dest->s + dest->length, s);
sewardjde4a1d02002-03-22 01:27:54 +0000339 dest->length += len;
340 return 1;
341}
342
343/* Appends C to the end of DEST. Returns 1 on success. On failiure,
344 if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
345
346int
sewardj4f2683a2008-10-26 11:53:30 +0000347dyn_string_append_char (dyn_string_t dest, int c)
sewardjde4a1d02002-03-22 01:27:54 +0000348{
349 /* Make room for the extra character. */
350 if (dyn_string_resize (dest, dest->length + 1) == NULL)
351 return 0;
352 /* Append the character; it will overwrite the old NUL. */
353 dest->s[dest->length] = c;
354 /* Add a new NUL at the end. */
355 dest->s[dest->length + 1] = '\0';
356 /* Update the length. */
357 ++(dest->length);
358 return 1;
359}
360
361/* Sets the contents of DEST to the substring of SRC starting at START
362 and ending before END. START must be less than or equal to END,
363 and both must be between zero and the length of SRC, inclusive.
364 Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
365 deletes DEST and returns 0. */
366
367int
sewardj4f2683a2008-10-26 11:53:30 +0000368dyn_string_substring (dyn_string_t dest, dyn_string_t src,
369 int start, int end)
sewardjde4a1d02002-03-22 01:27:54 +0000370{
371 int i;
372 int length = end - start;
373
sewardj4f2683a2008-10-26 11:53:30 +0000374 if (start > end || start > src->length || end > src->length)
375 abort ();
sewardjde4a1d02002-03-22 01:27:54 +0000376
377 /* Make room for the substring. */
378 if (dyn_string_resize (dest, length) == NULL)
379 return 0;
380 /* Copy the characters in the substring, */
381 for (i = length; --i >= 0; )
382 dest->s[i] = src->s[start + i];
383 /* NUL-terimate the result. */
384 dest->s[length] = '\0';
385 /* Record the length of the substring. */
386 dest->length = length;
387
388 return 1;
389}
390
391/* Returns non-zero if DS1 and DS2 have the same contents. */
392
393int
sewardj4f2683a2008-10-26 11:53:30 +0000394dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2)
sewardjde4a1d02002-03-22 01:27:54 +0000395{
396 /* If DS1 and DS2 have different lengths, they must not be the same. */
397 if (ds1->length != ds2->length)
398 return 0;
399 else
sewardj4f2683a2008-10-26 11:53:30 +0000400 return !strcmp (ds1->s, ds2->s);
sewardjde4a1d02002-03-22 01:27:54 +0000401}