4 * (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
10 #include <lib/config.h>
11 #include <lib/string.h>
13 extern byte *_U_cat[], *_U_sig[];
14 extern word *_U_upper[], *_U_lower[], *_U_unaccent[];
16 static inline uns Ucategory(word x)
19 return _U_cat[x >> 8U][x & 0xff];
24 static inline word Utoupper(word x)
26 word w = (_U_upper[x >> 8U]) ? _U_upper[x >> 8U][x & 0xff] : 0;
30 static inline word Utolower(word x)
32 word w = (_U_lower[x >> 8U]) ? _U_lower[x >> 8U][x & 0xff] : 0;
36 static inline word Uunaccent(word x)
38 word w = (_U_unaccent[x >> 8U]) ? _U_unaccent[x >> 8U][x & 0xff] : 0;
42 static inline byte Usig(word x)
45 return _U_sig[x >> 8U][x & 0xff] ? : 0xff;
50 #define UCat(x,y) (Ucategory(x) & (y))
52 #define Uupper(x) UCat(x, _C_UPPER)
53 #define Ulower(x) UCat(x, _C_LOWER)
54 #define Ualpha(x) UCat(x, _C_ALPHA)
55 #define Ualnum(x) UCat(x, _C_ALNUM)
56 #define Uprint(x) !Uctrl(x)
57 #define Udigit(x) UCat(x, _C_DIGIT)
58 #define Uxdigit(x) UCat(x, _C_XDIGIT)
59 #define Uword(x) UCat(x, _C_WORD)
60 #define Ublank(x) UCat(x, _C_BLANK)
61 #define Uctrl(x) UCat(x, _C_CTRL)
62 #define Uspace(x) Ublank(x)
64 #define UNI_REPLACEMENT 0xfffc
66 #define PUT_UTF8(p,u) do { \
71 *p++ = 0xc0 | (u >> 6); \
72 *p++ = 0x80 | (u & 0x3f); \
76 *p++ = 0xe0 | (u >> 12); \
77 *p++ = 0x80 | ((u >> 6) & 0x3f); \
78 *p++ = 0x80 | (u & 0x3f); \
82 #define IS_UTF8(c) ((c) >= 0xc0)
84 #define GET_UTF8_CHAR(p,u) do { \
86 { /* Too large, use replacement char */ \
88 while ((*p & 0xc0) == 0x80) \
90 u = UNI_REPLACEMENT; \
92 else if (*p >= 0xe0) \
95 if ((*p & 0xc0) == 0x80) \
96 u = (u << 6) | (*p++ & 0x3f); \
97 if ((*p & 0xc0) == 0x80) \
98 u = (u << 6) | (*p++ & 0x3f); \
103 if ((*p & 0xc0) == 0x80) \
104 u = (u << 6) | (*p++ & 0x3f); \
108 #define GET_UTF8(p,u) \
110 GET_UTF8_CHAR(p,u); \
114 #define UTF8_SPACE(u) ((u) < 0x80 ? 1 : (u) < 0x800 ? 2 : 3)
116 uns ucs2_to_utf8(byte *, word *);
117 uns utf8_to_ucs2(word *, byte *);
118 byte *static_ucs2_to_utf8(word *);