]> mj.ucw.cz Git - libucw.git/blob - charset/unicode.h
written parsing config files and command line parameters, untested yet
[libucw.git] / charset / unicode.h
1 /*
2  *      The UniCode Library
3  *
4  *      (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #ifndef _UNICODE_H
8 #define _UNICODE_H
9
10 #include "lib/chartype.h"
11
12 extern byte *_U_cat[], *_U_sig[];
13 extern word *_U_upper[], *_U_lower[], *_U_unaccent[];
14
15 static inline uns Ucategory(word x)
16 {
17   if (_U_cat[x >> 8U])
18     return _U_cat[x >> 8U][x & 0xff];
19   else
20     return 0;
21 }
22
23 static inline word Utoupper(word x)
24 {
25   word w = (_U_upper[x >> 8U]) ? _U_upper[x >> 8U][x & 0xff] : 0;
26   return w ? w : x;
27 }
28
29 static inline word Utolower(word x)
30 {
31   word w = (_U_lower[x >> 8U]) ? _U_lower[x >> 8U][x & 0xff] : 0;
32   return w ? w : x;
33 }
34
35 static inline word Uunaccent(word x)
36 {
37   word w = (_U_unaccent[x >> 8U]) ? _U_unaccent[x >> 8U][x & 0xff] : 0;
38   return w ? w : x;
39 }
40
41 static inline byte Usig(word x)
42 {
43   if (_U_sig[x >> 8U])
44     return _U_sig[x >> 8U][x & 0xff] ? : 0xff;
45   else
46     return 0xff;
47 }
48
49 #define UCat(x,y) (Ucategory(x) & (y))
50
51 #define Uupper(x) UCat(x, _C_UPPER)
52 #define Ulower(x) UCat(x, _C_LOWER)
53 #define Ualpha(x) UCat(x, _C_ALPHA)
54 #define Ualnum(x) UCat(x, _C_ALNUM)
55 #define Uprint(x) !Uctrl(x)
56 #define Udigit(x) UCat(x, _C_DIGIT)
57 #define Uxdigit(x) UCat(x, _C_XDIGIT)
58 #define Uword(x) UCat(x, _C_WORD)
59 #define Ublank(x) UCat(x, _C_BLANK)
60 #define Uctrl(x) UCat(x, _C_CTRL)
61 #define Uspace(x) Ublank(x)
62
63 #define UNI_REPLACEMENT 0xfffc
64
65 #define PUT_UTF8(p,u) do {              \
66   if (u < 0x80)                         \
67     *p++ = u;                           \
68   else if (u < 0x800)                   \
69     {                                   \
70       *p++ = 0xc0 | (u >> 6);           \
71       *p++ = 0x80 | (u & 0x3f);         \
72     }                                   \
73   else                                  \
74     {                                   \
75       *p++ = 0xe0 | (u >> 12);          \
76       *p++ = 0x80 | ((u >> 6) & 0x3f);  \
77       *p++ = 0x80 | (u & 0x3f);         \
78     }                                   \
79   } while(0)
80
81 #define IS_UTF8(c) ((c) >= 0xc0)
82
83 #define GET_UTF8_CHAR(p,u) do {         \
84     if (*p >= 0xf0)                     \
85       { /* Too large, use replacement char */   \
86         p++;                            \
87         while ((*p & 0xc0) == 0x80)     \
88           p++;                          \
89         u = UNI_REPLACEMENT;            \
90       }                                 \
91     else if (*p >= 0xe0)                \
92       {                                 \
93         u = *p++ & 0x0f;                \
94         if ((*p & 0xc0) == 0x80)        \
95           u = (u << 6) | (*p++ & 0x3f); \
96         if ((*p & 0xc0) == 0x80)        \
97           u = (u << 6) | (*p++ & 0x3f); \
98       }                                 \
99     else                                \
100       {                                 \
101         u = *p++ & 0x1f;                \
102         if ((*p & 0xc0) == 0x80)        \
103           u = (u << 6) | (*p++ & 0x3f); \
104       }                                 \
105   } while (0)                           \
106
107 #define GET_UTF8(p,u)                   \
108     if (IS_UTF8(*p))                    \
109       GET_UTF8_CHAR(p,u);               \
110     else                                \
111       u = *p++
112
113 #define UTF8_SPACE(u) ((u) < 0x80 ? 1 : (u) < 0x800 ? 2 : 3)
114
115 uns ucs2_to_utf8(byte *, word *);
116 uns utf8_to_ucs2(word *, byte *);
117 byte *static_ucs2_to_utf8(word *);
118 uns Ustrlen(word *);
119
120 #endif