Boing.Boom.Tschak. More fixes in line with [0ef07198,ee4de6e4]
[s-mailx.git] / strings.c
blob3de98c7100857b2b47e11462649fa92d93cceb0e
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ String support routines.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE strings
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 #include <ctype.h>
44 FL char *
45 (savestr)(char const *str n_MEMORY_DEBUG_ARGS)
47 size_t size;
48 char *news;
49 NYD_ENTER;
51 size = strlen(str) +1;
52 news = (n_autorec_alloc)(NULL, size n_MEMORY_DEBUG_ARGSCALL);
53 memcpy(news, str, size);
54 NYD_LEAVE;
55 return news;
58 FL char *
59 (savestrbuf)(char const *sbuf, size_t sbuf_len n_MEMORY_DEBUG_ARGS)
61 char *news;
62 NYD_ENTER;
64 news = (n_autorec_alloc)(NULL, sbuf_len +1 n_MEMORY_DEBUG_ARGSCALL);
65 memcpy(news, sbuf, sbuf_len);
66 news[sbuf_len] = 0;
67 NYD_LEAVE;
68 return news;
71 FL char *
72 (savecatsep)(char const *s1, char sep, char const *s2 n_MEMORY_DEBUG_ARGS)
74 size_t l1, l2;
75 char *news;
76 NYD_ENTER;
78 l1 = (s1 != NULL) ? strlen(s1) : 0;
79 l2 = strlen(s2);
80 news = (n_autorec_alloc)(NULL, l1 + (sep != '\0') + l2 +1
81 n_MEMORY_DEBUG_ARGSCALL);
82 if (l1 > 0) {
83 memcpy(news + 0, s1, l1);
84 if (sep != '\0')
85 news[l1++] = sep;
87 memcpy(news + l1, s2, l2);
88 news[l1 + l2] = '\0';
89 NYD_LEAVE;
90 return news;
94 * Support routines, auto-reclaimed storage
97 FL char *
98 (i_strdup)(char const *src n_MEMORY_DEBUG_ARGS)
100 size_t sz;
101 char *dest;
102 NYD_ENTER;
104 sz = strlen(src) +1;
105 dest = (n_autorec_alloc)(NULL, sz n_MEMORY_DEBUG_ARGSCALL);
106 i_strcpy(dest, src, sz);
107 NYD_LEAVE;
108 return dest;
111 FL struct str *
112 str_concat_csvl(struct str *self, ...) /* XXX onepass maybe better here */
114 va_list vl;
115 size_t l;
116 char const *cs;
117 NYD_ENTER;
119 va_start(vl, self);
120 for (l = 0; (cs = va_arg(vl, char const*)) != NULL;)
121 l += strlen(cs);
122 va_end(vl);
124 self->l = l;
125 self->s = salloc(l +1);
127 va_start(vl, self);
128 for (l = 0; (cs = va_arg(vl, char const*)) != NULL;) {
129 size_t i = strlen(cs);
130 memcpy(self->s + l, cs, i);
131 l += i;
133 self->s[l] = '\0';
134 va_end(vl);
135 NYD_LEAVE;
136 return self;
139 FL struct str *
140 (str_concat_cpa)(struct str *self, char const * const *cpa,
141 char const *sep_o_null n_MEMORY_DEBUG_ARGS)
143 size_t sonl, l;
144 char const * const *xcpa;
145 NYD_ENTER;
147 sonl = (sep_o_null != NULL) ? strlen(sep_o_null) : 0;
149 for (l = 0, xcpa = cpa; *xcpa != NULL; ++xcpa)
150 l += strlen(*xcpa) + sonl;
152 self->l = l;
153 self->s = (n_autorec_alloc)(NULL, l +1 n_MEMORY_DEBUG_ARGSCALL);
155 for (l = 0, xcpa = cpa; *xcpa != NULL; ++xcpa) {
156 size_t i = strlen(*xcpa);
157 memcpy(self->s + l, *xcpa, i);
158 l += i;
159 if (sonl > 0) {
160 memcpy(self->s + l, sep_o_null, sonl);
161 l += sonl;
164 self->s[l] = '\0';
165 NYD_LEAVE;
166 return self;
170 * Routines that are not related to auto-reclaimed storage follow.
173 FL bool_t
174 n_anyof_buf(char const *template, char const *dat, size_t len){
175 char c;
176 NYD2_ENTER;
178 if(len == UIZ_MAX){
179 while((c = *template++) != '\0')
180 if(strchr(dat, c) != NULL)
181 break;
182 }else if(len > 0){
183 while((c = *template++) != '\0')
184 if(memchr(dat, c, len) != NULL)
185 break;
186 }else
187 c = '\0';
188 NYD2_LEAVE;
189 return (c != '\0');
192 FL char *
193 n_strsep(char **iolist, char sep, bool_t ignore_empty)
195 char *base, *cp;
196 NYD2_ENTER;
198 for (base = *iolist; base != NULL; base = *iolist) {
199 while (*base != '\0' && blankspacechar(*base))
200 ++base;
201 cp = strchr(base, sep);
202 if (cp != NULL)
203 *iolist = cp + 1;
204 else {
205 *iolist = NULL;
206 cp = base + strlen(base);
208 while (cp > base && blankspacechar(cp[-1]))
209 --cp;
210 *cp = '\0';
211 if (*base != '\0' || !ignore_empty)
212 break;
214 NYD2_LEAVE;
215 return base;
218 FL void
219 i_strcpy(char *dest, char const *src, size_t size)
221 NYD2_ENTER;
222 if (size > 0) {
223 for (;; ++dest, ++src)
224 if ((*dest = lowerconv(*src)) == '\0') {
225 break;
226 } else if (--size == 0) {
227 *dest = '\0';
228 break;
231 NYD2_LEAVE;
234 FL int
235 is_prefix(char const *as1, char const *as2) /* TODO arg order */
237 char c;
238 NYD2_ENTER;
240 for (; (c = *as1) == *as2 && c != '\0'; ++as1, ++as2)
241 if (*as2 == '\0')
242 break;
243 NYD2_LEAVE;
244 return (c == '\0');
247 FL char *
248 string_quote(char const *v) /* TODO too simpleminded (getrawlist(), +++ ..) */
250 char const *cp;
251 size_t i;
252 char c, *rv;
253 NYD2_ENTER;
255 for (i = 0, cp = v; (c = *cp) != '\0'; ++i, ++cp)
256 if (c == '"' || c == '\\')
257 ++i;
258 rv = salloc(i +1);
260 for (i = 0, cp = v; (c = *cp) != '\0'; rv[i++] = c, ++cp)
261 if (c == '"' || c == '\\')
262 rv[i++] = '\\';
263 rv[i] = '\0';
264 NYD2_LEAVE;
265 return rv;
268 FL char *
269 laststring(char *linebuf, bool_t *needs_list, bool_t strip)
271 char *cp, *p, quoted;
272 NYD_ENTER;
274 /* Anything to do at all? */
275 if (*(cp = linebuf) == '\0')
276 goto jnull;
277 cp += strlen(linebuf) -1;
279 /* Strip away trailing blanks */
280 while (spacechar(*cp) && cp > linebuf)
281 --cp;
282 cp[1] = '\0';
283 if (cp == linebuf)
284 goto jleave;
286 /* Now search for the BOS of the "last string" */
287 quoted = *cp;
288 if (quoted == '\'' || quoted == '"') {
289 if (strip)
290 *cp = '\0';
291 } else
292 quoted = ' ';
294 while (cp > linebuf) {
295 --cp;
296 if (quoted != ' ') {
297 if (*cp != quoted)
298 continue;
299 } else if (!spacechar(*cp))
300 continue;
301 if (cp == linebuf || cp[-1] != '\\') {
302 /* When in whitespace mode, WS prefix doesn't belong */
303 if (quoted == ' ')
304 ++cp;
305 break;
307 /* Expand the escaped quote character */
308 for (p = --cp; (p[0] = p[1]) != '\0'; ++p)
311 if (strip && quoted != ' ' && *cp == quoted)
312 for (p = cp; (p[0] = p[1]) != '\0'; ++p)
315 /* The "last string" has been skipped over, but still, try to step backwards
316 * until we are at BOS or see whitespace, so as to make possible things like
317 * "? copy +'x y.mbox'" or even "? copy +x\ y.mbox" */
318 while (cp > linebuf) {
319 --cp;
320 if (spacechar(*cp)) {
321 p = cp;
322 *cp++ = '\0';
323 /* We can furtherly release our callees if we now decide whether the
324 * remaining non-"last string" line content contains non-WS */
325 while (--p >= linebuf)
326 if (!spacechar(*p))
327 goto jleave;
328 linebuf = cp;
329 break;
333 jleave:
334 if (cp != NULL && *cp == '\0')
335 goto jnull;
336 *needs_list = (cp != linebuf && *linebuf != '\0');
337 j_leave:
338 NYD_LEAVE;
339 return cp;
340 jnull:
341 *needs_list = FAL0;
342 cp = NULL;
343 goto j_leave;
346 FL void
347 makelow(char *cp) /* TODO isn't that crap? --> */
349 NYD_ENTER;
350 #ifdef HAVE_C90AMEND1
351 if (mb_cur_max > 1) {
352 char *tp = cp;
353 wchar_t wc;
354 int len;
356 while (*cp != '\0') {
357 len = mbtowc(&wc, cp, mb_cur_max);
358 if (len < 0)
359 *tp++ = *cp++;
360 else {
361 wc = towlower(wc);
362 if (wctomb(tp, wc) == len)
363 tp += len, cp += len;
364 else
365 *tp++ = *cp++; /* <-- at least here */
368 } else
369 #endif
372 *cp = tolower((uc_i)*cp);
373 while (*cp++ != '\0');
375 NYD_LEAVE;
378 FL bool_t
379 substr(char const *str, char const *sub)
381 char const *cp, *backup;
382 NYD_ENTER;
384 cp = sub;
385 backup = str;
386 while (*str != '\0' && *cp != '\0') {
387 #ifdef HAVE_C90AMEND1
388 if (mb_cur_max > 1) {
389 wchar_t c, c2;
390 int sz;
392 if ((sz = mbtowc(&c, cp, mb_cur_max)) == -1)
393 goto Jsinglebyte;
394 cp += sz;
395 if ((sz = mbtowc(&c2, str, mb_cur_max)) == -1)
396 goto Jsinglebyte;
397 str += sz;
398 c = towupper(c);
399 c2 = towupper(c2);
400 if (c != c2) {
401 if ((sz = mbtowc(&c, backup, mb_cur_max)) > 0) {
402 backup += sz;
403 str = backup;
404 } else
405 str = ++backup;
406 cp = sub;
408 } else
409 Jsinglebyte:
410 #endif
412 int c, c2;
414 c = *cp++ & 0377;
415 if (islower(c))
416 c = toupper(c);
417 c2 = *str++ & 0377;
418 if (islower(c2))
419 c2 = toupper(c2);
420 if (c != c2) {
421 str = ++backup;
422 cp = sub;
426 NYD_LEAVE;
427 return (*cp == '\0');
430 FL char *
431 sstpcpy(char *dst, char const *src)
433 NYD2_ENTER;
434 while ((*dst = *src++) != '\0')
435 ++dst;
436 NYD2_LEAVE;
437 return dst;
440 FL char *
441 (sstrdup)(char const *cp n_MEMORY_DEBUG_ARGS)
443 char *dp;
444 NYD2_ENTER;
446 dp = (cp == NULL) ? NULL : (sbufdup)(cp, strlen(cp) n_MEMORY_DEBUG_ARGSCALL);
447 NYD2_LEAVE;
448 return dp;
451 FL char *
452 (sbufdup)(char const *cp, size_t len n_MEMORY_DEBUG_ARGS)
454 char *dp = NULL;
455 NYD2_ENTER;
457 dp = (n_alloc)(len +1 n_MEMORY_DEBUG_ARGSCALL);
458 if (cp != NULL)
459 memcpy(dp, cp, len);
460 dp[len] = '\0';
461 NYD2_LEAVE;
462 return dp;
465 FL ssize_t
466 n_strscpy(char *dst, char const *src, size_t dstsize){
467 ssize_t rv;
468 NYD2_ENTER;
470 if(n_LIKELY(dstsize > 0)){
471 rv = 0;
473 if((dst[rv] = src[rv]) == '\0')
474 goto jleave;
475 ++rv;
476 }while(--dstsize > 0);
477 dst[--rv] = '\0';
479 #ifdef HAVE_DEVEL
480 else
481 assert(dstsize > 0);
482 #endif
483 rv = -1;
484 jleave:
485 NYD2_LEAVE;
486 return rv;
489 FL int
490 asccasecmp(char const *s1, char const *s2)
492 int cmp;
493 NYD2_ENTER;
495 for (;;) {
496 char c1 = *s1++, c2 = *s2++;
497 if ((cmp = lowerconv(c1) - lowerconv(c2)) != 0 || c1 == '\0')
498 break;
500 NYD2_LEAVE;
501 return cmp;
504 FL int
505 ascncasecmp(char const *s1, char const *s2, size_t sz)
507 int cmp = 0;
508 NYD2_ENTER;
510 while (sz-- > 0) {
511 char c1 = *s1++, c2 = *s2++;
512 cmp = (ui8_t)lowerconv(c1);
513 cmp -= (ui8_t)lowerconv(c2);
514 if (cmp != 0 || c1 == '\0')
515 break;
517 NYD2_LEAVE;
518 return cmp;
521 FL char const *
522 asccasestr(char const *s1, char const *s2)
524 char c2, c1;
525 NYD2_ENTER;
527 for (c2 = *s2++, c2 = lowerconv(c2);;) {
528 if ((c1 = *s1++) == '\0') {
529 s1 = NULL;
530 break;
532 if (lowerconv(c1) == c2 && is_asccaseprefix(s2, s1)) {
533 --s1;
534 break;
537 NYD2_LEAVE;
538 return s1;
541 FL bool_t
542 is_asccaseprefix(char const *as1, char const *as2) /* TODO arg order */
544 char c1, c2;
545 NYD2_ENTER;
547 for(;; ++as1, ++as2){
548 c1 = *as1;
549 c1 = lowerconv(c1);
550 c2 = *as2;
551 c2 = lowerconv(c2);
553 if(c1 != c2 || c1 == '\0')
554 break;
555 if(c2 == '\0')
556 break;
558 NYD2_LEAVE;
559 return (c1 == '\0');
562 FL struct str *
563 (n_str_assign_buf)(struct str *self, char const *buf, uiz_t buflen
564 n_MEMORY_DEBUG_ARGS){
565 NYD_ENTER;
566 if(buflen == UIZ_MAX)
567 buflen = (buf == NULL) ? 0 : strlen(buf);
569 assert(buflen == 0 || buf != NULL);
571 if(n_LIKELY(buflen > 0)){
572 self->s = (n_realloc)(self->s, (self->l = buflen) +1
573 n_MEMORY_DEBUG_ARGSCALL);
574 memcpy(self->s, buf, buflen);
575 self->s[buflen] = '\0';
576 }else
577 self->l = 0;
578 NYD_LEAVE;
579 return self;
582 FL struct str *
583 (n_str_add_buf)(struct str *self, char const *buf, uiz_t buflen
584 n_MEMORY_DEBUG_ARGS){
585 NYD_ENTER;
586 if(buflen == UIZ_MAX)
587 buflen = (buf == NULL) ? 0 : strlen(buf);
589 assert(buflen == 0 || buf != NULL);
591 if(buflen > 0) {
592 size_t osl = self->l, nsl = osl + buflen;
594 self->s = (n_realloc)(self->s, (self->l = nsl) +1
595 n_MEMORY_DEBUG_ARGSCALL);
596 memcpy(self->s + osl, buf, buflen);
597 self->s[nsl] = '\0';
599 NYD_LEAVE;
600 return self;
604 * struct n_string TODO extend, optimize
607 FL struct n_string *
608 (n_string_clear)(struct n_string *self n_MEMORY_DEBUG_ARGS){
609 NYD_ENTER;
611 assert(self != NULL);
613 if(self->s_size != 0){
614 if(!self->s_auto){
615 (n_free)(self->s_dat n_MEMORY_DEBUG_ARGSCALL);
617 self->s_len = self->s_auto = self->s_size = 0;
618 self->s_dat = NULL;
620 NYD_LEAVE;
621 return self;
624 FL struct n_string *
625 (n_string_reserve)(struct n_string *self, size_t noof n_MEMORY_DEBUG_ARGS){
626 ui32_t i, l, s;
627 NYD_ENTER;
629 assert(self != NULL);
631 s = self->s_size;
632 l = self->s_len;
633 #if 0 /* FIXME memory alloc too large */
634 if(SI32_MAX - n_ALIGN(1) - l <= noof)
635 n_panic(_("Memory allocation too large"));
636 #endif
638 if((i = s - l) <= ++noof){
639 i += l + (ui32_t)noof;
640 i = n_ALIGN(i);
641 self->s_size = i -1;
643 if(!self->s_auto)
644 self->s_dat = (n_realloc)(self->s_dat, i n_MEMORY_DEBUG_ARGSCALL);
645 else{
646 char *ndat = (n_autorec_alloc)(NULL, i n_MEMORY_DEBUG_ARGSCALL);
648 if(l > 0)
649 memcpy(ndat, self->s_dat, l);
650 self->s_dat = ndat;
653 NYD_LEAVE;
654 return self;
657 FL struct n_string *
658 (n_string_resize)(struct n_string *self, size_t nlen n_MEMORY_DEBUG_ARGS){
659 NYD_ENTER;
661 assert(self != NULL);
662 #if 0 /* FIXME memory alloc too large */
663 if(SI32_MAX - n_ALIGN(1) - l <= noof)
664 n_panic(_("Memory allocation too large"));
665 #endif
667 if(self->s_len < nlen)
668 self = (n_string_reserve)(self, nlen n_MEMORY_DEBUG_ARGSCALL);
669 self->s_len = (ui32_t)nlen;
670 NYD_LEAVE;
671 return self;
674 FL struct n_string *
675 (n_string_push_buf)(struct n_string *self, char const *buf, size_t buflen
676 n_MEMORY_DEBUG_ARGS){
677 NYD_ENTER;
679 assert(self != NULL);
680 assert(buflen == 0 || buf != NULL);
682 if(buflen == UIZ_MAX)
683 buflen = (buf == NULL) ? 0 : strlen(buf);
685 if(buflen > 0){
686 ui32_t i;
688 self = (n_string_reserve)(self, buflen n_MEMORY_DEBUG_ARGSCALL);
689 memcpy(&self->s_dat[i = self->s_len], buf, buflen);
690 self->s_len = (i += (ui32_t)buflen);
692 NYD_LEAVE;
693 return self;
696 FL struct n_string *
697 (n_string_push_c)(struct n_string *self, char c n_MEMORY_DEBUG_ARGS){
698 NYD_ENTER;
700 assert(self != NULL);
702 if(self->s_len + 1 >= self->s_size)
703 self = (n_string_reserve)(self, 1 n_MEMORY_DEBUG_ARGSCALL);
704 self->s_dat[self->s_len++] = c;
705 NYD_LEAVE;
706 return self;
709 FL struct n_string *
710 (n_string_unshift_buf)(struct n_string *self, char const *buf, size_t buflen
711 n_MEMORY_DEBUG_ARGS){
712 NYD_ENTER;
714 assert(self != NULL);
715 assert(buflen == 0 || buf != NULL);
717 if(buflen == UIZ_MAX)
718 buflen = (buf == NULL) ? 0 : strlen(buf);
720 if(buflen > 0){
721 self = (n_string_reserve)(self, buflen n_MEMORY_DEBUG_ARGSCALL);
722 if(self->s_len > 0)
723 memmove(&self->s_dat[buflen], self->s_dat, self->s_len);
724 memcpy(self->s_dat, buf, buflen);
725 self->s_len += (ui32_t)buflen;
727 NYD_LEAVE;
728 return self;
731 FL struct n_string *
732 (n_string_unshift_c)(struct n_string *self, char c n_MEMORY_DEBUG_ARGS){
733 NYD_ENTER;
735 assert(self != NULL);
737 if(self->s_len + 1 >= self->s_size)
738 self = (n_string_reserve)(self, 1 n_MEMORY_DEBUG_ARGSCALL);
739 if(self->s_len > 0)
740 memmove(&self->s_dat[1], self->s_dat, self->s_len);
741 self->s_dat[0] = c;
742 ++self->s_len;
743 NYD_LEAVE;
744 return self;
747 FL struct n_string *
748 (n_string_insert_buf)(struct n_string *self, size_t idx,
749 char const *buf, size_t buflen n_MEMORY_DEBUG_ARGS){
750 NYD_ENTER;
752 assert(self != NULL);
753 assert(buflen == 0 || buf != NULL);
754 assert(idx <= self->s_len);
756 if(buflen == UIZ_MAX)
757 buflen = (buf == NULL) ? 0 : strlen(buf);
759 if(buflen > 0){
760 self = (n_string_reserve)(self, buflen n_MEMORY_DEBUG_ARGSCALL);
761 if(self->s_len > 0)
762 memmove(&self->s_dat[idx + buflen], &self->s_dat[idx],
763 self->s_len - idx);
764 memcpy(&self->s_dat[idx], buf, buflen);
765 self->s_len += (ui32_t)buflen;
767 NYD_LEAVE;
768 return self;
771 FL struct n_string *
772 (n_string_insert_c)(struct n_string *self, size_t idx,
773 char c n_MEMORY_DEBUG_ARGS){
774 NYD_ENTER;
776 assert(self != NULL);
777 assert(idx <= self->s_len);
779 if(self->s_len + 1 >= self->s_size)
780 self = (n_string_reserve)(self, 1 n_MEMORY_DEBUG_ARGSCALL);
781 if(self->s_len > 0)
782 memmove(&self->s_dat[idx + 1], &self->s_dat[idx], self->s_len - idx);
783 self->s_dat[idx] = c;
784 ++self->s_len;
785 NYD_LEAVE;
786 return self;
789 FL struct n_string *
790 n_string_cut(struct n_string *self, size_t idx, size_t len){
791 NYD_ENTER;
793 assert(self != NULL);
794 assert(UIZ_MAX - idx > len);
795 assert(SI32_MAX >= idx + len);
796 assert(idx + len <= self->s_len);
798 if(len > 0)
799 memmove(&self->s_dat[idx], &self->s_dat[idx + len],
800 (self->s_len -= len) - idx);
801 NYD_LEAVE;
802 return self;
805 FL char *
806 (n_string_cp)(struct n_string *self n_MEMORY_DEBUG_ARGS){
807 char *rv;
808 NYD2_ENTER;
810 assert(self != NULL);
812 if(self->s_size == 0)
813 self = (n_string_reserve)(self, 1 n_MEMORY_DEBUG_ARGSCALL);
815 (rv = self->s_dat)[self->s_len] = '\0';
816 NYD2_LEAVE;
817 return rv;
820 FL char const *
821 n_string_cp_const(struct n_string const *self){
822 char const *rv;
823 NYD2_ENTER;
825 assert(self != NULL);
827 if(self->s_size != 0){
828 ((struct n_string*)n_UNCONST(self))->s_dat[self->s_len] = '\0';
829 rv = self->s_dat;
830 }else
831 rv = n_empty;
832 NYD2_LEAVE;
833 return rv;
837 * UTF-8
840 FL ui32_t
841 n_utf8_to_utf32(char const **bdat, size_t *blen) /* TODO check false UTF8 */
843 char const *cp;
844 size_t l;
845 ui32_t c, x;
846 NYD2_ENTER;
848 cp = *bdat;
849 l = *blen - 1;
850 x = (ui8_t)*cp++;
852 if (x <= 0x7Fu)
853 c = x;
854 else {
855 if ((x & 0xE0u) == 0xC0u) {
856 if (l < 1)
857 goto jerr;
858 l -= 1;
859 c = x & ~0xC0u;
860 } else if ((x & 0xF0u) == 0xE0u) {
861 if (l < 2)
862 goto jerr;
863 l -= 2;
864 c = x & ~0xE0u;
865 c <<= 6;
866 x = (ui8_t)*cp++;
867 c |= x & 0x7Fu;
868 } else {
869 if (l < 3)
870 goto jerr;
871 l -= 3;
872 c = x & ~0xF0u;
873 c <<= 6;
874 x = (ui8_t)*cp++;
875 c |= x & 0x7Fu;
876 c <<= 6;
877 x = (ui8_t)*cp++;
878 c |= x & 0x7Fu;
880 c <<= 6;
881 x = (ui8_t)*cp++;
882 c |= x & 0x7Fu;
885 jleave:
886 *bdat = cp;
887 *blen = l;
888 NYD2_LEAVE;
889 return c;
890 jerr:
891 c = UI32_MAX;
892 goto jleave;
895 FL size_t
896 n_utf32_to_utf8(ui32_t c, char *buf)
898 struct {
899 ui32_t lower_bound;
900 ui32_t upper_bound;
901 ui8_t enc_leader;
902 ui8_t enc_lval;
903 ui8_t dec_leader_mask;
904 ui8_t dec_leader_val_mask;
905 ui8_t dec_bytes_togo;
906 ui8_t cat_index;
907 ui8_t __dummy[2];
908 } const _cat[] = {
909 {0x00000000, 0x00000000, 0x00, 0, 0x00, 0x00, 0, 0, {0,}},
910 {0x00000000, 0x0000007F, 0x00, 1, 0x80, 0x7F, 1-1, 1, {0,}},
911 {0x00000080, 0x000007FF, 0xC0, 2, 0xE0, 0xFF-0xE0, 2-1, 2, {0,}},
912 /* We assume surrogates are U+D800 - U+DFFF, _cat index 3 */
913 /* xxx _from_utf32() simply assumes magic code points for surrogates!
914 * xxx (However, should we ever get yet another surrogate range we
915 * xxx need to deal with that all over the place anyway? */
916 {0x00000800, 0x0000FFFF, 0xE0, 3, 0xF0, 0xFF-0xF0, 3-1, 3, {0,}},
917 {0x00010000, 0x0010FFFF, 0xF0, 4, 0xF8, 0xFF-0xF8, 4-1, 4, {0,}},
918 }, *catp = _cat;
919 size_t l;
921 if (c <= _cat[0].upper_bound) { catp += 0; goto j0; }
922 if (c <= _cat[1].upper_bound) { catp += 1; goto j1; }
923 if (c <= _cat[2].upper_bound) { catp += 2; goto j2; }
924 if (c <= _cat[3].upper_bound) {
925 /* Surrogates may not be converted (Compatibility rule C10) */
926 if (c >= 0xD800u && c <= 0xDFFFu)
927 goto jerr;
928 catp += 3;
929 goto j3;
931 if (c <= _cat[4].upper_bound) { catp += 4; goto j4; }
932 jerr:
933 c = 0xFFFDu; /* Unicode replacement character */
934 catp += 3;
935 goto j3;
937 buf[3] = (char)0x80u | (char)(c & 0x3Fu); c >>= 6;
939 buf[2] = (char)0x80u | (char)(c & 0x3Fu); c >>= 6;
941 buf[1] = (char)0x80u | (char)(c & 0x3Fu); c >>= 6;
943 buf[0] = (char)catp->enc_leader | (char)(c);
945 buf[catp->enc_lval] = '\0';
946 l = catp->enc_lval;
947 NYD2_LEAVE;
948 return l;
952 * Our iconv(3) wrapper
954 #ifdef HAVE_ICONV
956 static void _ic_toupper(char *dest, char const *src);
957 static void _ic_stripdash(char *p);
959 static void
960 _ic_toupper(char *dest, char const *src)
962 NYD2_ENTER;
964 *dest++ = upperconv(*src);
965 while (*src++ != '\0');
966 NYD2_LEAVE;
969 static void
970 _ic_stripdash(char *p)
972 char *q = p;
973 NYD2_ENTER;
976 if (*(q = p) != '-')
977 ++q;
978 while (*p++ != '\0');
979 NYD2_LEAVE;
982 FL iconv_t
983 n_iconv_open(char const *tocode, char const *fromcode)
985 iconv_t id;
986 char *t, *f;
987 NYD_ENTER;
989 if ((!asccasecmp(fromcode, "unknown-8bit") ||
990 !asccasecmp(fromcode, "binary")) &&
991 (fromcode = ok_vlook(charset_unknown_8bit)) == NULL)
992 fromcode = ok_vlook(CHARSET_8BIT_OKEY);
994 if ((id = iconv_open(tocode, fromcode)) != (iconv_t)-1)
995 goto jleave;
997 /* Remove the "iso-" prefixes for Solaris */
998 if (!ascncasecmp(tocode, "iso-", 4))
999 tocode += 4;
1000 else if (!ascncasecmp(tocode, "iso", 3))
1001 tocode += 3;
1002 if (!ascncasecmp(fromcode, "iso-", 4))
1003 fromcode += 4;
1004 else if (!ascncasecmp(fromcode, "iso", 3))
1005 fromcode += 3;
1006 if (*tocode == '\0' || *fromcode == '\0') {
1007 id = (iconv_t)-1;
1008 goto jleave;
1010 if ((id = iconv_open(tocode, fromcode)) != (iconv_t)-1)
1011 goto jleave;
1013 /* Solaris prefers upper-case charset names. Don't ask... */
1014 t = salloc(strlen(tocode) +1);
1015 _ic_toupper(t, tocode);
1016 f = salloc(strlen(fromcode) +1);
1017 _ic_toupper(f, fromcode);
1018 if ((id = iconv_open(t, f)) != (iconv_t)-1)
1019 goto jleave;
1021 /* Strip dashes for UnixWare */
1022 _ic_stripdash(t);
1023 _ic_stripdash(f);
1024 if ((id = iconv_open(t, f)) != (iconv_t)-1)
1025 goto jleave;
1027 /* Add your vendor's sillynesses here */
1029 /* If the encoding names are equal at this point, they are just not
1030 * understood by iconv(), and we cannot sensibly use it in any way. We do
1031 * not perform this as an optimization above since iconv() can otherwise be
1032 * used to check the validity of the input even with identical encoding
1033 * names */
1034 if (!strcmp(t, f))
1035 errno = 0;
1036 jleave:
1037 NYD_LEAVE;
1038 return id;
1041 FL void
1042 n_iconv_close(iconv_t cd)
1044 NYD_ENTER;
1045 iconv_close(cd);
1046 if (cd == iconvd)
1047 iconvd = (iconv_t)-1;
1048 NYD_LEAVE;
1051 FL void
1052 n_iconv_reset(iconv_t cd)
1054 NYD_ENTER;
1055 iconv(cd, NULL, NULL, NULL, NULL);
1056 NYD_LEAVE;
1059 /* (2012-09-24: export and use it exclusively to isolate prototype problems
1060 * (*inb* is 'char const **' except in POSIX) in a single place.
1061 * GNU libiconv even allows for configuration time const/non-const..
1062 * In the end it's an ugly guess, but we can't do better since make(1) doesn't
1063 * support compiler invocations which bail on error, so no -Werror */
1064 /* Citrus project? */
1065 # if defined _ICONV_H_ && defined __ICONV_F_HIDE_INVALID
1066 /* DragonFly 3.2.1 is special TODO newer DragonFly too, but different */
1067 # if n_OS_DRAGONFLY
1068 # define __INBCAST(S) (char ** __restrict__)n_UNCONST(S)
1069 # else
1070 # define __INBCAST(S) (char const **)n_UNCONST(S)
1071 # endif
1072 # elif n_OS_SUNOS || n_OS_SOLARIS
1073 # define __INBCAST(S) (char const ** __restrict__)n_UNCONST(S)
1074 # endif
1075 # ifndef __INBCAST
1076 # define __INBCAST(S) (char **)n_UNCONST(S)
1077 # endif
1079 FL int
1080 n_iconv_buf(iconv_t cd, enum n_iconv_flags icf,
1081 char const **inb, size_t *inbleft, char **outb, size_t *outbleft){
1082 int err;
1083 NYD2_ENTER;
1085 if((icf & n_ICONV_UNIREPL) && !(options & OPT_UNICODE))
1086 icf &= ~n_ICONV_UNIREPL;
1088 for(;;){
1089 size_t sz;
1091 sz = iconv(cd, __INBCAST(inb), inbleft, outb, outbleft);
1092 if(sz > 0 && !(icf & n_ICONV_IGN_NOREVERSE)){
1093 err = ENOENT;
1094 goto jleave;
1096 if(sz != (size_t)-1)
1097 break;
1099 err = errno;
1100 if(!(icf & n_ICONV_IGN_ILSEQ) || err != EILSEQ)
1101 goto jleave;
1102 if(*inbleft > 0){
1103 ++(*inb);
1104 --(*inbleft);
1105 if(icf & n_ICONV_UNIREPL){
1106 if(*outbleft >= sizeof(n_unirepl) -1){
1107 memcpy(*outb, n_unirepl, sizeof(n_unirepl) -1);
1108 *outb += sizeof(n_unirepl) -1;
1109 *outbleft -= sizeof(n_unirepl) -1;
1110 continue;
1112 }else if(*outbleft > 0){
1113 *(*outb)++ = '?';
1114 --*outbleft;
1115 continue;
1117 err = E2BIG;
1118 goto jleave;
1119 }else if(*outbleft > 0){
1120 **outb = '\0';
1121 goto jleave;
1124 err = 0;
1125 jleave:
1126 NYD2_LEAVE;
1127 return err;
1129 # undef __INBCAST
1131 FL int
1132 n_iconv_str(iconv_t cd, enum n_iconv_flags icf,
1133 struct str *out, struct str const *in, struct str *in_rest_or_null)
1135 int err;
1136 char *obb, *ob;
1137 char const *ib;
1138 size_t olb, ol, il;
1139 NYD2_ENTER;
1141 err = 0;
1142 obb = out->s;
1143 olb = out->l;
1144 ol = in->l;
1146 ol = (ol << 1) - (ol >> 4);
1147 if (olb <= ol) {
1148 olb = ol;
1149 goto jrealloc;
1152 for (;;) {
1153 ib = in->s;
1154 il = in->l;
1155 ob = obb;
1156 ol = olb;
1157 if((err = n_iconv_buf(cd, icf, &ib, &il, &ob, &ol)) == 0 || err != E2BIG)
1158 break;
1159 err = 0;
1160 olb += in->l;
1161 jrealloc:
1162 obb = n_realloc(obb, olb +1);
1165 if (in_rest_or_null != NULL) {
1166 in_rest_or_null->s = n_UNCONST(ib);
1167 in_rest_or_null->l = il;
1169 out->s = obb;
1170 out->s[out->l = olb - ol] = '\0';
1171 NYD2_LEAVE;
1172 return err;
1175 FL char *
1176 n_iconv_onetime_cp(enum n_iconv_flags icf,
1177 char const *tocode, char const *fromcode, char const *input){
1178 struct str out, in;
1179 iconv_t icd;
1180 char *rv;
1181 NYD2_ENTER;
1183 rv = NULL;
1184 if(tocode == NULL)
1185 tocode = ok_vlook(ttycharset);
1186 if(fromcode == NULL)
1187 fromcode = "utf-8";
1189 if((icd = iconv_open(tocode, fromcode)) == (iconv_t)-1)
1190 goto jleave;
1192 in.l = strlen(in.s = n_UNCONST(input)); /* logical */
1193 out.s = NULL, out.l = 0;
1194 if(!n_iconv_str(icd, icf, &out, &in, NULL))
1195 rv = savestrbuf(out.s, out.l);
1196 if(out.s != NULL)
1197 free(out.s);
1199 iconv_close(icd);
1200 jleave:
1201 NYD2_LEAVE;
1202 return rv;
1204 #endif /* HAVE_ICONV */
1206 /* s-it-mode */