1 /* $NetBSD: mstring.c,v 1.3 2015/01/04 01:34:20 christos Exp $ */
3 /* Id: mstring.c,v 1.6 2014/04/22 23:36:31 tom Exp */
4 #if HAVE_NBTOOL_CONFIG_H
5 #include "nbtool_config.h"
9 __RCSID("$NetBSD: mstring.c,v 1.3 2015/01/04 01:34:20 christos Exp $");
18 /* parameters about string length. HEAD is the starting size and
19 ** HEAD+TAIL should be a power of two */
26 static size_t buf_len
;
29 msprintf(struct mstring
*s
, const char *fmt
,...)
42 buf_ptr
= malloc(buf_len
= 4096);
53 len
= (size_t) vsnprintf(buf_ptr
, buf_len
, fmt
, args
);
55 if ((changed
= (len
> buf_len
)) != 0)
57 char *new_ptr
= realloc(buf_ptr
, (buf_len
* 3) / 2);
70 len
= (size_t) vsprintf(buf_ptr
, fmt
, args
);
76 if (len
> (size_t) (s
->end
- s
->ptr
))
79 size_t cp
= (size_t) (s
->ptr
- s
->base
);
80 size_t cl
= (size_t) (s
->end
- s
->base
);
82 while (len
> (nl
- cp
))
84 if ((new_base
= realloc(s
->base
, nl
)))
87 s
->ptr
= s
->base
+ cp
;
88 s
->end
= s
->base
+ nl
;
99 memcpy(s
->ptr
, buf_ptr
, len
);
105 mputchar(struct mstring
*s
, int ch
)
109 if (s
->ptr
== s
->end
)
111 size_t len
= (size_t) (s
->end
- s
->base
);
112 if ((s
->base
= realloc(s
->base
, len
+ len
+ TAIL
)))
114 s
->ptr
= s
->base
+ len
;
115 s
->end
= s
->base
+ len
+ len
+ TAIL
;
123 *s
->ptr
++ = (char)ch
;
130 struct mstring
*n
= TMALLOC(struct mstring
, 1);
134 if ((n
->base
= n
->ptr
= MALLOC(HEAD
)) != 0)
136 n
->end
= n
->base
+ HEAD
;
148 msdone(struct mstring
*s
)
160 #if defined(YYBTYACC)
161 /* compare two strings, ignoring whitespace, except between two letters or
162 ** digits (and treat all of these as equal) */
164 strnscmp(const char *a
, const char *b
)
168 while (isspace((unsigned char)*a
))
170 while (isspace((unsigned char)*b
))
172 while (*a
&& *a
== *b
)
174 if (isspace((unsigned char)*a
))
176 if (isalnum((unsigned char)a
[-1]) && isalnum((unsigned char)*b
))
179 else if (isspace((unsigned char)*b
))
181 if (isalnum((unsigned char)b
[-1]) && isalnum((unsigned char)*a
))
191 strnshash(const char *s
)
197 if (!isspace((unsigned char)*s
))
198 h
= (h
<< 5) - h
+ (unsigned char)*s
;
209 #if defined(YYBTYACC)