1 /* An abstract string datatype.
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Mark Mitchell (mark@markmitchell.com).
5 This file is part of GNU CC.
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)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
34 #include "libiberty.h"
35 #include "dyn-string.h"
37 /* Performs in-place initialization of a dyn_string struct. This
38 function can be used with a dyn_string struct on the stack or
39 embedded in another object. The contents of of the string itself
40 are still dynamically allocated. The string initially is capable
41 of holding at least SPACE characeters, including the terminating
42 NUL. If SPACE is 0, it will silently be increated to 1. */
45 dyn_string_init (ds_struct_ptr, space)
46 struct dyn_string *ds_struct_ptr;
49 /* We need at least one byte in which to store the terminating NUL. */
53 ds_struct_ptr->allocated = space;
54 ds_struct_ptr->s = (char *) xmalloc (space);
55 ds_struct_ptr->length = 0;
56 ds_struct_ptr->s[0] = '\0';
59 /* Create a new dynamic string capable of holding at least SPACE characters,
60 including the terminating NUL. If SPACE is 0, it will be silently
64 dyn_string_new (space)
67 dyn_string_t result = (dyn_string_t) xmalloc (sizeof (struct dyn_string));
68 dyn_string_init (result, space);
72 /* Free the memory used by DS. */
75 dyn_string_delete (ds)
82 /* Returns the contents of DS in a buffer allocated with malloc. It
83 is the caller's responsibility to deallocate the buffer using free.
84 DS is then set to the empty string. */
87 dyn_string_release (ds)
90 /* Store the old buffer. */
92 /* The buffer is no longer owned by DS. */
94 /* Reinitialize DS to the empty string. */
95 dyn_string_init (ds, 0);
96 /* Return the old buffer. */
100 /* Increase the capacity of DS so it can hold at least SPACE
101 characters, plus the terminating NUL. This function will not (at
102 present) reduce the capacity of DS. */
105 dyn_string_resize (ds, space)
109 int new_allocated = ds->allocated;
111 /* Increase SPACE to hold the NUL termination. */
114 while (space > new_allocated)
117 if (new_allocated != ds->allocated)
119 /* We actually need more space. */
120 ds->allocated = new_allocated;
121 ds->s = (char *) xrealloc (ds->s, ds->allocated);
127 /* Sets the contents of DS to the empty string. */
130 dyn_string_clear (ds)
133 /* A dyn_string always has room for at least the NUL terminator. */
138 /* Makes the contents of DEST the same as the contents of SRC. DEST
139 and SRC must be distinct. */
142 dyn_string_copy (dest, src)
149 /* Make room in DEST. */
150 dyn_string_resize (dest, src->length);
151 /* Copy DEST into SRC. */
152 strcpy (dest->s, src->s);
153 /* Update the size of DEST. */
154 dest->length = src->length;
157 /* Copies SRC, a NUL-terminated string, into DEST. */
160 dyn_string_copy_cstr (dest, src)
164 int length = strlen (src);
165 /* Make room in DEST. */
166 dyn_string_resize (dest, length);
167 /* Copy DEST into SRC. */
168 strcpy (dest->s, src);
169 /* Update the size of DEST. */
170 dest->length = length;
173 /* Inserts SRC at the beginning of DEST. DEST is expanded as
174 necessary. SRC and DEST must be distinct. */
177 dyn_string_prepend (dest, src)
181 dyn_string_insert (dest, 0, src);
184 /* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
185 DEST is expanded as necessary. */
188 dyn_string_prepend_cstr (dest, src)
192 dyn_string_insert_cstr (dest, 0, src);
195 /* Inserts SRC into DEST starting at position POS. DEST is expanded as
196 necessary. SRC and DEST must be distinct. */
199 dyn_string_insert (dest, pos, src)
209 dyn_string_resize (dest, dest->length + src->length);
210 /* Make room for the insertion. Be sure to copy the NUL. */
211 for (i = dest->length; i >= pos; --i)
212 dest->s[i + src->length] = dest->s[i];
213 /* Splice in the new stuff. */
214 strncpy (dest->s + pos, src->s, src->length);
215 /* Compute the new length. */
216 dest->length += src->length;
219 /* Inserts SRC, a NUL-terminated string, into DEST starting at
220 position POS. DEST is expanded as necessary. */
223 dyn_string_insert_cstr (dest, pos, src)
229 int length = strlen (src);
231 dyn_string_resize (dest, dest->length + length);
232 /* Make room for the insertion. Be sure to copy the NUL. */
233 for (i = dest->length; i >= pos; --i)
234 dest->s[i + length] = dest->s[i];
235 /* Splice in the new stuff. */
236 strncpy (dest->s + pos, src, length);
237 /* Compute the new length. */
238 dest->length += length;
241 /* Append S to DS, resizing DS if necessary. Returns DS. */
244 dyn_string_append (ds, s)
248 dyn_string_resize (ds, ds->length + s->length);
249 strcpy (ds->s + ds->length, s->s);
250 ds->length += s->length;
254 /* Append the NUL-terminated string S to DS, resizing DS if necessary.
258 dyn_string_append_cstr (ds, s)
262 int len = strlen (s);
264 /* The new length is the old length plus the size of our string, plus
265 one for the null at the end. */
266 dyn_string_resize (ds, ds->length + len);
267 strcpy (ds->s + ds->length, s);
273 /* Appends C to the end of DS. */
276 dyn_string_append_char (ds, c)
280 /* Make room for the extra character. */
281 dyn_string_resize (ds, ds->length + 1);
282 /* Append the character; it will overwrite the old NUL. */
283 ds->s[ds->length] = c;
284 /* Add a new NUL at the end. */
285 ds->s[ds->length + 1] = '\0';
286 /* Update the length. */
291 /* Sets the contents of DEST to the substring of SRC starting at START
292 and ending before END. START must be less than or equal to END,
293 and both must be between zero and the length of SRC, inclusive. */
296 dyn_string_substring (dest, src, start, end)
303 int length = end - start;
305 if (start > end || start > src->length || end > src->length)
308 /* Make room for the substring. */
309 dyn_string_resize (dest, length);
310 /* Copy the characters in the substring, */
311 for (i = length; --i >= 0; )
312 dest->s[i] = src->s[start + i];
313 /* NUL-terimate the result. */
314 dest->s[length] = '\0';
315 /* Record the length of the substring. */
316 dest->length = length;
319 /* Returns non-zero if DS1 and DS2 have the same contents. */
322 dyn_string_eq (ds1, ds2)
326 /* If DS1 and DS2 have different lengths, they must not be the same. */
327 if (ds1->length != ds2->length)
330 return !strcmp (ds1->s, ds2->s);