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
19 utf8_put(byte *p, uns u)
25 *p++ = 0xc0 | (u >> 6);
26 *p++ = 0x80 | (u & 0x3f);
31 *p++ = 0xe0 | (u >> 12);
32 *p++ = 0x80 | ((u >> 6) & 0x3f);
33 *p++ = 0x80 | (u & 0x3f);
39 utf8_32_put(byte *p, uns u)
45 *p++ = 0xc0 | (u >> 6);
50 *p++ = 0xe0 | (u >> 12);
55 *p++ = 0xf0 | (u >> 18);
60 *p++ = 0xf8 | (u >> 24);
63 else if (u < (1U<<31))
65 *p++ = 0xfc | (u >> 30);
66 *p++ = 0x80 | ((u >> 24) & 0x3f);
67 put4: *p++ = 0x80 | ((u >> 18) & 0x3f);
68 put3: *p++ = 0x80 | ((u >> 12) & 0x3f);
69 put2: *p++ = 0x80 | ((u >> 6) & 0x3f);
70 put1: *p++ = 0x80 | (u & 0x3f);
77 #define UTF8_GET_NEXT if (unlikely((*p & 0xc0) != 0x80)) goto bad; u = (u << 6) | (*p++ & 0x3f)
79 static inline const byte *
80 utf8_get(const byte *p, uns *uu)
85 else if (unlikely(u < 0xc0))
87 /* Incorrect byte sequence */
96 else if (likely(u < 0xf0))
109 utf8_32_get(byte *p, uns *uu)
114 else if (unlikely(u < 0xc0))
116 /* Incorrect byte sequence */
155 #define PUT_UTF8(p,u) p = utf8_put(p, u)
156 #define GET_UTF8(p,u) p = (byte*)utf8_get(p, &(u))
158 #define PUT_UTF8_32(p,u) p = utf8_32_put(p, u)
159 #define GET_UTF8_32(p,u) p = (byte*)utf8_32_get(p, &(u))
161 #define UTF8_SKIP(p) do { \
164 while (c & 0x40 && *p >= 0x80 && *p < 0xc0) \
168 #define UTF8_SKIP_BWD(p) while ((*--(p) & 0xc0) == 0x80)
187 utf8_encoding_len(uns c)
191 ASSERT(c >= 0xc0 && c < 0xfe);
205 uns utf8_strlen(byte *str);
206 uns utf8_strnlen(byte *str, uns n);