From 51c598fe0bbc0df29e8c94f51c90aa242b8bd41e Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Mon, 10 Dec 2007 11:18:13 +0100 Subject: [PATCH] UCW: Added UTF-8 routines with custom replacement char (lib/unicode.h). --- lib/unicode.h | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/unicode.h b/lib/unicode.h index fb170955..fb8722d0 100644 --- a/lib/unicode.h +++ b/lib/unicode.h @@ -84,9 +84,9 @@ put1: *p++ = 0x80 | (u & 0x3f); #define UTF8_GET_NEXT if (unlikely((*p & 0xc0) != 0x80)) goto bad; u = (u << 6) | (*p++ & 0x3f) /* Decode a character from the basic multilingual plane [0, 0xFFFF] - * or return UNI_REPLACEMENT if the encoding has been corrupted */ + * or return 'repl' if the encoding has been corrupted */ static inline byte * -utf8_get(const byte *p, uns *uu) +utf8_get_repl(const byte *p, uns *uu, uns repl) { uns u = *p++; if (u < 0x80) @@ -95,7 +95,7 @@ utf8_get(const byte *p, uns *uu) { /* Incorrect byte sequence */ bad: - u = UNI_REPLACEMENT; + u = repl; } else if (u < 0xe0) { @@ -115,9 +115,9 @@ utf8_get(const byte *p, uns *uu) } /* Decode a value from the range [0, 0x7FFFFFFF] - * or return UNI_REPLACEMENT if the encoding has been corrupted */ + * or return 'repl' if the encoding has been corrupted */ static inline byte * -utf8_32_get(const byte *p, uns *uu) +utf8_32_get_repl(const byte *p, uns *uu, uns repl) { uns u = *p++; if (u < 0x80) @@ -126,7 +126,7 @@ utf8_32_get(const byte *p, uns *uu) { /* Incorrect byte sequence */ bad: - u = UNI_REPLACEMENT; + u = repl; } else if (u < 0xe0) { @@ -163,6 +163,22 @@ get1: UTF8_GET_NEXT; return (byte *)p; } +/* Decode a character from the basic multilingual plane [0, 0xFFFF] + * or return UNI_REPLACEMENT if the encoding has been corrupted */ +static inline byte * +utf8_get(const byte *p, uns *uu) +{ + return utf8_get_repl(p, uu, UNI_REPLACEMENT); +} + +/* Decode a value from the range [0, 0x7FFFFFFF] + * or return UNI_REPLACEMENT if the encoding has been corrupted */ +static inline byte * +utf8_32_get(const byte *p, uns *uu) +{ + return utf8_32_get_repl(p, uu, UNI_REPLACEMENT); +} + #define PUT_UTF8(p,u) p = utf8_put(p, u) #define GET_UTF8(p,u) p = (byte*)utf8_get(p, &(u)) -- 2.39.2