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