]> mj.ucw.cz Git - libucw.git/blob - ucw/unicode.h
Libucw: unicode.h - Remove macros with the same functionality as functions.
[libucw.git] / ucw / unicode.h
1 /*
2  *      UCW Library -- Unicode Characters
3  *
4  *      (c) 1997--2004 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *      (c) 2007 Pavel Charvat <pchar@ucw.cz>
7  *
8  *      This software may be freely distributed and used according to the terms
9  *      of the GNU Lesser General Public License.
10  */
11
12 #ifndef _UCW_UNICODE_H
13 #define _UCW_UNICODE_H
14
15 #include "ucw/unaligned.h"
16
17 /* Macros for handling UTF-8 */
18
19 #define UNI_REPLACEMENT 0xfffc
20
21 /* Encode a character from the basic multilingual plane [0, 0xFFFF]
22  * (subset of Unicode 4.0); up to 3 bytes needed (RFC2279) */
23 static inline byte *
24 utf8_put(byte *p, uns u)
25 {
26   if (u < 0x80)
27     *p++ = u;
28   else if (u < 0x800)
29     {
30       *p++ = 0xc0 | (u >> 6);
31       *p++ = 0x80 | (u & 0x3f);
32     }
33   else
34     {
35       ASSERT(u < 0x10000);
36       *p++ = 0xe0 | (u >> 12);
37       *p++ = 0x80 | ((u >> 6) & 0x3f);
38       *p++ = 0x80 | (u & 0x3f);
39     }
40   return p;
41 }
42
43 /* Encode a value from the range [0, 0x7FFFFFFF];
44  * (superset of Unicode 4.0) up to 6 bytes needed (RFC2279) */
45 static inline byte *
46 utf8_32_put(byte *p, uns u)
47 {
48   if (u < 0x80)
49     *p++ = u;
50   else if (u < 0x800)
51     {
52       *p++ = 0xc0 | (u >> 6);
53       goto put1;
54     }
55   else if (u < (1<<16))
56     {
57       *p++ = 0xe0 | (u >> 12);
58       goto put2;
59     }
60   else if (u < (1<<21))
61     {
62       *p++ = 0xf0 | (u >> 18);
63       goto put3;
64     }
65   else if (u < (1<<26))
66     {
67       *p++ = 0xf8 | (u >> 24);
68       goto put4;
69     }
70   else if (u < (1U<<31))
71     {
72       *p++ = 0xfc | (u >> 30);
73       *p++ = 0x80 | ((u >> 24) & 0x3f);
74 put4: *p++ = 0x80 | ((u >> 18) & 0x3f);
75 put3: *p++ = 0x80 | ((u >> 12) & 0x3f);
76 put2: *p++ = 0x80 | ((u >> 6) & 0x3f);
77 put1: *p++ = 0x80 | (u & 0x3f);
78     }
79   else
80     ASSERT(0);
81   return p;
82 }
83
84 #define UTF8_GET_NEXT if (unlikely((*p & 0xc0) != 0x80)) goto bad; u = (u << 6) | (*p++ & 0x3f)
85
86 /* Decode a character from the basic multilingual plane [0, 0xFFFF]
87  * or return 'repl' if the encoding has been corrupted */
88 static inline byte *
89 utf8_get_repl(const byte *p, uns *uu, uns repl)
90 {
91   uns u = *p++;
92   if (u < 0x80)
93     ;
94   else if (unlikely(u < 0xc0))
95     {
96       /* Incorrect byte sequence */
97     bad:
98       u = repl;
99     }
100   else if (u < 0xe0)
101     {
102       u &= 0x1f;
103       UTF8_GET_NEXT;
104     }
105   else if (likely(u < 0xf0))
106     {
107       u &= 0x0f;
108       UTF8_GET_NEXT;
109       UTF8_GET_NEXT;
110     }
111   else
112     goto bad;
113   *uu = u;
114   return (byte *)p;
115 }
116
117 /* Decode a value from the range [0, 0x7FFFFFFF] 
118  * or return 'repl' if the encoding has been corrupted */
119 static inline byte *
120 utf8_32_get_repl(const byte *p, uns *uu, uns repl)
121 {
122   uns u = *p++;
123   if (u < 0x80)
124     ;
125   else if (unlikely(u < 0xc0))
126     {
127       /* Incorrect byte sequence */
128     bad:
129       u = repl;
130     }
131   else if (u < 0xe0)
132     {
133       u &= 0x1f;
134       goto get1;
135     }
136   else if (u < 0xf0)
137     {
138       u &= 0x0f;
139       goto get2;
140     }
141   else if (u < 0xf8)
142     {
143       u &= 0x07;
144       goto get3;
145     }
146   else if (u < 0xfc)
147     {
148       u &= 0x03;
149       goto get4;
150     }
151   else if (u < 0xfe)
152     {
153       u &= 0x01;
154       UTF8_GET_NEXT;
155 get4: UTF8_GET_NEXT;
156 get3: UTF8_GET_NEXT;
157 get2: UTF8_GET_NEXT;
158 get1: UTF8_GET_NEXT;
159     }
160   else
161     goto bad;
162   *uu = u;
163   return (byte *)p;
164 }
165
166 /* Decode a character from the basic multilingual plane [0, 0xFFFF]
167  * or return UNI_REPLACEMENT if the encoding has been corrupted */
168 static inline byte *
169 utf8_get(const byte *p, uns *uu)
170 {
171   return utf8_get_repl(p, uu, UNI_REPLACEMENT);
172 }
173
174 /* Decode a value from the range [0, 0x7FFFFFFF] 
175  * or return UNI_REPLACEMENT if the encoding has been corrupted */
176 static inline byte *
177 utf8_32_get(const byte *p, uns *uu)
178 {
179   return utf8_32_get_repl(p, uu, UNI_REPLACEMENT);
180 }
181
182 #define UTF8_SKIP(p) do {                               \
183     uns c = *p++;                                       \
184     if (c >= 0xc0)                                      \
185       while (c & 0x40 && *p >= 0x80 && *p < 0xc0)       \
186         p++, c <<= 1;                                   \
187   } while (0)
188
189 #define UTF8_SKIP_BWD(p) while ((*--(p) & 0xc0) == 0x80)
190
191 static inline uns
192 utf8_space(uns u)
193 {
194   if (u < 0x80)
195     return 1;
196   if (u < 0x800)
197     return 2;
198   if (u < (1<<16))
199     return 3;
200   if (u < (1<<21))
201     return 4;
202   if (u < (1<<26))
203     return 5;
204   return 6;
205 }
206
207 static inline uns
208 utf8_encoding_len(uns c)
209 {
210   if (c < 0x80)
211     return 1;
212   ASSERT(c >= 0xc0 && c < 0xfe);
213   if (c < 0xe0)
214     return 2;
215   if (c < 0xf0)
216     return 3;
217   if (c < 0xf8)
218     return 4;
219   if (c < 0xfc)
220     return 5;
221   return 6;
222 }
223
224 /* Encode a character from the range [0, 0xD7FF] or [0xE000,0x11FFFF];
225  * up to 4 bytes needed */
226 static inline void *
227 utf16_le_put(void *p, uns u)
228 {
229   if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
230     {
231       put_u16_le(p, u);
232       return p + 2;
233     }
234   else if ((u -= 0x10000) < 0x100000)
235     {
236       put_u16_le(p, 0xd800 | (u >> 10));
237       put_u16_le(p + 2, 0xdc00 | (u & 0x3ff));
238       return p + 4;
239     }
240   else
241     ASSERT(0);
242 }
243
244 static inline void *
245 utf16_be_put(void *p, uns u)
246 {
247   if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
248     {
249       put_u16_be(p, u);
250       return p + 2;
251     }
252   else if ((u -= 0x10000) < 0x100000)
253     {
254       put_u16_be(p, 0xd800 | (u >> 10));
255       put_u16_be(p + 2, 0xdc00 | (u & 0x3ff));
256       return p + 4;
257     }
258   else
259     ASSERT(0);
260 }
261
262 /* Decode a character from the range [0, 0xD7FF] or [0xE000,11FFFF]
263  * or return `repl' if the encoding has been corrupted */
264 static inline void *
265 utf16_le_get_repl(const void *p, uns *uu, uns repl)
266 {
267   uns u = get_u16_le(p), x, y;
268   x = u - 0xd800;
269   if (x < 0x800)
270     if (x < 0x400 && (y = get_u16_le(p + 2) - 0xdc00) < 0x400)
271       {
272         u = 0x10000 + (x << 10) + y;
273         p += 2;
274       }
275     else
276       u = repl;
277   *uu = u;
278   return (void *)(p + 2);
279 }
280
281 static inline void *
282 utf16_be_get_repl(const void *p, uns *uu, uns repl)
283 {
284   uns u = get_u16_be(p), x, y;
285   x = u - 0xd800;
286   if (x < 0x800)
287     if (x < 0x400 && (y = get_u16_be(p + 2) - 0xdc00) < 0x400)
288       {
289         u = 0x10000 + (x << 10) + y;
290         p += 2;
291       }
292     else
293       u = repl;
294   *uu = u;
295   return (void *)(p + 2);
296 }
297
298 /* Decode a character from the range [0, 0xD7FF] or [0xE000,11FFFF]
299  * or return UNI_REPLACEMENT if the encoding has been corrupted */
300 static inline void *
301 utf16_le_get(const void *p, uns *uu)
302 {
303   return utf16_le_get_repl(p, uu, UNI_REPLACEMENT);
304 }
305
306 static inline void *
307 utf16_be_get(const void *p, uns *uu)
308 {
309   return utf16_be_get_repl(p, uu, UNI_REPLACEMENT);
310 }
311
312 static inline uns
313 unicode_sanitize_char(uns u)
314 {
315   if (u >= 0x10000 ||                   // We don't accept anything outside the basic plane
316       u >= 0xd800 && u < 0xf900 ||      // neither we do surrogates
317       u >= 0x80 && u < 0xa0 ||          // nor latin-1 control chars
318       u < 0x20 && u != '\t')
319     return UNI_REPLACEMENT;
320   return u;
321 }
322
323 /* unicode-utf8.c */
324
325 uns utf8_strlen(const byte *str);
326 uns utf8_strnlen(const byte *str, uns n);
327
328 #endif