diff options
author | Andrew Wilson <andrew.wilson@linn.co.uk> | 2012-09-04 00:12:47 +0400 |
---|---|---|
committer | Rodrigo Kumpera <kumpera@gmail.com> | 2012-10-11 00:28:54 +0400 |
commit | 338b245134a3c496f8cbef8916ee425866e89fbc (patch) | |
tree | 7c4f80b8bcdb7c8488bb6fd77ac2457666adf7a5 /eglib | |
parent | 301266f2da811515fae31c176d021de675e2e4b2 (diff) |
Add g_utf8_to_utf16_with_nuls for strings containing nul.
Diffstat (limited to 'eglib')
-rw-r--r-- | eglib/src/giconv.c | 25 | ||||
-rw-r--r-- | eglib/src/glib.h | 1 |
2 files changed, 22 insertions, 4 deletions
diff --git a/eglib/src/giconv.c b/eglib/src/giconv.c index d8dbe6bf154..8f507de13a7 100644 --- a/eglib/src/giconv.c +++ b/eglib/src/giconv.c @@ -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) { diff --git a/eglib/src/glib.h b/eglib/src/glib.h index 28e68d3e748..c54a50d8fc7 100644 --- a/eglib/src/glib.h +++ b/eglib/src/glib.h @@ -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); |