]> mj.ucw.cz Git - libucw.git/blob - lib/ff-unicode.h
Merge with git+ssh://git.ucw.cz/projects/sherlock/GIT/sherlock.git
[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 /*** UTF-8 ***/
18
19 int bget_utf8_slow(struct fastbuf *b, uns repl);
20 int bget_utf8_32_slow(struct fastbuf *b, uns repl);
21 void bput_utf8_slow(struct fastbuf *b, uns u);
22 void bput_utf8_32_slow(struct fastbuf *b, uns u);
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   if (bavailw(b) >= 3)
66     b->bptr = utf8_put(b->bptr, u);
67   else
68     bput_utf8_slow(b, u);
69 }
70
71 static inline void
72 bput_utf8_32(struct fastbuf *b, uns u)
73 {
74   if (bavailw(b) >= 6)
75     b->bptr = utf8_32_put(b->bptr, u);
76   else
77     bput_utf8_32_slow(b, u);
78 }
79
80 /*** UTF-16 ***/
81
82 int bget_utf16_be_slow(struct fastbuf *b, uns repl);
83 int bget_utf16_le_slow(struct fastbuf *b, uns repl);
84 void bput_utf16_be_slow(struct fastbuf *b, uns u);
85 void bput_utf16_le_slow(struct fastbuf *b, uns u);
86
87 static inline int
88 bget_utf16_be_repl(struct fastbuf *b, uns repl)
89 {
90   uns u;
91   if (bavailr(b) >= 4)
92     {
93       b->bptr = utf16_be_get_repl(b->bptr, &u, repl);
94       return u;
95     }
96   else
97     return bget_utf16_be_slow(b, repl);
98 }
99
100 static inline int
101 bget_utf16_le_repl(struct fastbuf *b, uns repl)
102 {
103   uns u;
104   if (bavailr(b) >= 4)
105     {
106       b->bptr = utf16_le_get_repl(b->bptr, &u, repl);
107       return u;
108     }
109   else
110     return bget_utf16_le_slow(b, repl);
111 }
112
113 static inline int
114 bget_utf16_be(struct fastbuf *b)
115 {
116   return bget_utf16_be_repl(b, UNI_REPLACEMENT);
117 }
118
119 static inline int
120 bget_utf16_le(struct fastbuf *b)
121 {
122   return bget_utf16_le_repl(b, UNI_REPLACEMENT);
123 }
124
125 static inline void
126 bput_utf16_be(struct fastbuf *b, uns u)
127 {
128   if (bavailw(b) >= 4)
129     b->bptr = utf16_be_put(b->bptr, u);
130   else
131     bput_utf16_be_slow(b, u);
132 }
133
134 static inline void
135 bput_utf16_lbe(struct fastbuf *b, uns u)
136 {
137   if (bavailw(b) >= 4)
138     b->bptr = utf16_le_put(b->bptr, u);
139   else
140     bput_utf16_le_slow(b, u);
141 }
142
143 #endif