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