From 4d029660e16fe39db5ad4a94cfecafb554b58e2c Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 5 May 2006 05:09:54 +0000 Subject: [PATCH] * Implemented the private _cucul_utf32_to_utf8() helper. --- cucul/charset.c | 30 ++++++++++++++++++++++++++++++ cucul/cucul_internals.h | 1 + 2 files changed, 31 insertions(+) diff --git a/cucul/charset.c b/cucul/charset.c index f223827..e37c795 100644 --- a/cucul/charset.c +++ b/cucul/charset.c @@ -102,6 +102,36 @@ uint32_t _cucul_utf8_to_utf32(char const *s) return ret; } +unsigned int _cucul_utf32_to_utf8(void *buf, uint32_t ch) +{ + static const uint8_t mark[7] = + { + 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC + }; + + char *parser = buf; + int bytes; + + if(ch < 0x80) + { + *parser++ = ch; + return 1; + } + + bytes = (ch < 0x800) ? 2 : (ch < 0x10000) ? 3 : 4; + parser += bytes; + + switch(bytes) + { + case 4: *--parser = (ch | 0x80) & 0xbf; ch >>= 6; + case 3: *--parser = (ch | 0x80) & 0xbf; ch >>= 6; + case 2: *--parser = (ch | 0x80) & 0xbf; ch >>= 6; + } + *--parser = ch | mark[bytes]; + + return bytes; +} + /* * CP437 handling */ diff --git a/cucul/cucul_internals.h b/cucul/cucul_internals.h index b4f4e0f..0226107 100644 --- a/cucul/cucul_internals.h +++ b/cucul/cucul_internals.h @@ -58,6 +58,7 @@ extern void _cucul_putchar32(cucul_canvas_t *, int, int, uint32_t); extern unsigned int _cucul_strlen_utf8(char const *); extern char const *_cucul_skip_utf8(char const *, unsigned int); extern uint32_t _cucul_utf8_to_utf32(char const *); +extern unsigned int _cucul_utf32_to_utf8(void *, uint32_t); extern uint8_t _cucul_utf32_to_cp437(uint32_t); extern uint32_t _cucul_cp437_to_utf32(uint8_t);