1 /* $Header: /p/tcsh/cvsroot/tcsh/tc.str.c,v 3.30 2009/06/25 21:27:38 christos Exp $ */
3 * tc.str.c: Short string package
4 * This has been a lesson of how to write buggy code!
7 * Copyright (c) 1980, 1991 The Regents of the University of California.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 RCSID("$tcsh: tc.str.c,v 3.30 2009/06/25 21:27:38 christos Exp $")
40 #define MALLOC_INCR 128
42 #define MALLOC_SURPLUS MB_LEN_MAX /* Space for one multibyte character */
44 #define MALLOC_SURPLUS 0
49 one_mbtowc(wchar_t *pwc, const char *s, size_t n)
53 len = rt_mbtowc(pwc, s, n);
56 *pwc = (unsigned char)*s | INVALID_BYTE;
64 one_wctomb(char *s, wchar_t wchar)
68 if (wchar & INVALID_BYTE) {
72 len = wctomb(s, wchar);
82 rt_mbtowc(wchar_t *pwc, const char *s, size_t n)
85 char back[MB_LEN_MAX];
87 ret = mbtowc(pwc, s, n);
88 if (ret > 0 && (wctomb(back, *pwc) != ret || memcmp(s, back, ret) != 0))
104 for (n = 0; src[n] != NULL; n++)
106 sdst = dst = xmalloc((n + 1) * sizeof(Char *));
108 for (; *src != NULL; src++)
115 short2blk(Char **src)
123 for (n = 0; src[n] != NULL; n++)
125 sdst = dst = xmalloc((n + 1) * sizeof(char *));
127 for (; *src != NULL; src++)
128 *dst++ = strsave(short2str(*src));
134 str2short(const char *src)
136 static struct Strbuf buf; /* = Strbuf_INIT; */
145 src += one_mbtowc(&wc, src, MB_LEN_MAX);
146 Strbuf_append1(&buf, wc);
148 Strbuf_terminate(&buf);
153 short2str(const Char *src)
155 static char *sdst = NULL;
156 static size_t dstsize = 0;
163 dstsize = MALLOC_INCR;
164 sdst = xmalloc((dstsize + MALLOC_SURPLUS) * sizeof(char));
167 edst = &dst[dstsize];
169 dst += one_wctomb(dst, *src & CHAR);
175 dstsize += MALLOC_INCR;
176 sdst = xrealloc(sdst, (dstsize + MALLOC_SURPLUS) * sizeof(char));
177 edst = &sdst[dstsize];
178 dst = &edst[-MALLOC_INCR];
179 while (wdst > wedst) {
191 s_strcpy(Char *dst, const Char *src)
196 while ((*dst++ = *src++) != '\0')
202 s_strncpy(Char *dst, const Char *src, size_t n)
211 if ((*dst++ = *src++) == '\0') {
221 s_strcat(Char *dst, const Char *src)
223 Strcpy(Strend(dst), src);
229 s_strncat(Char *dst, const Char *src, size_t n)
242 if ((*dst++ = *src++) == '\0')
254 s_strchr(const Char *str, int ch)
258 return ((Char *)(intptr_t)str);
264 s_strrchr(const Char *str, int ch)
273 return ((Char *)(intptr_t)rstr);
277 s_strlen(const Char *str)
281 for (n = 0; *str++; n++)
287 s_strcmp(const Char *str1, const Char *str2)
289 for (; *str1 && *str1 == *str2; str1++, str2++)
292 * The following case analysis is necessary so that characters which look
293 * negative collate low against normal characters but high against the
296 if (*str1 == '\0' && *str2 == '\0')
298 else if (*str1 == '\0')
300 else if (*str2 == '\0')
303 return (*str1 - *str2);
307 s_strncmp(const Char *str1, const Char *str2, size_t n)
312 if (*str1 != *str2) {
314 * The following case analysis is necessary so that characters
315 * which look negative collate low against normal characters
316 * but high against the end-of-string NUL.
320 else if (*str2 == '\0')
323 return (*str1 - *str2);
331 #endif /* not WIDE_STRINGS */
334 s_strcasecmp(const Char *str1, const Char *str2)
337 wchar_t l1 = 0, l2 = 0;
338 for (; *str1 && ((*str1 == *str2 && (l1 = l2 = 0) == 0) ||
339 (l1 = towlower(*str1)) == (l2 = towlower(*str2))); str1++, str2++)
343 unsigned char c1, c2, l1 = 0, l2 = 0;
344 for (; *str1 && ((*str1 == *str2 && (l1 = l2 = 0) == 0) ||
345 ((c1 = (unsigned char)*str1) == *str1 &&
346 (c2 = (unsigned char)*str2) == *str2 &&
347 (l1 = tolower(c1)) == (l2 = tolower(c2)))); str1++, str2++)
351 * The following case analysis is necessary so that characters which look
352 * negative collate low against normal characters but high against the
355 if (*str1 == '\0' && *str2 == '\0')
357 else if (*str1 == '\0')
359 else if (*str2 == '\0')
361 else if (l1 == l2) /* They are zero when they are equal */
362 return (*str1 - *str2);
368 s_strnsave(const Char *s, size_t len)
372 n = xmalloc((len + 1) * sizeof (*n));
373 memcpy(n, s, len * sizeof (*n));
379 s_strsave(const Char *s)
386 size = (Strlen(s) + 1) * sizeof(*n);
393 s_strspl(const Char *cp, const Char *dp)
406 res = xmalloc(((p - cp) + (q - dp) - 1) * sizeof(Char));
407 for (ep = res, q = cp; (*ep++ = *q++) != '\0';)
409 for (ep--, q = dp; (*ep++ = *q++) != '\0';)
415 s_strend(const Char *cp)
418 return ((Char *)(intptr_t) cp);
421 return ((Char *)(intptr_t) cp);
425 s_strstr(const Char *s, const Char *t)
433 return ((Char *)(intptr_t) s);
434 while (*ss++ == *tt++);
435 } while (*s++ != '\0');
439 #else /* !SHORT_STRINGS */
441 caching_strip(const char *s)
443 static char *buf = NULL;
444 static size_t buf_size = 0;
449 size = strlen(s) + 1;
450 if (buf_size < size) {
451 buf = xrealloc(buf, size);
454 memcpy(buf, s, size);
461 short2qstr(const Char *src)
463 static char *sdst = NULL;
464 static size_t dstsize = 0;
471 dstsize = MALLOC_INCR;
472 sdst = xmalloc((dstsize + MALLOC_SURPLUS) * sizeof(char));
475 edst = &dst[dstsize];
480 dstsize += MALLOC_INCR;
481 sdst = xrealloc(sdst,
482 (dstsize + MALLOC_SURPLUS) * sizeof(char));
483 edst = &sdst[dstsize];
484 dst = &edst[-MALLOC_INCR];
487 dst += one_wctomb(dst, *src & CHAR);
490 ptrdiff_t i = dst - edst;
491 dstsize += MALLOC_INCR;
492 sdst = xrealloc(sdst, (dstsize + MALLOC_SURPLUS) * sizeof(char));
493 edst = &sdst[dstsize];
494 dst = &edst[-MALLOC_INCR + i];
504 return xcalloc(1, sizeof(struct blk_buf));
508 bb_store(struct blk_buf *bb, Char *str)
510 if (bb->len == bb->size) { /* Keep space for terminating NULL */
512 bb->size = 16; /* Arbitrary */
515 bb->vec = xrealloc(bb->vec, bb->size * sizeof (*bb->vec));
517 bb->vec[bb->len] = str;
521 bb_append(struct blk_buf *bb, Char *str)
528 bb_cleanup(void *xbb)
534 for (i = 0; i < bb->len; i++)
547 bb_finish(struct blk_buf *bb)
550 return xrealloc(bb->vec, (bb->len + 1) * sizeof (*bb->vec));
553 #define DO_STRBUF(STRBUF, CHAR, STRLEN) \
556 STRBUF##_alloc(void) \
558 return xcalloc(1, sizeof(struct STRBUF)); \
562 STRBUF##_store1(struct STRBUF *buf, CHAR c) \
564 if (buf->size == buf->len) { \
565 if (buf->size == 0) \
566 buf->size = 64; /* Arbitrary */ \
569 buf->s = xrealloc(buf->s, buf->size * sizeof(*buf->s)); \
571 buf->s[buf->len] = c; \
574 /* Like strbuf_append1(buf, '\0'), but don't advance len */ \
576 STRBUF##_terminate(struct STRBUF *buf) \
578 STRBUF##_store1(buf, '\0'); \
582 STRBUF##_append1(struct STRBUF *buf, CHAR c) \
584 STRBUF##_store1(buf, c); \
589 STRBUF##_appendn(struct STRBUF *buf, const CHAR *s, size_t len) \
591 if (buf->size < buf->len + len) { \
592 if (buf->size == 0) \
593 buf->size = 64; /* Arbitrary */ \
594 while (buf->size < buf->len + len) \
596 buf->s = xrealloc(buf->s, buf->size * sizeof(*buf->s)); \
598 memcpy(buf->s + buf->len, s, len * sizeof(*buf->s)); \
603 STRBUF##_append(struct STRBUF *buf, const CHAR *s) \
605 STRBUF##_appendn(buf, s, STRLEN(s)); \
609 STRBUF##_finish(struct STRBUF *buf) \
611 STRBUF##_append1(buf, 0); \
612 return xrealloc(buf->s, buf->len * sizeof(*buf->s)); \
616 STRBUF##_cleanup(void *xbuf) \
618 struct STRBUF *buf; \
625 STRBUF##_free(void *xbuf) \
627 STRBUF##_cleanup(xbuf); \
631 const struct STRBUF STRBUF##_init /* = STRBUF##_INIT; */
633 DO_STRBUF(strbuf, char, strlen);
634 DO_STRBUF(Strbuf, Char, Strlen);