1 /* sb.c - string buffer manipulation routines
2 Copyright 1994, 1995, 2000, 2003, 2006, 2007
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
28 /* These routines are about manipulating strings.
30 They are managed in things called `sb's which is an abbreviation
31 for string buffers. An sb has to be created, things can be glued
32 on to it, and at the end of it's life it should be freed. The
33 contents should never be pointed at whilst it is still growing,
34 since it could be moved at any time
38 sb_grow... (&foo,...);
43 static void sb_check (sb
*, int);
45 /* Statistics of sb structures. */
46 static int string_count
[sb_max_power_two
];
48 /* Free list of sb structures. */
51 sb_element
*size
[sb_max_power_two
];
54 /* Initializes an sb. */
57 sb_build (sb
*ptr
, int size
)
59 /* See if we can find one to allocate. */
62 gas_assert (size
< sb_max_power_two
);
64 e
= free_list
.size
[size
];
67 /* Nothing there, allocate one and stick into the free list. */
68 e
= (sb_element
*) xmalloc (sizeof (sb_element
) + (1 << size
));
69 e
->next
= free_list
.size
[size
];
71 free_list
.size
[size
] = e
;
75 /* Remove from free list. */
76 free_list
.size
[size
] = e
->next
;
78 /* Copy into callers world. */
88 sb_build (ptr
, dsize
);
91 /* Deallocate the sb at ptr. */
96 /* Return item to free list. */
97 ptr
->item
->next
= free_list
.size
[ptr
->pot
];
98 free_list
.size
[ptr
->pot
] = ptr
->item
;
101 /* Add the sb at s to the end of the sb at ptr. */
104 sb_add_sb (sb
*ptr
, sb
*s
)
106 sb_check (ptr
, s
->len
);
107 memcpy (ptr
->ptr
+ ptr
->len
, s
->ptr
, s
->len
);
111 /* Helper for sb_scrub_and_add_sb. */
113 static sb
*sb_to_scrub
;
114 static char *scrub_position
;
116 scrub_from_sb (char *buf
, int buflen
)
119 copy
= sb_to_scrub
->len
- (scrub_position
- sb_to_scrub
->ptr
);
122 memcpy (buf
, scrub_position
, copy
);
123 scrub_position
+= copy
;
127 /* Run the sb at s through do_scrub_chars and add the result to the sb
131 sb_scrub_and_add_sb (sb
*ptr
, sb
*s
)
134 scrub_position
= s
->ptr
;
136 sb_check (ptr
, s
->len
);
137 ptr
->len
+= do_scrub_chars (scrub_from_sb
, ptr
->ptr
+ ptr
->len
, s
->len
);
143 /* Make sure that the sb at ptr has room for another len characters,
144 and grow it if it doesn't. */
147 sb_check (sb
*ptr
, int len
)
149 if (ptr
->len
+ len
>= 1 << ptr
->pot
)
154 while (ptr
->len
+ len
>= 1 << pot
)
156 sb_build (&tmp
, pot
);
157 sb_add_sb (&tmp
, ptr
);
163 /* Make the sb at ptr point back to the beginning. */
171 /* Add character c to the end of the sb at ptr. */
174 sb_add_char (sb
*ptr
, int c
)
177 ptr
->ptr
[ptr
->len
++] = c
;
180 /* Add null terminated string s to the end of sb at ptr. */
183 sb_add_string (sb
*ptr
, const char *s
)
185 int len
= strlen (s
);
187 memcpy (ptr
->ptr
+ ptr
->len
, s
, len
);
191 /* Add string at s of length len to sb at ptr */
194 sb_add_buffer (sb
*ptr
, const char *s
, int len
)
197 memcpy (ptr
->ptr
+ ptr
->len
, s
, len
);
201 /* Like sb_name, but don't include the null byte in the string. */
204 sb_terminate (sb
*in
)
211 /* Start at the index idx into the string in sb at ptr and skip
212 whitespace. return the index of the first non whitespace character. */
215 sb_skip_white (int idx
, sb
*ptr
)
217 while (idx
< ptr
->len
218 && (ptr
->ptr
[idx
] == ' '
219 || ptr
->ptr
[idx
] == '\t'))
224 /* Start at the index idx into the sb at ptr. skips whitespace,
225 a comma and any following whitespace. returns the index of the
229 sb_skip_comma (int idx
, sb
*ptr
)
231 while (idx
< ptr
->len
232 && (ptr
->ptr
[idx
] == ' '
233 || ptr
->ptr
[idx
] == '\t'))
237 && ptr
->ptr
[idx
] == ',')
240 while (idx
< ptr
->len
241 && (ptr
->ptr
[idx
] == ' '
242 || ptr
->ptr
[idx
] == '\t'))