add refcounted types
[apps/madmutt.git] / lib-lib / utf8.c
1 /*
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.
6  *
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.
11  *
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,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2006 Pierre Habouzit
18  */
19
20 #include "lib-lib.h"
21
22 /** \addtogroup mutt_strings */
23 /*@{*/
24
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,
34 };
35
36 static uint32_t const __utf8_offs[6] = {
37     0x00000000UL, 0x00003080UL, 0x000e2080UL,
38     0x03c82080UL, 0xfa082080UL, 0x82082080UL
39 };
40
41 static const uint8_t __utf8_mark[7] = {
42     0x00, 0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc
43 };
44
45 int m_ustrlen(const char *s)
46 {
47     int len = 0;
48
49     while (*s) {
50         len += (*s & 0xc0) != 0x80;
51     }
52
53     return len;
54 }
55
56 int m_ustrgetc(const char *s, const char **outp)
57 {
58     uint32_t ret = 0;
59     int trail = __utf8_trail[(unsigned char)*s];
60
61     switch (trail) {
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++;
68     }
69
70     if (*outp)
71         *outp = s;
72     return ret - __utf8_offs[trail];
73 }
74
75 int m_ustrputc(char *dst, ssize_t n, int c)
76 {
77     int bytes = 1 + (c >= 0x80) + (c >= 0x800) + (c >= 0x10000);
78
79     if (bytes >= n)
80         return bytes;
81
82     dst[bytes] = '\0';
83
84     switch (bytes) {
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]);
89     }
90
91     return bytes;
92 }
93
94
95 /*@}*/