]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* sb.c - string buffer manipulation routines |
ebd1c875 | 2 | Copyright 1994, 1995, 2000, 2003, 2006 Free Software Foundation, Inc. |
252b5132 RH |
3 | |
4 | Written by Steve and Judy Chamberlain of Cygnus Support, | |
5 | [email protected] | |
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 | |
11 | the Free Software Foundation; either version 2, or (at your option) | |
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 RH |
26 | |
27 | /* These routines are about manipulating strings. | |
28 | ||
29 | They are managed in things called `sb's which is an abbreviation | |
30 | for string buffers. An sb has to be created, things can be glued | |
31 | on to it, and at the end of it's life it should be freed. The | |
32 | contents should never be pointed at whilst it is still growing, | |
33 | since it could be moved at any time | |
34 | ||
35 | eg: | |
36 | sb_new (&foo); | |
37 | sb_grow... (&foo,...); | |
38 | use foo->ptr[*]; | |
93a9f991 | 39 | sb_kill (&foo); */ |
252b5132 | 40 | |
87c245cc | 41 | static int dsize = 5; |
24361518 | 42 | static void sb_check (sb *, int); |
252b5132 RH |
43 | |
44 | /* Statistics of sb structures. */ | |
87c245cc | 45 | static int string_count[sb_max_power_two]; |
252b5132 RH |
46 | |
47 | /* Free list of sb structures. */ | |
58633d9a BE |
48 | static struct |
49 | { | |
50 | sb_element *size[sb_max_power_two]; | |
51 | } free_list; | |
252b5132 | 52 | |
93a9f991 | 53 | /* Initializes an sb. */ |
252b5132 | 54 | |
87c245cc | 55 | static void |
24361518 | 56 | sb_build (sb *ptr, int size) |
252b5132 | 57 | { |
93a9f991 | 58 | /* See if we can find one to allocate. */ |
252b5132 RH |
59 | sb_element *e; |
60 | ||
58633d9a | 61 | assert (size < sb_max_power_two); |
252b5132 RH |
62 | |
63 | e = free_list.size[size]; | |
64 | if (!e) | |
65 | { | |
93a9f991 | 66 | /* Nothing there, allocate one and stick into the free list. */ |
252b5132 RH |
67 | e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size)); |
68 | e->next = free_list.size[size]; | |
69 | e->size = 1 << size; | |
70 | free_list.size[size] = e; | |
71 | string_count[size]++; | |
72 | } | |
73 | ||
93a9f991 | 74 | /* Remove from free list. */ |
252b5132 RH |
75 | free_list.size[size] = e->next; |
76 | ||
93a9f991 | 77 | /* Copy into callers world. */ |
252b5132 RH |
78 | ptr->ptr = e->data; |
79 | ptr->pot = size; | |
80 | ptr->len = 0; | |
81 | ptr->item = e; | |
82 | } | |
83 | ||
252b5132 | 84 | void |
24361518 | 85 | sb_new (sb *ptr) |
252b5132 RH |
86 | { |
87 | sb_build (ptr, dsize); | |
88 | } | |
89 | ||
93a9f991 | 90 | /* Deallocate the sb at ptr. */ |
252b5132 RH |
91 | |
92 | void | |
24361518 | 93 | sb_kill (sb *ptr) |
252b5132 | 94 | { |
93a9f991 | 95 | /* Return item to free list. */ |
252b5132 RH |
96 | ptr->item->next = free_list.size[ptr->pot]; |
97 | free_list.size[ptr->pot] = ptr->item; | |
98 | } | |
99 | ||
93a9f991 | 100 | /* Add the sb at s to the end of the sb at ptr. */ |
252b5132 RH |
101 | |
102 | void | |
24361518 | 103 | sb_add_sb (sb *ptr, sb *s) |
252b5132 RH |
104 | { |
105 | sb_check (ptr, s->len); | |
106 | memcpy (ptr->ptr + ptr->len, s->ptr, s->len); | |
107 | ptr->len += s->len; | |
108 | } | |
109 | ||
c19d1205 ZW |
110 | /* Helper for sb_scrub_and_add_sb. */ |
111 | ||
112 | static sb *sb_to_scrub; | |
113 | static char *scrub_position; | |
114 | static int | |
115 | scrub_from_sb (char *buf, int buflen) | |
116 | { | |
117 | int copy; | |
118 | copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr); | |
119 | if (copy > buflen) | |
120 | copy = buflen; | |
121 | memcpy (buf, scrub_position, copy); | |
122 | scrub_position += copy; | |
123 | return copy; | |
124 | } | |
125 | ||
126 | /* Run the sb at s through do_scrub_chars and add the result to the sb | |
127 | at ptr. */ | |
128 | ||
129 | void | |
130 | sb_scrub_and_add_sb (sb *ptr, sb *s) | |
131 | { | |
132 | sb_to_scrub = s; | |
133 | scrub_position = s->ptr; | |
134 | ||
135 | sb_check (ptr, s->len); | |
136 | ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len); | |
137 | ||
138 | sb_to_scrub = 0; | |
139 | scrub_position = 0; | |
140 | } | |
141 | ||
93a9f991 | 142 | /* Make sure that the sb at ptr has room for another len characters, |
f0e652b4 | 143 | and grow it if it doesn't. */ |
252b5132 RH |
144 | |
145 | static void | |
24361518 | 146 | sb_check (sb *ptr, int len) |
252b5132 RH |
147 | { |
148 | if (ptr->len + len >= 1 << ptr->pot) | |
149 | { | |
150 | sb tmp; | |
151 | int pot = ptr->pot; | |
93a9f991 | 152 | |
252b5132 RH |
153 | while (ptr->len + len >= 1 << pot) |
154 | pot++; | |
155 | sb_build (&tmp, pot); | |
156 | sb_add_sb (&tmp, ptr); | |
157 | sb_kill (ptr); | |
158 | *ptr = tmp; | |
159 | } | |
160 | } | |
161 | ||
93a9f991 | 162 | /* Make the sb at ptr point back to the beginning. */ |
252b5132 RH |
163 | |
164 | void | |
24361518 | 165 | sb_reset (sb *ptr) |
252b5132 RH |
166 | { |
167 | ptr->len = 0; | |
168 | } | |
169 | ||
93a9f991 | 170 | /* Add character c to the end of the sb at ptr. */ |
252b5132 RH |
171 | |
172 | void | |
24361518 | 173 | sb_add_char (sb *ptr, int c) |
252b5132 RH |
174 | { |
175 | sb_check (ptr, 1); | |
176 | ptr->ptr[ptr->len++] = c; | |
177 | } | |
178 | ||
93a9f991 | 179 | /* Add null terminated string s to the end of sb at ptr. */ |
252b5132 RH |
180 | |
181 | void | |
24361518 | 182 | sb_add_string (sb *ptr, const char *s) |
252b5132 RH |
183 | { |
184 | int len = strlen (s); | |
185 | sb_check (ptr, len); | |
186 | memcpy (ptr->ptr + ptr->len, s, len); | |
187 | ptr->len += len; | |
188 | } | |
189 | ||
93a9f991 | 190 | /* Add string at s of length len to sb at ptr */ |
3c9aabc7 JB |
191 | |
192 | void | |
193 | sb_add_buffer (sb *ptr, const char *s, int len) | |
194 | { | |
195 | sb_check (ptr, len); | |
196 | memcpy (ptr->ptr + ptr->len, s, len); | |
197 | ptr->len += len; | |
198 | } | |
199 | ||
93a9f991 | 200 | /* Like sb_name, but don't include the null byte in the string. */ |
252b5132 RH |
201 | |
202 | char * | |
24361518 | 203 | sb_terminate (sb *in) |
252b5132 RH |
204 | { |
205 | sb_add_char (in, 0); | |
206 | --in->len; | |
207 | return in->ptr; | |
208 | } | |
209 | ||
93a9f991 NC |
210 | /* Start at the index idx into the string in sb at ptr and skip |
211 | whitespace. return the index of the first non whitespace character. */ | |
252b5132 RH |
212 | |
213 | int | |
24361518 | 214 | sb_skip_white (int idx, sb *ptr) |
252b5132 RH |
215 | { |
216 | while (idx < ptr->len | |
217 | && (ptr->ptr[idx] == ' ' | |
218 | || ptr->ptr[idx] == '\t')) | |
219 | idx++; | |
220 | return idx; | |
221 | } | |
222 | ||
93a9f991 | 223 | /* Start at the index idx into the sb at ptr. skips whitespace, |
47eebc20 | 224 | a comma and any following whitespace. returns the index of the |
f0e652b4 | 225 | next character. */ |
252b5132 RH |
226 | |
227 | int | |
24361518 | 228 | sb_skip_comma (int idx, sb *ptr) |
252b5132 RH |
229 | { |
230 | while (idx < ptr->len | |
231 | && (ptr->ptr[idx] == ' ' | |
232 | || ptr->ptr[idx] == '\t')) | |
233 | idx++; | |
234 | ||
235 | if (idx < ptr->len | |
236 | && ptr->ptr[idx] == ',') | |
237 | idx++; | |
238 | ||
239 | while (idx < ptr->len | |
240 | && (ptr->ptr[idx] == ' ' | |
241 | || ptr->ptr[idx] == '\t')) | |
242 | idx++; | |
243 | ||
244 | return idx; | |
245 | } |