aio: open |... should use ::popen
[jimtcl.git] / utf8.c
blobc31a57f101c5335a46958a982ebb2155f9781d9f
1 /**
2 * UTF-8 utility functions
4 * (c) 2010 Steve Bennett <steveb@workware.net.au>
6 * See LICENCE for licence details.
7 */
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stdio.h>
13 #include <assert.h>
14 #include "utf8.h"
16 /* This one is always implemented */
17 int utf8_fromunicode(char *p, unsigned uc)
19 if (uc <= 0x7f) {
20 *p = uc;
21 return 1;
23 else if (uc <= 0x7ff) {
24 *p++ = 0xc0 | ((uc & 0x7c0) >> 6);
25 *p = 0x80 | (uc & 0x3f);
26 return 2;
28 else if (uc <= 0xffff) {
29 *p++ = 0xe0 | ((uc & 0xf000) >> 12);
30 *p++ = 0x80 | ((uc & 0xfc0) >> 6);
31 *p = 0x80 | (uc & 0x3f);
32 return 3;
34 /* Note: We silently truncate to 21 bits here: 0x1fffff */
35 else {
36 *p++ = 0xf0 | ((uc & 0x1c0000) >> 18);
37 *p++ = 0x80 | ((uc & 0x3f000) >> 12);
38 *p++ = 0x80 | ((uc & 0xfc0) >> 6);
39 *p = 0x80 | (uc & 0x3f);
40 return 4;
44 #if defined(JIM_UTF8) && !defined(JIM_BOOTSTRAP)
45 int utf8_charlen(int c)
47 if ((c & 0x80) == 0) {
48 return 1;
50 if ((c & 0xe0) == 0xc0) {
51 return 2;
53 if ((c & 0xf0) == 0xe0) {
54 return 3;
56 if ((c & 0xf8) == 0xf0) {
57 return 4;
59 /* Invalid sequence */
60 return -1;
63 int utf8_strlen(const char *str, int bytelen)
65 int charlen = 0;
66 if (bytelen < 0) {
67 bytelen = strlen(str);
69 while (bytelen) {
70 int c;
71 int l = utf8_tounicode(str, &c);
72 charlen++;
73 str += l;
74 bytelen -= l;
76 return charlen;
79 int utf8_index(const char *str, int index)
81 const char *s = str;
82 while (index--) {
83 int c;
84 s += utf8_tounicode(s, &c);
86 return s - str;
89 int utf8_charequal(const char *s1, const char *s2)
91 int c1, c2;
93 utf8_tounicode(s1, &c1);
94 utf8_tounicode(s2, &c2);
96 return c1 == c2;
99 int utf8_prev_len(const char *str, int len)
101 int n = 1;
103 assert(len > 0);
105 /* Look up to len chars backward for a start-of-char byte */
106 while (--len) {
107 if ((str[-n] & 0x80) == 0) {
108 /* Start of a 1-byte char */
109 break;
111 if ((str[-n] & 0xc0) == 0xc0) {
112 /* Start of a multi-byte char */
113 break;
115 n++;
117 return n;
120 int utf8_tounicode(const char *str, int *uc)
122 unsigned const char *s = (unsigned const char *)str;
124 if (s[0] < 0xc0) {
125 *uc = s[0];
126 return 1;
128 if (s[0] < 0xe0) {
129 if ((s[1] & 0xc0) == 0x80) {
130 *uc = ((s[0] & ~0xc0) << 6) | (s[1] & ~0x80);
131 return 2;
134 else if (s[0] < 0xf0) {
135 if (((str[1] & 0xc0) == 0x80) && ((str[2] & 0xc0) == 0x80)) {
136 *uc = ((s[0] & ~0xe0) << 12) | ((s[1] & ~0x80) << 6) | (s[2] & ~0x80);
137 return 3;
140 else if (s[0] < 0xf8) {
141 if (((str[1] & 0xc0) == 0x80) && ((str[2] & 0xc0) == 0x80) && ((str[3] & 0xc0) == 0x80)) {
142 *uc = ((s[0] & ~0xf0) << 18) | ((s[1] & ~0x80) << 12) | ((s[2] & ~0x80) << 6) | (s[3] & ~0x80);
143 return 4;
147 /* Invalid sequence, so just return the byte */
148 *uc = *s;
149 return 1;
152 struct casemap {
153 unsigned short code; /* code point */
154 unsigned short altcode; /* alternate case code point */
157 /* Generated mapping tables */
158 #include "_unicode_mapping.c"
160 #define ARRAYSIZE(A) sizeof(A) / sizeof(*(A))
162 static int cmp_casemap(const void *key, const void *cm)
164 return *(int *)key - (int)((const struct casemap *)cm)->code;
167 static int utf8_map_case(const struct casemap *mapping, int num, int ch)
169 /* We only support 16 bit case mapping */
170 if (ch <= 0xffff) {
171 const struct casemap *cm =
172 bsearch(&ch, mapping, num, sizeof(*mapping), cmp_casemap);
174 if (cm) {
175 return cm->altcode;
178 return ch;
181 /* Some platforms don't have isascii */
182 #ifndef isascii
183 #define isascii(C) (!((C) & ~0x7f))
184 #endif
186 int utf8_upper(int ch)
188 if (isascii(ch)) {
189 return toupper(ch);
191 return utf8_map_case(unicode_case_mapping_upper, ARRAYSIZE(unicode_case_mapping_upper), ch);
194 int utf8_lower(int ch)
196 if (isascii(ch)) {
197 return tolower(ch);
199 return utf8_map_case(unicode_case_mapping_lower, ARRAYSIZE(unicode_case_mapping_lower), ch);
202 int utf8_title(int ch)
204 int newch = utf8_map_case(unicode_case_mapping_title, ARRAYSIZE(unicode_case_mapping_title), ch);
205 if (newch != ch) {
206 return newch ? newch : ch;
208 return utf8_upper(ch);
211 #endif /* JIM_BOOTSTRAP */