1 /* sb.c - string buffer manipulation routines
2 Copyright 1994, 1995, 2000, 2003, 2005, 2006, 2007, 2009, 2012
3 Free Software Foundation, Inc.
5 Written by Steve and Judy Chamberlain of Cygnus Support,
8 This file is part of GAS, the GNU Assembler.
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
12 the Free Software Foundation; either version 3, or (at your option)
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.
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
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
35 /* These routines are about manipulating strings.
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
45 sb_grow... (&foo,...);
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)
56 static void sb_check (sb *, size_t);
58 /* Initializes an sb. */
61 sb_build (sb *ptr, size_t size)
63 ptr->ptr = xmalloc (size + 1);
71 sb_build (ptr, INIT_ALLOC);
74 /* Deallocate the sb at ptr. */
82 /* Add the sb at s to the end of the sb at ptr. */
85 sb_add_sb (sb *ptr, sb *s)
87 sb_check (ptr, s->len);
88 memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
92 /* Helper for sb_scrub_and_add_sb. */
94 static sb *sb_to_scrub;
95 static char *scrub_position;
97 scrub_from_sb (char *buf, size_t buflen)
100 copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr);
103 memcpy (buf, scrub_position, copy);
104 scrub_position += copy;
108 /* Run the sb at s through do_scrub_chars and add the result to the sb
112 sb_scrub_and_add_sb (sb *ptr, sb *s)
115 scrub_position = s->ptr;
117 sb_check (ptr, s->len);
118 ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len);
124 /* Make sure that the sb at ptr has room for another len characters,
125 and grow it if it doesn't. */
128 sb_check (sb *ptr, size_t len)
130 size_t want = ptr->len + len;
136 want += MALLOC_OVERHEAD + 1;
137 if ((ssize_t) want < 0)
138 as_fatal ("string buffer overflow");
139 #if GCC_VERSION >= 3004
140 max = (size_t) 1 << (CHAR_BIT * sizeof (want)
141 - (sizeof (want) <= sizeof (long)
142 ? __builtin_clzl ((long) want)
143 : __builtin_clzll ((long long) want)));
149 max -= MALLOC_OVERHEAD + 1;
151 ptr->ptr = xrealloc (ptr->ptr, max + 1);
155 /* Make the sb at ptr point back to the beginning. */
163 /* Add character c to the end of the sb at ptr. */
166 sb_add_char (sb *ptr, size_t c)
169 ptr->ptr[ptr->len++] = c;
172 /* Add null terminated string s to the end of sb at ptr. */
175 sb_add_string (sb *ptr, const char *s)
177 size_t len = strlen (s);
179 memcpy (ptr->ptr + ptr->len, s, len);
183 /* Add string at s of length len to sb at ptr */
186 sb_add_buffer (sb *ptr, const char *s, size_t len)
189 memcpy (ptr->ptr + ptr->len, s, len);
193 /* Write terminating NUL and return string. */
196 sb_terminate (sb *in)
198 in->ptr[in->len] = 0;
202 /* Start at the index idx into the string in sb at ptr and skip
203 whitespace. return the index of the first non whitespace character. */
206 sb_skip_white (size_t idx, sb *ptr)
208 while (idx < ptr->len
209 && (ptr->ptr[idx] == ' '
210 || ptr->ptr[idx] == '\t'))
215 /* Start at the index idx into the sb at ptr. skips whitespace,
216 a comma and any following whitespace. returns the index of the
220 sb_skip_comma (size_t idx, sb *ptr)
222 while (idx < ptr->len
223 && (ptr->ptr[idx] == ' '
224 || ptr->ptr[idx] == '\t'))
228 && ptr->ptr[idx] == ',')
231 while (idx < ptr->len
232 && (ptr->ptr[idx] == ' '
233 || ptr->ptr[idx] == '\t'))