1 /* sb.c - string buffer manipulation routines
2 Copyright (C) 1994-2024 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
32 /* These routines are about manipulating strings.
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
42 sb_grow... (&foo,...);
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)
53 static void sb_check (sb
*, size_t);
55 /* Initializes an sb. */
58 sb_build (sb
*ptr
, size_t size
)
60 ptr
->ptr
= XNEWVEC (char, size
+ 1);
68 sb_build (ptr
, INIT_ALLOC
);
71 /* Deallocate the sb at ptr. */
79 /* Add the sb at s to the end of the sb at ptr. */
82 sb_add_sb (sb
*ptr
, sb
*s
)
84 sb_check (ptr
, s
->len
);
85 memcpy (ptr
->ptr
+ ptr
->len
, s
->ptr
, s
->len
);
89 /* Helper for sb_scrub_and_add_sb. */
91 static sb
*sb_to_scrub
;
92 static char *scrub_position
;
94 scrub_from_sb (char *buf
, size_t buflen
)
97 copy
= sb_to_scrub
->len
- (scrub_position
- sb_to_scrub
->ptr
);
100 memcpy (buf
, scrub_position
, copy
);
101 scrub_position
+= copy
;
105 /* Run the sb at s through do_scrub_chars and add the result to the sb
109 sb_scrub_and_add_sb (sb
*ptr
, sb
*s
)
112 scrub_position
= s
->ptr
;
114 /* do_scrub_chars can expand text, for example when replacing
117 \t.linefile 123 "filename"
118 or when replacing a 'c with the decimal ascii number for c.
119 So we loop until the input S is consumed. */
122 size_t copy
= s
->len
- (scrub_position
- s
->ptr
) + do_scrub_pending ();
125 sb_check (ptr
, copy
);
126 ptr
->len
+= do_scrub_chars (scrub_from_sb
, ptr
->ptr
+ ptr
->len
,
127 ptr
->max
- ptr
->len
, false);
134 /* Make sure that the sb at ptr has room for another len characters,
135 and grow it if it doesn't. */
138 sb_check (sb
*ptr
, size_t len
)
140 size_t want
= ptr
->len
+ len
;
146 want
+= MALLOC_OVERHEAD
+ 1;
147 if ((ssize_t
) want
< 0)
148 as_fatal ("string buffer overflow");
149 #if GCC_VERSION >= 3004
150 max
= (size_t) 1 << (CHAR_BIT
* sizeof (want
)
151 - (sizeof (want
) <= sizeof (long)
152 ? __builtin_clzl ((long) want
)
153 : __builtin_clzll ((long long) want
)));
159 max
-= MALLOC_OVERHEAD
+ 1;
161 ptr
->ptr
= XRESIZEVEC (char, ptr
->ptr
, max
+ 1);
165 /* Make the sb at ptr point back to the beginning. */
173 /* Add character c to the end of the sb at ptr. */
176 sb_add_char (sb
*ptr
, char c
)
179 ptr
->ptr
[ptr
->len
++] = c
;
182 /* Add null terminated string s to the end of sb at ptr. */
185 sb_add_string (sb
*ptr
, const char *s
)
187 size_t len
= strlen (s
);
189 memcpy (ptr
->ptr
+ ptr
->len
, s
, len
);
193 /* Add string at s of length len to sb at ptr */
196 sb_add_buffer (sb
*ptr
, const char *s
, size_t len
)
199 memcpy (ptr
->ptr
+ ptr
->len
, s
, len
);
203 /* Write terminating NUL and return string. */
206 sb_terminate (sb
*in
)
208 in
->ptr
[in
->len
] = 0;
212 /* Start at the index idx into the string in sb at ptr and skip
213 whitespace. return the index of the first non whitespace character. */
216 sb_skip_white (size_t idx
, sb
*ptr
)
218 while (idx
< ptr
->len
219 && (ptr
->ptr
[idx
] == ' '
220 || ptr
->ptr
[idx
] == '\t'))
225 /* Start at the index idx into the sb at ptr. skips whitespace,
226 a comma and any following whitespace. returns the index of the
230 sb_skip_comma (size_t idx
, sb
*ptr
)
232 while (idx
< ptr
->len
233 && (ptr
->ptr
[idx
] == ' '
234 || ptr
->ptr
[idx
] == '\t'))
238 && ptr
->ptr
[idx
] == ',')
241 while (idx
< ptr
->len
242 && (ptr
->ptr
[idx
] == ' '
243 || ptr
->ptr
[idx
] == '\t'))