dyn-string.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* An abstract string datatype.
  2. Copyright (C) 1998, 1999, 2000, 2002, 2004 Free Software Foundation, Inc.
  3. Contributed by Mark Mitchell (mark@markmitchell.com).
  4. This file is part of GNU CC.
  5. GNU CC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. In addition to the permissions in the GNU General Public License, the
  10. Free Software Foundation gives you unlimited permission to link the
  11. compiled version of this file into combinations with other programs,
  12. and to distribute those combinations without any restriction coming
  13. from the use of this file. (The General Public License restrictions
  14. do apply in other respects; for example, they cover modification of
  15. the file, and distribution when not linked into a combined
  16. executable.)
  17. GNU CC is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with GNU CC; see the file COPYING. If not, write to
  23. the Free Software Foundation, 51 Franklin Street - Fifth Floor,
  24. Boston, MA 02110-1301, USA. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include <stdio.h>
  29. #ifdef HAVE_STRING_H
  30. #include <string.h>
  31. #endif
  32. #ifdef HAVE_STDLIB_H
  33. #include <stdlib.h>
  34. #endif
  35. #include "libiberty.h"
  36. #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.
  43. If RETURN_ON_ALLOCATION_FAILURE is defined and memory allocation
  44. fails, returns 0. Otherwise returns 1. */
  45. int
  46. dyn_string_init (struct dyn_string *ds_struct_ptr, int space)
  47. {
  48. /* We need at least one byte in which to store the terminating NUL. */
  49. if (space == 0)
  50. space = 1;
  51. #ifdef RETURN_ON_ALLOCATION_FAILURE
  52. ds_struct_ptr->s = (char *) malloc (space);
  53. if (ds_struct_ptr->s == NULL)
  54. return 0;
  55. #else
  56. ds_struct_ptr->s = XNEWVEC (char, space);
  57. #endif
  58. ds_struct_ptr->allocated = space;
  59. ds_struct_ptr->length = 0;
  60. ds_struct_ptr->s[0] = '\0';
  61. return 1;
  62. }
  63. /* Create a new dynamic string capable of holding at least SPACE
  64. characters, including the terminating NUL. If SPACE is 0, it will
  65. be silently increased to 1. If RETURN_ON_ALLOCATION_FAILURE is
  66. defined and memory allocation fails, returns NULL. Otherwise
  67. returns the newly allocated string. */
  68. dyn_string_t
  69. dyn_string_new (int space)
  70. {
  71. dyn_string_t result;
  72. #ifdef RETURN_ON_ALLOCATION_FAILURE
  73. result = (dyn_string_t) malloc (sizeof (struct dyn_string));
  74. if (result == NULL)
  75. return NULL;
  76. if (!dyn_string_init (result, space))
  77. {
  78. free (result);
  79. return NULL;
  80. }
  81. #else
  82. result = XNEW (struct dyn_string);
  83. dyn_string_init (result, space);
  84. #endif
  85. return result;
  86. }
  87. /* Free the memory used by DS. */
  88. void
  89. dyn_string_delete (dyn_string_t ds)
  90. {
  91. free (ds->s);
  92. free (ds);
  93. }
  94. /* Returns the contents of DS in a buffer allocated with malloc. It
  95. is the caller's responsibility to deallocate the buffer using free.
  96. DS is then set to the empty string. Deletes DS itself. */
  97. char*
  98. dyn_string_release (dyn_string_t ds)
  99. {
  100. /* Store the old buffer. */
  101. char* result = ds->s;
  102. /* The buffer is no longer owned by DS. */
  103. ds->s = NULL;
  104. /* Delete DS. */
  105. free (ds);
  106. /* Return the old buffer. */
  107. return result;
  108. }
  109. /* Increase the capacity of DS so it can hold at least SPACE
  110. characters, plus the terminating NUL. This function will not (at
  111. present) reduce the capacity of DS. Returns DS on success.
  112. If RETURN_ON_ALLOCATION_FAILURE is defined and a memory allocation
  113. operation fails, deletes DS and returns NULL. */
  114. dyn_string_t
  115. dyn_string_resize (dyn_string_t ds, int space)
  116. {
  117. int new_allocated = ds->allocated;
  118. /* Increase SPACE to hold the NUL termination. */
  119. ++space;
  120. /* Increase allocation by factors of two. */
  121. while (space > new_allocated)
  122. new_allocated *= 2;
  123. if (new_allocated != ds->allocated)
  124. {
  125. ds->allocated = new_allocated;
  126. /* We actually need more space. */
  127. #ifdef RETURN_ON_ALLOCATION_FAILURE
  128. ds->s = (char *) realloc (ds->s, ds->allocated);
  129. if (ds->s == NULL)
  130. {
  131. free (ds);
  132. return NULL;
  133. }
  134. #else
  135. ds->s = XRESIZEVEC (char, ds->s, ds->allocated);
  136. #endif
  137. }
  138. return ds;
  139. }
  140. /* Sets the contents of DS to the empty string. */
  141. void
  142. dyn_string_clear (dyn_string_t ds)
  143. {
  144. /* A dyn_string always has room for at least the NUL terminator. */
  145. ds->s[0] = '\0';
  146. ds->length = 0;
  147. }
  148. /* Makes the contents of DEST the same as the contents of SRC. DEST
  149. and SRC must be distinct. Returns 1 on success. On failure, if
  150. RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
  151. int
  152. dyn_string_copy (dyn_string_t dest, dyn_string_t src)
  153. {
  154. if (dest == src)
  155. abort ();
  156. /* Make room in DEST. */
  157. if (dyn_string_resize (dest, src->length) == NULL)
  158. return 0;
  159. /* Copy DEST into SRC. */
  160. strcpy (dest->s, src->s);
  161. /* Update the size of DEST. */
  162. dest->length = src->length;
  163. return 1;
  164. }
  165. /* Copies SRC, a NUL-terminated string, into DEST. Returns 1 on
  166. success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
  167. and returns 0. */
  168. int
  169. dyn_string_copy_cstr (dyn_string_t dest, const char *src)
  170. {
  171. int length = strlen (src);
  172. /* Make room in DEST. */
  173. if (dyn_string_resize (dest, length) == NULL)
  174. return 0;
  175. /* Copy DEST into SRC. */
  176. strcpy (dest->s, src);
  177. /* Update the size of DEST. */
  178. dest->length = length;
  179. return 1;
  180. }
  181. /* Inserts SRC at the beginning of DEST. DEST is expanded as
  182. necessary. SRC and DEST must be distinct. Returns 1 on success.
  183. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
  184. returns 0. */
  185. int
  186. dyn_string_prepend (dyn_string_t dest, dyn_string_t src)
  187. {
  188. return dyn_string_insert (dest, 0, src);
  189. }
  190. /* Inserts SRC, a NUL-terminated string, at the beginning of DEST.
  191. DEST is expanded as necessary. Returns 1 on success. On failure,
  192. if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
  193. int
  194. dyn_string_prepend_cstr (dyn_string_t dest, const char *src)
  195. {
  196. return dyn_string_insert_cstr (dest, 0, src);
  197. }
  198. /* Inserts SRC into DEST starting at position POS. DEST is expanded
  199. as necessary. SRC and DEST must be distinct. Returns 1 on
  200. success. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST
  201. and returns 0. */
  202. int
  203. dyn_string_insert (dyn_string_t dest, int pos, dyn_string_t src)
  204. {
  205. int i;
  206. if (src == dest)
  207. abort ();
  208. if (dyn_string_resize (dest, dest->length + src->length) == NULL)
  209. return 0;
  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. return 1;
  218. }
  219. /* Inserts SRC, a NUL-terminated string, into DEST starting at
  220. position POS. DEST is expanded as necessary. Returns 1 on
  221. success. On failure, RETURN_ON_ALLOCATION_FAILURE, deletes DEST
  222. and returns 0. */
  223. int
  224. dyn_string_insert_cstr (dyn_string_t dest, int pos, const char *src)
  225. {
  226. int i;
  227. int length = strlen (src);
  228. if (dyn_string_resize (dest, dest->length + length) == NULL)
  229. return 0;
  230. /* Make room for the insertion. Be sure to copy the NUL. */
  231. for (i = dest->length; i >= pos; --i)
  232. dest->s[i + length] = dest->s[i];
  233. /* Splice in the new stuff. */
  234. strncpy (dest->s + pos, src, length);
  235. /* Compute the new length. */
  236. dest->length += length;
  237. return 1;
  238. }
  239. /* Inserts character C into DEST starting at position POS. DEST is
  240. expanded as necessary. Returns 1 on success. On failure,
  241. RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
  242. int
  243. dyn_string_insert_char (dyn_string_t dest, int pos, int c)
  244. {
  245. int i;
  246. if (dyn_string_resize (dest, dest->length + 1) == NULL)
  247. return 0;
  248. /* Make room for the insertion. Be sure to copy the NUL. */
  249. for (i = dest->length; i >= pos; --i)
  250. dest->s[i + 1] = dest->s[i];
  251. /* Add the new character. */
  252. dest->s[pos] = c;
  253. /* Compute the new length. */
  254. ++dest->length;
  255. return 1;
  256. }
  257. /* Append S to DS, resizing DS if necessary. Returns 1 on success.
  258. On failure, if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and
  259. returns 0. */
  260. int
  261. dyn_string_append (dyn_string_t dest, dyn_string_t s)
  262. {
  263. if (dyn_string_resize (dest, dest->length + s->length) == 0)
  264. return 0;
  265. strcpy (dest->s + dest->length, s->s);
  266. dest->length += s->length;
  267. return 1;
  268. }
  269. /* Append the NUL-terminated string S to DS, resizing DS if necessary.
  270. Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
  271. deletes DEST and returns 0. */
  272. int
  273. dyn_string_append_cstr (dyn_string_t dest, const char *s)
  274. {
  275. int len = strlen (s);
  276. /* The new length is the old length plus the size of our string, plus
  277. one for the null at the end. */
  278. if (dyn_string_resize (dest, dest->length + len) == NULL)
  279. return 0;
  280. strcpy (dest->s + dest->length, s);
  281. dest->length += len;
  282. return 1;
  283. }
  284. /* Appends C to the end of DEST. Returns 1 on success. On failure,
  285. if RETURN_ON_ALLOCATION_FAILURE, deletes DEST and returns 0. */
  286. int
  287. dyn_string_append_char (dyn_string_t dest, int c)
  288. {
  289. /* Make room for the extra character. */
  290. if (dyn_string_resize (dest, dest->length + 1) == NULL)
  291. return 0;
  292. /* Append the character; it will overwrite the old NUL. */
  293. dest->s[dest->length] = c;
  294. /* Add a new NUL at the end. */
  295. dest->s[dest->length + 1] = '\0';
  296. /* Update the length. */
  297. ++(dest->length);
  298. return 1;
  299. }
  300. /* Sets the contents of DEST to the substring of SRC starting at START
  301. and ending before END. START must be less than or equal to END,
  302. and both must be between zero and the length of SRC, inclusive.
  303. Returns 1 on success. On failure, if RETURN_ON_ALLOCATION_FAILURE,
  304. deletes DEST and returns 0. */
  305. int
  306. dyn_string_substring (dyn_string_t dest, dyn_string_t src,
  307. int start, int end)
  308. {
  309. int i;
  310. int length = end - start;
  311. if (start > end || start > src->length || end > src->length)
  312. abort ();
  313. /* Make room for the substring. */
  314. if (dyn_string_resize (dest, length) == NULL)
  315. return 0;
  316. /* Copy the characters in the substring, */
  317. for (i = length; --i >= 0; )
  318. dest->s[i] = src->s[start + i];
  319. /* NUL-terimate the result. */
  320. dest->s[length] = '\0';
  321. /* Record the length of the substring. */
  322. dest->length = length;
  323. return 1;
  324. }
  325. /* Returns non-zero if DS1 and DS2 have the same contents. */
  326. int
  327. dyn_string_eq (dyn_string_t ds1, dyn_string_t ds2)
  328. {
  329. /* If DS1 and DS2 have different lengths, they must not be the same. */
  330. if (ds1->length != ds2->length)
  331. return 0;
  332. else
  333. return !strcmp (ds1->s, ds2->s);
  334. }