Add g_utf8_to_utf16_with_nuls for strings containing nul.
authorAndrew Wilson <andrew.wilson@linn.co.uk>
Mon, 3 Sep 2012 20:12:47 +0000 (21:12 +0100)
committerRodrigo Kumpera <kumpera@gmail.com>
Wed, 10 Oct 2012 20:28:54 +0000 (16:28 -0400)
eglib/src/giconv.c
eglib/src/glib.h

index d8dbe6bf1541ced2c69ba61921c9f4fd2ab242aa..8f507de13a74b190383b35c06a91aa46e1771338 100644 (file)
@@ -887,7 +887,7 @@ g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written)
 }
 
 gunichar2 *
-g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
+g_utf8_to_utf16_general (const gchar *str, glong len, glong *items_read, glong *items_written, gboolean include_nuls, GError **err)
 {
        gunichar2 *outbuf, *outptr;
        size_t outlen = 0;
@@ -898,8 +898,13 @@ g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_wr
        
        g_return_val_if_fail (str != NULL, NULL);
        
-       if (len < 0)
+       if (len < 0) {
+               if (include_nuls) {
+                       g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED, "Conversions with embedded nulls must pass the string length");
+                       return NULL;
+               }
                len = strlen (str);
+       }
        
        inptr = (char *) str;
        inleft = len;
@@ -924,7 +929,7 @@ g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_wr
                                *items_written = 0;
                        
                        return NULL;
-               } else if (c == 0)
+               } else if (c == 0 && !include_nuls)
                        break;
                
                outlen += g_unichar_to_utf16 (c, NULL);
@@ -945,7 +950,7 @@ g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_wr
        while (inleft > 0) {
                if ((n = decode_utf8 (inptr, inleft, &c)) < 0)
                        break;
-               else if (c == 0)
+               else if (c == 0 && !include_nuls)
                        break;
                
                outptr += g_unichar_to_utf16 (c, outptr);
@@ -958,6 +963,18 @@ g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_wr
        return outbuf;
 }
 
+gunichar2 *
+g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
+{
+       return g_utf8_to_utf16_general (str, len, items_read, items_written, FALSE, err);
+}
+
+gunichar2 *
+g_utf8_to_utf16_with_nuls (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
+{
+       return g_utf8_to_utf16_general (str, len, items_read, items_written, TRUE, err);
+}
+
 gunichar *
 g_utf8_to_ucs4 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
 {
index 28e68d3e748c5b72f5291bdb57028f938763fea7..c54a50d8fc757a6437446a3ca9ce9a30d873ed85 100644 (file)
@@ -751,6 +751,7 @@ gint       g_unichar_to_utf8 (gunichar c, gchar *outbuf);
 gunichar  *g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written);
 gunichar  *g_utf8_to_ucs4 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err);
 gunichar2 *g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err);
+gunichar2 *g_utf8_to_utf16_with_nuls (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err);
 gchar     *g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err);
 gunichar  *g_utf16_to_ucs4 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err);
 gchar     *g_ucs4_to_utf8  (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **err);