freebsd-skq/contrib/byacc/mstring.c

217 lines
3.4 KiB
C
Raw Normal View History

2020-06-22 22:42:20 +00:00
/* $Id: mstring.c,v 1.9 2019/11/19 23:54:53 tom Exp $ */
2014-04-22 22:11:22 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include "defs.h"
/* parameters about string length. HEAD is the starting size and
** HEAD+TAIL should be a power of two */
#define HEAD 24
#define TAIL 8
2014-05-25 00:06:54 +00:00
static char *buf_ptr;
static size_t buf_len;
2014-04-22 22:11:22 +00:00
void
2020-06-22 22:42:20 +00:00
msprintf(struct mstring *s, const char *fmt, ...)
2014-04-22 22:11:22 +00:00
{
va_list args;
size_t len;
2014-05-25 00:06:54 +00:00
#ifdef HAVE_VSNPRINTF
int changed;
#endif
2014-04-22 22:11:22 +00:00
if (!s || !s->base)
return;
2014-05-25 00:06:54 +00:00
if (buf_len == 0)
{
buf_ptr = malloc(buf_len = 4096);
}
if (buf_ptr == 0)
{
return;
}
#ifdef HAVE_VSNPRINTF
do
{
va_start(args, fmt);
len = (size_t) vsnprintf(buf_ptr, buf_len, fmt, args);
va_end(args);
if ((changed = (len > buf_len)) != 0)
{
char *new_ptr = realloc(buf_ptr, (buf_len * 3) / 2);
if (new_ptr == 0)
{
free(buf_ptr);
buf_ptr = 0;
return;
}
buf_ptr = new_ptr;
}
}
while (changed);
#else
2014-04-22 22:11:22 +00:00
va_start(args, fmt);
2014-05-25 00:06:54 +00:00
len = (size_t) vsprintf(buf_ptr, fmt, args);
2014-04-22 22:11:22 +00:00
va_end(args);
2014-05-25 00:06:54 +00:00
if (len >= buf_len)
return;
#endif
2014-04-22 22:11:22 +00:00
if (len > (size_t) (s->end - s->ptr))
{
2014-05-25 00:06:54 +00:00
char *new_base;
2014-04-22 22:11:22 +00:00
size_t cp = (size_t) (s->ptr - s->base);
size_t cl = (size_t) (s->end - s->base);
size_t nl = cl;
while (len > (nl - cp))
nl = nl + nl + TAIL;
2014-05-25 00:06:54 +00:00
if ((new_base = realloc(s->base, nl)))
2014-04-22 22:11:22 +00:00
{
2014-05-25 00:06:54 +00:00
s->base = new_base;
2014-04-22 22:11:22 +00:00
s->ptr = s->base + cp;
s->end = s->base + nl;
}
else
{
2014-05-25 00:06:54 +00:00
free(s->base);
s->base = 0;
s->ptr = 0;
s->end = 0;
2014-04-22 22:11:22 +00:00
return;
}
}
2014-05-25 00:06:54 +00:00
memcpy(s->ptr, buf_ptr, len);
2014-04-22 22:11:22 +00:00
s->ptr += len;
}
int
mputchar(struct mstring *s, int ch)
{
if (!s || !s->base)
return ch;
if (s->ptr == s->end)
{
size_t len = (size_t) (s->end - s->base);
if ((s->base = realloc(s->base, len + len + TAIL)))
{
s->ptr = s->base + len;
s->end = s->base + len + len + TAIL;
}
else
{
s->ptr = s->end = 0;
return ch;
}
}
*s->ptr++ = (char)ch;
return ch;
}
struct mstring *
msnew(void)
{
2014-05-25 00:06:54 +00:00
struct mstring *n = TMALLOC(struct mstring, 1);
2014-04-22 22:11:22 +00:00
if (n)
{
2014-05-25 00:06:54 +00:00
if ((n->base = n->ptr = MALLOC(HEAD)) != 0)
2014-04-22 22:11:22 +00:00
{
n->end = n->base + HEAD;
}
else
{
free(n);
n = 0;
}
}
return n;
}
2020-06-22 22:42:20 +00:00
struct mstring *
msrenew(char *value)
{
struct mstring *r = 0;
if (value != 0)
{
r = msnew();
r->base = value;
r->end = value + strlen(value);
r->ptr = r->end;
}
return r;
}
2014-04-22 22:11:22 +00:00
char *
msdone(struct mstring *s)
{
char *r = 0;
if (s)
{
mputc(s, 0);
r = s->base;
free(s);
}
return r;
}
#if defined(YYBTYACC)
/* compare two strings, ignoring whitespace, except between two letters or
** digits (and treat all of these as equal) */
int
strnscmp(const char *a, const char *b)
{
while (1)
{
2016-12-05 21:23:44 +00:00
while (isspace(UCH(*a)))
2014-04-22 22:11:22 +00:00
a++;
2016-12-05 21:23:44 +00:00
while (isspace(UCH(*b)))
2014-04-22 22:11:22 +00:00
b++;
while (*a && *a == *b)
a++, b++;
2016-12-05 21:23:44 +00:00
if (isspace(UCH(*a)))
2014-04-22 22:11:22 +00:00
{
2016-12-05 21:23:44 +00:00
if (isalnum(UCH(a[-1])) && isalnum(UCH(*b)))
2014-04-22 22:11:22 +00:00
break;
}
2016-12-05 21:23:44 +00:00
else if (isspace(UCH(*b)))
2014-04-22 22:11:22 +00:00
{
2016-12-05 21:23:44 +00:00
if (isalnum(UCH(b[-1])) && isalnum(UCH(*a)))
2014-04-22 22:11:22 +00:00
break;
}
else
break;
}
return *a - *b;
}
unsigned int
strnshash(const char *s)
{
unsigned int h = 0;
while (*s)
{
2016-12-05 21:23:44 +00:00
if (!isspace(UCH(*s)))
2014-04-22 22:11:22 +00:00
h = (h << 5) - h + (unsigned char)*s;
s++;
}
return h;
}
#endif
2014-05-25 00:06:54 +00:00
#ifdef NO_LEAKS
void
mstring_leaks(void)
{
free(buf_ptr);
buf_ptr = 0;
buf_len = 0;
}
#endif