1 /* sb.c - string buffer manipulation routines
2 Copyright 1994, 1995, 2000, 2003 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 2, 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 #include "libiberty.h"
37 /* These routines are about manipulating strings.
39 They are managed in things called `sb's which is an abbreviation
40 for string buffers. An sb has to be created, things can be glued
41 on to it, and at the end of it's life it should be freed. The
42 contents should never be pointed at whilst it is still growing,
43 since it could be moved at any time
47 sb_grow... (&foo,...);
54 static void sb_check (sb *, int);
56 /* Statistics of sb structures. */
57 static int string_count[sb_max_power_two];
59 /* Free list of sb structures. */
60 static sb_list_vector free_list;
62 /* initializes an sb. */
65 sb_build (sb *ptr, int size)
67 /* see if we can find one to allocate */
70 if (size > sb_max_power_two)
73 e = free_list.size[size];
76 /* nothing there, allocate one and stick into the free list */
77 e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
78 e->next = free_list.size[size];
80 free_list.size[size] = e;
84 /* remove from free list */
86 free_list.size[size] = e->next;
88 /* copy into callers world */
98 sb_build (ptr, dsize);
101 /* deallocate the sb at ptr */
106 /* return item to free list */
107 ptr->item->next = free_list.size[ptr->pot];
108 free_list.size[ptr->pot] = ptr->item;
111 /* add the sb at s to the end of the sb at ptr */
114 sb_add_sb (sb *ptr, sb *s)
116 sb_check (ptr, s->len);
117 memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
121 /* make sure that the sb at ptr has room for another len characters,
122 and grow it if it doesn't. */
125 sb_check (sb *ptr, int len)
127 if (ptr->len + len >= 1 << ptr->pot)
131 while (ptr->len + len >= 1 << pot)
133 sb_build (&tmp, pot);
134 sb_add_sb (&tmp, ptr);
140 /* make the sb at ptr point back to the beginning. */
148 /* add character c to the end of the sb at ptr. */
151 sb_add_char (sb *ptr, int c)
154 ptr->ptr[ptr->len++] = c;
157 /* add null terminated string s to the end of sb at ptr. */
160 sb_add_string (sb *ptr, const char *s)
162 int len = strlen (s);
164 memcpy (ptr->ptr + ptr->len, s, len);
168 /* add string at s of length len to sb at ptr */
171 sb_add_buffer (sb *ptr, const char *s, int len)
174 memcpy (ptr->ptr + ptr->len, s, len);
178 /* like sb_name, but don't include the null byte in the string. */
181 sb_terminate (sb *in)
188 /* start at the index idx into the string in sb at ptr and skip
189 whitespace. return the index of the first non whitespace character */
192 sb_skip_white (int idx, sb *ptr)
194 while (idx < ptr->len
195 && (ptr->ptr[idx] == ' '
196 || ptr->ptr[idx] == '\t'))
201 /* start at the index idx into the sb at ptr. skips whitespace,
202 a comma and any following whitespace. returns the index of the
206 sb_skip_comma (int idx, sb *ptr)
208 while (idx < ptr->len
209 && (ptr->ptr[idx] == ' '
210 || ptr->ptr[idx] == '\t'))
214 && ptr->ptr[idx] == ',')
217 while (idx < ptr->len
218 && (ptr->ptr[idx] == ' '
219 || ptr->ptr[idx] == '\t'))