Prechádzať zdrojové kódy

2006-10-08 Gonzalo Paniagua Javier <[email protected]>

	* test/string-util.c:
	* TODO:
	* src/gstr.c:
	* src/glib.h: implemented g_strescape.


svn path=/trunk/mono/; revision=66395
Gonzalo Paniagua Javier 19 rokov pred
rodič
commit
33f8d093b3
5 zmenil súbory, kde vykonal 87 pridanie a 1 odobranie
  1. 7 0
      eglib/ChangeLog
  2. 0 1
      eglib/TODO
  3. 1 0
      eglib/src/glib.h
  4. 58 0
      eglib/src/gstr.c
  5. 21 0
      eglib/test/string-util.c

+ 7 - 0
eglib/ChangeLog

@@ -1,3 +1,10 @@
+2006-10-08 Gonzalo Paniagua Javier <[email protected]>
+
+	* test/string-util.c:
+	* TODO:
+	* src/gstr.c:
+	* src/glib.h: implemented g_strescape.
+
 2006-10-08 Gonzalo Paniagua Javier <[email protected]>
 
 	* test/string-util.c:

+ 0 - 1
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

+ 1 - 0
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);

+ 58 - 0
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;
+}
+

+ 21 - 0
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}
 };