]> mj.ucw.cz Git - libucw.git/blob - lib/ff-unicode.h
UCW: Added bget_utf16_{le,be} routines.
[libucw.git] / lib / ff-unicode.h
1 /*
2  *      UCW Library: Reading and writing of UTF-8 on Fastbuf Streams
3  *
4  *      (c) 2001--2004 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_FF_UTF8_H
12 #define _UCW_FF_UTF8_H
13
14 #include "lib/fastbuf.h"
15 #include "lib/unicode.h"
16
17 int bget_utf8_slow(struct fastbuf *b, uns repl);
18 int bget_utf8_32_slow(struct fastbuf *b, uns repl);
19 void bput_utf8_slow(struct fastbuf *b, uns u);
20 void bput_utf8_32_slow(struct fastbuf *b, uns u);
21 int bget_utf16_be_slow(struct fastbuf *b, uns repl);
22 int bget_utf16_le_slow(struct fastbuf *b, uns repl);
23
24 static inline int
25 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
38 bget_utf8_32_repl(struct fastbuf *b, uns repl)
39 {
40   uns u;
41   if (bavailr(b) >= 6)
42     {
43       b->bptr = utf8_32_get_repl(b->bptr, &u, repl);
44       return u;
45     }
46   else
47     return bget_utf8_32_slow(b, repl);
48 }
49
50 static inline int
51 bget_utf8(struct fastbuf *b)
52 {
53   return bget_utf8_repl(b, UNI_REPLACEMENT);
54 }
55
56 static inline int
57 bget_utf8_32(struct fastbuf *b)
58 {
59   return bget_utf8_32_repl(b, UNI_REPLACEMENT);
60 }
61
62 static inline void
63 bput_utf8(struct fastbuf *b, uns u)
64 {
65   ASSERT(u < 65536);
66   if (bavailw(b) >= 3)
67     b->bptr = utf8_put(b->bptr, u);
68   else
69     bput_utf8_slow(b, u);
70 }
71
72 static inline void
73 bput_utf8_32(struct fastbuf *b, uns u)
74 {
75   ASSERT(u < (1U<<31));
76   if (bavailw(b) >= 6)
77     b->bptr = utf8_32_put(b->bptr, u);
78   else
79     bput_utf8_32_slow(b, u);
80 }
81
82 static inline int
83 bget_utf16_be_repl(struct fastbuf *b, uns repl)
84 {
85   uns u;
86   if (bavailr(b) >= 4)
87     {
88       b->bptr = utf16_be_get_repl(b->bptr, &u, repl);
89       return u;
90     }
91   else
92     return bget_utf16_be_slow(b, repl);
93 }
94
95 static inline int
96 bget_utf16_le_repl(struct fastbuf *b, uns repl)
97 {
98   uns u;
99   if (bavailr(b) >= 4)
100     {
101       b->bptr = utf16_le_get_repl(b->bptr, &u, repl);
102       return u;
103     }
104   else
105     return bget_utf16_le_slow(b, repl);
106 }
107
108 static inline int
109 bget_utf16_be(struct fastbuf *b)
110 {
111   return bget_utf16_be_repl(b, UNI_REPLACEMENT);
112 }
113
114 static inline int
115 bget_utf16_le(struct fastbuf *b)
116 {
117   return bget_utf16_le_repl(b, UNI_REPLACEMENT);
118 }
119
120 #endif