]> git.ipfire.org Git - thirdparty/gcc.git/blob - libiberty/dyn-string.c
In include,
[thirdparty/gcc.git] / libiberty / dyn-string.c
1 /* An abstract string datatype.
2 Copyright (C) 1998, 1999, 2000 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 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.
16
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. */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33
34 #include "libiberty.h"
35 #include "dyn-string.h"
36
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. */
43
44 void
45 dyn_string_init (ds_struct_ptr, space)
46 struct dyn_string *ds_struct_ptr;
47 int space;
48 {
49 /* We need at least one byte in which to store the terminating NUL. */
50 if (space == 0)
51 space = 1;
52
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';
57 }
58
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
61 increased to 1. */
62
63 dyn_string_t
64 dyn_string_new (space)
65 int space;
66 {
67 dyn_string_t result = (dyn_string_t) xmalloc (sizeof (struct dyn_string));
68 dyn_string_init (result, space);
69 return result;
70 }
71
72 /* Free the memory used by DS. */
73
74 void
75 dyn_string_delete (ds)
76 dyn_string_t ds;
77 {
78 free (ds->s);
79 free (ds);
80 }
81
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. */
85
86 char*
87 dyn_string_release (ds)
88 dyn_string_t ds;
89 {
90 /* Store the old buffer. */
91 char* result = ds->s;
92 /* The buffer is no longer owned by DS. */
93 ds->s = NULL;
94 /* Reinitialize DS to the empty string. */
95 dyn_string_init (ds, 0);
96 /* Return the old buffer. */
97 return result;
98 }
99
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. */
103
104 dyn_string_t
105 dyn_string_resize (ds, space)
106 dyn_string_t ds;
107 int space;
108 {
109 int new_allocated = ds->allocated;
110
111 /* Increase SPACE to hold the NUL termination. */
112 ++space;
113
114 while (space > new_allocated)
115 new_allocated *= 2;
116
117 if (new_allocated != ds->allocated)
118 {
119 /* We actually need more space. */
120 ds->allocated = new_allocated;
121 ds->s = (char *) xrealloc (ds->s, ds->allocated);
122 }
123
124 return ds;
125 }
126
127 /* Sets the contents of DS to the empty string. */
128
129 void
130 dyn_string_clear (ds)
131 dyn_string_t ds;
132 {
133 /* A dyn_string always has room for at least the NUL terminator. */
134 ds->s[0] = '\0';
135 ds->length = 0;
136 }
137
138 /* Makes the contents of DEST the same as the contents of SRC. DEST
139 and SRC must be distinct. */
140
141 void
142 dyn_string_copy (dest, src)
143 dyn_string_t dest;
144 dyn_string_t src;
145 {
146 if (dest == src)
147 abort ();
148
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;
155 }
156
157 /* Copies SRC, a NUL-terminated string, into DEST. */
158
159 void
160 dyn_string_copy_cstr (dest, src)
161 dyn_string_t dest;
162 const char *src;
163 {
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;
171 }
172
173 /* Inserts SRC at the beginning of DEST. DEST is expanded as
174 necessary. SRC and DEST must be distinct. */
175
176 void
177 dyn_string_prepend (dest, src)
178 dyn_string_t dest;
179 dyn_string_t src;
180 {
181 dyn_string_insert (dest, 0, src);
182 }
183
184 /* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
185 DEST is expanded as necessary. */
186
187 void
188 dyn_string_prepend_cstr (dest, src)
189 dyn_string_t dest;
190 const char *src;
191 {
192 dyn_string_insert_cstr (dest, 0, src);
193 }
194
195 /* Inserts SRC into DEST starting at position POS. DEST is expanded as
196 necessary. SRC and DEST must be distinct. */
197
198 void
199 dyn_string_insert (dest, pos, src)
200 dyn_string_t dest;
201 int pos;
202 dyn_string_t src;
203 {
204 int i;
205
206 if (src == dest)
207 abort ();
208
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;
217 }
218
219 /* Inserts SRC, a NUL-terminated string, into DEST starting at
220 position POS. DEST is expanded as necessary. */
221
222 void
223 dyn_string_insert_cstr (dest, pos, src)
224 dyn_string_t dest;
225 int pos;
226 const char *src;
227 {
228 int i;
229 int length = strlen (src);
230
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;
239 }
240
241 /* Append S to DS, resizing DS if necessary. Returns DS. */
242
243 dyn_string_t
244 dyn_string_append (ds, s)
245 dyn_string_t ds;
246 dyn_string_t s;
247 {
248 dyn_string_resize (ds, ds->length + s->length);
249 strcpy (ds->s + ds->length, s->s);
250 ds->length += s->length;
251 return ds;
252 }
253
254 /* Append the NUL-terminated string S to DS, resizing DS if necessary.
255 Returns DS. */
256
257 dyn_string_t
258 dyn_string_append_cstr (ds, s)
259 dyn_string_t ds;
260 const char *s;
261 {
262 int len = strlen (s);
263
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);
268 ds->length += len;
269
270 return ds;
271 }
272
273 /* Appends C to the end of DS. */
274
275 dyn_string_t
276 dyn_string_append_char (ds, c)
277 dyn_string_t ds;
278 char c;
279 {
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. */
287 ++(ds->length);
288 return ds;
289 }
290
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. */
294
295 void
296 dyn_string_substring (dest, src, start, end)
297 dyn_string_t dest;
298 dyn_string_t src;
299 int start;
300 int end;
301 {
302 int i;
303 int length = end - start;
304
305 if (start > end || start > src->length || end > src->length)
306 abort ();
307
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;
317 }
318
319 /* Returns non-zero if DS1 and DS2 have the same contents. */
320
321 int
322 dyn_string_eq (ds1, ds2)
323 dyn_string_t ds1;
324 dyn_string_t ds2;
325 {
326 /* If DS1 and DS2 have different lengths, they must not be the same. */
327 if (ds1->length != ds2->length)
328 return 0;
329 else
330 return !strcmp (ds1->s, ds2->s);
331 }