X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fff-unicode.h;h=af39bbda7ab50fd706072c61a432fcdc1013aaf4;hb=3bb641c18d60f38d6e646211aa5c6a4b63ba9180;hp=31510ff4411b5aceb85c9c921f20ff9d8f086474;hpb=b18ffb4f5ef9c5d83d7a3e28c37fabbb7df44be2;p=libucw.git diff --git a/lib/ff-unicode.h b/lib/ff-unicode.h index 31510ff4..af39bbda 100644 --- a/lib/ff-unicode.h +++ b/lib/ff-unicode.h @@ -1,70 +1,144 @@ /* - * UCW Library: Reading and writing of UTF-8 on Fastbuf Streams + * UCW Library: Reading and writing of UTF-8 and UTF-16 on Fastbuf Streams * * (c) 2001--2004 Martin Mares * (c) 2004 Robert Spalek + * (c) 2007--2008 Pavel Charvat * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. */ -#ifndef _UCW_FF_UTF8_H -#define _UCW_FF_UTF8_H +#ifndef _UCW_FF_UNICODE_H +#define _UCW_FF_UNICODE_H #include "lib/fastbuf.h" #include "lib/unicode.h" -int bget_utf8_slow(struct fastbuf *b); -int bget_utf8_32_slow(struct fastbuf *b); +/*** UTF-8 ***/ + +int bget_utf8_slow(struct fastbuf *b, uns repl); +int bget_utf8_32_slow(struct fastbuf *b, uns repl); void bput_utf8_slow(struct fastbuf *b, uns u); void bput_utf8_32_slow(struct fastbuf *b, uns u); static inline int -bget_utf8(struct fastbuf *b) +bget_utf8_repl(struct fastbuf *b, uns repl) { uns u; - if (bavailr(b) >= 3) { - GET_UTF8(b->bptr, u); + b->bptr = utf8_get_repl(b->bptr, &u, repl); return u; } else - return bget_utf8_slow(b); + return bget_utf8_slow(b, repl); +} + +static inline int +bget_utf8_32_repl(struct fastbuf *b, uns repl) +{ + uns u; + if (bavailr(b) >= 6) + { + b->bptr = utf8_32_get_repl(b->bptr, &u, repl); + return u; + } + else + return bget_utf8_32_slow(b, repl); +} + +static inline int +bget_utf8(struct fastbuf *b) +{ + return bget_utf8_repl(b, UNI_REPLACEMENT); +} + +static inline int +bget_utf8_32(struct fastbuf *b) +{ + return bget_utf8_32_repl(b, UNI_REPLACEMENT); } static inline void bput_utf8(struct fastbuf *b, uns u) { - ASSERT(u < 65536); if (bavailw(b) >= 3) - PUT_UTF8(b->bptr, u); + b->bptr = utf8_put(b->bptr, u); else bput_utf8_slow(b, u); } +static inline void +bput_utf8_32(struct fastbuf *b, uns u) +{ + if (bavailw(b) >= 6) + b->bptr = utf8_32_put(b->bptr, u); + else + bput_utf8_32_slow(b, u); +} + +/*** UTF-16 ***/ + +int bget_utf16_be_slow(struct fastbuf *b, uns repl); +int bget_utf16_le_slow(struct fastbuf *b, uns repl); +void bput_utf16_be_slow(struct fastbuf *b, uns u); +void bput_utf16_le_slow(struct fastbuf *b, uns u); + static inline int -bget_utf8_32(struct fastbuf *b) +bget_utf16_be_repl(struct fastbuf *b, uns repl) { uns u; + if (bavailr(b) >= 4) + { + b->bptr = utf16_be_get_repl(b->bptr, &u, repl); + return u; + } + else + return bget_utf16_be_slow(b, repl); +} - if (bavailr(b) >= 6) +static inline int +bget_utf16_le_repl(struct fastbuf *b, uns repl) +{ + uns u; + if (bavailr(b) >= 4) { - GET_UTF8_32(b->bptr, u); + b->bptr = utf16_le_get_repl(b->bptr, &u, repl); return u; } else - return bget_utf8_32_slow(b); + return bget_utf16_le_slow(b, repl); +} + +static inline int +bget_utf16_be(struct fastbuf *b) +{ + return bget_utf16_be_repl(b, UNI_REPLACEMENT); +} + +static inline int +bget_utf16_le(struct fastbuf *b) +{ + return bget_utf16_le_repl(b, UNI_REPLACEMENT); } static inline void -bput_utf8_32(struct fastbuf *b, uns u) +bput_utf16_be(struct fastbuf *b, uns u) { - ASSERT(u < (1U<<31)); - if (bavailw(b) >= 6) - PUT_UTF8_32(b->bptr, u); + if (bavailw(b) >= 4) + b->bptr = utf16_be_put(b->bptr, u); else - bput_utf8_32_slow(b, u); + bput_utf16_be_slow(b, u); +} + +static inline void +bput_utf16_lbe(struct fastbuf *b, uns u) +{ + if (bavailw(b) >= 4) + b->bptr = utf16_le_put(b->bptr, u); + else + bput_utf16_le_slow(b, u); } #endif