main(), nail.1: unify synopsis arg-repitition indicator(s); nits
[s-mailx.git] / strings.c
blob3058d5a17a1957ee608fee0616af91cb3ecd310b
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_from_pool)(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_from_pool)(NULL, sbuf_len +1
65 n_MEMORY_DEBUG_ARGSCALL);
66 memcpy(news, sbuf, sbuf_len);
67 news[sbuf_len] = 0;
68 NYD_LEAVE;
69 return news;
72 FL char *
73 (savecatsep)(char const *s1, char sep, char const *s2 n_MEMORY_DEBUG_ARGS)
75 size_t l1, l2;
76 char *news;
77 NYD_ENTER;
79 l1 = (s1 != NULL) ? strlen(s1) : 0;
80 l2 = strlen(s2);
81 news = (n_autorec_alloc_from_pool)(NULL, l1 + (sep != '\0') + l2 +1
82 n_MEMORY_DEBUG_ARGSCALL);
83 if (l1 > 0) {
84 memcpy(news + 0, s1, l1);
85 if (sep != '\0')
86 news[l1++] = sep;
88 memcpy(news + l1, s2, l2);
89 news[l1 + l2] = '\0';
90 NYD_LEAVE;
91 return news;
95 * Support routines, auto-reclaimed storage
98 FL char *
99 (i_strdup)(char const *src n_MEMORY_DEBUG_ARGS)
101 size_t sz;
102 char *dest;
103 NYD_ENTER;
105 sz = strlen(src) +1;
106 dest = (n_autorec_alloc_from_pool)(NULL, sz n_MEMORY_DEBUG_ARGSCALL);
107 i_strcpy(dest, src, sz);
108 NYD_LEAVE;
109 return dest;
112 FL struct str *
113 str_concat_csvl(struct str *self, ...) /* XXX onepass maybe better here */
115 va_list vl;
116 size_t l;
117 char const *cs;
118 NYD_ENTER;
120 va_start(vl, self);
121 for (l = 0; (cs = va_arg(vl, char const*)) != NULL;)
122 l += strlen(cs);
123 va_end(vl);
125 self->l = l;
126 self->s = salloc(l +1);
128 va_start(vl, self);
129 for (l = 0; (cs = va_arg(vl, char const*)) != NULL;) {
130 size_t i = strlen(cs);
131 memcpy(self->s + l, cs, i);
132 l += i;
134 self->s[l] = '\0';
135 va_end(vl);
136 NYD_LEAVE;
137 return self;
140 FL struct str *
141 (str_concat_cpa)(struct str *self, char const * const *cpa,
142 char const *sep_o_null n_MEMORY_DEBUG_ARGS)
144 size_t sonl, l;
145 char const * const *xcpa;
146 NYD_ENTER;
148 sonl = (sep_o_null != NULL) ? strlen(sep_o_null) : 0;
150 for (l = 0, xcpa = cpa; *xcpa != NULL; ++xcpa)
151 l += strlen(*xcpa) + sonl;
153 self->l = l;
154 self->s = (n_autorec_alloc_from_pool)(NULL, l +1 n_MEMORY_DEBUG_ARGSCALL);
156 for (l = 0, xcpa = cpa; *xcpa != NULL; ++xcpa) {
157 size_t i = strlen(*xcpa);
158 memcpy(self->s + l, *xcpa, i);
159 l += i;
160 if (sonl > 0) {
161 memcpy(self->s + l, sep_o_null, sonl);
162 l += sonl;
165 self->s[l] = '\0';
166 NYD_LEAVE;
167 return self;
171 * Routines that are not related to auto-reclaimed storage follow.
174 FL bool_t
175 n_anyof_buf(char const *template, char const *dat, size_t len){
176 char c;
177 NYD2_ENTER;
179 if(len == UIZ_MAX){
180 while((c = *template++) != '\0')
181 if(strchr(dat, c) != NULL)
182 break;
183 }else if(len > 0){
184 while((c = *template++) != '\0')
185 if(memchr(dat, c, len) != NULL)
186 break;
187 }else
188 c = '\0';
189 NYD2_LEAVE;
190 return (c != '\0');
193 FL char *
194 n_strsep(char **iolist, char sep, bool_t ignore_empty)
196 char *base, *cp;
197 NYD2_ENTER;
199 for (base = *iolist; base != NULL; base = *iolist) {
200 while (*base != '\0' && blankspacechar(*base))
201 ++base;
202 cp = strchr(base, sep);
203 if (cp != NULL)
204 *iolist = cp + 1;
205 else {
206 *iolist = NULL;
207 cp = base + strlen(base);
209 while (cp > base && blankspacechar(cp[-1]))
210 --cp;
211 *cp = '\0';
212 if (*base != '\0' || !ignore_empty)
213 break;
215 NYD2_LEAVE;
216 return base;
219 FL void
220 i_strcpy(char *dest, char const *src, size_t size)
222 NYD2_ENTER;
223 if (size > 0) {
224 for (;; ++dest, ++src)
225 if ((*dest = lowerconv(*src)) == '\0') {
226 break;
227 } else if (--size == 0) {
228 *dest = '\0';
229 break;
232 NYD2_LEAVE;
235 FL int
236 is_prefix(char const *as1, char const *as2) /* TODO arg order */
238 char c;
239 NYD2_ENTER;
241 for (; (c = *as1) == *as2 && c != '\0'; ++as1, ++as2)
242 if (*as2 == '\0')
243 break;
244 NYD2_LEAVE;
245 return (c == '\0');
248 FL char *
249 string_quote(char const *v) /* TODO too simpleminded (getrawlist(), +++ ..) */
251 char const *cp;
252 size_t i;
253 char c, *rv;
254 NYD2_ENTER;
256 for (i = 0, cp = v; (c = *cp) != '\0'; ++i, ++cp)
257 if (c == '"' || c == '\\')
258 ++i;
259 rv = salloc(i +1);
261 for (i = 0, cp = v; (c = *cp) != '\0'; rv[i++] = c, ++cp)
262 if (c == '"' || c == '\\')
263 rv[i++] = '\\';
264 rv[i] = '\0';
265 NYD2_LEAVE;
266 return rv;
269 FL char *
270 laststring(char *linebuf, bool_t *needs_list, bool_t strip)
272 char *cp, *p, quoted;
273 NYD_ENTER;
275 /* Anything to do at all? */
276 if (*(cp = linebuf) == '\0')
277 goto jnull;
278 cp += strlen(linebuf) -1;
280 /* Strip away trailing blanks */
281 while (spacechar(*cp) && cp > linebuf)
282 --cp;
283 cp[1] = '\0';
284 if (cp == linebuf)
285 goto jleave;
287 /* Now search for the BOS of the "last string" */
288 quoted = *cp;
289 if (quoted == '\'' || quoted == '"') {
290 if (strip)
291 *cp = '\0';
292 } else
293 quoted = ' ';
295 while (cp > linebuf) {
296 --cp;
297 if (quoted != ' ') {
298 if (*cp != quoted)
299 continue;
300 } else if (!spacechar(*cp))
301 continue;
302 if (cp == linebuf || cp[-1] != '\\') {
303 /* When in whitespace mode, WS prefix doesn't belong */
304 if (quoted == ' ')
305 ++cp;
306 break;
308 /* Expand the escaped quote character */
309 for (p = --cp; (p[0] = p[1]) != '\0'; ++p)
312 if (strip && quoted != ' ' && *cp == quoted)
313 for (p = cp; (p[0] = p[1]) != '\0'; ++p)
316 /* The "last string" has been skipped over, but still, try to step backwards
317 * until we are at BOS or see whitespace, so as to make possible things like
318 * "? copy +'x y.mbox'" or even "? copy +x\ y.mbox" */
319 while (cp > linebuf) {
320 --cp;
321 if (spacechar(*cp)) {
322 p = cp;
323 *cp++ = '\0';
324 /* We can furtherly release our callees if we now decide whether the
325 * remaining non-"last string" line content contains non-WS */
326 while (--p >= linebuf)
327 if (!spacechar(*p))
328 goto jleave;
329 linebuf = cp;
330 break;
334 jleave:
335 if (cp != NULL && *cp == '\0')
336 goto jnull;
337 *needs_list = (cp != linebuf && *linebuf != '\0');
338 j_leave:
339 NYD_LEAVE;
340 return cp;
341 jnull:
342 *needs_list = FAL0;
343 cp = NULL;
344 goto j_leave;
347 FL void
348 makelow(char *cp) /* TODO isn't that crap? --> */
350 NYD_ENTER;
351 #ifdef HAVE_C90AMEND1
352 if (n_mb_cur_max > 1) {
353 char *tp = cp;
354 wchar_t wc;
355 int len;
357 while (*cp != '\0') {
358 len = mbtowc(&wc, cp, n_mb_cur_max);
359 if (len < 0)
360 *tp++ = *cp++;
361 else {
362 wc = towlower(wc);
363 if (wctomb(tp, wc) == len)
364 tp += len, cp += len;
365 else
366 *tp++ = *cp++; /* <-- at least here */
369 } else
370 #endif
373 *cp = tolower((uc_i)*cp);
374 while (*cp++ != '\0');
376 NYD_LEAVE;
379 FL bool_t
380 substr(char const *str, char const *sub)
382 char const *cp, *backup;
383 NYD_ENTER;
385 cp = sub;
386 backup = str;
387 while (*str != '\0' && *cp != '\0') {
388 #ifdef HAVE_C90AMEND1
389 if (n_mb_cur_max > 1) {
390 wchar_t c, c2;
391 int sz;
393 if ((sz = mbtowc(&c, cp, n_mb_cur_max)) == -1)
394 goto Jsinglebyte;
395 cp += sz;
396 if ((sz = mbtowc(&c2, str, n_mb_cur_max)) == -1)
397 goto Jsinglebyte;
398 str += sz;
399 c = towupper(c);
400 c2 = towupper(c2);
401 if (c != c2) {
402 if ((sz = mbtowc(&c, backup, n_mb_cur_max)) > 0) {
403 backup += sz;
404 str = backup;
405 } else
406 str = ++backup;
407 cp = sub;
409 } else
410 Jsinglebyte:
411 #endif
413 int c, c2;
415 c = *cp++ & 0377;
416 if (islower(c))
417 c = toupper(c);
418 c2 = *str++ & 0377;
419 if (islower(c2))
420 c2 = toupper(c2);
421 if (c != c2) {
422 str = ++backup;
423 cp = sub;
427 NYD_LEAVE;
428 return (*cp == '\0');
431 FL char *
432 sstpcpy(char *dst, char const *src)
434 NYD2_ENTER;
435 while ((*dst = *src++) != '\0')
436 ++dst;
437 NYD2_LEAVE;
438 return dst;
441 FL char *
442 (sstrdup)(char const *cp n_MEMORY_DEBUG_ARGS)
444 char *dp;
445 NYD2_ENTER;
447 dp = (cp == NULL) ? NULL : (sbufdup)(cp, strlen(cp) n_MEMORY_DEBUG_ARGSCALL);
448 NYD2_LEAVE;
449 return dp;
452 FL char *
453 (sbufdup)(char const *cp, size_t len n_MEMORY_DEBUG_ARGS)
455 char *dp = NULL;
456 NYD2_ENTER;
458 dp = (n_alloc)(len +1 n_MEMORY_DEBUG_ARGSCALL);
459 if (cp != NULL)
460 memcpy(dp, cp, len);
461 dp[len] = '\0';
462 NYD2_LEAVE;
463 return dp;
466 FL ssize_t
467 n_strscpy(char *dst, char const *src, size_t dstsize){
468 ssize_t rv;
469 NYD2_ENTER;
471 if(n_LIKELY(dstsize > 0)){
472 rv = 0;
474 if((dst[rv] = src[rv]) == '\0')
475 goto jleave;
476 ++rv;
477 }while(--dstsize > 0);
478 dst[--rv] = '\0';
480 #ifdef HAVE_DEVEL
481 else
482 assert(dstsize > 0);
483 #endif
484 rv = -1;
485 jleave:
486 NYD2_LEAVE;
487 return rv;
490 FL int
491 asccasecmp(char const *s1, char const *s2)
493 int cmp;
494 NYD2_ENTER;
496 for (;;) {
497 char c1 = *s1++, c2 = *s2++;
498 if ((cmp = lowerconv(c1) - lowerconv(c2)) != 0 || c1 == '\0')
499 break;
501 NYD2_LEAVE;
502 return cmp;
505 FL int
506 ascncasecmp(char const *s1, char const *s2, size_t sz)
508 int cmp = 0;
509 NYD2_ENTER;
511 while (sz-- > 0) {
512 char c1 = *s1++, c2 = *s2++;
513 cmp = (ui8_t)lowerconv(c1);
514 cmp -= (ui8_t)lowerconv(c2);
515 if (cmp != 0 || c1 == '\0')
516 break;
518 NYD2_LEAVE;
519 return cmp;
522 FL char const *
523 asccasestr(char const *s1, char const *s2)
525 char c2, c1;
526 NYD2_ENTER;
528 for (c2 = *s2++, c2 = lowerconv(c2);;) {
529 if ((c1 = *s1++) == '\0') {
530 s1 = NULL;
531 break;
533 if (lowerconv(c1) == c2 && is_asccaseprefix(s2, s1)) {
534 --s1;
535 break;
538 NYD2_LEAVE;
539 return s1;
542 FL bool_t
543 is_asccaseprefix(char const *as1, char const *as2) /* TODO arg order */
545 char c1, c2;
546 NYD2_ENTER;
548 for(;; ++as1, ++as2){
549 c1 = *as1;
550 c1 = lowerconv(c1);
551 c2 = *as2;
552 c2 = lowerconv(c2);
554 if(c1 != c2 || c1 == '\0')
555 break;
556 if(c2 == '\0')
557 break;
559 NYD2_LEAVE;
560 return (c1 == '\0');
563 FL bool_t
564 is_ascncaseprefix(char const *as1, char const *as2, size_t sz)
566 char c1, c2;
567 bool_t rv;
568 NYD2_ENTER;
570 for(rv = TRU1; sz-- > 0; ++as1, ++as2){
571 c1 = *as1;
572 c1 = lowerconv(c1);
573 c2 = *as2;
574 c2 = lowerconv(c2);
576 if(!(rv = (c1 == c2)) || c1 == '\0')
577 break;
578 if(c2 == '\0')
579 break;
581 NYD2_LEAVE;
582 return rv;
586 FL struct str *
587 (n_str_assign_buf)(struct str *self, char const *buf, uiz_t buflen
588 n_MEMORY_DEBUG_ARGS){
589 NYD_ENTER;
590 if(buflen == UIZ_MAX)
591 buflen = (buf == NULL) ? 0 : strlen(buf);
593 assert(buflen == 0 || buf != NULL);
595 if(n_LIKELY(buflen > 0)){
596 self->s = (n_realloc)(self->s, (self->l = buflen) +1
597 n_MEMORY_DEBUG_ARGSCALL);
598 memcpy(self->s, buf, buflen);
599 self->s[buflen] = '\0';
600 }else
601 self->l = 0;
602 NYD_LEAVE;
603 return self;
606 FL struct str *
607 (n_str_add_buf)(struct str *self, char const *buf, uiz_t buflen
608 n_MEMORY_DEBUG_ARGS){
609 NYD_ENTER;
610 if(buflen == UIZ_MAX)
611 buflen = (buf == NULL) ? 0 : strlen(buf);
613 assert(buflen == 0 || buf != NULL);
615 if(buflen > 0) {
616 size_t osl = self->l, nsl = osl + buflen;
618 self->s = (n_realloc)(self->s, (self->l = nsl) +1
619 n_MEMORY_DEBUG_ARGSCALL);
620 memcpy(self->s + osl, buf, buflen);
621 self->s[nsl] = '\0';
623 NYD_LEAVE;
624 return self;
628 * struct n_string TODO extend, optimize
631 FL struct n_string *
632 (n_string_clear)(struct n_string *self n_MEMORY_DEBUG_ARGS){
633 NYD_ENTER;
635 assert(self != NULL);
637 if(self->s_size != 0){
638 if(!self->s_auto){
639 (n_free)(self->s_dat n_MEMORY_DEBUG_ARGSCALL);
641 self->s_len = self->s_auto = self->s_size = 0;
642 self->s_dat = NULL;
644 NYD_LEAVE;
645 return self;
648 FL struct n_string *
649 (n_string_reserve)(struct n_string *self, size_t noof n_MEMORY_DEBUG_ARGS){
650 ui32_t i, l, s;
651 NYD_ENTER;
653 assert(self != NULL);
655 s = self->s_size;
656 l = self->s_len;
657 #if 0 /* FIXME memory alloc too large */
658 if(SI32_MAX - n_ALIGN(1) - l <= noof)
659 n_panic(_("Memory allocation too large"));
660 #endif
662 if((i = s - l) <= ++noof){
663 i += l + (ui32_t)noof;
664 i = n_ALIGN(i);
665 self->s_size = i -1;
667 if(!self->s_auto)
668 self->s_dat = (n_realloc)(self->s_dat, i n_MEMORY_DEBUG_ARGSCALL);
669 else{
670 char *ndat = (n_autorec_alloc_from_pool)(NULL, i
671 n_MEMORY_DEBUG_ARGSCALL);
673 if(l > 0)
674 memcpy(ndat, self->s_dat, l);
675 self->s_dat = ndat;
678 NYD_LEAVE;
679 return self;
682 FL struct n_string *
683 (n_string_resize)(struct n_string *self, size_t nlen n_MEMORY_DEBUG_ARGS){
684 NYD_ENTER;
686 assert(self != NULL);
687 #if 0 /* FIXME memory alloc too large */
688 if(SI32_MAX - n_ALIGN(1) - l <= noof)
689 n_panic(_("Memory allocation too large"));
690 #endif
692 if(self->s_len < nlen)
693 self = (n_string_reserve)(self, nlen n_MEMORY_DEBUG_ARGSCALL);
694 self->s_len = (ui32_t)nlen;
695 NYD_LEAVE;
696 return self;
699 FL struct n_string *
700 (n_string_push_buf)(struct n_string *self, char const *buf, size_t buflen
701 n_MEMORY_DEBUG_ARGS){
702 NYD_ENTER;
704 assert(self != NULL);
705 assert(buflen == 0 || buf != NULL);
707 if(buflen == UIZ_MAX)
708 buflen = (buf == NULL) ? 0 : strlen(buf);
710 if(buflen > 0){
711 ui32_t i;
713 self = (n_string_reserve)(self, buflen n_MEMORY_DEBUG_ARGSCALL);
714 memcpy(&self->s_dat[i = self->s_len], buf, buflen);
715 self->s_len = (i += (ui32_t)buflen);
717 NYD_LEAVE;
718 return self;
721 FL struct n_string *
722 (n_string_push_c)(struct n_string *self, char c n_MEMORY_DEBUG_ARGS){
723 NYD_ENTER;
725 assert(self != NULL);
727 if(self->s_len + 1 >= self->s_size)
728 self = (n_string_reserve)(self, 1 n_MEMORY_DEBUG_ARGSCALL);
729 self->s_dat[self->s_len++] = c;
730 NYD_LEAVE;
731 return self;
734 FL struct n_string *
735 (n_string_unshift_buf)(struct n_string *self, char const *buf, size_t buflen
736 n_MEMORY_DEBUG_ARGS){
737 NYD_ENTER;
739 assert(self != NULL);
740 assert(buflen == 0 || buf != NULL);
742 if(buflen == UIZ_MAX)
743 buflen = (buf == NULL) ? 0 : strlen(buf);
745 if(buflen > 0){
746 self = (n_string_reserve)(self, buflen n_MEMORY_DEBUG_ARGSCALL);
747 if(self->s_len > 0)
748 memmove(&self->s_dat[buflen], self->s_dat, self->s_len);
749 memcpy(self->s_dat, buf, buflen);
750 self->s_len += (ui32_t)buflen;
752 NYD_LEAVE;
753 return self;
756 FL struct n_string *
757 (n_string_unshift_c)(struct n_string *self, char c n_MEMORY_DEBUG_ARGS){
758 NYD_ENTER;
760 assert(self != NULL);
762 if(self->s_len + 1 >= self->s_size)
763 self = (n_string_reserve)(self, 1 n_MEMORY_DEBUG_ARGSCALL);
764 if(self->s_len > 0)
765 memmove(&self->s_dat[1], self->s_dat, self->s_len);
766 self->s_dat[0] = c;
767 ++self->s_len;
768 NYD_LEAVE;
769 return self;
772 FL struct n_string *
773 (n_string_insert_buf)(struct n_string *self, size_t idx,
774 char const *buf, size_t buflen n_MEMORY_DEBUG_ARGS){
775 NYD_ENTER;
777 assert(self != NULL);
778 assert(buflen == 0 || buf != NULL);
779 assert(idx <= self->s_len);
781 if(buflen == UIZ_MAX)
782 buflen = (buf == NULL) ? 0 : strlen(buf);
784 if(buflen > 0){
785 self = (n_string_reserve)(self, buflen n_MEMORY_DEBUG_ARGSCALL);
786 if(self->s_len > 0)
787 memmove(&self->s_dat[idx + buflen], &self->s_dat[idx],
788 self->s_len - idx);
789 memcpy(&self->s_dat[idx], buf, buflen);
790 self->s_len += (ui32_t)buflen;
792 NYD_LEAVE;
793 return self;
796 FL struct n_string *
797 (n_string_insert_c)(struct n_string *self, size_t idx,
798 char c n_MEMORY_DEBUG_ARGS){
799 NYD_ENTER;
801 assert(self != NULL);
802 assert(idx <= self->s_len);
804 if(self->s_len + 1 >= self->s_size)
805 self = (n_string_reserve)(self, 1 n_MEMORY_DEBUG_ARGSCALL);
806 if(self->s_len > 0)
807 memmove(&self->s_dat[idx + 1], &self->s_dat[idx], self->s_len - idx);
808 self->s_dat[idx] = c;
809 ++self->s_len;
810 NYD_LEAVE;
811 return self;
814 FL struct n_string *
815 n_string_cut(struct n_string *self, size_t idx, size_t len){
816 NYD_ENTER;
818 assert(self != NULL);
819 assert(UIZ_MAX - idx > len);
820 assert(SI32_MAX >= idx + len);
821 assert(idx + len <= self->s_len);
823 if(len > 0)
824 memmove(&self->s_dat[idx], &self->s_dat[idx + len],
825 (self->s_len -= len) - idx);
826 NYD_LEAVE;
827 return self;
830 FL char *
831 (n_string_cp)(struct n_string *self n_MEMORY_DEBUG_ARGS){
832 char *rv;
833 NYD2_ENTER;
835 assert(self != NULL);
837 if(self->s_size == 0)
838 self = (n_string_reserve)(self, 1 n_MEMORY_DEBUG_ARGSCALL);
840 (rv = self->s_dat)[self->s_len] = '\0';
841 NYD2_LEAVE;
842 return rv;
845 FL char const *
846 n_string_cp_const(struct n_string const *self){
847 char const *rv;
848 NYD2_ENTER;
850 assert(self != NULL);
852 if(self->s_size != 0){
853 ((struct n_string*)n_UNCONST(self))->s_dat[self->s_len] = '\0';
854 rv = self->s_dat;
855 }else
856 rv = n_empty;
857 NYD2_LEAVE;
858 return rv;
862 * UTF-8
865 FL ui32_t
866 n_utf8_to_utf32(char const **bdat, size_t *blen) /* TODO check false UTF8 */
868 char const *cp;
869 size_t l;
870 ui32_t c, x;
871 NYD2_ENTER;
873 cp = *bdat;
874 l = *blen - 1;
875 x = (ui8_t)*cp++;
877 if (x <= 0x7Fu)
878 c = x;
879 else {
880 if ((x & 0xE0u) == 0xC0u) {
881 if (l < 1)
882 goto jerr;
883 l -= 1;
884 c = x & ~0xC0u;
885 } else if ((x & 0xF0u) == 0xE0u) {
886 if (l < 2)
887 goto jerr;
888 l -= 2;
889 c = x & ~0xE0u;
890 c <<= 6;
891 x = (ui8_t)*cp++;
892 c |= x & 0x7Fu;
893 } else {
894 if (l < 3)
895 goto jerr;
896 l -= 3;
897 c = x & ~0xF0u;
898 c <<= 6;
899 x = (ui8_t)*cp++;
900 c |= x & 0x7Fu;
901 c <<= 6;
902 x = (ui8_t)*cp++;
903 c |= x & 0x7Fu;
905 c <<= 6;
906 x = (ui8_t)*cp++;
907 c |= x & 0x7Fu;
910 jleave:
911 *bdat = cp;
912 *blen = l;
913 NYD2_LEAVE;
914 return c;
915 jerr:
916 c = UI32_MAX;
917 goto jleave;
920 FL size_t
921 n_utf32_to_utf8(ui32_t c, char *buf)
923 struct {
924 ui32_t lower_bound;
925 ui32_t upper_bound;
926 ui8_t enc_leader;
927 ui8_t enc_lval;
928 ui8_t dec_leader_mask;
929 ui8_t dec_leader_val_mask;
930 ui8_t dec_bytes_togo;
931 ui8_t cat_index;
932 ui8_t __dummy[2];
933 } const _cat[] = {
934 {0x00000000, 0x00000000, 0x00, 0, 0x00, 0x00, 0, 0, {0,}},
935 {0x00000000, 0x0000007F, 0x00, 1, 0x80, 0x7F, 1-1, 1, {0,}},
936 {0x00000080, 0x000007FF, 0xC0, 2, 0xE0, 0xFF-0xE0, 2-1, 2, {0,}},
937 /* We assume surrogates are U+D800 - U+DFFF, _cat index 3 */
938 /* xxx _from_utf32() simply assumes magic code points for surrogates!
939 * xxx (However, should we ever get yet another surrogate range we
940 * xxx need to deal with that all over the place anyway? */
941 {0x00000800, 0x0000FFFF, 0xE0, 3, 0xF0, 0xFF-0xF0, 3-1, 3, {0,}},
942 {0x00010000, 0x0010FFFF, 0xF0, 4, 0xF8, 0xFF-0xF8, 4-1, 4, {0,}},
943 }, *catp = _cat;
944 size_t l;
946 if (c <= _cat[0].upper_bound) { catp += 0; goto j0; }
947 if (c <= _cat[1].upper_bound) { catp += 1; goto j1; }
948 if (c <= _cat[2].upper_bound) { catp += 2; goto j2; }
949 if (c <= _cat[3].upper_bound) {
950 /* Surrogates may not be converted (Compatibility rule C10) */
951 if (c >= 0xD800u && c <= 0xDFFFu)
952 goto jerr;
953 catp += 3;
954 goto j3;
956 if (c <= _cat[4].upper_bound) { catp += 4; goto j4; }
957 jerr:
958 c = 0xFFFDu; /* Unicode replacement character */
959 catp += 3;
960 goto j3;
962 buf[3] = (char)0x80u | (char)(c & 0x3Fu); c >>= 6;
964 buf[2] = (char)0x80u | (char)(c & 0x3Fu); c >>= 6;
966 buf[1] = (char)0x80u | (char)(c & 0x3Fu); c >>= 6;
968 buf[0] = (char)catp->enc_leader | (char)(c);
970 buf[catp->enc_lval] = '\0';
971 l = catp->enc_lval;
972 NYD2_LEAVE;
973 return l;
977 * Our iconv(3) wrapper
980 #ifdef HAVE_ICONV
981 FL iconv_t
982 n_iconv_open(char const *tocode, char const *fromcode){
983 iconv_t id;
984 NYD_ENTER;
986 if((!asccasecmp(fromcode, "unknown-8bit") ||
987 !asccasecmp(fromcode, "binary")) &&
988 (fromcode = ok_vlook(charset_unknown_8bit)) == NULL)
989 fromcode = ok_vlook(CHARSET_8BIT_OKEY);
991 id = iconv_open(tocode, fromcode);
993 /* If the encoding names are equal at this point, they are just not
994 * understood by iconv(), and we cannot sensibly use it in any way. We do
995 * not perform this as an optimization above since iconv() can otherwise be
996 * used to check the validity of the input even with identical encoding
997 * names */
998 if (id == (iconv_t)-1 && !asccasecmp(tocode, fromcode))
999 n_err_no = n_ERR_NONE;
1000 NYD_LEAVE;
1001 return id;
1004 FL void
1005 n_iconv_close(iconv_t cd){
1006 NYD_ENTER;
1007 iconv_close(cd);
1008 if(cd == iconvd)
1009 iconvd = (iconv_t)-1;
1010 NYD_LEAVE;
1013 FL void
1014 n_iconv_reset(iconv_t cd){
1015 NYD_ENTER;
1016 iconv(cd, NULL, NULL, NULL, NULL);
1017 NYD_LEAVE;
1020 /* (2012-09-24: export and use it exclusively to isolate prototype problems
1021 * (*inb* is 'char const **' except in POSIX) in a single place.
1022 * GNU libiconv even allows for configuration time const/non-const..
1023 * In the end it's an ugly guess, but we can't do better since make(1) doesn't
1024 * support compiler invocations which bail on error, so no -Werror */
1025 /* Citrus project? */
1026 # if defined _ICONV_H_ && defined __ICONV_F_HIDE_INVALID
1027 /* DragonFly 3.2.1 is special TODO newer DragonFly too, but different */
1028 # if n_OS_DRAGONFLY
1029 # define __INBCAST(S) (char ** __restrict__)n_UNCONST(S)
1030 # else
1031 # define __INBCAST(S) (char const **)n_UNCONST(S)
1032 # endif
1033 # elif n_OS_SUNOS || n_OS_SOLARIS
1034 # define __INBCAST(S) (char const ** __restrict__)n_UNCONST(S)
1035 # endif
1036 # ifndef __INBCAST
1037 # define __INBCAST(S) (char **)n_UNCONST(S)
1038 # endif
1040 FL int
1041 n_iconv_buf(iconv_t cd, enum n_iconv_flags icf,
1042 char const **inb, size_t *inbleft, char **outb, size_t *outbleft){
1043 int err;
1044 NYD2_ENTER;
1046 if((icf & n_ICONV_UNIREPL) && !(n_psonce & n_PSO_UNICODE))
1047 icf &= ~n_ICONV_UNIREPL;
1049 for(;;){
1050 size_t sz;
1052 sz = iconv(cd, __INBCAST(inb), inbleft, outb, outbleft);
1053 if(sz > 0 && !(icf & n_ICONV_IGN_NOREVERSE)){
1054 err = n_ERR_NOENT;
1055 goto jleave;
1057 if(sz != (size_t)-1)
1058 break;
1060 err = n_err_no;
1061 if(!(icf & n_ICONV_IGN_ILSEQ) || err != n_ERR_ILSEQ)
1062 goto jleave;
1063 if(*inbleft > 0){
1064 ++(*inb);
1065 --(*inbleft);
1066 if(icf & n_ICONV_UNIREPL){
1067 if(*outbleft >= sizeof(n_unirepl) -1){
1068 memcpy(*outb, n_unirepl, sizeof(n_unirepl) -1);
1069 *outb += sizeof(n_unirepl) -1;
1070 *outbleft -= sizeof(n_unirepl) -1;
1071 continue;
1073 }else if(*outbleft > 0){
1074 *(*outb)++ = '?';
1075 --*outbleft;
1076 continue;
1078 err = E2BIG;
1079 goto jleave;
1080 }else if(*outbleft > 0){
1081 **outb = '\0';
1082 goto jleave;
1085 err = 0;
1086 jleave:
1087 NYD2_LEAVE;
1088 return err;
1090 # undef __INBCAST
1092 FL int
1093 n_iconv_str(iconv_t cd, enum n_iconv_flags icf,
1094 struct str *out, struct str const *in, struct str *in_rest_or_null)
1096 int err;
1097 char *obb, *ob;
1098 char const *ib;
1099 size_t olb, ol, il;
1100 NYD2_ENTER;
1102 err = 0;
1103 obb = out->s;
1104 olb = out->l;
1105 ol = in->l;
1107 ol = (ol << 1) - (ol >> 4);
1108 if (olb <= ol) {
1109 olb = ol;
1110 goto jrealloc;
1113 for (;;) {
1114 ib = in->s;
1115 il = in->l;
1116 ob = obb;
1117 ol = olb;
1118 if((err = n_iconv_buf(cd, icf, &ib, &il, &ob, &ol)) == 0 || err != E2BIG)
1119 break;
1120 err = 0;
1121 olb += in->l;
1122 jrealloc:
1123 obb = n_realloc(obb, olb +1);
1126 if (in_rest_or_null != NULL) {
1127 in_rest_or_null->s = n_UNCONST(ib);
1128 in_rest_or_null->l = il;
1130 out->s = obb;
1131 out->s[out->l = olb - ol] = '\0';
1132 NYD2_LEAVE;
1133 return err;
1136 FL char *
1137 n_iconv_onetime_cp(enum n_iconv_flags icf,
1138 char const *tocode, char const *fromcode, char const *input){
1139 struct str out, in;
1140 iconv_t icd;
1141 char *rv;
1142 NYD2_ENTER;
1144 rv = NULL;
1145 if(tocode == NULL)
1146 tocode = ok_vlook(ttycharset);
1147 if(fromcode == NULL)
1148 fromcode = "utf-8";
1150 if((icd = iconv_open(tocode, fromcode)) == (iconv_t)-1)
1151 goto jleave;
1153 in.l = strlen(in.s = n_UNCONST(input)); /* logical */
1154 out.s = NULL, out.l = 0;
1155 if(!n_iconv_str(icd, icf, &out, &in, NULL))
1156 rv = savestrbuf(out.s, out.l);
1157 if(out.s != NULL)
1158 free(out.s);
1160 iconv_close(icd);
1161 jleave:
1162 NYD2_LEAVE;
1163 return rv;
1165 #endif /* HAVE_ICONV */
1167 /* s-it-mode */