1 /* sb.c - string buffer manipulation routines
2 Copyright 1994, 1995, 2000 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, 59 Temple Place - Suite 330, 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,...);
55 static void sb_check (sb
*, int);
57 /* Statistics of sb structures. */
59 int string_count
[sb_max_power_two
];
61 /* Free list of sb structures. */
63 static sb_list_vector free_list
;
65 /* initializes an sb. */
68 sb_build (sb
*ptr
, int size
)
70 /* see if we can find one to allocate */
73 if (size
> sb_max_power_two
)
76 e
= free_list
.size
[size
];
79 /* nothing there, allocate one and stick into the free list */
80 e
= (sb_element
*) xmalloc (sizeof (sb_element
) + (1 << size
));
81 e
->next
= free_list
.size
[size
];
83 free_list
.size
[size
] = e
;
87 /* remove from free list */
89 free_list
.size
[size
] = e
->next
;
91 /* copy into callers world */
101 sb_build (ptr
, dsize
);
104 /* deallocate the sb at ptr */
109 /* return item to free list */
110 ptr
->item
->next
= free_list
.size
[ptr
->pot
];
111 free_list
.size
[ptr
->pot
] = ptr
->item
;
114 /* add the sb at s to the end of the sb at ptr */
117 sb_add_sb (sb
*ptr
, sb
*s
)
119 sb_check (ptr
, s
->len
);
120 memcpy (ptr
->ptr
+ ptr
->len
, s
->ptr
, 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
, int len
)
130 if (ptr
->len
+ len
>= 1 << ptr
->pot
)
134 while (ptr
->len
+ len
>= 1 << pot
)
136 sb_build (&tmp
, pot
);
137 sb_add_sb (&tmp
, ptr
);
143 /* make the sb at ptr point back to the beginning. */
151 /* add character c to the end of the sb at ptr. */
154 sb_add_char (sb
*ptr
, int c
)
157 ptr
->ptr
[ptr
->len
++] = c
;
160 /* add null terminated string s to the end of sb at ptr. */
163 sb_add_string (sb
*ptr
, const char *s
)
165 int len
= strlen (s
);
167 memcpy (ptr
->ptr
+ ptr
->len
, s
, len
);
171 /* add string at s of length len to sb at ptr */
174 sb_add_buffer (sb
*ptr
, const char *s
, int len
)
177 memcpy (ptr
->ptr
+ ptr
->len
, s
, len
);
181 /* print the sb at ptr to the output file */
184 sb_print (FILE *outfile
, sb
*ptr
)
189 for (i
= 0; i
< ptr
->len
; i
++)
193 fprintf (outfile
, ",");
195 fprintf (outfile
, "%d", ptr
->ptr
[i
]);
201 sb_print_at (FILE *outfile
, int idx
, sb
*ptr
)
204 for (i
= idx
; i
< ptr
->len
; i
++)
205 putc (ptr
->ptr
[i
], outfile
);
208 /* put a null at the end of the sb at in and return the start of the
209 string, so that it can be used as an arg to printf %s. */
214 /* stick a null on the end of the string */
219 /* like sb_name, but don't include the null byte in the string. */
222 sb_terminate (sb
*in
)
229 /* start at the index idx into the string in sb at ptr and skip
230 whitespace. return the index of the first non whitespace character */
233 sb_skip_white (int idx
, sb
*ptr
)
235 while (idx
< ptr
->len
236 && (ptr
->ptr
[idx
] == ' '
237 || ptr
->ptr
[idx
] == '\t'))
242 /* start at the index idx into the sb at ptr. skips whitespace,
243 a comma and any following whitespace. returns the index of the
247 sb_skip_comma (int idx
, sb
*ptr
)
249 while (idx
< ptr
->len
250 && (ptr
->ptr
[idx
] == ' '
251 || ptr
->ptr
[idx
] == '\t'))
255 && ptr
->ptr
[idx
] == ',')
258 while (idx
< ptr
->len
259 && (ptr
->ptr
[idx
] == ' '
260 || ptr
->ptr
[idx
] == '\t'))