From 1c6c7f748956caae98a79be89120c537c2032b7e Mon Sep 17 00:00:00 2001 From: Tor Lillqvist Date: Mon, 2 Aug 1999 23:16:32 +0000 Subject: [PATCH] glib.h gstrfuncs.c Rename g_strccpy to g_strcompress and g_strecpy to * glib.h * gstrfuncs.c * tests/strfunc-test.c: Rename g_strccpy to g_strcompress and g_strecpy to g_strescape per Tim Janik's suggestion. Dropped the destination parameter, always g_malloc a new string. Fix bug in g_strcompress, octal digits were gobbled up without limit, should use max three. Sources that use g_strescape must have ifdefs to be compilable both with GLib 1.2 and 1.3. --- ChangeLog | 13 +++++++++++++ ChangeLog.pre-2-0 | 13 +++++++++++++ ChangeLog.pre-2-10 | 13 +++++++++++++ ChangeLog.pre-2-12 | 13 +++++++++++++ ChangeLog.pre-2-2 | 13 +++++++++++++ ChangeLog.pre-2-4 | 13 +++++++++++++ ChangeLog.pre-2-6 | 13 +++++++++++++ ChangeLog.pre-2-8 | 13 +++++++++++++ NEWS | 3 ++- glib.def | 3 +-- glib.h | 30 ++++++++++++++++-------------- glib/glib.def | 3 +-- glib/glib.h | 30 ++++++++++++++++-------------- glib/gstrfuncs.c | 20 ++++++++++---------- gstrfuncs.c | 20 ++++++++++---------- tests/strfunc-test.c | 15 +++++++-------- 16 files changed, 167 insertions(+), 61 deletions(-) diff --git a/ChangeLog b/ChangeLog index 60df74c36..8e4cd9499 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +1999-08-03 Tor Lillqvist + + * glib.h + * gstrfuncs.c + * tests/strfunc-test.c: Rename g_strccpy to g_strcompress and + g_strecpy to g_strescape per Tim Janik's suggestion. Dropped the + destination parameter, always g_malloc a new string. Fix bug in + g_strcompress, octal digits were gobbled up without limit, should + use max three. + + Sources that use g_strescape must have ifdefs to be compilable + both with GLib 1.2 and 1.3. + Sat Jul 31 17:52:03 PDT 1999 Manish Singh * glib.h diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 60df74c36..8e4cd9499 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,16 @@ +1999-08-03 Tor Lillqvist + + * glib.h + * gstrfuncs.c + * tests/strfunc-test.c: Rename g_strccpy to g_strcompress and + g_strecpy to g_strescape per Tim Janik's suggestion. Dropped the + destination parameter, always g_malloc a new string. Fix bug in + g_strcompress, octal digits were gobbled up without limit, should + use max three. + + Sources that use g_strescape must have ifdefs to be compilable + both with GLib 1.2 and 1.3. + Sat Jul 31 17:52:03 PDT 1999 Manish Singh * glib.h diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 60df74c36..8e4cd9499 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,16 @@ +1999-08-03 Tor Lillqvist + + * glib.h + * gstrfuncs.c + * tests/strfunc-test.c: Rename g_strccpy to g_strcompress and + g_strecpy to g_strescape per Tim Janik's suggestion. Dropped the + destination parameter, always g_malloc a new string. Fix bug in + g_strcompress, octal digits were gobbled up without limit, should + use max three. + + Sources that use g_strescape must have ifdefs to be compilable + both with GLib 1.2 and 1.3. + Sat Jul 31 17:52:03 PDT 1999 Manish Singh * glib.h diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 60df74c36..8e4cd9499 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,16 @@ +1999-08-03 Tor Lillqvist + + * glib.h + * gstrfuncs.c + * tests/strfunc-test.c: Rename g_strccpy to g_strcompress and + g_strecpy to g_strescape per Tim Janik's suggestion. Dropped the + destination parameter, always g_malloc a new string. Fix bug in + g_strcompress, octal digits were gobbled up without limit, should + use max three. + + Sources that use g_strescape must have ifdefs to be compilable + both with GLib 1.2 and 1.3. + Sat Jul 31 17:52:03 PDT 1999 Manish Singh * glib.h diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 60df74c36..8e4cd9499 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,16 @@ +1999-08-03 Tor Lillqvist + + * glib.h + * gstrfuncs.c + * tests/strfunc-test.c: Rename g_strccpy to g_strcompress and + g_strecpy to g_strescape per Tim Janik's suggestion. Dropped the + destination parameter, always g_malloc a new string. Fix bug in + g_strcompress, octal digits were gobbled up without limit, should + use max three. + + Sources that use g_strescape must have ifdefs to be compilable + both with GLib 1.2 and 1.3. + Sat Jul 31 17:52:03 PDT 1999 Manish Singh * glib.h diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 60df74c36..8e4cd9499 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,16 @@ +1999-08-03 Tor Lillqvist + + * glib.h + * gstrfuncs.c + * tests/strfunc-test.c: Rename g_strccpy to g_strcompress and + g_strecpy to g_strescape per Tim Janik's suggestion. Dropped the + destination parameter, always g_malloc a new string. Fix bug in + g_strcompress, octal digits were gobbled up without limit, should + use max three. + + Sources that use g_strescape must have ifdefs to be compilable + both with GLib 1.2 and 1.3. + Sat Jul 31 17:52:03 PDT 1999 Manish Singh * glib.h diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 60df74c36..8e4cd9499 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,16 @@ +1999-08-03 Tor Lillqvist + + * glib.h + * gstrfuncs.c + * tests/strfunc-test.c: Rename g_strccpy to g_strcompress and + g_strecpy to g_strescape per Tim Janik's suggestion. Dropped the + destination parameter, always g_malloc a new string. Fix bug in + g_strcompress, octal digits were gobbled up without limit, should + use max three. + + Sources that use g_strescape must have ifdefs to be compilable + both with GLib 1.2 and 1.3. + Sat Jul 31 17:52:03 PDT 1999 Manish Singh * glib.h diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 60df74c36..8e4cd9499 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,16 @@ +1999-08-03 Tor Lillqvist + + * glib.h + * gstrfuncs.c + * tests/strfunc-test.c: Rename g_strccpy to g_strcompress and + g_strecpy to g_strescape per Tim Janik's suggestion. Dropped the + destination parameter, always g_malloc a new string. Fix bug in + g_strcompress, octal digits were gobbled up without limit, should + use max three. + + Sources that use g_strescape must have ifdefs to be compilable + both with GLib 1.2 and 1.3. + Sat Jul 31 17:52:03 PDT 1999 Manish Singh * glib.h diff --git a/NEWS b/NEWS index 97073e951..571ab697d 100644 --- a/NEWS +++ b/NEWS @@ -9,7 +9,8 @@ What's new in GLib 1.3.1: * Double ended queue implementation. * GLib macros for printf() formatting, e.g. G_GULONG_FORMAT = "lu" * New thread-safe random number generator Mersenne Twister. - +* g_strcompress() added, g_strescape() had a slight API change, and + more tightly defined semantics. Overview of Changes in GLib 1.2.1: diff --git a/glib.def b/glib.def index fc6882b94..654e071e8 100644 --- a/glib.def +++ b/glib.def @@ -366,14 +366,13 @@ EXPORTS g_strcasecmp g_strchomp g_strchug + g_strcompress g_strconcat - g_strccpy g_strdelimit g_strdown g_strdup g_strdup_printf g_strdup_vprintf - g_strecpy g_strerror g_strescape g_strfreev diff --git a/glib.h b/glib.h index dac8f51c9..72e6d68da 100644 --- a/glib.h +++ b/glib.h @@ -1500,7 +1500,7 @@ gchar* g_strchomp (gchar *string); #define g_strstrip( string ) g_strchomp (g_strchug (string)) /* String utility functions that return a newly allocated string which - * ought to be freed from the caller at some point. + * ought to be freed with g_free from the caller at some point. */ gchar* g_strdup (const gchar *str); gchar* g_strdup_printf (const gchar *format, @@ -1515,21 +1515,23 @@ gchar* g_strconcat (const gchar *string1, ...); /* NULL terminated */ gchar* g_strjoin (const gchar *separator, ...); /* NULL terminated */ -/* Copy a string interpreting C string -style escape sequences. - * The recognized sequences are \b \f \n \r \t \\ \" and the octal format. +/* Make a copy of a string interpreting C string -style escape + * sequences. Inverse of g_strescape. The recognized sequences are \b + * \f \n \r \t \\ \" and the octal format. */ -gchar* g_strccpy (gchar *dest, - const gchar *source); -/* Copy a string escaping nonprintable characters like in C strings. - * Inverse of g_strccpy. The exceptions parameter if non-NULL points - * to a string containing characters that are not escaped. - */ -gchar* g_strecpy (gchar *dest, - const gchar *source, - const gchar *exceptions); +gchar* g_strcompress (const gchar *source); -/* deprecated function (used to be a real function) */ -#define g_strescape(src) g_strecpy (g_new (char, strlen (src)*4+1), (src), NULL) +/* Copy a string escaping nonprintable characters like in C strings. + * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points + * to a string containing characters that are not to be escaped. + */ +gchar* g_strescape (const gchar *source, + const gchar *exceptions); +/* Deprecated API: + * gchar* g_strescape (const gchar *source); + * Luckily this function wasn't much used. + * Add a second NULL parameter in calls for mostly identical semantics. + */ gpointer g_memdup (gconstpointer mem, guint byte_size); diff --git a/glib/glib.def b/glib/glib.def index fc6882b94..654e071e8 100644 --- a/glib/glib.def +++ b/glib/glib.def @@ -366,14 +366,13 @@ EXPORTS g_strcasecmp g_strchomp g_strchug + g_strcompress g_strconcat - g_strccpy g_strdelimit g_strdown g_strdup g_strdup_printf g_strdup_vprintf - g_strecpy g_strerror g_strescape g_strfreev diff --git a/glib/glib.h b/glib/glib.h index dac8f51c9..72e6d68da 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -1500,7 +1500,7 @@ gchar* g_strchomp (gchar *string); #define g_strstrip( string ) g_strchomp (g_strchug (string)) /* String utility functions that return a newly allocated string which - * ought to be freed from the caller at some point. + * ought to be freed with g_free from the caller at some point. */ gchar* g_strdup (const gchar *str); gchar* g_strdup_printf (const gchar *format, @@ -1515,21 +1515,23 @@ gchar* g_strconcat (const gchar *string1, ...); /* NULL terminated */ gchar* g_strjoin (const gchar *separator, ...); /* NULL terminated */ -/* Copy a string interpreting C string -style escape sequences. - * The recognized sequences are \b \f \n \r \t \\ \" and the octal format. +/* Make a copy of a string interpreting C string -style escape + * sequences. Inverse of g_strescape. The recognized sequences are \b + * \f \n \r \t \\ \" and the octal format. */ -gchar* g_strccpy (gchar *dest, - const gchar *source); -/* Copy a string escaping nonprintable characters like in C strings. - * Inverse of g_strccpy. The exceptions parameter if non-NULL points - * to a string containing characters that are not escaped. - */ -gchar* g_strecpy (gchar *dest, - const gchar *source, - const gchar *exceptions); +gchar* g_strcompress (const gchar *source); -/* deprecated function (used to be a real function) */ -#define g_strescape(src) g_strecpy (g_new (char, strlen (src)*4+1), (src), NULL) +/* Copy a string escaping nonprintable characters like in C strings. + * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points + * to a string containing characters that are not to be escaped. + */ +gchar* g_strescape (const gchar *source, + const gchar *exceptions); +/* Deprecated API: + * gchar* g_strescape (const gchar *source); + * Luckily this function wasn't much used. + * Add a second NULL parameter in calls for mostly identical semantics. + */ gpointer g_memdup (gconstpointer mem, guint byte_size); diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index b71fd3f0f..e8cc1bae2 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -1073,14 +1073,12 @@ g_strdelimit (gchar *string, } gchar* -g_strccpy (gchar *dest, - const gchar *source) +g_strcompress (const gchar *source) { - const gchar *p = source; + const gchar *p = source, *octal; + gchar *dest = g_malloc (strlen (source) + 1); gchar *q = dest; - g_return_val_if_fail (dest != NULL, NULL); - while (*p) { if (*p == '\\') @@ -1091,7 +1089,8 @@ g_strccpy (gchar *dest, case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': *q = 0; - while ((*p >= '0') && (*p <= '7')) + octal = p; + while ((p < octal + 3) && (*p >= '0') && (*p <= '7')) { *q = (*q * 8) + (*p - '0'); p++; @@ -1128,11 +1127,12 @@ g_strccpy (gchar *dest, } gchar * -g_strecpy (gchar *dest, - const gchar *src, - const gchar *exceptions) +g_strescape (const gchar *source, + const gchar *exceptions) { - const guchar *p = (guchar *) src; + const guchar *p = (guchar *) source; + /* Each source byte needs maximally four destination chars (\777) */ + gchar *dest = g_malloc (strlen (source) * 4 + 1); gchar *q = dest; guchar excmap[256]; diff --git a/gstrfuncs.c b/gstrfuncs.c index b71fd3f0f..e8cc1bae2 100644 --- a/gstrfuncs.c +++ b/gstrfuncs.c @@ -1073,14 +1073,12 @@ g_strdelimit (gchar *string, } gchar* -g_strccpy (gchar *dest, - const gchar *source) +g_strcompress (const gchar *source) { - const gchar *p = source; + const gchar *p = source, *octal; + gchar *dest = g_malloc (strlen (source) + 1); gchar *q = dest; - g_return_val_if_fail (dest != NULL, NULL); - while (*p) { if (*p == '\\') @@ -1091,7 +1089,8 @@ g_strccpy (gchar *dest, case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': *q = 0; - while ((*p >= '0') && (*p <= '7')) + octal = p; + while ((p < octal + 3) && (*p >= '0') && (*p <= '7')) { *q = (*q * 8) + (*p - '0'); p++; @@ -1128,11 +1127,12 @@ g_strccpy (gchar *dest, } gchar * -g_strecpy (gchar *dest, - const gchar *src, - const gchar *exceptions) +g_strescape (const gchar *source, + const gchar *exceptions) { - const guchar *p = (guchar *) src; + const guchar *p = (guchar *) source; + /* Each source byte needs maximally four destination chars (\777) */ + gchar *dest = g_malloc (strlen (source) * 4 + 1); gchar *q = dest; guchar excmap[256]; diff --git a/tests/strfunc-test.c b/tests/strfunc-test.c index 16d5584cf..ecb874297 100644 --- a/tests/strfunc-test.c +++ b/tests/strfunc-test.c @@ -99,15 +99,14 @@ main (int argc, g_assert (strcmp(string, "00021 test ") == 0); g_free (string); - g_assert (strcmp(g_strccpy(g_malloc(100), - "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313"), - "abc\\\"\b\f\n\r\t\003\177\234\313") == 0); - g_assert (strcmp(g_strecpy(g_malloc(100), - "abc\\\"\b\f\n\r\t\003\177\234\313", - NULL), + g_assert (strcmp + (g_strcompress("abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313\\12345z"), + "abc\\\"\b\f\n\r\t\003\177\234\313\12345z") == 0); + g_assert (strcmp(g_strescape("abc\\\"\b\f\n\r\t\003\177\234\313", + NULL), "abc\\\\\\\"\\b\\f\\n\\r\\t\\003\\177\\234\\313") == 0); - g_assert (strcmp(g_strecpy(g_malloc(100),"abc\\\"\b\f\n\r\t\003\177\234\313", - "\b\f\001\002\003\004"), + g_assert (strcmp(g_strescape("abc\\\"\b\f\n\r\t\003\177\234\313", + "\b\f\001\002\003\004"), "abc\\\\\\\"\b\f\\n\\r\\t\003\\177\\234\\313") == 0); return 0;