]> mj.ucw.cz Git - libucw.git/blob - ucw/ff-unicode.h
Added a list of exported symbols, which are subject to renaming
[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 /* ** UTF-8 ** */
19
20 int bget_utf8_slow(struct fastbuf *b, uns repl);
21 int bget_utf8_32_slow(struct fastbuf *b, uns repl);
22 void bput_utf8_slow(struct fastbuf *b, uns u);
23 void bput_utf8_32_slow(struct fastbuf *b, uns u);
24
25 static inline int bget_utf8_repl(struct fastbuf *b, uns repl)
26 {
27   uns u;
28   if (bavailr(b) >= 3)
29     {
30       b->bptr = utf8_get_repl(b->bptr, &u, repl);
31       return u;
32     }
33   else
34     return bget_utf8_slow(b, repl);
35 }
36
37 static inline int bget_utf8_32_repl(struct fastbuf *b, uns repl)
38 {
39   uns u;
40   if (bavailr(b) >= 6)
41     {
42       b->bptr = utf8_32_get_repl(b->bptr, &u, repl);
43       return u;
44     }
45   else
46     return bget_utf8_32_slow(b, repl);
47 }
48
49 static inline int bget_utf8(struct fastbuf *b) /** Read a single utf8 character from range [0, 0xffff]. **/
50 {
51   return bget_utf8_repl(b, UNI_REPLACEMENT);
52 }
53
54 static inline int bget_utf8_32(struct fastbuf *b) /** Read a single utf8 character (from the whole unicode range). **/
55 {
56   return bget_utf8_32_repl(b, UNI_REPLACEMENT);
57 }
58
59 static inline void bput_utf8(struct fastbuf *b, uns u) /** Write a single utf8 character from range [0, 0xffff]. **/
60 {
61   if (bavailw(b) >= 3)
62     b->bptr = utf8_put(b->bptr, u);
63   else
64     bput_utf8_slow(b, u);
65 }
66
67 static inline void bput_utf8_32(struct fastbuf *b, uns u) /** Write a single utf8 character (from the whole unicode range). **/
68 {
69   if (bavailw(b) >= 6)
70     b->bptr = utf8_32_put(b->bptr, u);
71   else
72     bput_utf8_32_slow(b, u);
73 }
74
75 /* ** UTF-16 ** */
76
77 int bget_utf16_be_slow(struct fastbuf *b, uns repl);
78 int bget_utf16_le_slow(struct fastbuf *b, uns repl);
79 void bput_utf16_be_slow(struct fastbuf *b, uns u);
80 void bput_utf16_le_slow(struct fastbuf *b, uns u);
81
82 static inline int bget_utf16_be_repl(struct fastbuf *b, uns repl)
83 {
84   uns u;
85   if (bavailr(b) >= 4)
86     {
87       b->bptr = utf16_be_get_repl(b->bptr, &u, repl);
88       return u;
89     }
90   else
91     return bget_utf16_be_slow(b, repl);
92 }
93
94 static inline int bget_utf16_le_repl(struct fastbuf *b, uns repl)
95 {
96   uns u;
97   if (bavailr(b) >= 4)
98     {
99       b->bptr = utf16_le_get_repl(b->bptr, &u, repl);
100       return u;
101     }
102   else
103     return bget_utf16_le_slow(b, repl);
104 }
105
106 /**
107  * Read an utf16 character from fastbuf.
108  * Big endian version.
109  **/
110 static inline int bget_utf16_be(struct fastbuf *b)
111 {
112   return bget_utf16_be_repl(b, UNI_REPLACEMENT);
113 }
114
115 /**
116  * Read an utf16 character from fastbuf.
117  * Little endian version.
118  **/
119 static inline int bget_utf16_le(struct fastbuf *b)
120 {
121   return bget_utf16_le_repl(b, UNI_REPLACEMENT);
122 }
123
124 /**
125  * Write an utf16 character to fastbuf.
126  * Big endian version.
127  **/
128 static inline void bput_utf16_be(struct fastbuf *b, uns u)
129 {
130   if (bavailw(b) >= 4)
131     b->bptr = utf16_be_put(b->bptr, u);
132   else
133     bput_utf16_be_slow(b, u);
134 }
135
136 /**
137  * Write an utf16 character to fastbuf.
138  * Little endian version.
139  **/
140 static inline void bput_utf16_le(struct fastbuf *b, uns u)
141 {
142   if (bavailw(b) >= 4)
143     b->bptr = utf16_le_put(b->bptr, u);
144   else
145     bput_utf16_le_slow(b, u);
146 }
147
148 #endif