s3:lib/util_str: add strlen_m_ext() that takes input and output charset
authorMichael Adam <obnox@samba.org>
Sat, 30 Oct 2010 00:03:02 +0000 (02:03 +0200)
committerMichael Adam <obnox@samba.org>
Wed, 3 Nov 2010 22:45:19 +0000 (22:45 +0000)
The function calculates the number of units (8 or 16-bit, depending
on the destination charset), that would be needed to convert the
input string which is expected to be in in src_charset encoding
to the dst_charset (which should be a unicode charset).

source3/include/proto.h
source3/lib/util_str.c

index 89aa623314f1da7fe6f564121e85f27f130f5dfc..5e88476e4435d5d42877dbdc3d845c30366533ac 100644 (file)
@@ -1433,6 +1433,8 @@ char *strnrchr_m(const char *s, char c, unsigned int n);
 char *strstr_m(const char *src, const char *findstr);
 void strlower_m(char *s);
 void strupper_m(char *s);
+size_t strlen_m_ext(const char *s, const charset_t src_charset,
+                   const charset_t dst_charset);
 size_t strlen_m(const char *s);
 size_t strlen_m_term(const char *s);
 size_t strlen_m_term_null(const char *s);
index 4e16fb3a45e91d99522d8ad3bab904b4d6d88da3..27147365cd5a2569bab77a5dc33595ea7125fa24 100644 (file)
@@ -1454,14 +1454,14 @@ void strupper_m(char *s)
 }
 
 /**
- * Calculate the number of 16-bit units that would be needed to convert
- * the input string which is expected to be in CH_UNIX encoding to UTF16.
- *
- * This will be the same as the number of bytes in a string for single
- * byte strings, but will be different for multibyte.
+ * Calculate the number of units (8 or 16-bit, depending on the
+ * destination charset), that would be needed to convert the input
+ * string which is expected to be in in src_charset encoding to the
+ * destination charset (which should be a unicode charset).
  */
 
-size_t strlen_m(const char *s)
+size_t strlen_m_ext(const char *s, const charset_t src_charset,
+                   const charset_t dst_charset)
 {
        size_t count = 0;
 
@@ -1480,20 +1480,62 @@ size_t strlen_m(const char *s)
 
        while (*s) {
                size_t c_size;
-               codepoint_t c = next_codepoint(s, &c_size);
-               if (c < 0x10000) {
-                       /* Unicode char fits into 16 bits. */
+               codepoint_t c = next_codepoint_ext(s, src_charset, &c_size);
+               s += c_size;
+
+               switch (dst_charset) {
+               case CH_UTF16LE:
+               case CH_UTF16BE:
+               case CH_UTF16MUNGED:
+                       if (c < 0x10000) {
+                               /* Unicode char fits into 16 bits. */
+                               count += 1;
+                       } else {
+                               /* Double-width unicode char - 32 bits. */
+                               count += 2;
+                       }
+                       break;
+               case CH_UTF8:
+                       /*
+                        * this only checks ranges, and does not
+                        * check for invalid codepoints
+                        */
+                       if (c < 0x80) {
+                               count += 1;
+                       } else if (c < 0x800) {
+                               count += 2;
+                       } else if (c < 0x1000) {
+                               count += 3;
+                       } else {
+                               count += 4;
+                       }
+                       break;
+               default:
+                       /*
+                        * non-unicode encoding:
+                        * assume that each codepoint fits into
+                        * one unit in the destination encoding.
+                        */
                        count += 1;
-               } else {
-                       /* Double-width unicode char - 32 bits. */
-                       count += 2;
                }
-               s += c_size;
        }
 
        return count;
 }
 
+/**
+ * Calculate the number of 16-bit units that would bee needed to convert
+ * the input string which is expected to be in CH_UNIX encoding to UTF16.
+ *
+ * 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)
+{
+       return strlen_m_ext(s, CH_UNIX, CH_UTF16LE);
+}
+
 /**
  Count the number of UCS2 characters in a string including the null
  terminator.