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