diff options
author | Gonzalo Paniagua Javier <gonzalo.mono@gmail.com> | 2006-10-08 09:43:40 +0400 |
---|---|---|
committer | Gonzalo Paniagua Javier <gonzalo.mono@gmail.com> | 2006-10-08 09:43:40 +0400 |
commit | 33f8d093b3f9ca2c02ca351d2cae63b559c7c5dd (patch) | |
tree | 53f923108d49e52455b0392a44cc1809bcc02e37 /eglib | |
parent | 423dac532236f11fe1ac2785ad2c1330b5972290 (diff) |
2006-10-08 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* test/string-util.c:
* TODO:
* src/gstr.c:
* src/glib.h: implemented g_strescape.
svn path=/trunk/mono/; revision=66395
Diffstat (limited to 'eglib')
-rw-r--r-- | eglib/ChangeLog | 7 | ||||
-rw-r--r-- | eglib/TODO | 1 | ||||
-rw-r--r-- | eglib/src/glib.h | 1 | ||||
-rw-r--r-- | eglib/src/gstr.c | 58 | ||||
-rw-r--r-- | eglib/test/string-util.c | 21 |
5 files changed, 87 insertions, 1 deletions
diff --git a/eglib/ChangeLog b/eglib/ChangeLog index aed2fcb1146..62e08c1e9e5 100644 --- a/eglib/ChangeLog +++ b/eglib/ChangeLog @@ -1,6 +1,13 @@ 2006-10-08 Gonzalo Paniagua Javier <gonzalo@ximian.com> * test/string-util.c: + * TODO: + * src/gstr.c: + * src/glib.h: implemented g_strescape. + +2006-10-08 Gonzalo Paniagua Javier <gonzalo@ximian.com> + + * test/string-util.c: * configure.ac: * TODO: * src/gstr.c: diff --git a/eglib/TODO b/eglib/TODO index 8e8bb0833c5..a5d87289f15 100644 --- a/eglib/TODO +++ b/eglib/TODO @@ -21,7 +21,6 @@ Important Groups: * String manipulation 1 g_filename_from_utf8 [LIMITATION: UTF8 only today] - 1 g_strescape * Miscelaneous 3 g_spaced_primes_closest diff --git a/eglib/src/glib.h b/eglib/src/glib.h index 3629a294b76..54879979102 100644 --- a/eglib/src/glib.h +++ b/eglib/src/glib.h @@ -176,6 +176,7 @@ gchar *g_strjoin (const gchar *separator, ...); gchar *g_strchug (gchar *str); gchar *g_strchomp (gchar *str); gchar *g_strdelimit (gchar *string, const gchar *delimiters, gchar new_delimiter); +gchar *g_strescape (const gchar *source, const gchar *exceptions); gchar *g_filename_to_uri (const gchar *filename, const gchar *hostname, GError **error); gchar *g_filename_from_uri (const gchar *uri, gchar **hostname, GError **error); diff --git a/eglib/src/gstr.c b/eglib/src/gstr.c index 5cd6ad479a0..d4493a5c42b 100644 --- a/eglib/src/gstr.c +++ b/eglib/src/gstr.c @@ -561,3 +561,61 @@ g_strlcpy (gchar *dest, const gchar *src, gsize dest_size) } #endif +static gchar escaped_dflt [256] = { + 1, 1, 1, 1, 1, 1, 1, 1, 'b', 't', 'n', 1, 'f', 'r', 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 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, 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, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 +}; + +gchar * +g_strescape (const gchar *source, const gchar *exceptions) +{ + gchar escaped [256]; + const gchar *ptr; + gchar c; + int op; + gchar *result; + gchar *res_ptr; + + g_return_val_if_fail (source != NULL, NULL); + + memcpy (escaped, escaped_dflt, 256); + if (exceptions != NULL) { + for (ptr = exceptions; *ptr; ptr++) + escaped [(int) *ptr] = 0; + } + result = g_malloc (strlen (source) * 4 + 1); /* Worst case: everything octal. */ + res_ptr = result; + for (ptr = source; *ptr; ptr++) { + c = *ptr; + op = escaped [(int) c]; + if (op == 0) { + *res_ptr++ = c; + } else { + *res_ptr++ = '\\'; + if (op != 1) { + *res_ptr++ = op; + } else { + *res_ptr++ = '0' + ((c >> 6) & 7); + *res_ptr++ = '0' + ((c >> 3) & 7); + *res_ptr++ = '0' + (c & 7); + } + } + } + *res_ptr = '\0'; + return result; +} + diff --git a/eglib/test/string-util.c b/eglib/test/string-util.c index 8483918952a..6d27a0600cf 100644 --- a/eglib/test/string-util.c +++ b/eglib/test/string-util.c @@ -322,6 +322,26 @@ test_strlcpy () return OK; } +RESULT +test_strescape () +{ + gchar *str; + + str = g_strescape ("abc", NULL); + if (strcmp ("abc", str)) + return FAILED ("#1"); + str = g_strescape ("\t\b\f\n\r\\\"abc", NULL); + if (strcmp ("\\t\\b\\f\\n\\r\\\\\\\"abc", str)) + return FAILED ("#2 %s", str); + str = g_strescape ("\001abc", NULL); + if (strcmp ("\\001abc", str)) + return FAILED ("#3 %s", str); + str = g_strescape ("\001abc", "\001"); + if (strcmp ("\001abc", str)) + return FAILED ("#3 %s", str); + return OK; +} + static Test strutil_tests [] = { {"g_strfreev", test_strfreev}, {"g_strconcat", test_concat}, @@ -336,6 +356,7 @@ static Test strutil_tests [] = { {"g_ascii_xdigit_value", test_ascii_xdigit_value}, {"g_strdelimit", test_strdelimit}, {"g_strlcpy", test_strlcpy}, + {"g_strescape", test_strescape}, {NULL, NULL} }; |