1 /* sb.c - string buffer manipulation routines
2 Copyright (C) 1994-2017 Free Software Foundation, Inc.
4 Written by Steve and Judy Chamberlain of Cygnus Support,
7 This file is part of GAS, the GNU Assembler.
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 3, or (at your option)
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.
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
21 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
34 /* These routines are about manipulating strings.
36 They are managed in things called `sb's which is an abbreviation
37 for string buffers. An sb has to be created, things can be glued
38 on to it, and at the end of it's life it should be freed. The
39 contents should never be pointed at whilst it is still growing,
40 since it could be moved at any time
44 sb_grow... (&foo,...);
48 /* Buffers start at INIT_ALLOC size, and roughly double each time we
49 go over the current allocation. MALLOC_OVERHEAD is a guess at the
50 system malloc overhead. We aim to not waste any memory in the
51 underlying page/chunk allocated by the system malloc. */
52 #define MALLOC_OVERHEAD (2 * sizeof (size_t))
53 #define INIT_ALLOC (64 - MALLOC_OVERHEAD - 1)
55 static void sb_check (sb
*, size_t);
57 /* Initializes an sb. */
60 sb_build (sb
*ptr
, size_t size
)
62 ptr
->ptr
= XNEWVEC (char, size
+ 1);
70 sb_build (ptr
, INIT_ALLOC
);
73 /* Deallocate the sb at ptr. */
81 /* Add the sb at s to the end of the sb at ptr. */
84 sb_add_sb (sb
*ptr
, sb
*s
)
86 sb_check (ptr
, s
->len
);
87 memcpy (ptr
->ptr
+ ptr
->len
, s
->ptr
, s
->len
);
91 /* Helper for sb_scrub_and_add_sb. */
93 static sb
*sb_to_scrub
;
94 static char *scrub_position
;
96 scrub_from_sb (char *buf
, size_t buflen
)
99 copy
= sb_to_scrub
->len
- (scrub_position
- sb_to_scrub
->ptr
);
102 memcpy (buf
, scrub_position
, copy
);
103 scrub_position
+= copy
;
107 /* Run the sb at s through do_scrub_chars and add the result to the sb
111 sb_scrub_and_add_sb (sb
*ptr
, sb
*s
)
114 scrub_position
= s
->ptr
;
116 sb_check (ptr
, s
->len
);
117 ptr
->len
+= do_scrub_chars (scrub_from_sb
, ptr
->ptr
+ ptr
->len
, s
->len
);
123 /* Make sure that the sb at ptr has room for another len characters,
124 and grow it if it doesn't. */
127 sb_check (sb
*ptr
, size_t len
)
129 size_t want
= ptr
->len
+ len
;
135 want
+= MALLOC_OVERHEAD
+ 1;
136 if ((ssize_t
) want
< 0)
137 as_fatal ("string buffer overflow");
138 #if GCC_VERSION >= 3004
139 max
= (size_t) 1 << (CHAR_BIT
* sizeof (want
)
140 - (sizeof (want
) <= sizeof (long)
141 ? __builtin_clzl ((long) want
)
142 : __builtin_clzll ((long long) want
)));
148 max
-= MALLOC_OVERHEAD
+ 1;
150 ptr
->ptr
= XRESIZEVEC (char, ptr
->ptr
, max
+ 1);
154 /* Make the sb at ptr point back to the beginning. */
162 /* Add character c to the end of the sb at ptr. */
165 sb_add_char (sb
*ptr
, size_t c
)
168 ptr
->ptr
[ptr
->len
++] = c
;
171 /* Add null terminated string s to the end of sb at ptr. */
174 sb_add_string (sb
*ptr
, const char *s
)
176 size_t len
= strlen (s
);
178 memcpy (ptr
->ptr
+ ptr
->len
, s
, len
);
182 /* Add string at s of length len to sb at ptr */
185 sb_add_buffer (sb
*ptr
, const char *s
, size_t len
)
188 memcpy (ptr
->ptr
+ ptr
->len
, s
, len
);
192 /* Write terminating NUL and return string. */
195 sb_terminate (sb
*in
)
197 in
->ptr
[in
->len
] = 0;
201 /* Start at the index idx into the string in sb at ptr and skip
202 whitespace. return the index of the first non whitespace character. */
205 sb_skip_white (size_t idx
, sb
*ptr
)
207 while (idx
< ptr
->len
208 && (ptr
->ptr
[idx
] == ' '
209 || ptr
->ptr
[idx
] == '\t'))
214 /* Start at the index idx into the sb at ptr. skips whitespace,
215 a comma and any following whitespace. returns the index of the
219 sb_skip_comma (size_t idx
, sb
*ptr
)
221 while (idx
< ptr
->len
222 && (ptr
->ptr
[idx
] == ' '
223 || ptr
->ptr
[idx
] == '\t'))
227 && ptr
->ptr
[idx
] == ',')
230 while (idx
< ptr
->len
231 && (ptr
->ptr
[idx
] == ' '
232 || ptr
->ptr
[idx
] == '\t'))
This page took 0.033835 seconds and 4 git commands to generate.