sb.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* sb.c - string buffer manipulation routines
  2. Copyright (C) 1994-2015 Free Software Foundation, Inc.
  3. Written by Steve and Judy Chamberlain of Cygnus Support,
  4. sac@cygnus.com
  5. This file is part of GAS, the GNU Assembler.
  6. GAS is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. GAS is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with GAS; see the file COPYING. If not, write to the Free
  16. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. #include "as.h"
  19. #include "sb.h"
  20. #ifdef HAVE_LIMITS_H
  21. #include <limits.h>
  22. #endif
  23. #ifndef CHAR_BIT
  24. #define CHAR_BIT 8
  25. #endif
  26. /* These routines are about manipulating strings.
  27. They are managed in things called `sb's which is an abbreviation
  28. for string buffers. An sb has to be created, things can be glued
  29. on to it, and at the end of it's life it should be freed. The
  30. contents should never be pointed at whilst it is still growing,
  31. since it could be moved at any time
  32. eg:
  33. sb_new (&foo);
  34. sb_grow... (&foo,...);
  35. use foo->ptr[*];
  36. sb_kill (&foo); */
  37. /* Buffers start at INIT_ALLOC size, and roughly double each time we
  38. go over the current allocation. MALLOC_OVERHEAD is a guess at the
  39. system malloc overhead. We aim to not waste any memory in the
  40. underlying page/chunk allocated by the system malloc. */
  41. #define MALLOC_OVERHEAD (2 * sizeof (size_t))
  42. #define INIT_ALLOC (64 - MALLOC_OVERHEAD - 1)
  43. static void sb_check (sb *, size_t);
  44. /* Initializes an sb. */
  45. void
  46. sb_build (sb *ptr, size_t size)
  47. {
  48. ptr->ptr = xmalloc (size + 1);
  49. ptr->max = size;
  50. ptr->len = 0;
  51. }
  52. void
  53. sb_new (sb *ptr)
  54. {
  55. sb_build (ptr, INIT_ALLOC);
  56. }
  57. /* Deallocate the sb at ptr. */
  58. void
  59. sb_kill (sb *ptr)
  60. {
  61. free (ptr->ptr);
  62. }
  63. /* Add the sb at s to the end of the sb at ptr. */
  64. void
  65. sb_add_sb (sb *ptr, sb *s)
  66. {
  67. sb_check (ptr, s->len);
  68. memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
  69. ptr->len += s->len;
  70. }
  71. /* Helper for sb_scrub_and_add_sb. */
  72. static sb *sb_to_scrub;
  73. static char *scrub_position;
  74. static size_t
  75. scrub_from_sb (char *buf, size_t buflen)
  76. {
  77. size_t copy;
  78. copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr);
  79. if (copy > buflen)
  80. copy = buflen;
  81. memcpy (buf, scrub_position, copy);
  82. scrub_position += copy;
  83. return copy;
  84. }
  85. /* Run the sb at s through do_scrub_chars and add the result to the sb
  86. at ptr. */
  87. void
  88. sb_scrub_and_add_sb (sb *ptr, sb *s)
  89. {
  90. sb_to_scrub = s;
  91. scrub_position = s->ptr;
  92. sb_check (ptr, s->len);
  93. ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len);
  94. sb_to_scrub = 0;
  95. scrub_position = 0;
  96. }
  97. /* Make sure that the sb at ptr has room for another len characters,
  98. and grow it if it doesn't. */
  99. static void
  100. sb_check (sb *ptr, size_t len)
  101. {
  102. size_t want = ptr->len + len;
  103. if (want > ptr->max)
  104. {
  105. size_t max;
  106. want += MALLOC_OVERHEAD + 1;
  107. if ((ssize_t) want < 0)
  108. as_fatal ("string buffer overflow");
  109. #if GCC_VERSION >= 3004
  110. max = (size_t) 1 << (CHAR_BIT * sizeof (want)
  111. - (sizeof (want) <= sizeof (long)
  112. ? __builtin_clzl ((long) want)
  113. : __builtin_clzll ((long long) want)));
  114. #else
  115. max = 128;
  116. while (want > max)
  117. max <<= 1;
  118. #endif
  119. max -= MALLOC_OVERHEAD + 1;
  120. ptr->max = max;
  121. ptr->ptr = xrealloc (ptr->ptr, max + 1);
  122. }
  123. }
  124. /* Make the sb at ptr point back to the beginning. */
  125. void
  126. sb_reset (sb *ptr)
  127. {
  128. ptr->len = 0;
  129. }
  130. /* Add character c to the end of the sb at ptr. */
  131. void
  132. sb_add_char (sb *ptr, size_t c)
  133. {
  134. sb_check (ptr, 1);
  135. ptr->ptr[ptr->len++] = c;
  136. }
  137. /* Add null terminated string s to the end of sb at ptr. */
  138. void
  139. sb_add_string (sb *ptr, const char *s)
  140. {
  141. size_t len = strlen (s);
  142. sb_check (ptr, len);
  143. memcpy (ptr->ptr + ptr->len, s, len);
  144. ptr->len += len;
  145. }
  146. /* Add string at s of length len to sb at ptr */
  147. void
  148. sb_add_buffer (sb *ptr, const char *s, size_t len)
  149. {
  150. sb_check (ptr, len);
  151. memcpy (ptr->ptr + ptr->len, s, len);
  152. ptr->len += len;
  153. }
  154. /* Write terminating NUL and return string. */
  155. char *
  156. sb_terminate (sb *in)
  157. {
  158. in->ptr[in->len] = 0;
  159. return in->ptr;
  160. }
  161. /* Start at the index idx into the string in sb at ptr and skip
  162. whitespace. return the index of the first non whitespace character. */
  163. size_t
  164. sb_skip_white (size_t idx, sb *ptr)
  165. {
  166. while (idx < ptr->len
  167. && (ptr->ptr[idx] == ' '
  168. || ptr->ptr[idx] == '\t'))
  169. idx++;
  170. return idx;
  171. }
  172. /* Start at the index idx into the sb at ptr. skips whitespace,
  173. a comma and any following whitespace. returns the index of the
  174. next character. */
  175. size_t
  176. sb_skip_comma (size_t idx, sb *ptr)
  177. {
  178. while (idx < ptr->len
  179. && (ptr->ptr[idx] == ' '
  180. || ptr->ptr[idx] == '\t'))
  181. idx++;
  182. if (idx < ptr->len
  183. && ptr->ptr[idx] == ',')
  184. idx++;
  185. while (idx < ptr->len
  186. && (ptr->ptr[idx] == ' '
  187. || ptr->ptr[idx] == '\t'))
  188. idx++;
  189. return idx;
  190. }