From: Pavel Charvat Date: Mon, 10 Dec 2007 11:36:50 +0000 (+0100) Subject: UCW: Implemented bput_utf16_*. X-Git-Tag: holmes-import~488 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=abd00be4bf89ed0ad7bdecfa1b0130f0b1994c42;p=libucw.git UCW: Implemented bput_utf16_*. --- diff --git a/lib/ff-unicode.c b/lib/ff-unicode.c index 047fff84..280b3b75 100644 --- a/lib/ff-unicode.c +++ b/lib/ff-unicode.c @@ -14,6 +14,8 @@ #include "lib/ff-unicode.h" #include "lib/ff-binary.h" +/*** UTF-8 ***/ + int bget_utf8_slow(struct fastbuf *b, uns repl) { @@ -167,6 +169,8 @@ bput_utf8_32_slow(struct fastbuf *b, uns u) } } +/*** UTF-16 ***/ + int bget_utf16_be_slow(struct fastbuf *b, uns repl) { @@ -196,3 +200,41 @@ bget_utf16_le_slow(struct fastbuf *b, uns repl) return repl; return 0x10000 + (x << 10) + y; } + +void +bput_utf16_be_slow(struct fastbuf *b, uns u) +{ + if (u < 0xd800 || (u < 0x10000 && u >= 0xe000)) + { + bputc(b, u >> 8); + bputc(b, u & 0xff); + } + else if ((u -= 0x10000) < 0x100000) + { + bputc(b, 0xd8 | (u >> 18)); + bputc(b, (u >> 10) & 0xff); + bputc(b, 0xdc | ((u >> 8) & 0x3)); + bputc(b, u & 0xff); + } + else + ASSERT(0); +} + +void +bput_utf16_le_slow(struct fastbuf *b, uns u) +{ + if (u < 0xd800 || (u < 0x10000 && u >= 0xe000)) + { + bputc(b, u & 0xff); + bputc(b, u >> 8); + } + else if ((u -= 0x10000) < 0x100000) + { + bputc(b, (u >> 10) & 0xff); + bputc(b, 0xd8 | (u >> 18)); + bputc(b, u & 0xff); + bputc(b, 0xdc | ((u >> 8) & 0x3)); + } + else + ASSERT(0); +} diff --git a/lib/ff-unicode.h b/lib/ff-unicode.h index f577c635..93f8a06e 100644 --- a/lib/ff-unicode.h +++ b/lib/ff-unicode.h @@ -14,12 +14,12 @@ #include "lib/fastbuf.h" #include "lib/unicode.h" +/*** 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); -int bget_utf16_be_slow(struct fastbuf *b, uns repl); -int bget_utf16_le_slow(struct fastbuf *b, uns repl); static inline int bget_utf8_repl(struct fastbuf *b, uns repl) @@ -62,7 +62,6 @@ bget_utf8_32(struct fastbuf *b) static inline void bput_utf8(struct fastbuf *b, uns u) { - ASSERT(u < 65536); if (bavailw(b) >= 3) b->bptr = utf8_put(b->bptr, u); else @@ -72,13 +71,19 @@ bput_utf8(struct fastbuf *b, uns u) static inline void bput_utf8_32(struct fastbuf *b, uns u) { - ASSERT(u < (1U<<31)); 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_utf16_be_repl(struct fastbuf *b, uns repl) { @@ -117,4 +122,22 @@ bget_utf16_le(struct fastbuf *b) return bget_utf16_le_repl(b, UNI_REPLACEMENT); } +static inline void +bput_utf16_be(struct fastbuf *b, uns u) +{ + if (bavailw(b) >= 4) + b->bptr = utf16_be_put(b->bptr, u); + else + 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 diff --git a/lib/unicode.h b/lib/unicode.h index fb8722d0..9a3fe07a 100644 --- a/lib/unicode.h +++ b/lib/unicode.h @@ -239,8 +239,8 @@ utf16_le_put(void *p, uns u) } else if ((u -= 0x10000) < 0x100000) { - put_u16_le(p, u >> 10); - put_u16_le(p + 2, u & 0x7ff); + put_u16_le(p, 0xd800 | (u >> 10)); + put_u16_le(p + 2, 0xdc00 | (u & 0x3ff)); return p + 4; } else @@ -257,8 +257,8 @@ utf16_be_put(void *p, uns u) } else if ((u -= 0x10000) < 0x100000) { - put_u16_be(p, u >> 10); - put_u16_be(p + 2, u & 0x7ff); + put_u16_be(p, 0xd800 | (u >> 10)); + put_u16_be(p + 2, 0xdc00 | (u & 0x3ff)); return p + 4; } else