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 | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 31 | ///////////////////////////// |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
| 34 | ///////////////////////////// |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 35 | #ifdef HAVE_CONFIG_H |
| 36 | #include "config.h" |
| 37 | #endif |
| 38 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 39 | #include <stdio.h> |
| 40 | |
| 41 | #ifdef HAVE_STRING_H |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 42 | #include <string.h> |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 43 | #endif |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 44 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 45 | #ifdef HAVE_STDLIB_H |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 46 | #include <stdlib.h> |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 47 | #endif |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 48 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 49 | #include "libiberty.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 50 | #include "dyn-string.h" |
| 51 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 52 | /* Performs in-place initialization of a dyn_string struct. This |
| 53 | function can be used with a dyn_string struct on the stack or |
| 54 | embedded in another object. The contents of of the string itself |
| 55 | are still dynamically allocated. The string initially is capable |
| 56 | of holding at least SPACE characeters, including the terminating |
| 57 | NUL. If SPACE is 0, it will silently be increated to 1. |
| 58 | |
| 59 | If RETURN_ON_ALLOCATION_FAILURE is defined and memory allocation |
| 60 | fails, returns 0. Otherwise returns 1. */ |
| 61 | |
| 62 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 63 | dyn_string_init (struct dyn_string *ds_struct_ptr, int space) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 64 | { |
| 65 | /* We need at least one byte in which to store the terminating NUL. */ |
| 66 | if (space == 0) |
| 67 | space = 1; |
| 68 | |
| 69 | #ifdef RETURN_ON_ALLOCATION_FAILURE |
| 70 | ds_struct_ptr->s = (char *) malloc (space); |
| 71 | if (ds_struct_ptr->s == NULL) |
| 72 | return 0; |
| 73 | #else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 74 | ds_struct_ptr->s = XNEWVEC (char, space); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 75 | #endif |
| 76 | ds_struct_ptr->allocated = space; |
| 77 | ds_struct_ptr->length = 0; |
| 78 | ds_struct_ptr->s[0] = '\0'; |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | /* Create a new dynamic string capable of holding at least SPACE |
| 84 | characters, including the terminating NUL. If SPACE is 0, it will |
| 85 | be silently increased to 1. If RETURN_ON_ALLOCATION_FAILURE is |
| 86 | defined and memory allocation fails, returns NULL. Otherwise |
| 87 | returns the newly allocated string. */ |
| 88 | |
| 89 | dyn_string_t |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 90 | dyn_string_new (int space) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 91 | { |
| 92 | dyn_string_t result; |
| 93 | #ifdef RETURN_ON_ALLOCATION_FAILURE |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 94 | result = (dyn_string_t) malloc (sizeof (struct dyn_string)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 95 | if (result == NULL) |
| 96 | return NULL; |
| 97 | if (!dyn_string_init (result, space)) |
| 98 | { |
| 99 | free (result); |
| 100 | return NULL; |
| 101 | } |
| 102 | #else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 103 | result = XNEW (struct dyn_string); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 104 | dyn_string_init (result, space); |
| 105 | #endif |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | /* Free the memory used by DS. */ |
| 110 | |
| 111 | void |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 112 | dyn_string_delete (dyn_string_t ds) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 113 | { |
| 114 | free (ds->s); |
| 115 | free (ds); |
| 116 | } |
| 117 | |
| 118 | /* Returns the contents of DS in a buffer allocated with malloc. It |
| 119 | is the caller's responsibility to deallocate the buffer using free. |
| 120 | DS is then set to the empty string. Deletes DS itself. */ |
| 121 | |
| 122 | char* |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 123 | dyn_string_release (dyn_string_t ds) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 124 | { |
| 125 | /* Store the old buffer. */ |
| 126 | char* result = ds->s; |
| 127 | /* The buffer is no longer owned by DS. */ |
| 128 | ds->s = NULL; |
| 129 | /* Delete DS. */ |
| 130 | free (ds); |
| 131 | /* Return the old buffer. */ |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | /* Increase the capacity of DS so it can hold at least SPACE |
| 136 | characters, plus the terminating NUL. This function will not (at |
| 137 | present) reduce the capacity of DS. Returns DS on success. |
| 138 | |
| 139 | If RETURN_ON_ALLOCATION_FAILURE is defined and a memory allocation |
| 140 | operation fails, deletes DS and returns NULL. */ |
| 141 | |
| 142 | dyn_string_t |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 143 | dyn_string_resize (dyn_string_t ds, int space) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 144 | { |
| 145 | int new_allocated = ds->allocated; |
| 146 | |
| 147 | /* Increase SPACE to hold the NUL termination. */ |
| 148 | ++space; |
| 149 | |
| 150 | /* Increase allocation by factors of two. */ |
| 151 | while (space > new_allocated) |
| 152 | new_allocated *= 2; |
| 153 | |
| 154 | if (new_allocated != ds->allocated) |
| 155 | { |
| 156 | ds->allocated = new_allocated; |
| 157 | /* We actually need more space. */ |
| 158 | #ifdef RETURN_ON_ALLOCATION_FAILURE |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 159 | ds->s = (char *) realloc (ds->s, ds->allocated); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 160 | if (ds->s == NULL) |
| 161 | { |
| 162 | free (ds); |
| 163 | return NULL; |
| 164 | } |
| 165 | #else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 166 | ds->s = XRESIZEVEC (char, ds->s, ds->allocated); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 167 | #endif |
| 168 | } |
| 169 | |
| 170 | return ds; |
| 171 | } |
| 172 | |
| 173 | /* Sets the contents of DS to the empty string. */ |
| 174 | |
| 175 | void |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 176 | dyn_string_clear (dyn_string_t ds) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 177 | { |
| 178 | /* A dyn_string always has room for at least the NUL terminator. */ |
| 179 | ds->s[0] = '\0'; |
| 180 | ds->length = 0; |
| 181 | } |
| 182 | |
| 183 | /* Makes the contents of DEST the same as the contents of SRC. DEST |
| 184 | and SRC must be distinct. Returns 1 on success. On failure, if |
| 185 | RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */ |
| 186 | |
| 187 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 188 | dyn_string_copy (dyn_string_t dest, dyn_string_t src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 189 | { |
| 190 | if (dest == src) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 191 | abort (); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 192 | |
| 193 | /* Make room in DEST. */ |
| 194 | if (dyn_string_resize (dest, src->length) == NULL) |
| 195 | return 0; |
| 196 | /* Copy DEST into SRC. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 197 | strcpy (dest->s, src->s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 198 | /* Update the size of DEST. */ |
| 199 | dest->length = src->length; |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | /* Copies SRC, a NUL-terminated string, into DEST. Returns 1 on |
| 204 | success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST |
| 205 | and returns 0. */ |
| 206 | |
| 207 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 208 | dyn_string_copy_cstr (dyn_string_t dest, const char *src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 209 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 210 | int length = strlen (src); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 211 | /* Make room in DEST. */ |
| 212 | if (dyn_string_resize (dest, length) == NULL) |
| 213 | return 0; |
| 214 | /* Copy DEST into SRC. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 215 | strcpy (dest->s, src); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 216 | /* Update the size of DEST. */ |
| 217 | dest->length = length; |
| 218 | return 1; |
| 219 | } |
| 220 | |
| 221 | /* Inserts SRC at the beginning of DEST. DEST is expanded as |
| 222 | necessary. SRC and DEST must be distinct. Returns 1 on success. |
| 223 | On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and |
| 224 | returns 0. */ |
| 225 | |
| 226 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 227 | dyn_string_prepend (dyn_string_t dest, dyn_string_t src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 228 | { |
| 229 | return dyn_string_insert (dest, 0, src); |
| 230 | } |
| 231 | |
| 232 | /* Inserts SRC, a NUL-terminated string, at the beginning of DEST. |
| 233 | DEST is expanded as necessary. Returns 1 on success. On failure, |
| 234 | if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */ |
| 235 | |
| 236 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 237 | dyn_string_prepend_cstr (dyn_string_t dest, const char *src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 238 | { |
| 239 | return dyn_string_insert_cstr (dest, 0, src); |
| 240 | } |
| 241 | |
| 242 | /* Inserts SRC into DEST starting at position POS. DEST is expanded |
| 243 | as necessary. SRC and DEST must be distinct. Returns 1 on |
| 244 | success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST |
| 245 | and returns 0. */ |
| 246 | |
| 247 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 248 | dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 249 | { |
| 250 | int i; |
| 251 | |
| 252 | if (src == dest) |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 253 | abort (); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 254 | |
| 255 | if (dyn_string_resize (dest, dest->length + src->length) == NULL) |
| 256 | return 0; |
| 257 | /* Make room for the insertion. Be sure to copy the NUL. */ |
| 258 | for (i = dest->length; i >= pos; --i) |
| 259 | dest->s[i + src->length] = dest->s[i]; |
| 260 | /* Splice in the new stuff. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 261 | strncpy (dest->s + pos, src->s, src->length); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 262 | /* Compute the new length. */ |
| 263 | dest->length += src->length; |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | /* Inserts SRC, a NUL-terminated string, into DEST starting at |
| 268 | position POS. DEST is expanded as necessary. Returns 1 on |
| 269 | success. On failure, RETURN_ON_ALLOCATION_FAILURE, deletes DEST |
| 270 | and returns 0. */ |
| 271 | |
| 272 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 273 | dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 274 | { |
| 275 | int i; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 276 | int length = strlen (src); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 277 | |
| 278 | if (dyn_string_resize (dest, dest->length + length) == NULL) |
| 279 | return 0; |
| 280 | /* Make room for the insertion. Be sure to copy the NUL. */ |
| 281 | for (i = dest->length; i >= pos; --i) |
| 282 | dest->s[i + length] = dest->s[i]; |
| 283 | /* Splice in the new stuff. */ |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 284 | strncpy (dest->s + pos, src, length); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 285 | /* Compute the new length. */ |
| 286 | dest->length += length; |
| 287 | return 1; |
| 288 | } |
| 289 | |
| 290 | /* Inserts character C into DEST starting at position POS. DEST is |
| 291 | expanded as necessary. Returns 1 on success. On failure, |
| 292 | RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */ |
| 293 | |
| 294 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 295 | dyn_string_insert_char (dyn_string_t dest, int pos, int c) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 296 | { |
| 297 | int i; |
| 298 | |
| 299 | if (dyn_string_resize (dest, dest->length + 1) == NULL) |
| 300 | return 0; |
| 301 | /* Make room for the insertion. Be sure to copy the NUL. */ |
| 302 | for (i = dest->length; i >= pos; --i) |
| 303 | dest->s[i + 1] = dest->s[i]; |
| 304 | /* Add the new character. */ |
| 305 | dest->s[pos] = c; |
| 306 | /* Compute the new length. */ |
| 307 | ++dest->length; |
| 308 | return 1; |
| 309 | } |
| 310 | |
| 311 | /* Append S to DS, resizing DS if necessary. Returns 1 on success. |
| 312 | On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and |
| 313 | returns 0. */ |
| 314 | |
| 315 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 316 | dyn_string_append (dyn_string_t dest, dyn_string_t s) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 317 | { |
| 318 | if (dyn_string_resize (dest, dest->length + s->length) == 0) |
| 319 | return 0; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 320 | strcpy (dest->s + dest->length, s->s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 321 | dest->length += s->length; |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | /* Append the NUL-terminated string S to DS, resizing DS if necessary. |
| 326 | Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE, |
| 327 | deletes DEST and returns 0. */ |
| 328 | |
| 329 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 330 | dyn_string_append_cstr (dyn_string_t dest, const char *s) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 331 | { |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 332 | int len = strlen (s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 333 | |
| 334 | /* The new length is the old length plus the size of our string, plus |
| 335 | one for the null at the end. */ |
| 336 | if (dyn_string_resize (dest, dest->length + len) == NULL) |
| 337 | return 0; |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 338 | strcpy (dest->s + dest->length, s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 339 | dest->length += len; |
| 340 | return 1; |
| 341 | } |
| 342 | |
| 343 | /* Appends C to the end of DEST. Returns 1 on success. On failiure, |
| 344 | if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */ |
| 345 | |
| 346 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 347 | dyn_string_append_char (dyn_string_t dest, int c) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 348 | { |
| 349 | /* Make room for the extra character. */ |
| 350 | if (dyn_string_resize (dest, dest->length + 1) == NULL) |
| 351 | return 0; |
| 352 | /* Append the character; it will overwrite the old NUL. */ |
| 353 | dest->s[dest->length] = c; |
| 354 | /* Add a new NUL at the end. */ |
| 355 | dest->s[dest->length + 1] = '\0'; |
| 356 | /* Update the length. */ |
| 357 | ++(dest->length); |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | /* Sets the contents of DEST to the substring of SRC starting at START |
| 362 | and ending before END. START must be less than or equal to END, |
| 363 | and both must be between zero and the length of SRC, inclusive. |
| 364 | Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE, |
| 365 | deletes DEST and returns 0. */ |
| 366 | |
| 367 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 368 | dyn_string_substring (dyn_string_t dest, dyn_string_t src, |
| 369 | int start, int end) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 370 | { |
| 371 | int i; |
| 372 | int length = end - start; |
| 373 | |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 374 | if (start > end || start > src->length || end > src->length) |
| 375 | abort (); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 376 | |
| 377 | /* Make room for the substring. */ |
| 378 | if (dyn_string_resize (dest, length) == NULL) |
| 379 | return 0; |
| 380 | /* Copy the characters in the substring, */ |
| 381 | for (i = length; --i >= 0; ) |
| 382 | dest->s[i] = src->s[start + i]; |
| 383 | /* NUL-terimate the result. */ |
| 384 | dest->s[length] = '\0'; |
| 385 | /* Record the length of the substring. */ |
| 386 | dest->length = length; |
| 387 | |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | /* Returns non-zero if DS1 and DS2 have the same contents. */ |
| 392 | |
| 393 | int |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 394 | dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 395 | { |
| 396 | /* If DS1 and DS2 have different lengths, they must not be the same. */ |
| 397 | if (ds1->length != ds2->length) |
| 398 | return 0; |
| 399 | else |
sewardj | 4f2683a | 2008-10-26 11:53:30 +0000 | [diff] [blame^] | 400 | return !strcmp (ds1->s, ds2->s); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 401 | } |