]>
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 | [email protected] | |
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 AM |
139 | #if GCC_VERSION >= 3004 |
140 | max = (size_t) 1 << (CHAR_BIT * sizeof (want) - __builtin_clzl (want)); | |
141 | #else | |
142 | max = 128; | |
143 | while (want > max) | |
144 | max <<= 1; | |
145 | #endif | |
146 | max -= MALLOC_OVERHEAD + 1; | |
39a45edc AM |
147 | ptr->max = max; |
148 | ptr->ptr = xrealloc (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 | } |