2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * Copyright © 2006 Pierre Habouzit
22 /** \addtogroup mutt_strings */
25 static char const __utf8_trail[256] = {
26 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
27 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
28 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
29 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
30 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
31 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
32 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
33 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5,
36 static uint32_t const __utf8_offs[6] = {
37 0x00000000UL, 0x00003080UL, 0x000e2080UL,
38 0x03c82080UL, 0xfa082080UL, 0x82082080UL
41 static const uint8_t __utf8_mark[7] = {
42 0x00, 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
45 int m_ustrlen(const char *s)
50 len += (*s & 0xc0) != 0x80;
56 int m_ustrgetc(const char *s, const char **outp)
59 int trail = __utf8_trail[(unsigned char)*s];
62 case 5: ret += (unsigned char)*s++; ret <<= 6; if (!*s) return -1;
63 case 4: ret += (unsigned char)*s++; ret <<= 6; if (!*s) return -1;
64 case 3: ret += (unsigned char)*s++; ret <<= 6; if (!*s) return -1;
65 case 2: ret += (unsigned char)*s++; ret <<= 6; if (!*s) return -1;
66 case 1: ret += (unsigned char)*s++; ret <<= 6; if (!*s) return -1;
67 case 0: ret += (unsigned char)*s++;
72 return ret - __utf8_offs[trail];
75 int m_ustrputc(char *dst, ssize_t n, int c)
77 int bytes = 1 + (c >= 0x80) + (c >= 0x800) + (c >= 0x10000);
85 case 4: dst[3] = (c | 0x80) & 0xbf; c >>= 6;
86 case 3: dst[2] = (c | 0x80) & 0xbf; c >>= 6;
87 case 2: dst[1] = (c | 0x80) & 0xbf; c >>= 6;
88 case 1: dst[0] = (c | __utf8_mark[bytes]);