usr.sbin/makefs: Add -o c|C option to specify comp|check type
[dragonfly.git] / contrib / binutils-2.34 / gas / sb.c
blob8b5ea8a56e269e1cf968d5e6c6bb7a0cd529817c
1 /* sb.c - string buffer manipulation routines
2 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4 Written by Steve and Judy Chamberlain of Cygnus Support,
5 sac@cygnus.com
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)
12 any later version.
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
22 02110-1301, USA. */
24 #include "as.h"
25 #include "sb.h"
27 #ifdef HAVE_LIMITS_H
28 #include <limits.h>
29 #endif
30 #ifndef CHAR_BIT
31 #define CHAR_BIT 8
32 #endif
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
42 eg:
43 sb_new (&foo);
44 sb_grow... (&foo,...);
45 use foo->ptr[*];
46 sb_kill (&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. */
59 void
60 sb_build (sb *ptr, size_t size)
62 ptr->ptr = XNEWVEC (char, size + 1);
63 ptr->max = size;
64 ptr->len = 0;
67 void
68 sb_new (sb *ptr)
70 sb_build (ptr, INIT_ALLOC);
73 /* Deallocate the sb at ptr. */
75 void
76 sb_kill (sb *ptr)
78 free (ptr->ptr);
81 /* Add the sb at s to the end of the sb at ptr. */
83 void
84 sb_add_sb (sb *ptr, sb *s)
86 sb_check (ptr, s->len);
87 memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
88 ptr->len += s->len;
91 /* Helper for sb_scrub_and_add_sb. */
93 static sb *sb_to_scrub;
94 static char *scrub_position;
95 static size_t
96 scrub_from_sb (char *buf, size_t buflen)
98 size_t copy;
99 copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr);
100 if (copy > buflen)
101 copy = buflen;
102 memcpy (buf, scrub_position, copy);
103 scrub_position += copy;
104 return copy;
107 /* Run the sb at s through do_scrub_chars and add the result to the sb
108 at ptr. */
110 void
111 sb_scrub_and_add_sb (sb *ptr, sb *s)
113 sb_to_scrub = 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);
119 sb_to_scrub = 0;
120 scrub_position = 0;
123 /* Make sure that the sb at ptr has room for another len characters,
124 and grow it if it doesn't. */
126 static void
127 sb_check (sb *ptr, size_t len)
129 size_t want = ptr->len + len;
131 if (want > ptr->max)
133 size_t max;
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)));
143 #else
144 max = 128;
145 while (want > max)
146 max <<= 1;
147 #endif
148 max -= MALLOC_OVERHEAD + 1;
149 ptr->max = max;
150 ptr->ptr = XRESIZEVEC (char, ptr->ptr, max + 1);
154 /* Make the sb at ptr point back to the beginning. */
156 void
157 sb_reset (sb *ptr)
159 ptr->len = 0;
162 /* Add character c to the end of the sb at ptr. */
164 void
165 sb_add_char (sb *ptr, size_t c)
167 sb_check (ptr, 1);
168 ptr->ptr[ptr->len++] = c;
171 /* Add null terminated string s to the end of sb at ptr. */
173 void
174 sb_add_string (sb *ptr, const char *s)
176 size_t len = strlen (s);
177 sb_check (ptr, len);
178 memcpy (ptr->ptr + ptr->len, s, len);
179 ptr->len += len;
182 /* Add string at s of length len to sb at ptr */
184 void
185 sb_add_buffer (sb *ptr, const char *s, size_t len)
187 sb_check (ptr, len);
188 memcpy (ptr->ptr + ptr->len, s, len);
189 ptr->len += len;
192 /* Write terminating NUL and return string. */
194 char *
195 sb_terminate (sb *in)
197 in->ptr[in->len] = 0;
198 return in->ptr;
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. */
204 size_t
205 sb_skip_white (size_t idx, sb *ptr)
207 while (idx < ptr->len
208 && (ptr->ptr[idx] == ' '
209 || ptr->ptr[idx] == '\t'))
210 idx++;
211 return idx;
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
216 next character. */
218 size_t
219 sb_skip_comma (size_t idx, sb *ptr)
221 while (idx < ptr->len
222 && (ptr->ptr[idx] == ' '
223 || ptr->ptr[idx] == '\t'))
224 idx++;
226 if (idx < ptr->len
227 && ptr->ptr[idx] == ',')
228 idx++;
230 while (idx < ptr->len
231 && (ptr->ptr[idx] == ' '
232 || ptr->ptr[idx] == '\t'))
233 idx++;
235 return idx;