]> mj.ucw.cz Git - libucw.git/blobdiff - lib/unicode.h
bgetl() should return uns instead of u32
[libucw.git] / lib / unicode.h
index fb170955f8d00c84a30e1d5d6254caf36a67a52f..9a3fe07a27ea2bd7785b2f55c440145fd7d7b266 100644 (file)
@@ -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))
 
@@ -223,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
@@ -241,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