]> mj.ucw.cz Git - libucw.git/blob - ucw/ff-unicode.h
Doc: Updated the list of contributors
[libucw.git] / ucw / ff-unicode.h
1 /*
2  *      UCW Library: Reading and writing of UTF-8 and UTF-16 on Fastbuf Streams
3  *
4  *      (c) 2001--2004 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *      (c) 2007--2008 Pavel Charvat <pchar@ucw.cz>
7  *
8  *      This software may be freely distributed and used according to the terms
9  *      of the GNU Lesser General Public License.
10  */
11
12 #ifndef _UCW_FF_UNICODE_H
13 #define _UCW_FF_UNICODE_H
14
15 #include <ucw/fastbuf.h>
16 #include <ucw/unicode.h>
17
18 #ifdef CONFIG_UCW_CLEAN_ABI
19 #define bget_utf16_be_slow ucw_bget_utf16_be_slow
20 #define bget_utf16_le_slow ucw_bget_utf16_le_slow
21 #define bget_utf8_32_slow ucw_bget_utf8_32_slow
22 #define bget_utf8_slow ucw_bget_utf8_slow
23 #define bput_utf16_be_slow ucw_bput_utf16_be_slow
24 #define bput_utf16_le_slow ucw_bput_utf16_le_slow
25 #define bput_utf8_32_slow ucw_bput_utf8_32_slow
26 #define bput_utf8_slow ucw_bput_utf8_slow
27 #endif
28
29 /* ** UTF-8 ** */
30
31 int bget_utf8_slow(struct fastbuf *b, uns repl);
32 int bget_utf8_32_slow(struct fastbuf *b, uns repl);
33 void bput_utf8_slow(struct fastbuf *b, uns u);
34 void bput_utf8_32_slow(struct fastbuf *b, uns u);
35
36 static inline int bget_utf8_repl(struct fastbuf *b, uns repl)
37 {
38   uns u;
39   if (bavailr(b) >= 3)
40     {
41       b->bptr = utf8_get_repl(b->bptr, &u, repl);
42       return u;
43     }
44   else
45     return bget_utf8_slow(b, repl);
46 }
47
48 static inline int bget_utf8_32_repl(struct fastbuf *b, uns repl)
49 {
50   uns u;
51   if (bavailr(b) >= 6)
52     {
53       b->bptr = utf8_32_get_repl(b->bptr, &u, repl);
54       return u;
55     }
56   else
57     return bget_utf8_32_slow(b, repl);
58 }
59
60 static inline int bget_utf8(struct fastbuf *b) /** Read a single utf8 character from range [0, 0xffff]. **/
61 {
62   return bget_utf8_repl(b, UNI_REPLACEMENT);
63 }
64
65 static inline int bget_utf8_32(struct fastbuf *b) /** Read a single utf8 character (from the whole unicode range). **/
66 {
67   return bget_utf8_32_repl(b, UNI_REPLACEMENT);
68 }
69
70 static inline void bput_utf8(struct fastbuf *b, uns u) /** Write a single utf8 character from range [0, 0xffff]. **/
71 {
72   if (bavailw(b) >= 3)
73     b->bptr = utf8_put(b->bptr, u);
74   else
75     bput_utf8_slow(b, u);
76 }
77
78 static inline void bput_utf8_32(struct fastbuf *b, uns u) /** Write a single utf8 character (from the whole unicode range). **/
79 {
80   if (bavailw(b) >= 6)
81     b->bptr = utf8_32_put(b->bptr, u);
82   else
83     bput_utf8_32_slow(b, u);
84 }
85
86 /* ** UTF-16 ** */
87
88 int bget_utf16_be_slow(struct fastbuf *b, uns repl);
89 int bget_utf16_le_slow(struct fastbuf *b, uns repl);
90 void bput_utf16_be_slow(struct fastbuf *b, uns u);
91 void bput_utf16_le_slow(struct fastbuf *b, uns u);
92
93 static inline int bget_utf16_be_repl(struct fastbuf *b, uns repl)
94 {
95   uns u;
96   if (bavailr(b) >= 4)
97     {
98       b->bptr = utf16_be_get_repl(b->bptr, &u, repl);
99       return u;
100     }
101   else
102     return bget_utf16_be_slow(b, repl);
103 }
104
105 static inline int bget_utf16_le_repl(struct fastbuf *b, uns repl)
106 {
107   uns u;
108   if (bavailr(b) >= 4)
109     {
110       b->bptr = utf16_le_get_repl(b->bptr, &u, repl);
111       return u;
112     }
113   else
114     return bget_utf16_le_slow(b, repl);
115 }
116
117 /**
118  * Read an utf16 character from fastbuf.
119  * Big endian version.
120  **/
121 static inline int bget_utf16_be(struct fastbuf *b)
122 {
123   return bget_utf16_be_repl(b, UNI_REPLACEMENT);
124 }
125
126 /**
127  * Read an utf16 character from fastbuf.
128  * Little endian version.
129  **/
130 static inline int bget_utf16_le(struct fastbuf *b)
131 {
132   return bget_utf16_le_repl(b, UNI_REPLACEMENT);
133 }
134
135 /**
136  * Write an utf16 character to fastbuf.
137  * Big endian version.
138  **/
139 static inline void bput_utf16_be(struct fastbuf *b, uns u)
140 {
141   if (bavailw(b) >= 4)
142     b->bptr = utf16_be_put(b->bptr, u);
143   else
144     bput_utf16_be_slow(b, u);
145 }
146
147 /**
148  * Write an utf16 character to fastbuf.
149  * Little endian version.
150  **/
151 static inline void bput_utf16_le(struct fastbuf *b, uns u)
152 {
153   if (bavailw(b) >= 4)
154     b->bptr = utf16_le_put(b->bptr, u);
155   else
156     bput_utf16_le_slow(b, u);
157 }
158
159 #endif