]> git.sur5r.net Git - openldap/blobdiff - libraries/libldap/utf-8.c
More cleanup in ldap_pvt_tls_destroy()
[openldap] / libraries / libldap / utf-8.c
index 6b2ac969e1046d0f8be70daa30daa521fde189eb..542814410acf7ddaf9035d6e18b0d01d97c6627c 100644 (file)
@@ -1,6 +1,6 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
 
@@ -30,9 +30,6 @@
 #include "ldap-int.h"
 #include "ldap_defaults.h"
 
-#undef LDAP_IS_ASCII
-#define LDAP_IS_ASCII(uc)      ((uc) < 0x80)
-
 /*
  * Basic UTF-8 routines
  */
@@ -60,7 +57,7 @@ ber_len_t ldap_utf8_chars( const char * p )
 
        for( ; *p ; LDAP_UTF8_INCR(p) ) {
                chars++;
-       };
+       }
 
        return chars;
 }
@@ -76,36 +73,22 @@ int ldap_utf8_offset( const char * p )
  *
  * This function should use a table lookup.
  */
+const char ldap_utf8_lentab[] = {
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+       3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
+       4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 };
+
 int ldap_utf8_charlen( const char * p )
 {
-       unsigned c = * (const unsigned char *) p;
-
-       if ((c & 0xfe ) == 0xfc) {
-               return 6;
-       }
-
-       if ((c & 0xfc ) == 0xf8) {
-               return 5;
-       }
-
-       if ((c & 0xf8 ) == 0xf0) {
-               return 4;
-       }
-
-       if ((c & 0xf0 ) == 0xe0) {
-               return 3;
-       }
-
-       if ((c & 0xe0 ) == 0xc0) {
-               return 2;
-       }
-
-       if ((c & 0x80 ) == 0x80) {
-               /* INVALID */
-               return 0;
-       }
+       if (!(*p & 0x80))
+               return 1;
 
-       return 1;
+       return ldap_utf8_lentab[*(unsigned char *)p ^ 0x80];
 }
 
 /* conv UTF-8 to UCS-4, useful for comparisons */
@@ -264,6 +247,7 @@ int ldap_utf8_copy( char* dst, const char *src )
        return i;
 }
 
+#ifndef UTF8_ALPHA_CTYPE
 /*
  * UTF-8 ctype routines
  * Only deals with characters < 0x80 (ie: US-ASCII)
@@ -272,34 +256,32 @@ int ldap_utf8_copy( char* dst, const char *src )
 int ldap_utf8_isascii( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
-       return LDAP_IS_ASCII(c);
+       return LDAP_ASCII(c);
 }
 
 int ldap_utf8_isdigit( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!LDAP_IS_ASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return c >= '0' && c <= '9';
+       return LDAP_DIGIT( c );
 }
 
 int ldap_utf8_isxdigit( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!LDAP_IS_ASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return ( c >= '0' && c <= '9' )
-               || ( c >= 'A' && c <= 'F' )
-               || ( c >= 'a' && c <= 'f' );
+       return LDAP_HEX(c);
 }
 
 int ldap_utf8_isspace( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!LDAP_IS_ASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
        switch(c) {
        case ' ':
@@ -314,7 +296,6 @@ int ldap_utf8_isspace( const char * p )
        return 0;
 }
 
-#ifndef UTF8_ALPHA_CTYPE
 /*
  * These are not needed by the C SDK and are
  * not "good enough" for general use.
@@ -323,39 +304,36 @@ int ldap_utf8_isalpha( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!LDAP_IS_ASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return ( c >= 'A' && c <= 'Z' )
-               || ( c >= 'a' && c <= 'z' );
+       return LDAP_ALPHA(c);
 }
 
 int ldap_utf8_isalnum( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!LDAP_IS_ASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return ( c >= '0' && c <= '9' )
-               || ( c >= 'A' && c <= 'Z' )
-               || ( c >= 'a' && c <= 'z' );
+       return LDAP_ALNUM(c);
 }
 
 int ldap_utf8_islower( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!LDAP_IS_ASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return ( c >= 'a' && c <= 'z' );
+       return LDAP_LOWER(c);
 }
 
 int ldap_utf8_isupper( const char * p )
 {
        unsigned c = * (const unsigned char *) p;
 
-       if(!LDAP_IS_ASCII(c)) return 0;
+       if(!LDAP_ASCII(c)) return 0;
 
-       return ( c >= 'A' && c <= 'Z' );
+       return LDAP_UPPER(c);
 }
 #endif