Convert Samba3 to use the common lib/util/charset code (partial)
[abartlet/samba.git/.git] / source3 / lib / util_str.c
index b9ccb83e556134a2a54a717b7f3811d943c7870d..ffa998bca282fb0540b586a02717d518d5bd4458 100644 (file)
@@ -384,11 +384,11 @@ size_t str_ascii_charnum(const char *s)
 {
        size_t ret, converted_size;
        char *tmpbuf2 = NULL;
-       if (!push_ascii_allocate(&tmpbuf2, s, &converted_size)) {
+       if (!push_ascii_talloc(NULL, &tmpbuf2, s, &converted_size)) {
                return 0;
        }
        ret = strlen(tmpbuf2);
-       SAFE_FREE(tmpbuf2);
+       TALLOC_FREE(tmpbuf2);
        return ret;
 }
 
@@ -1139,137 +1139,6 @@ char *string_truncate(char *s, unsigned int length)
        return s;
 }
 
-/**
- Strchr and strrchr_m are very hard to do on general multi-byte strings.
- We convert via ucs2 for now.
-**/
-
-char *strchr_m(const char *src, char c)
-{
-       smb_ucs2_t *ws = NULL;
-       char *s2 = NULL;
-       smb_ucs2_t *p;
-       const char *s;
-       char *ret;
-       size_t converted_size;
-
-       /* characters below 0x3F are guaranteed to not appear in
-          non-initial position in multi-byte charsets */
-       if ((c & 0xC0) == 0) {
-               return strchr(src, c);
-       }
-
-       /* this is quite a common operation, so we want it to be
-          fast. We optimise for the ascii case, knowing that all our
-          supported multi-byte character sets are ascii-compatible
-          (ie. they match for the first 128 chars) */
-
-       for (s = src; *s && !(((unsigned char)s[0]) & 0x80); s++) {
-               if (*s == c)
-                       return (char *)s;
-       }
-
-       if (!*s)
-               return NULL;
-
-#ifdef BROKEN_UNICODE_COMPOSE_CHARACTERS
-       /* With compose characters we must restart from the beginning. JRA. */
-       s = src;
-#endif
-
-       if (!push_ucs2_allocate(&ws, s, &converted_size)) {
-               /* Wrong answer, but what can we do... */
-               return strchr(src, c);
-       }
-       p = strchr_w(ws, UCS2_CHAR(c));
-       if (!p) {
-               SAFE_FREE(ws);
-               return NULL;
-       }
-       *p = 0;
-       if (!pull_ucs2_allocate(&s2, ws, &converted_size)) {
-               SAFE_FREE(ws);
-               /* Wrong answer, but what can we do... */
-               return strchr(src, c);
-       }
-       ret = (char *)(s+strlen(s2));
-       SAFE_FREE(ws);
-       SAFE_FREE(s2);
-       return ret;
-}
-
-char *strrchr_m(const char *s, char c)
-{
-       /* characters below 0x3F are guaranteed to not appear in
-          non-initial position in multi-byte charsets */
-       if ((c & 0xC0) == 0) {
-               return strrchr(s, c);
-       }
-
-       /* this is quite a common operation, so we want it to be
-          fast. We optimise for the ascii case, knowing that all our
-          supported multi-byte character sets are ascii-compatible
-          (ie. they match for the first 128 chars). Also, in Samba
-          we only search for ascii characters in 'c' and that
-          in all mb character sets with a compound character
-          containing c, if 'c' is not a match at position
-          p, then p[-1] > 0x7f. JRA. */
-
-       {
-               size_t len = strlen(s);
-               const char *cp = s;
-               bool got_mb = false;
-
-               if (len == 0)
-                       return NULL;
-               cp += (len - 1);
-               do {
-                       if (c == *cp) {
-                               /* Could be a match. Part of a multibyte ? */
-                               if ((cp > s) &&
-                                       (((unsigned char)cp[-1]) & 0x80)) {
-                                       /* Yep - go slow :-( */
-                                       got_mb = true;
-                                       break;
-                               }
-                               /* No - we have a match ! */
-                               return (char *)cp;
-                       }
-               } while (cp-- != s);
-               if (!got_mb)
-                       return NULL;
-       }
-
-       /* String contained a non-ascii char. Slow path. */
-       {
-               smb_ucs2_t *ws = NULL;
-               char *s2 = NULL;
-               smb_ucs2_t *p;
-               char *ret;
-               size_t converted_size;
-
-               if (!push_ucs2_allocate(&ws, s, &converted_size)) {
-                       /* Wrong answer, but what can we do. */
-                       return strrchr(s, c);
-               }
-               p = strrchr_w(ws, UCS2_CHAR(c));
-               if (!p) {
-                       SAFE_FREE(ws);
-                       return NULL;
-               }
-               *p = 0;
-               if (!pull_ucs2_allocate(&s2, ws, &converted_size)) {
-                       SAFE_FREE(ws);
-                       /* Wrong answer, but what can we do. */
-                       return strrchr(s, c);
-               }
-               ret = (char *)(s+strlen(s2));
-               SAFE_FREE(ws);
-               SAFE_FREE(s2);
-               return ret;
-       }
-}
-
 /***********************************************************************
  Return the equivalent of doing strrchr 'n' times - always going
  backwards.
@@ -1385,144 +1254,6 @@ char *strstr_m(const char *src, const char *findstr)
        return retp;
 }
 
-/**
- Convert a string to lower case.
-**/
-
-void strlower_m(char *s)
-{
-       size_t len;
-       int errno_save;
-
-       /* this is quite a common operation, so we want it to be
-          fast. We optimise for the ascii case, knowing that all our
-          supported multi-byte character sets are ascii-compatible
-          (ie. they match for the first 128 chars) */
-
-       while (*s && !(((unsigned char)s[0]) & 0x80)) {
-               *s = tolower_ascii((unsigned char)*s);
-               s++;
-       }
-
-       if (!*s)
-               return;
-
-       /* I assume that lowercased string takes the same number of bytes
-        * as source string even in UTF-8 encoding. (VIV) */
-       len = strlen(s) + 1;
-       errno_save = errno;
-       errno = 0;
-       unix_strlower(s,len,s,len);
-       /* Catch mb conversion errors that may not terminate. */
-       if (errno)
-               s[len-1] = '\0';
-       errno = errno_save;
-}
-
-/**
- Convert a string to upper case.
-**/
-
-void strupper_m(char *s)
-{
-       size_t len;
-       int errno_save;
-
-       /* this is quite a common operation, so we want it to be
-          fast. We optimise for the ascii case, knowing that all our
-          supported multi-byte character sets are ascii-compatible
-          (ie. they match for the first 128 chars) */
-
-       while (*s && !(((unsigned char)s[0]) & 0x80)) {
-               *s = toupper_ascii_fast((unsigned char)*s);
-               s++;
-       }
-
-       if (!*s)
-               return;
-
-       /* I assume that lowercased string takes the same number of bytes
-        * as source string even in multibyte encoding. (VIV) */
-       len = strlen(s) + 1;
-       errno_save = errno;
-       errno = 0;
-       unix_strupper(s,len,s,len);
-       /* Catch mb conversion errors that may not terminate. */
-       if (errno)
-               s[len-1] = '\0';
-       errno = errno_save;
-}
-
-/**
- Count the number of UCS2 characters in a string. Normally this will
- be the same as the number of bytes in a string for single byte strings,
- but will be different for multibyte.
-**/
-
-size_t strlen_m(const char *s)
-{
-       size_t count = 0;
-
-       if (!s) {
-               return 0;
-       }
-
-       while (*s && !(((uint8_t)*s) & 0x80)) {
-               s++;
-               count++;
-       }
-
-       if (!*s) {
-               return count;
-       }
-
-       while (*s) {
-               size_t c_size;
-               codepoint_t c = next_codepoint(s, &c_size);
-               if (c < 0x10000) {
-                       /* Unicode char fits into 16 bits. */
-                       count += 1;
-               } else {
-                       /* Double-width unicode char - 32 bits. */
-                       count += 2;
-               }
-               s += c_size;
-       }
-
-       return count;
-}
-
-/**
- Count the number of UCS2 characters in a string including the null
- terminator.
-**/
-
-size_t strlen_m_term(const char *s)
-{
-       if (!s) {
-               return 0;
-       }
-       return strlen_m(s) + 1;
-}
-
-/*
- * Weird helper routine for the winreg pipe: If nothing is around, return 0,
- * if a string is there, include the terminator.
- */
-
-size_t strlen_m_term_null(const char *s)
-{
-       size_t len;
-       if (!s) {
-               return 0;
-       }
-       len = strlen_m(s);
-       if (len == 0) {
-               return 0;
-       }
-
-       return len+1;
-}
 /**
  Return a RFC2254 binary string representation of a buffer.
  Used in LDAP filters.