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. */
36 #include "libiberty.h"
37 #include "dyn-string.h"
39 /* Performs in-place initialization of a dyn_string struct. This
40 function can be used with a dyn_string struct on the stack or
41 embedded in another object. The contents of of the string itself
42 are still dynamically allocated. The string initially is capable
43 of holding at least SPACE characeters, including the terminating
44 NUL. If SPACE is 0, it will silently be increated to 1. */
47 dyn_string_init (ds_struct_ptr
, space
)
48 struct dyn_string
*ds_struct_ptr
;
51 /* We need at least one byte in which to store the terminating NUL. */
55 ds_struct_ptr
->allocated
= space
;
56 ds_struct_ptr
->s
= (char *) xmalloc (space
);
57 ds_struct_ptr
->length
= 0;
58 ds_struct_ptr
->s
[0] = '\0';
61 /* Create a new dynamic string capable of holding at least SPACE characters,
62 including the terminating NUL. If SPACE is 0, it will be silently
66 dyn_string_new (space
)
69 dyn_string_t result
= (dyn_string_t
) xmalloc (sizeof (struct dyn_string
));
70 dyn_string_init (result
, space
);
74 /* Free the memory used by DS. */
77 dyn_string_delete (ds
)
84 /* Returns the contents of DS in a buffer allocated with malloc. It
85 is the caller's responsibility to deallocate the buffer using free.
86 DS is then set to the empty string. */
89 dyn_string_release (ds
)
92 /* Store the old buffer. */
94 /* The buffer is no longer owned by DS. */
96 /* Reinitialize DS to the empty string. */
97 dyn_string_init (ds
, 0);
98 /* Return the old buffer. */
102 /* Increase the capacity of DS so it can hold at least SPACE
103 characters, plus the terminating NUL. This function will not (at
104 present) reduce the capacity of DS. */
107 dyn_string_resize (ds
, space
)
111 int new_allocated
= ds
->allocated
;
113 /* Increase SPACE to hold the NUL termination. */
116 while (space
> new_allocated
)
119 if (new_allocated
!= ds
->allocated
)
121 /* We actually need more space. */
122 ds
->allocated
= new_allocated
;
123 ds
->s
= (char *) xrealloc (ds
->s
, ds
->allocated
);
129 /* Sets the contents of DS to the empty string. */
132 dyn_string_clear (ds
)
135 /* A dyn_string always has room for at least the NUL terminator. */
140 /* Makes the contents of DEST the same as the contents of SRC. DEST
141 and SRC must be distinct. */
144 dyn_string_copy (dest
, src
)
151 /* Make room in DEST. */
152 dyn_string_resize (dest
, src
->length
);
153 /* Copy DEST into SRC. */
154 strcpy (dest
->s
, src
->s
);
155 /* Update the size of DEST. */
156 dest
->length
= src
->length
;
159 /* Copies SRC, a NUL-terminated string, into DEST. */
162 dyn_string_copy_cstr (dest
, src
)
166 int length
= strlen (src
);
167 /* Make room in DEST. */
168 dyn_string_resize (dest
, length
);
169 /* Copy DEST into SRC. */
170 strcpy (dest
->s
, src
);
171 /* Update the size of DEST. */
172 dest
->length
= length
;
175 /* Inserts SRC at the beginning of DEST. DEST is expanded as
176 necessary. SRC and DEST must be distinct. */
179 dyn_string_prepend (dest
, src
)
183 dyn_string_insert (dest
, 0, src
);
186 /* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
187 DEST is expanded as necessary. */
190 dyn_string_prepend_cstr (dest
, src
)
194 dyn_string_insert_cstr (dest
, 0, src
);
197 /* Inserts SRC into DEST starting at position POS. DEST is expanded as
198 necessary. SRC and DEST must be distinct. */
201 dyn_string_insert (dest
, pos
, src
)
211 dyn_string_resize (dest
, dest
->length
+ src
->length
);
212 /* Make room for the insertion. Be sure to copy the NUL. */
213 for (i
= dest
->length
; i
>= pos
; --i
)
214 dest
->s
[i
+ src
->length
] = dest
->s
[i
];
215 /* Splice in the new stuff. */
216 strncpy (dest
->s
+ pos
, src
->s
, src
->length
);
217 /* Compute the new length. */
218 dest
->length
+= src
->length
;
221 /* Inserts SRC, a NUL-terminated string, into DEST starting at
222 position POS. DEST is expanded as necessary. */
225 dyn_string_insert_cstr (dest
, pos
, src
)
231 int length
= strlen (src
);
233 dyn_string_resize (dest
, dest
->length
+ length
);
234 /* Make room for the insertion. Be sure to copy the NUL. */
235 for (i
= dest
->length
; i
>= pos
; --i
)
236 dest
->s
[i
+ length
] = dest
->s
[i
];
237 /* Splice in the new stuff. */
238 strncpy (dest
->s
+ pos
, src
, length
);
239 /* Compute the new length. */
240 dest
->length
+= length
;
243 /* Append S to DS, resizing DS if necessary. Returns DS. */
246 dyn_string_append (ds
, s
)
250 dyn_string_resize (ds
, ds
->length
+ s
->length
);
251 strcpy (ds
->s
+ ds
->length
, s
->s
);
252 ds
->length
+= s
->length
;
256 /* Append the NUL-terminated string S to DS, resizing DS if necessary.
260 dyn_string_append_cstr (ds
, s
)
264 int len
= strlen (s
);
266 /* The new length is the old length plus the size of our string, plus
267 one for the null at the end. */
268 dyn_string_resize (ds
, ds
->length
+ len
);
269 strcpy (ds
->s
+ ds
->length
, s
);
275 /* Appends C to the end of DS. */
278 dyn_string_append_char (ds
, c
)
282 /* Make room for the extra character. */
283 dyn_string_resize (ds
, ds
->length
+ 1);
284 /* Append the character; it will overwrite the old NUL. */
285 ds
->s
[ds
->length
] = c
;
286 /* Add a new NUL at the end. */
287 ds
->s
[ds
->length
+ 1] = '\0';
288 /* Update the length. */
293 /* Sets the contents of DEST to the substring of SRC starting at START
294 and ending before END. START must be less than or equal to END,
295 and both must be between zero and the length of SRC, inclusive. */
298 dyn_string_substring (dest
, src
, start
, end
)
305 int length
= end
- start
;
307 if (start
> end
|| start
> src
->length
|| end
> src
->length
)
310 /* Make room for the substring. */
311 dyn_string_resize (dest
, length
);
312 /* Copy the characters in the substring, */
313 for (i
= length
; --i
>= 0; )
314 dest
->s
[i
] = src
->s
[start
+ i
];
315 /* NUL-terimate the result. */
316 dest
->s
[length
] = '\0';
317 /* Record the length of the substring. */
318 dest
->length
= length
;
321 /* Returns non-zero if DS1 and DS2 have the same contents. */
324 dyn_string_eq (ds1
, ds2
)
328 /* If DS1 and DS2 have different lengths, they must not be the same. */
329 if (ds1
->length
!= ds2
->length
)
332 return !strcmp (ds1
->s
, ds2
->s
);