]> mj.ucw.cz Git - libucw.git/blob - lib/ff-unicode.h
e3838b1c1a691d365b9b6c485d7ee13326e6ee81
[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
22 static inline int
23 bget_utf8_repl(struct fastbuf *b, uns repl)
24 {
25   uns u;
26   if (bavailr(b) >= 3)
27     {
28       b->bptr = utf8_get_repl(b->bptr, &u, repl);
29       return u;
30     }
31   else
32     return bget_utf8_slow(b, repl);
33 }
34
35 static inline int
36 bget_utf8_32_repl(struct fastbuf *b, uns repl)
37 {
38   uns u;
39   if (bavailr(b) >= 6)
40     {
41       b->bptr = utf8_32_get_repl(b->bptr, &u, repl);
42       return u;
43     }
44   else
45     return bget_utf8_32_slow(b, repl);
46 }
47
48 static inline int
49 bget_utf8(struct fastbuf *b)
50 {
51   return bget_utf8_repl(b, UNI_REPLACEMENT);
52 }
53
54 static inline int
55 bget_utf8_32(struct fastbuf *b)
56 {
57   return bget_utf8_32_repl(b, UNI_REPLACEMENT);
58 }
59
60 static inline void
61 bput_utf8(struct fastbuf *b, uns u)
62 {
63   ASSERT(u < 65536);
64   if (bavailw(b) >= 3)
65     b->bptr = utf8_put(b->bptr, u);
66   else
67     bput_utf8_slow(b, u);
68 }
69
70 static inline void
71 bput_utf8_32(struct fastbuf *b, uns u)
72 {
73   ASSERT(u < (1U<<31));
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 #endif