]> mj.ucw.cz Git - libucw.git/commitdiff
libucw: added UTF-8 correctness checker
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Thu, 15 Mar 2007 20:56:46 +0000 (21:56 +0100)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Thu, 15 Mar 2007 20:56:46 +0000 (21:56 +0100)
lib/unicode-utf8.c
lib/unicode.h

index 5f7b8186e55c62d19f88bb2179599ae497fa9e98..8c8ac1bdf0df9a2308de945df8a87d67d19fa85a 100644 (file)
@@ -36,6 +36,35 @@ utf8_strnlen(byte *str, uns n)
   return len;
 }
 
+uns
+utf8_check(byte *s)
+{
+#define UTF8_CHECK_NEXT if (unlikely((*s & 0xc0) != 0x80)) goto bad; s++
+  while (*s)
+    {
+      uns u = *s++;
+      if (u < 0x80)
+       ;
+      else if (unlikely(u < 0xc0))
+        {
+bad:
+         return 0;
+       }
+      else if (u < 0xe0)
+        {
+         UTF8_CHECK_NEXT;
+       }
+      else if (likely(u < 0xf0))
+        {
+         UTF8_CHECK_NEXT;
+         UTF8_CHECK_NEXT;
+       }
+      else
+       goto bad;
+    }
+  return 1;
+}
+
 #ifdef TEST
 #include <string.h>
 #include <stdio.h>
index 79934540c586adefee3459b7dc1e1d24a44ff9d8..5e8dac8f91dd553cbfeef1c253c80bc7ef1200ba 100644 (file)
@@ -204,5 +204,6 @@ utf8_encoding_len(uns c)
 
 uns utf8_strlen(byte *str);
 uns utf8_strnlen(byte *str, uns n);
+uns utf8_check(byte *str);
 
 #endif