2 * UCW Library -- Unicode Characters
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>
8 * This software may be freely distributed and used according to the terms
9 * of the GNU Lesser General Public License.
12 #ifndef _UCW_UNICODE_H
13 #define _UCW_UNICODE_H
15 #include "ucw/unaligned.h"
17 /* Macros for handling UTF-8 */
19 #define UNI_REPLACEMENT 0xfffc /** Unicode value used as a default replacement of invalid characters. **/
22 * Encode a value from the range `[0, 0xFFFF]`
23 * (basic multilingual plane); up to 3 bytes needed (RFC2279).
25 static inline byte *utf8_put(byte *p, uns u)
31 *p++ = 0xc0 | (u >> 6);
32 *p++ = 0x80 | (u & 0x3f);
37 *p++ = 0xe0 | (u >> 12);
38 *p++ = 0x80 | ((u >> 6) & 0x3f);
39 *p++ = 0x80 | (u & 0x3f);
45 * Encode a value from the range `[0, 0x7FFFFFFF]`;
46 * (superset of Unicode 4.0) up to 6 bytes needed (RFC2279).
48 static inline byte *utf8_32_put(byte *p, uns u)
54 *p++ = 0xc0 | (u >> 6);
59 *p++ = 0xe0 | (u >> 12);
64 *p++ = 0xf0 | (u >> 18);
69 *p++ = 0xf8 | (u >> 24);
72 else if (u < (1U<<31))
74 *p++ = 0xfc | (u >> 30);
75 *p++ = 0x80 | ((u >> 24) & 0x3f);
76 put4: *p++ = 0x80 | ((u >> 18) & 0x3f);
77 put3: *p++ = 0x80 | ((u >> 12) & 0x3f);
78 put2: *p++ = 0x80 | ((u >> 6) & 0x3f);
79 put1: *p++ = 0x80 | (u & 0x3f);
86 #define UTF8_GET_NEXT if (unlikely((*p & 0xc0) != 0x80)) goto bad; u = (u << 6) | (*p++ & 0x3f)
89 * Decode a value from the range `[0, 0xFFFF]` (basic multilingual plane)
90 * or return @repl if the encoding has been corrupted.
92 static inline byte *utf8_get_repl(const byte *p, uns *uu, uns repl)
97 else if (unlikely(u < 0xc0))
99 /* Incorrect byte sequence */
108 else if (likely(u < 0xf0))
121 * Decode a value from the range `[0, 0x7FFFFFFF]`
122 * or return @repl if the encoding has been corrupted.
124 static inline byte *utf8_32_get_repl(const byte *p, uns *uu, uns repl)
129 else if (unlikely(u < 0xc0))
131 /* Incorrect byte sequence */
171 * Decode a value from the range `[0, 0xFFFF]` (basic multilignual plane)
172 * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
174 static inline byte *utf8_get(const byte *p, uns *uu)
176 return utf8_get_repl(p, uu, UNI_REPLACEMENT);
180 * Decode a value from the range `[0, 0x7FFFFFFF]`
181 * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
183 static inline byte *utf8_32_get(const byte *p, uns *uu)
185 return utf8_32_get_repl(p, uu, UNI_REPLACEMENT);
188 #define UTF8_SKIP(p) do { \
191 while (c & 0x40 && *p >= 0x80 && *p < 0xc0) \
195 #define UTF8_SKIP_BWD(p) while ((*--(p) & 0xc0) == 0x80)
198 * Return the number of bytes needed to encode a given value from the range `[0, 0x7FFFFFFF]` to UTF-8.
200 static inline uns utf8_space(uns u)
216 * Compute the length of a single UTF-8 character from its first byte. The encoding must be valid.
218 static inline uns utf8_encoding_len(uns c)
222 ASSERT(c >= 0xc0 && c < 0xfe);
235 * Encode an UTF-16LE character from the range `[0, 0xD7FF]` or `[0xE000,0x11FFFF]`;
236 * up to 4 bytes needed.
238 static inline void *utf16_le_put(void *p, uns u)
240 if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
245 else if ((u -= 0x10000) < 0x100000)
247 put_u16_le(p, 0xd800 | (u >> 10));
248 put_u16_le(p + 2, 0xdc00 | (u & 0x3ff));
256 * Encode a UTF-16BE character from the range `[0, 0xD7FF]` or `[0xE000,0x11FFFF]`;
257 * up to 4 bytes needed.
259 static inline void *utf16_be_put(void *p, uns u)
261 if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
266 else if ((u -= 0x10000) < 0x100000)
268 put_u16_be(p, 0xd800 | (u >> 10));
269 put_u16_be(p + 2, 0xdc00 | (u & 0x3ff));
277 * Decode a UTF-16LE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
278 * or return @repl if the encoding has been corrupted.
280 static inline void *utf16_le_get_repl(const void *p, uns *uu, uns repl)
282 uns u = get_u16_le(p), x, y;
285 if (x < 0x400 && (y = get_u16_le(p + 2) - 0xdc00) < 0x400)
287 u = 0x10000 + (x << 10) + y;
293 return (void *)(p + 2);
297 * Decode a UTF-16BE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
298 * or return @repl if the encoding has been corrupted.
300 static inline void *utf16_be_get_repl(const void *p, uns *uu, uns repl)
302 uns u = get_u16_be(p), x, y;
305 if (x < 0x400 && (y = get_u16_be(p + 2) - 0xdc00) < 0x400)
307 u = 0x10000 + (x << 10) + y;
313 return (void *)(p + 2);
317 * Decode a UTF-16LE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
318 * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
320 static inline void *utf16_le_get(const void *p, uns *uu)
322 return utf16_le_get_repl(p, uu, UNI_REPLACEMENT);
326 * Decode a UTF-16BE character from the range `[0, 0xD7FF]` or `[0xE000,11FFFF]`
327 * or return `UNI_REPLACEMENT` if the encoding has been corrupted.
329 static inline void *utf16_be_get(const void *p, uns *uu)
331 return utf16_be_get_repl(p, uu, UNI_REPLACEMENT);
335 * Basic sanity check on Unicode characters. Return `UNI_REPLACEMENT` if the input
336 * character is a surrogate, ASCII or Latin-1 control character different from the tab,
337 * or if it lies outside the basic plane. In all other cases, it acts as an identity.
339 static inline uns unicode_sanitize_char(uns u)
341 if (u >= 0x10000 || // We don't accept anything outside the basic plane
342 u >= 0xd800 && u < 0xf900 || // neither we do surrogates
343 u >= 0x80 && u < 0xa0 || // nor latin-1 control chars
344 u < 0x20 && u != '\t')
345 return UNI_REPLACEMENT;
352 * Count the number of Unicode characters in a zero-terminated UTF-8 string.
353 * Returned value for corrupted encoding is undefined, but is never greater than strlen().
355 uns utf8_strlen(const byte *str);
358 * Same as @utf8_strlen(), but returns at most @n characters.
360 uns utf8_strnlen(const byte *str, uns n);