sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | /* An abstract string datatype. |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 2 | Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 12 | In addition to the permissions in the GNU General Public License, the |
| 13 | Free Software Foundation gives you unlimited permission to link the |
| 14 | compiled version of this file into combinations with other programs, |
| 15 | and to distribute those combinations without any restriction coming |
| 16 | from the use of this file. (The General Public License restrictions |
| 17 | do apply in other respects; for example, they cover modification of |
| 18 | the file, and distribution when not linked into a combined |
| 19 | executable.) |
| 20 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 21 | GNU CC is distributed in the hope that it will be useful, |
| 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | GNU General Public License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU General Public License |
| 27 | along with GNU CC; see the file COPYING. If not, write to |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 28 | the Free Software Foundation, 51 Franklin Street - Fifth Floor, |
| 29 | Boston, MA 02110-1301, USA. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 30 | |
sewardj | 9a2ce3d | 2008-10-27 09:54:14 +0000 | [diff] [blame^] | 31 | #if 0 /* in valgrind */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 32 | #ifdef HAVE_CONFIG_H |
| 33 | #include "config.h" |
| 34 | #endif |
sewardj | 9a2ce3d | 2008-10-27 09:54:14 +0000 | [diff] [blame^] | 35 | #endif /* ! in valgrind */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 36 | |
sewardj | 9a2ce3d | 2008-10-27 09:54:14 +0000 | [diff] [blame^] | 37 | #if 0 /* in valgrind */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
sewardj | 9a2ce3d | 2008-10-27 09:54:14 +0000 | [diff] [blame^] | 39 | #endif /* ! in valgrind */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 40 | |
sewardj | 9a2ce3d | 2008-10-27 09:54:14 +0000 | [diff] [blame^] | 41 | #if 0 /* in valgrind */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 42 | #ifdef HAVE_STRING_H |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 43 | #include <string.h> |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 44 | #endif |
sewardj | 9a2ce3d | 2008-10-27 09:54:14 +0000 | [diff] [blame^] | 45 | #endif /* ! in valgrind */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 46 | |
sewardj | 9a2ce3d | 2008-10-27 09:54:14 +0000 | [diff] [blame^] | 47 | #if 0 /* in valgrind */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 48 | #ifdef HAVE_STDLIB_H |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 49 | #include <stdlib.h> |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 50 | #endif |
sewardj | 9a2ce3d | 2008-10-27 09:54:14 +0000 | [diff] [blame^] | 51 | #endif /* ! in valgrind */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 52 | |
sewardj | 9a2ce3d | 2008-10-27 09:54:14 +0000 | [diff] [blame^] | 53 | #if 0 /* in valgrind */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 54 | #include "libiberty.h" |
sewardj | 9a2ce3d | 2008-10-27 09:54:14 +0000 | [diff] [blame^] | 55 | #endif /* ! in valgrind */ |
| 56 | |
| 57 | #include "vg_libciface.h" |
| 58 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 59 | #include "dyn-string.h" |
| 60 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 71 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 72 | dyn_string_init (struct dyn_string *ds_struct_ptr, int space) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 83 | ds_struct_ptr->s = XNEWVEC (char, space); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 98 | dyn_string_t |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 99 | dyn_string_new (int space) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 100 | { |
| 101 | dyn_string_t result; |
| 102 | #ifdef RETURN_ON_ALLOCATION_FAILURE |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 103 | result = (dyn_string_t) malloc (sizeof (struct dyn_string)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 104 | if (result == NULL) |
| 105 | return NULL; |
| 106 | if (!dyn_string_init (result, space)) |
| 107 | { |
| 108 | free (result); |
| 109 | return NULL; |
| 110 | } |
| 111 | #else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 112 | result = XNEW (struct dyn_string); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 113 | dyn_string_init (result, space); |
| 114 | #endif |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | /* Free the memory used by DS. */ |
| 119 | |
| 120 | void |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 121 | dyn_string_delete (dyn_string_t ds) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 131 | char* |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 132 | dyn_string_release (dyn_string_t ds) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 151 | dyn_string_t |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 152 | dyn_string_resize (dyn_string_t ds, int space) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 168 | ds->s = (char *) realloc (ds->s, ds->allocated); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 169 | if (ds->s == NULL) |
| 170 | { |
| 171 | free (ds); |
| 172 | return NULL; |
| 173 | } |
| 174 | #else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 175 | ds->s = XRESIZEVEC (char, ds->s, ds->allocated); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 176 | #endif |
| 177 | } |
| 178 | |
| 179 | return ds; |
| 180 | } |
| 181 | |
| 182 | /* Sets the contents of DS to the empty string. */ |
| 183 | |
| 184 | void |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 185 | dyn_string_clear (dyn_string_t ds) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 196 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 197 | dyn_string_copy (dyn_string_t dest, dyn_string_t src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 198 | { |
| 199 | if (dest == src) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 200 | abort (); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 201 | |
| 202 | /* Make room in DEST. */ |
| 203 | if (dyn_string_resize (dest, src->length) == NULL) |
| 204 | return 0; |
| 205 | /* Copy DEST into SRC. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 206 | strcpy (dest->s, src->s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 216 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 217 | dyn_string_copy_cstr (dyn_string_t dest, const char *src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 218 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 219 | int length = strlen (src); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 220 | /* Make room in DEST. */ |
| 221 | if (dyn_string_resize (dest, length) == NULL) |
| 222 | return 0; |
| 223 | /* Copy DEST into SRC. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 224 | strcpy (dest->s, src); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 235 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 236 | dyn_string_prepend (dyn_string_t dest, dyn_string_t src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 245 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 246 | dyn_string_prepend_cstr (dyn_string_t dest, const char *src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 256 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 257 | dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 258 | { |
| 259 | int i; |
| 260 | |
| 261 | if (src == dest) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 262 | abort (); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 270 | strncpy (dest->s + pos, src->s, src->length); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 281 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 282 | dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 283 | { |
| 284 | int i; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 285 | int length = strlen (src); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 293 | strncpy (dest->s + pos, src, length); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 303 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 304 | dyn_string_insert_char (dyn_string_t dest, int pos, int c) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 324 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 325 | dyn_string_append (dyn_string_t dest, dyn_string_t s) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 326 | { |
| 327 | if (dyn_string_resize (dest, dest->length + s->length) == 0) |
| 328 | return 0; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 329 | strcpy (dest->s + dest->length, s->s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 338 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 339 | dyn_string_append_cstr (dyn_string_t dest, const char *s) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 340 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 341 | int len = strlen (s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 347 | strcpy (dest->s + dest->length, s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 348 | dest->length += len; |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | /* Appends C to the end of DEST. Returns 1 on success. On failiure, |
| 353 | if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */ |
| 354 | |
| 355 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 356 | dyn_string_append_char (dyn_string_t dest, int c) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 376 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 377 | dyn_string_substring (dyn_string_t dest, dyn_string_t src, |
| 378 | int start, int end) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 379 | { |
| 380 | int i; |
| 381 | int length = end - start; |
| 382 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 383 | if (start > end || start > src->length || end > src->length) |
| 384 | abort (); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
| 402 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 403 | dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame] | 409 | return !strcmp (ds1->s, ds2->s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 410 | } |