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 character set [0, 0xFFFF]
19 * (subset of the formal Unicode range); 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]; up to 6 bytes needed (RFC2279) */
42 utf8_32_put(byte *p, uns u)
48 *p++ = 0xc0 | (u >> 6);
53 *p++ = 0xe0 | (u >> 12);
58 *p++ = 0xf0 | (u >> 18);
63 *p++ = 0xf8 | (u >> 24);
66 else if (u < (1U<<31))
68 *p++ = 0xfc | (u >> 30);
69 *p++ = 0x80 | ((u >> 24) & 0x3f);
70 put4: *p++ = 0x80 | ((u >> 18) & 0x3f);
71 put3: *p++ = 0x80 | ((u >> 12) & 0x3f);
72 put2: *p++ = 0x80 | ((u >> 6) & 0x3f);
73 put1: *p++ = 0x80 | (u & 0x3f);
80 #define UTF8_GET_NEXT if (unlikely((*p & 0xc0) != 0x80)) goto bad; u = (u << 6) | (*p++ & 0x3f)
82 /* Decode a character from the basic character set [0, 0xFFFF]
83 * or return UNI_REPLACEMENT if the encoding has been corrupted */
85 utf8_get(const byte *p, uns *uu)
90 else if (unlikely(u < 0xc0))
92 /* Incorrect byte sequence */
101 else if (likely(u < 0xf0))
113 /* Decode a value from the range [0, 0x7FFFFFFF]
114 * or return UNI_REPLACEMENT if the encoding has been corrupted */
116 utf8_32_get(const byte *p, uns *uu)
121 else if (unlikely(u < 0xc0))
123 /* Incorrect byte sequence */
162 #define PUT_UTF8(p,u) p = utf8_put(p, u)
163 #define GET_UTF8(p,u) p = (byte*)utf8_get(p, &(u))
165 #define PUT_UTF8_32(p,u) p = utf8_32_put(p, u)
166 #define GET_UTF8_32(p,u) p = (byte*)utf8_32_get(p, &(u))
168 #define UTF8_SKIP(p) do { \
171 while (c & 0x40 && *p >= 0x80 && *p < 0xc0) \
175 #define UTF8_SKIP_BWD(p) while ((*--(p) & 0xc0) == 0x80)
194 utf8_encoding_len(uns c)
198 ASSERT(c >= 0xc0 && c < 0xfe);
212 uns utf8_strlen(const byte *str);
213 uns utf8_strnlen(const byte *str, uns n);
214 uns utf8_check(const byte *str);