2 * UCW Library: Reading and writing of UTF-8 and UTF-16 on Fastbuf Streams
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>
8 * This software may be freely distributed and used according to the terms
9 * of the GNU Lesser General Public License.
12 #ifndef _UCW_FF_UNICODE_H
13 #define _UCW_FF_UNICODE_H
15 #include <ucw/fastbuf.h>
16 #include <ucw/unicode.h>
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);
25 static inline int bget_utf8_repl(struct fastbuf *b, uns repl)
30 b->bptr = utf8_get_repl(b->bptr, &u, repl);
34 return bget_utf8_slow(b, repl);
37 static inline int bget_utf8_32_repl(struct fastbuf *b, uns repl)
42 b->bptr = utf8_32_get_repl(b->bptr, &u, repl);
46 return bget_utf8_32_slow(b, repl);
49 static inline int bget_utf8(struct fastbuf *b) /** Read a single utf8 character from range [0, 0xffff]. **/
51 return bget_utf8_repl(b, UNI_REPLACEMENT);
54 static inline int bget_utf8_32(struct fastbuf *b) /** Read a single utf8 character (from the whole unicode range). **/
56 return bget_utf8_32_repl(b, UNI_REPLACEMENT);
59 static inline void bput_utf8(struct fastbuf *b, uns u) /** Write a single utf8 character from range [0, 0xffff]. **/
62 b->bptr = utf8_put(b->bptr, u);
67 static inline void bput_utf8_32(struct fastbuf *b, uns u) /** Write a single utf8 character (from the whole unicode range). **/
70 b->bptr = utf8_32_put(b->bptr, u);
72 bput_utf8_32_slow(b, u);
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);
82 static inline int bget_utf16_be_repl(struct fastbuf *b, uns repl)
87 b->bptr = utf16_be_get_repl(b->bptr, &u, repl);
91 return bget_utf16_be_slow(b, repl);
94 static inline int bget_utf16_le_repl(struct fastbuf *b, uns repl)
99 b->bptr = utf16_le_get_repl(b->bptr, &u, repl);
103 return bget_utf16_le_slow(b, repl);
107 * Read an utf16 character from fastbuf.
108 * Big endian version.
110 static inline int bget_utf16_be(struct fastbuf *b)
112 return bget_utf16_be_repl(b, UNI_REPLACEMENT);
116 * Read an utf16 character from fastbuf.
117 * Little endian version.
119 static inline int bget_utf16_le(struct fastbuf *b)
121 return bget_utf16_le_repl(b, UNI_REPLACEMENT);
125 * Write an utf16 character to fastbuf.
126 * Big endian version.
128 static inline void bput_utf16_be(struct fastbuf *b, uns u)
131 b->bptr = utf16_be_put(b->bptr, u);
133 bput_utf16_be_slow(b, u);
137 * Write an utf16 character to fastbuf.
138 * Little endian version.
140 static inline void bput_utf16_le(struct fastbuf *b, uns u)
143 b->bptr = utf16_le_put(b->bptr, u);
145 bput_utf16_le_slow(b, u);