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
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
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
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
28the Free Software Foundation, 51 Franklin Street - Fifth Floor,
29Boston, MA 02110-1301, USA.  */
30
31#if 0 /* in valgrind */
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35#endif /* ! in valgrind */
36
37#if 0 /* in valgrind */
38#include <stdio.h>
39#endif /* ! in valgrind */
40
41#if 0 /* in valgrind */
42#ifdef HAVE_STRING_H
43#include <string.h>
44#endif
45#endif /* ! in valgrind */
46
47#if 0 /* in valgrind */
48#ifdef HAVE_STDLIB_H
49#include <stdlib.h>
50#endif
51#endif /* ! in valgrind */
52
53#if 0 /* in valgrind */
54#include "libiberty.h"
55#endif /* ! in valgrind */
56
57#include "vg_libciface.h"
58
59#include "dyn-string.h"
60
61/* 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
72dyn_string_init (struct dyn_string *ds_struct_ptr, int space)
73{
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
83  ds_struct_ptr->s = XNEWVEC (char, space);
84#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
99dyn_string_new (int space)
100{
101  dyn_string_t result;
102#ifdef RETURN_ON_ALLOCATION_FAILURE
103  result = (dyn_string_t) malloc (sizeof (struct dyn_string));
104  if (result == NULL)
105    return NULL;
106  if (!dyn_string_init (result, space))
107    {
108      free (result);
109      return NULL;
110    }
111#else
112  result = XNEW (struct dyn_string);
113  dyn_string_init (result, space);
114#endif
115  return result;
116}
117
118/* Free the memory used by DS.  */
119
120void
121dyn_string_delete (dyn_string_t ds)
122{
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*
132dyn_string_release (dyn_string_t ds)
133{
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
152dyn_string_resize (dyn_string_t ds, int space)
153{
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
168      ds->s = (char *) realloc (ds->s, ds->allocated);
169      if (ds->s == NULL)
170	{
171	  free (ds);
172	  return NULL;
173	}
174#else
175      ds->s = XRESIZEVEC (char, ds->s, ds->allocated);
176#endif
177    }
178
179  return ds;
180}
181
182/* Sets the contents of DS to the empty string.  */
183
184void
185dyn_string_clear (dyn_string_t ds)
186{
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
197dyn_string_copy (dyn_string_t dest, dyn_string_t src)
198{
199  if (dest == src)
200    abort ();
201
202  /* Make room in DEST.  */
203  if (dyn_string_resize (dest, src->length) == NULL)
204    return 0;
205  /* Copy DEST into SRC.  */
206  strcpy (dest->s, src->s);
207  /* 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
217dyn_string_copy_cstr (dyn_string_t dest, const char *src)
218{
219  int length = strlen (src);
220  /* Make room in DEST.  */
221  if (dyn_string_resize (dest, length) == NULL)
222    return 0;
223  /* Copy DEST into SRC.  */
224  strcpy (dest->s, src);
225  /* 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
236dyn_string_prepend (dyn_string_t dest, dyn_string_t src)
237{
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
246dyn_string_prepend_cstr (dyn_string_t dest, const char *src)
247{
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
257dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src)
258{
259  int i;
260
261  if (src == dest)
262    abort ();
263
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.  */
270  strncpy (dest->s + pos, src->s, src->length);
271  /* 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
282dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src)
283{
284  int i;
285  int length = strlen (src);
286
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.  */
293  strncpy (dest->s + pos, src, length);
294  /* 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
304dyn_string_insert_char (dyn_string_t dest, int pos, int c)
305{
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
325dyn_string_append (dyn_string_t dest, dyn_string_t s)
326{
327  if (dyn_string_resize (dest, dest->length + s->length) == 0)
328    return 0;
329  strcpy (dest->s + dest->length, s->s);
330  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
339dyn_string_append_cstr (dyn_string_t dest, const char *s)
340{
341  int len = strlen (s);
342
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;
347  strcpy (dest->s + dest->length, s);
348  dest->length += len;
349  return 1;
350}
351
352/* Appends C to the end of DEST.  Returns 1 on success.  On failure,
353   if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0.  */
354
355int
356dyn_string_append_char (dyn_string_t dest, int c)
357{
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
377dyn_string_substring (dyn_string_t dest, dyn_string_t src,
378                      int start, int end)
379{
380  int i;
381  int length = end - start;
382
383  if (start > end || start > src->length || end > src->length)
384    abort ();
385
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
403dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2)
404{
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
409    return !strcmp (ds1->s, ds2->s);
410}
411