2 * tc.str.c: Short string package
3 * This has been a lesson of how to write buggy code!
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #define MALLOC_INCR 128
40 #define MALLOC_SURPLUS MB_LEN_MAX /* Space for one multibyte character */
42 #define MALLOC_SURPLUS 0
47 one_mbtowc(Char *pwc, const char *s, size_t n)
51 len = rt_mbtowc(pwc, s, n);
54 *pwc = (unsigned char)*s | INVALID_BYTE;
62 one_wctomb(char *s, Char wchar)
67 if ((wchar & INVALID_BYTE) == INVALID_BYTE) { /* wchar >= INVALID_BYTE */
70 * if wchar = f0000090(=90|INVALID_BYTE), then *s = ffffff90 */
74 if (wchar & (CHAR & INVALID_BYTE)) {
75 s[0] = wchar & (CHAR & 0xFF);
85 if (wchar >= 0x10000) {
86 /* UTF-16 systems can't handle these values directly in calls to
87 wctomb. Convert value to UTF-16 surrogate and call wcstombs to
88 convert the "string" to the correct multibyte representation,
92 ws[0] = 0xd800 | (wchar >> 10);
93 ws[1] = 0xdc00 | (wchar & 0x3ff);
95 /* The return value of wcstombs excludes the trailing 0, so len is
96 the correct number of multibytes for the Unicode char. */
97 len = wcstombs (s, ws, MB_CUR_MAX + 1);
100 len = wctomb(s, (wchar_t) wchar);
110 rt_mbtowc(Char *pwc, const char *s, size_t n)
113 char back[MB_LEN_MAX];
115 #if defined(UTF16_STRINGS) && defined(HAVE_MBRTOWC)
116 # if defined(AUTOSET_KANJI)
117 static mbstate_t mb_zero, mb;
119 * Workaround the Shift-JIS endcoding that translates unshifted 7 bit ASCII!
121 if (!adrof(STRnokanji) && n && pwc && s && (*s == '\\' || *s == '~') &&
122 !memcmp(&mb, &mb_zero, sizeof(mb)))
131 memset (&mb, 0, sizeof mb);
132 ret = mbrtowc(&tmp, s, n, &mb);
134 ret = mbtowc(&tmp, s, n);
138 #if defined(UTF16_STRINGS) && defined(HAVE_MBRTOWC)
139 if (tmp >= 0xd800 && tmp <= 0xdbff) {
140 /* UTF-16 surrogate pair. Fetch second half and compute
141 UTF-32 value. Dispense with the inverse test in this case. */
142 size_t n2 = mbrtowc(&tmp, s + ret, n - ret, &mb);
143 if (n2 == 0 || n2 == (size_t)-1 || n2 == (size_t)-2)
146 *pwc = (((*pwc & 0x3ff) << 10) | (tmp & 0x3ff)) + 0x10000;
151 if (wctomb(back, *pwc) != ret || memcmp(s, back, ret) != 0)
154 } else if (ret == -2)
165 blk2short(char **src)
173 for (n = 0; src[n] != NULL; n++)
175 sdst = dst = xmalloc((n + 1) * sizeof(Char *));
177 for (; *src != NULL; src++)
184 short2blk(Char **src)
192 for (n = 0; src[n] != NULL; n++)
194 sdst = dst = xmalloc((n + 1) * sizeof(char *));
196 for (; *src != NULL; src++)
197 *dst++ = strsave(short2str(*src));
203 str2short(const char *src)
205 static struct Strbuf buf; /* = Strbuf_INIT; */
214 src += one_mbtowc(&wc, src, MB_LEN_MAX);
215 Strbuf_append1(&buf, wc);
217 Strbuf_terminate(&buf);
222 short2str(const Char *src)
224 static char *sdst = NULL;
225 static size_t dstsize = 0;
232 dstsize = MALLOC_INCR;
233 sdst = xmalloc((dstsize + MALLOC_SURPLUS) * sizeof(char));
236 edst = &dst[dstsize];
238 dst += one_wctomb(dst, *src);
244 dstsize += MALLOC_INCR;
245 sdst = xrealloc(sdst, (dstsize + MALLOC_SURPLUS) * sizeof(char));
246 edst = &sdst[dstsize];
247 dst = &edst[-MALLOC_INCR];
248 while (wdst > wedst) {
258 #if !defined (WIDE_STRINGS) || defined (UTF16_STRINGS)
260 s_strcpy(Char *dst, const Char *src)
265 while ((*dst++ = *src++) != '\0')
271 s_strncpy(Char *dst, const Char *src, size_t n)
280 if ((*dst++ = *src++) == '\0') {
290 s_strcat(Char *dst, const Char *src)
292 Strcpy(Strend(dst), src);
298 s_strncat(Char *dst, const Char *src, size_t n)
311 if ((*dst++ = *src++) == '\0')
323 s_strchr(const Char *str, int ch)
327 return ((Char *)(intptr_t)str);
333 s_strrchr(const Char *str, int ch)
342 return ((Char *)(intptr_t)rstr);
346 s_strlen(const Char *str)
350 for (n = 0; *str++; n++)
356 s_strcmp(const Char *str1, const Char *str2)
358 for (; *str1 && *str1 == *str2; str1++, str2++)
361 * The following case analysis is necessary so that characters which look
362 * negative collate low against normal characters but high against the
365 if (*str1 == '\0' && *str2 == '\0')
367 else if (*str1 == '\0')
369 else if (*str2 == '\0')
372 return (*str1 - *str2);
376 s_strncmp(const Char *str1, const Char *str2, size_t n)
381 if (*str1 != *str2) {
383 * The following case analysis is necessary so that characters
384 * which look negative collate low against normal characters
385 * but high against the end-of-string NUL.
389 else if (*str2 == '\0')
392 return (*str1 - *str2);
400 #endif /* not WIDE_STRINGS */
403 s_strcasecmp(const Char *str1, const Char *str2)
406 wint_t l1 = 0, l2 = 0;
407 for (; *str1; str1++, str2++)
410 else if ((l1 = towlower(*str1)) != (l2 = towlower(*str2)))
413 unsigned char l1 = 0, l2 = 0;
414 for (; *str1; str1++, str2++)
417 else if ((l1 = tolower((unsigned char)*str1)) !=
418 (l2 = tolower((unsigned char)*str2)))
422 * The following case analysis is necessary so that characters which look
423 * negative collate low against normal characters but high against the
426 if (*str1 == '\0' && *str2 == '\0')
428 else if (*str1 == '\0')
430 else if (*str2 == '\0')
432 else if (l1 == l2) /* They are zero when they are equal */
433 return (*str1 - *str2);
439 s_strnsave(const Char *s, size_t len)
443 n = xmalloc((len + 1) * sizeof (*n));
444 memcpy(n, s, len * sizeof (*n));
450 s_strsave(const Char *s)
457 size = (Strlen(s) + 1) * sizeof(*n);
464 s_strspl(const Char *cp, const Char *dp)
477 res = xmalloc(((p - cp) + (q - dp) - 1) * sizeof(Char));
478 for (ep = res, q = cp; (*ep++ = *q++) != '\0';)
480 for (ep--, q = dp; (*ep++ = *q++) != '\0';)
486 s_strend(const Char *cp)
489 return ((Char *)(intptr_t) cp);
492 return ((Char *)(intptr_t) cp);
496 s_strstr(const Char *s, const Char *t)
504 return ((Char *)(intptr_t) s);
505 while (*ss++ == *tt++);
506 } while (*s++ != '\0');
510 #else /* !SHORT_STRINGS */
512 caching_strip(const char *s)
514 static char *buf = NULL;
515 static size_t buf_size = 0;
520 size = strlen(s) + 1;
521 if (buf_size < size) {
522 buf = xrealloc(buf, size);
525 memcpy(buf, s, size);
532 short2qstr(const Char *src)
534 static char *sdst = NULL;
535 static size_t dstsize = 0;
542 dstsize = MALLOC_INCR;
543 sdst = xmalloc((dstsize + MALLOC_SURPLUS) * sizeof(char));
546 edst = &dst[dstsize];
551 dstsize += MALLOC_INCR;
552 sdst = xrealloc(sdst,
553 (dstsize + MALLOC_SURPLUS) * sizeof(char));
554 edst = &sdst[dstsize];
555 dst = &edst[-MALLOC_INCR];
558 dst += one_wctomb(dst, *src);
561 ptrdiff_t i = dst - edst;
562 dstsize += MALLOC_INCR;
563 sdst = xrealloc(sdst, (dstsize + MALLOC_SURPLUS) * sizeof(char));
564 edst = &sdst[dstsize];
565 dst = &edst[-MALLOC_INCR + i];
575 return xcalloc(1, sizeof(struct blk_buf));
579 bb_store(struct blk_buf *bb, Char *str)
581 if (bb->len == bb->size) { /* Keep space for terminating NULL */
583 bb->size = 16; /* Arbitrary */
586 bb->vec = xrealloc(bb->vec, bb->size * sizeof (*bb->vec));
588 bb->vec[bb->len] = str;
592 bb_append(struct blk_buf *bb, Char *str)
599 bb_cleanup(void *xbb)
604 bb = (struct blk_buf *)xbb;
606 for (i = 0; i < bb->len; i++)
622 bb_finish(struct blk_buf *bb)
625 return xrealloc(bb->vec, (bb->len + 1) * sizeof (*bb->vec));
628 #define DO_STRBUF(STRBUF, CHAR, STRLEN) \
631 STRBUF##_alloc(void) \
633 return xcalloc(1, sizeof(struct STRBUF)); \
637 STRBUF##_store1(struct STRBUF *buf, CHAR c) \
639 if (buf->size == buf->len) { \
640 if (buf->size == 0) \
641 buf->size = 64; /* Arbitrary */ \
644 buf->s = xrealloc(buf->s, buf->size * sizeof(*buf->s)); \
647 buf->s[buf->len] = c; \
650 /* Like strbuf_append1(buf, '\0'), but don't advance len */ \
652 STRBUF##_terminate(struct STRBUF *buf) \
654 STRBUF##_store1(buf, '\0'); \
658 STRBUF##_append1(struct STRBUF *buf, CHAR c) \
660 STRBUF##_store1(buf, c); \
665 STRBUF##_appendn(struct STRBUF *buf, const CHAR *s, size_t len) \
667 if (buf->size < buf->len + len) { \
668 if (buf->size == 0) \
669 buf->size = 64; /* Arbitrary */ \
670 while (buf->size < buf->len + len) \
672 buf->s = xrealloc(buf->s, buf->size * sizeof(*buf->s)); \
674 memcpy(buf->s + buf->len, s, len * sizeof(*buf->s)); \
679 STRBUF##_append(struct STRBUF *buf, const CHAR *s) \
681 STRBUF##_appendn(buf, s, STRLEN(s)); \
685 STRBUF##_finish(struct STRBUF *buf) \
687 STRBUF##_append1(buf, 0); \
688 return xrealloc(buf->s, buf->len * sizeof(*buf->s)); \
692 STRBUF##_cleanup(void *xbuf) \
694 struct STRBUF *buf; \
701 STRBUF##_free(void *xbuf) \
703 STRBUF##_cleanup(xbuf); \
707 const struct STRBUF STRBUF##_init /* = STRBUF##_INIT; */
709 DO_STRBUF(strbuf, char, strlen);
710 DO_STRBUF(Strbuf, Char, Strlen);