2 * UCW Library -- Unicode Characters
4 * (c) 1997--2004 Martin Mares <mj@ucw.cz>
5 * (c) 2004 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
11 #ifndef _UCW_UNICODE_H
12 #define _UCW_UNICODE_H
14 /* Macros for handling UTF-8 */
16 #define UNI_REPLACEMENT 0xfffc
18 /* Encode a character from the basic multilingual plane [0, 0xFFFF]
19 * (subset of Unicode 4.0); up to 3 bytes needed (RFC2279) */
21 utf8_put(byte *p, uns u)
27 *p++ = 0xc0 | (u >> 6);
28 *p++ = 0x80 | (u & 0x3f);
33 *p++ = 0xe0 | (u >> 12);
34 *p++ = 0x80 | ((u >> 6) & 0x3f);
35 *p++ = 0x80 | (u & 0x3f);
40 /* Encode a value from the range [0, 0x7FFFFFFF];
41 * (superset of Unicode 4.0) up to 6 bytes needed (RFC2279) */
43 utf8_32_put(byte *p, uns u)
49 *p++ = 0xc0 | (u >> 6);
54 *p++ = 0xe0 | (u >> 12);
59 *p++ = 0xf0 | (u >> 18);
64 *p++ = 0xf8 | (u >> 24);
67 else if (u < (1U<<31))
69 *p++ = 0xfc | (u >> 30);
70 *p++ = 0x80 | ((u >> 24) & 0x3f);
71 put4: *p++ = 0x80 | ((u >> 18) & 0x3f);
72 put3: *p++ = 0x80 | ((u >> 12) & 0x3f);
73 put2: *p++ = 0x80 | ((u >> 6) & 0x3f);
74 put1: *p++ = 0x80 | (u & 0x3f);
81 #define UTF8_GET_NEXT if (unlikely((*p & 0xc0) != 0x80)) goto bad; u = (u << 6) | (*p++ & 0x3f)
83 /* Decode a character from the basic multilingual plane [0, 0xFFFF]
84 * or return UNI_REPLACEMENT if the encoding has been corrupted */
86 utf8_get(const byte *p, uns *uu)
91 else if (unlikely(u < 0xc0))
93 /* Incorrect byte sequence */
102 else if (likely(u < 0xf0))
114 /* Decode a value from the range [0, 0x7FFFFFFF]
115 * or return UNI_REPLACEMENT if the encoding has been corrupted */
117 utf8_32_get(const byte *p, uns *uu)
122 else if (unlikely(u < 0xc0))
124 /* Incorrect byte sequence */
163 #define PUT_UTF8(p,u) p = utf8_put(p, u)
164 #define GET_UTF8(p,u) p = (byte*)utf8_get(p, &(u))
166 #define PUT_UTF8_32(p,u) p = utf8_32_put(p, u)
167 #define GET_UTF8_32(p,u) p = (byte*)utf8_32_get(p, &(u))
169 #define UTF8_SKIP(p) do { \
172 while (c & 0x40 && *p >= 0x80 && *p < 0xc0) \
176 #define UTF8_SKIP_BWD(p) while ((*--(p) & 0xc0) == 0x80)
195 utf8_encoding_len(uns c)
199 ASSERT(c >= 0xc0 && c < 0xfe);
213 uns utf8_strlen(const byte *str);
214 uns utf8_strnlen(const byte *str, uns n);