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
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,...);
42 static size_t dsize
= 32;
43 static void sb_check (sb
*, size_t);
45 /* Initializes an sb. */
48 sb_build (sb
*ptr
, size_t size
)
50 ptr
->ptr
= xmalloc (size
+ 1);
58 sb_build (ptr
, dsize
);
61 /* Deallocate the sb at ptr. */
69 /* Add the sb at s to the end of the sb at ptr. */
72 sb_add_sb (sb
*ptr
, sb
*s
)
74 sb_check (ptr
, s
->len
);
75 memcpy (ptr
->ptr
+ ptr
->len
, s
->ptr
, s
->len
);
79 /* Helper for sb_scrub_and_add_sb. */
81 static sb
*sb_to_scrub
;
82 static char *scrub_position
;
84 scrub_from_sb (char *buf
, size_t buflen
)
87 copy
= sb_to_scrub
->len
- (scrub_position
- sb_to_scrub
->ptr
);
90 memcpy (buf
, scrub_position
, copy
);
91 scrub_position
+= copy
;
95 /* Run the sb at s through do_scrub_chars and add the result to the sb
99 sb_scrub_and_add_sb (sb
*ptr
, sb
*s
)
102 scrub_position
= s
->ptr
;
104 sb_check (ptr
, s
->len
);
105 ptr
->len
+= do_scrub_chars (scrub_from_sb
, ptr
->ptr
+ ptr
->len
, s
->len
);
111 /* Make sure that the sb at ptr has room for another len characters,
112 and grow it if it doesn't. */
115 sb_check (sb
*ptr
, size_t len
)
117 size_t max
= ptr
->max
;
119 while (ptr
->len
+ len
>= max
)
123 as_fatal ("string buffer overflow");
128 ptr
->ptr
= xrealloc (ptr
->ptr
, max
+ 1);
132 /* Make the sb at ptr point back to the beginning. */
140 /* Add character c to the end of the sb at ptr. */
143 sb_add_char (sb
*ptr
, size_t c
)
146 ptr
->ptr
[ptr
->len
++] = c
;
149 /* Add null terminated string s to the end of sb at ptr. */
152 sb_add_string (sb
*ptr
, const char *s
)
154 size_t len
= strlen (s
);
156 memcpy (ptr
->ptr
+ ptr
->len
, s
, len
);
160 /* Add string at s of length len to sb at ptr */
163 sb_add_buffer (sb
*ptr
, const char *s
, size_t len
)
166 memcpy (ptr
->ptr
+ ptr
->len
, s
, len
);
170 /* Like sb_name, but don't include the null byte in the string. */
173 sb_terminate (sb
*in
)
180 /* Start at the index idx into the string in sb at ptr and skip
181 whitespace. return the index of the first non whitespace character. */
184 sb_skip_white (size_t idx
, sb
*ptr
)
186 while (idx
< ptr
->len
187 && (ptr
->ptr
[idx
] == ' '
188 || ptr
->ptr
[idx
] == '\t'))
193 /* Start at the index idx into the sb at ptr. skips whitespace,
194 a comma and any following whitespace. returns the index of the
198 sb_skip_comma (size_t idx
, sb
*ptr
)
200 while (idx
< ptr
->len
201 && (ptr
->ptr
[idx
] == ' '
202 || ptr
->ptr
[idx
] == '\t'))
206 && ptr
->ptr
[idx
] == ',')
209 while (idx
< ptr
->len
210 && (ptr
->ptr
[idx
] == ' '
211 || ptr
->ptr
[idx
] == '\t'))
This page took 0.032684 seconds and 4 git commands to generate.