1999-05-03 07:29:11 +00:00
|
|
|
/* sb.c - string buffer manipulation routines
|
2005-03-03 01:29:54 +00:00
|
|
|
Copyright 1994, 1995, 2000, 2003 Free Software Foundation, Inc.
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
Written by Steve and Judy Chamberlain of Cygnus Support,
|
|
|
|
sac@cygnus.com
|
|
|
|
|
|
|
|
This file is part of GAS, the GNU Assembler.
|
|
|
|
|
|
|
|
GAS is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
GAS is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GAS; see the file COPYING. If not, write to the Free
|
2005-05-05 09:13:19 +00:00
|
|
|
Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
|
|
|
|
02110-1301, USA. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdio.h>
|
2000-07-19 17:47:08 +00:00
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
1999-05-03 07:29:11 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#else
|
|
|
|
#include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include "libiberty.h"
|
|
|
|
#include "sb.h"
|
|
|
|
|
|
|
|
/* These routines are about manipulating strings.
|
|
|
|
|
|
|
|
They are managed in things called `sb's which is an abbreviation
|
|
|
|
for string buffers. An sb has to be created, things can be glued
|
|
|
|
on to it, and at the end of it's life it should be freed. The
|
|
|
|
contents should never be pointed at whilst it is still growing,
|
|
|
|
since it could be moved at any time
|
|
|
|
|
|
|
|
eg:
|
|
|
|
sb_new (&foo);
|
|
|
|
sb_grow... (&foo,...);
|
|
|
|
use foo->ptr[*];
|
2005-05-06 08:49:28 +00:00
|
|
|
sb_kill (&foo); */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
2005-04-29 00:22:29 +00:00
|
|
|
static int dsize = 5;
|
2003-12-03 03:39:58 +00:00
|
|
|
static void sb_check (sb *, int);
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
/* Statistics of sb structures. */
|
2005-04-29 00:22:29 +00:00
|
|
|
static int string_count[sb_max_power_two];
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
/* Free list of sb structures. */
|
|
|
|
static sb_list_vector free_list;
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Initializes an sb. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
2005-04-29 00:22:29 +00:00
|
|
|
static void
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_build (sb *ptr, int size)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
2005-05-06 08:49:28 +00:00
|
|
|
/* See if we can find one to allocate. */
|
1999-05-03 07:29:11 +00:00
|
|
|
sb_element *e;
|
|
|
|
|
|
|
|
if (size > sb_max_power_two)
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
e = free_list.size[size];
|
|
|
|
if (!e)
|
|
|
|
{
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Nothing there, allocate one and stick into the free list. */
|
1999-05-03 07:29:11 +00:00
|
|
|
e = (sb_element *) xmalloc (sizeof (sb_element) + (1 << size));
|
|
|
|
e->next = free_list.size[size];
|
|
|
|
e->size = 1 << size;
|
|
|
|
free_list.size[size] = e;
|
|
|
|
string_count[size]++;
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Remove from free list. */
|
1999-05-03 07:29:11 +00:00
|
|
|
free_list.size[size] = e->next;
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Copy into callers world. */
|
1999-05-03 07:29:11 +00:00
|
|
|
ptr->ptr = e->data;
|
|
|
|
ptr->pot = size;
|
|
|
|
ptr->len = 0;
|
|
|
|
ptr->item = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_new (sb *ptr)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
|
|
|
sb_build (ptr, dsize);
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Deallocate the sb at ptr. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
void
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_kill (sb *ptr)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Return item to free list. */
|
1999-05-03 07:29:11 +00:00
|
|
|
ptr->item->next = free_list.size[ptr->pot];
|
|
|
|
free_list.size[ptr->pot] = ptr->item;
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Add the sb at s to the end of the sb at ptr. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
void
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_add_sb (sb *ptr, sb *s)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
|
|
|
sb_check (ptr, s->len);
|
|
|
|
memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
|
|
|
|
ptr->len += s->len;
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Make sure that the sb at ptr has room for another len characters,
|
2000-09-12 03:56:22 +00:00
|
|
|
and grow it if it doesn't. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
static void
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_check (sb *ptr, int len)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
|
|
|
if (ptr->len + len >= 1 << ptr->pot)
|
|
|
|
{
|
|
|
|
sb tmp;
|
|
|
|
int pot = ptr->pot;
|
2005-05-06 08:49:28 +00:00
|
|
|
|
1999-05-03 07:29:11 +00:00
|
|
|
while (ptr->len + len >= 1 << pot)
|
|
|
|
pot++;
|
|
|
|
sb_build (&tmp, pot);
|
|
|
|
sb_add_sb (&tmp, ptr);
|
|
|
|
sb_kill (ptr);
|
|
|
|
*ptr = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Make the sb at ptr point back to the beginning. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
void
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_reset (sb *ptr)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
|
|
|
ptr->len = 0;
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Add character c to the end of the sb at ptr. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
void
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_add_char (sb *ptr, int c)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
|
|
|
sb_check (ptr, 1);
|
|
|
|
ptr->ptr[ptr->len++] = c;
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Add null terminated string s to the end of sb at ptr. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
void
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_add_string (sb *ptr, const char *s)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
|
|
|
int len = strlen (s);
|
|
|
|
sb_check (ptr, len);
|
|
|
|
memcpy (ptr->ptr + ptr->len, s, len);
|
|
|
|
ptr->len += len;
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Add string at s of length len to sb at ptr */
|
2005-05-06 07:39:00 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
sb_add_buffer (sb *ptr, const char *s, int len)
|
|
|
|
{
|
|
|
|
sb_check (ptr, len);
|
|
|
|
memcpy (ptr->ptr + ptr->len, s, len);
|
|
|
|
ptr->len += len;
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Like sb_name, but don't include the null byte in the string. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
char *
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_terminate (sb *in)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
|
|
|
sb_add_char (in, 0);
|
|
|
|
--in->len;
|
|
|
|
return in->ptr;
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Start at the index idx into the string in sb at ptr and skip
|
|
|
|
whitespace. return the index of the first non whitespace character. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
int
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_skip_white (int idx, sb *ptr)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
|
|
|
while (idx < ptr->len
|
|
|
|
&& (ptr->ptr[idx] == ' '
|
|
|
|
|| ptr->ptr[idx] == '\t'))
|
|
|
|
idx++;
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2005-05-06 08:49:28 +00:00
|
|
|
/* Start at the index idx into the sb at ptr. skips whitespace,
|
2003-10-27 12:45:17 +00:00
|
|
|
a comma and any following whitespace. returns the index of the
|
2000-09-12 03:56:22 +00:00
|
|
|
next character. */
|
1999-05-03 07:29:11 +00:00
|
|
|
|
|
|
|
int
|
2003-12-03 03:39:58 +00:00
|
|
|
sb_skip_comma (int idx, sb *ptr)
|
1999-05-03 07:29:11 +00:00
|
|
|
{
|
|
|
|
while (idx < ptr->len
|
|
|
|
&& (ptr->ptr[idx] == ' '
|
|
|
|
|| ptr->ptr[idx] == '\t'))
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
if (idx < ptr->len
|
|
|
|
&& ptr->ptr[idx] == ',')
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
while (idx < ptr->len
|
|
|
|
&& (ptr->ptr[idx] == ' '
|
|
|
|
|| ptr->ptr[idx] == '\t'))
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|