Author: dick
Date: 2007-11-20 12:20:34 -0500 (Tue, 20 Nov 2007)
New Revision: 89995
Modified:
trunk/mono/eglib/ChangeLog
trunk/mono/eglib/src/glib.h
trunk/mono/eglib/src/gslist.c
trunk/mono/eglib/src/gstr.c
trunk/mono/eglib/test/slist.c
trunk/mono/eglib/test/string-util.c
Log:
2007-11-20 Dick Porter <[EMAIL PROTECTED]>
* src/gstr.c: Implement g_strsplit_set() and re-implement
g_strsplit() so it passes the additional tests. (g_strsplit()
should treat multi-char delimiters as just a single token, rather
than a set of delimiters.)
* src/gslist.c: Implement g_slist_index(), g_slist_nth() and
g_slist_nth_data().
* test/string-util.c: Test g_strsplit_set() and more tests for
g_strsplit().
* test/slist.c: Test g_slist_nth() and
g_slist_index().
Modified: trunk/mono/eglib/ChangeLog
===================================================================
--- trunk/mono/eglib/ChangeLog 2007-11-20 17:18:55 UTC (rev 89994)
+++ trunk/mono/eglib/ChangeLog 2007-11-20 17:20:34 UTC (rev 89995)
@@ -1,3 +1,19 @@
+2007-11-20 Dick Porter <[EMAIL PROTECTED]>
+
+ * src/gstr.c: Implement g_strsplit_set() and re-implement
+ g_strsplit() so it passes the additional tests. (g_strsplit()
+ should treat multi-char delimiters as just a single token, rather
+ than a set of delimiters.)
+
+ * src/gslist.c: Implement g_slist_index(), g_slist_nth() and
+ g_slist_nth_data().
+
+ * test/string-util.c: Test g_strsplit_set() and more tests for
+ g_strsplit().
+
+ * test/slist.c: Test g_slist_nth() and
+ g_slist_index().
+
2007-11-06 Geoff Norton <[EMAIL PROTECTED]>
* configure.ac: Check for libiconv_open as the previous check was
failing against
Modified: trunk/mono/eglib/src/glib.h
===================================================================
--- trunk/mono/eglib/src/glib.h 2007-11-20 17:18:55 UTC (rev 89994)
+++ trunk/mono/eglib/src/glib.h 2007-11-20 17:20:34 UTC (rev 89995)
@@ -212,6 +212,7 @@
void g_strfreev (gchar **str_array);
gchar *g_strconcat (const gchar *first, ...);
gchar **g_strsplit (const gchar *string, const gchar *delimiter,
gint max_tokens);
+gchar **g_strsplit_set (const gchar *string, const gchar *delimiter,
gint max_tokens);
gchar *g_strreverse (gchar *str);
gboolean g_str_has_prefix (const gchar *str, const gchar *prefix);
gboolean g_str_has_suffix (const gchar *str, const gchar *suffix);
@@ -332,7 +333,12 @@
gpointer data);
GSList *g_slist_sort (GSList *list,
GCompareFunc func);
-gpointer g_slist_nth_data (GSList *list, guint n);
+gint g_slist_index (GSList *list,
+ gconstpointer data);
+GSList *g_slist_nth (GSList *list,
+ guint n);
+gpointer g_slist_nth_data (GSList *list,
+ guint n);
#define g_slist_next(slist) ((slist) ? (((GSList *) (slist))->next) : NULL)
Modified: trunk/mono/eglib/src/gslist.c
===================================================================
--- trunk/mono/eglib/src/gslist.c 2007-11-20 17:18:55 UTC (rev 89994)
+++ trunk/mono/eglib/src/gslist.c 2007-11-20 17:20:34 UTC (rev 89995)
@@ -300,6 +300,40 @@
return list;
}
+gint
+g_slist_index (GSList *list, gconstpointer data)
+{
+ gint index = 0;
+
+ while (list) {
+ if (list->data == data)
+ return index;
+
+ index++;
+ list = list->next;
+ }
+
+ return -1;
+}
+
+GSList*
+g_slist_nth (GSList *list, guint n)
+{
+ for (; list; list = list->next) {
+ if (n == 0)
+ break;
+ n--;
+ }
+ return list;
+}
+
+gpointer
+g_slist_nth_data (GSList *list, guint n)
+{
+ GSList *node = g_slist_nth (list, n);
+ return node ? node->data : NULL;
+}
+
typedef GSList list_node;
#include "sort.frag.h"
@@ -310,14 +344,3 @@
return list;
return do_sort (list, func);
}
-
-gpointer
-g_slist_nth_data (GSList *list, guint n)
-{
- while (n-- && list != NULL){
- list = list->next;
- }
- if (list)
- return list->data;
- return NULL;
-}
Modified: trunk/mono/eglib/src/gstr.c
===================================================================
--- trunk/mono/eglib/src/gstr.c 2007-11-20 17:18:55 UTC (rev 89994)
+++ trunk/mono/eglib/src/gstr.c 2007-11-20 17:20:34 UTC (rev 89995)
@@ -171,68 +171,161 @@
return ret;
}
+static void
+add_to_vector (gchar ***vector, int size, gchar *token)
+{
+ *vector = *vector == NULL ?
+ (gchar **)g_malloc(2 * sizeof(*vector)) :
+ (gchar **)g_realloc(*vector, (size + 1) * sizeof(*vector));
+
+ (*vector)[size - 1] = token;
+}
+
gchar **
g_strsplit (const gchar *string, const gchar *delimiter, gint max_tokens)
{
- gchar *string_c;
- gchar *strtok_save, **vector;
- gchar *token, *token_c;
+ const gchar *c;
+ gchar *token, **vector;
gint size = 1;
- size_t token_length;
+
+ g_return_val_if_fail (string != NULL, NULL);
+ g_return_val_if_fail (delimiter != NULL, NULL);
+ g_return_val_if_fail (delimiter[0] != 0, NULL);
+
+ if (strncmp (string, delimiter, strlen (delimiter)) == 0) {
+ vector = (gchar **)g_malloc (2 * sizeof(vector));
+ vector[0] = g_strdup ("");
+ size++;
+ string += strlen (delimiter);
+ } else {
+ vector = NULL;
+ }
- g_return_val_if_fail(string != NULL, NULL);
- g_return_val_if_fail(delimiter != NULL, NULL);
- g_return_val_if_fail(delimiter[0] != 0, NULL);
+ while (*string && !(max_tokens > 0 && size >= max_tokens)) {
+ c = string;
+ if (strncmp (string, delimiter, strlen (delimiter)) == 0) {
+ token = g_strdup ("");
+ string += strlen (delimiter);
+ } else {
+ while (*string && strncmp (string, delimiter, strlen
(delimiter)) != 0) {
+ string++;
+ }
+
+ if (*string) {
+ int toklen = (string - c);
+ token = g_strndup (c, toklen);
+
+ /* Need to leave a trailing empty
+ * token if the delimiter is the last
+ * part of the string
+ */
+ if (strcmp (string, delimiter) != 0) {
+ string += strlen (delimiter);
+ }
+ } else {
+ token = g_strdup (c);
+ }
+ }
+
+ add_to_vector (&vector, size, token);
+ size++;
+ }
+
+ if (*string) {
+ /* Add the rest of the string as the last element */
+ add_to_vector (&vector, size, g_strdup (string));
+ size++;
+ }
- token_length = strlen(string);
- string_c = (gchar *)g_malloc(token_length + 1);
- memcpy(string_c, string, token_length);
- string_c[token_length] = 0;
+ if (vector == NULL) {
+ vector = (gchar **) g_malloc (2 * sizeof (vector));
+ vector [0] = NULL;
+ } else if (size > 0) {
+ vector[size - 1] = NULL;
+ }
+
+ return vector;
+}
- if (strncmp (string_c, delimiter, strlen (delimiter)) == 0){
- vector = (gchar **) g_malloc (2 * sizeof (vector));
- vector [0] = g_strdup ("");
+static gboolean
+charcmp (gchar testchar, const gchar *compare)
+{
+ while(*compare) {
+ if (*compare == testchar) {
+ return TRUE;
+ }
+ compare++;
+ }
+
+ return FALSE;
+}
+
+gchar **
+g_strsplit_set (const gchar *string, const gchar *delimiter, gint max_tokens)
+{
+ const gchar *c;
+ gchar *token, **vector;
+ gint size = 1;
+
+ g_return_val_if_fail (string != NULL, NULL);
+ g_return_val_if_fail (delimiter != NULL, NULL);
+ g_return_val_if_fail (delimiter[0] != 0, NULL);
+
+ if (charcmp (*string, delimiter)) {
+ vector = (gchar **)g_malloc (2 * sizeof(vector));
+ vector[0] = g_strdup ("");
size++;
- } else
+ string++;
+ } else {
vector = NULL;
- token = (gchar *)strtok_r(string_c, delimiter, &strtok_save);
+ }
- if (!(max_tokens > 0 && size >= max_tokens)){
- while(token != NULL) {
- token_length = strlen(token);
- token_c = (gchar *)g_malloc(token_length + 1);
- memcpy(token_c, token, token_length);
- token_c[token_length] = 0;
+ c = string;
+ while (*string && !(max_tokens > 0 && size >= max_tokens)) {
+ if (charcmp (*string, delimiter)) {
+ int toklen = (string - c);
+ if (toklen == 0) {
+ token = g_strdup ("");
+ } else {
+ token = g_strndup (c, toklen);
+ }
- vector = vector == NULL ?
- (gchar **)g_malloc(2 * sizeof(vector)) :
- (gchar **)g_realloc(vector, (size + 1) *
sizeof(vector));
+ c = string + 1;
- vector[size - 1] = token_c;
+ add_to_vector (&vector, size, token);
size++;
-
- if(max_tokens > 0 && size >= max_tokens) {
- if(size > max_tokens) {
- break;
- }
-
- token = *strtok_save ? strtok_save : NULL;
- } else {
- token = (gchar *)strtok_r(NULL, delimiter,
&strtok_save);
- }
}
+
+ string++;
}
- if (vector == NULL){
+ if (max_tokens > 0 && size >= max_tokens) {
+ if (*string) {
+ /* Add the rest of the string as the last element */
+ add_to_vector (&vector, size, g_strdup (string));
+ size++;
+ }
+ } else {
+ if (*c) {
+ /* Fill in the trailing last token */
+ add_to_vector (&vector, size, g_strdup (c));
+ size++;
+ } else {
+ /* Need to leave a trailing empty token if the
+ * delimiter is the last part of the string
+ */
+ add_to_vector (&vector, size, g_strdup (""));
+ size++;
+ }
+ }
+
+ if (vector == NULL) {
vector = (gchar **) g_malloc (2 * sizeof (vector));
vector [0] = NULL;
- } else if (size > 0){
+ } else if (size > 0) {
vector[size - 1] = NULL;
}
- g_free(string_c);
- string_c = NULL;
-
return vector;
}
Modified: trunk/mono/eglib/test/slist.c
===================================================================
--- trunk/mono/eglib/test/slist.c 2007-11-20 17:18:55 UTC (rev 89994)
+++ trunk/mono/eglib/test/slist.c 2007-11-20 17:20:34 UTC (rev 89995)
@@ -3,7 +3,67 @@
#include <glib.h>
#include "test.h"
+
RESULT
+test_slist_nth ()
+{
+ char *foo = "foo";
+ char *bar = "bar";
+ char *baz = "baz";
+ GSList *nth, *list;
+ list = g_slist_prepend (NULL, baz);
+ list = g_slist_prepend (list, bar);
+ list = g_slist_prepend (list, foo);
+
+ nth = g_slist_nth (list, 0);
+ if (nth->data != foo)
+ return FAILED ("nth failed. #0");
+
+ nth = g_slist_nth (list, 1);
+ if (nth->data != bar)
+ return FAILED ("nth failed. #1");
+
+ nth = g_slist_nth (list, 2);
+ if (nth->data != baz)
+ return FAILED ("nth failed. #2");
+
+ nth = g_slist_nth (list, 3);
+ if (nth)
+ return FAILED ("nth failed. #3: %s", nth->data);
+
+ g_slist_free (list);
+ return OK;
+}
+
+RESULT
+test_slist_index ()
+{
+ int i;
+ char *foo = "foo";
+ char *bar = "bar";
+ char *baz = "baz";
+ GSList *list;
+ list = g_slist_prepend (NULL, baz);
+ list = g_slist_prepend (list, bar);
+ list = g_slist_prepend (list, foo);
+
+ i = g_slist_index (list, foo);
+ if (i != 0)
+ return FAILED ("index failed. #0: %d", i);
+
+ i = g_slist_index (list, bar);
+ if (i != 1)
+ return FAILED ("index failed. #1: %d", i);
+
+ i = g_slist_index (list, baz);
+ if (i != 2)
+ return FAILED ("index failed. #2: %d", i);
+
+ g_slist_free (list);
+ return OK;
+}
+
+RESULT
test_slist_append ()
{
GSList *foo;
@@ -239,6 +299,8 @@
}
static Test slist_tests [] = {
+ {"nth", test_slist_nth},
+ {"index", test_slist_index},
{"append", test_slist_append},
{"concat", test_slist_concat},
{"find", test_slist_find},
Modified: trunk/mono/eglib/test/string-util.c
===================================================================
--- trunk/mono/eglib/test/string-util.c 2007-11-20 17:18:55 UTC (rev 89994)
+++ trunk/mono/eglib/test/string-util.c 2007-11-20 17:20:34 UTC (rev 89995)
@@ -90,10 +90,210 @@
g_strfreev (v);
+ v = g_strsplit ("abcXYdefXghiXYjklYmno", "XY", 4);
+ if (strcmp (v [0], "abc") != 0)
+ return FAILED ("Invalid value 0");
+
+ if (strcmp (v [1], "defXghi") != 0)
+ return FAILED ("Invalid value 1");
+
+ if (strcmp (v [2], "jklYmno") != 0)
+ return FAILED ("Invalid value 2");
+
+ if (v [3] != NULL)
+ return FAILED ("Expected only 3 elements (1)");
+
+ g_strfreev (v);
+
+ v = g_strsplit ("abcXYdefXghiXYjklYmno", "XY", 2);
+ if (strcmp (v [0], "abc") != 0)
+ return FAILED ("Invalid value 3");
+
+ if (strcmp (v [1], "defXghiXYjklYmno") != 0)
+ return FAILED ("Invalid value 4");
+
+ if (v [2] != NULL)
+ return FAILED ("Expected only 2 elements (2)");
+
+ g_strfreev (v);
+
+ v = g_strsplit ("abcXYdefXghiXYjklYmnoXY", "XY", 3);
+ if (strcmp (v [0], "abc") != 0)
+ return FAILED ("Invalid value 5");
+
+ if (strcmp (v [1], "defXghi") != 0)
+ return FAILED ("Invalid value 6");
+
+ if (strcmp (v [2], "jklYmnoXY") != 0)
+ return FAILED ("Invalid value 7");
+
+ if (v [3] != NULL)
+ return FAILED ("Expected only 3 elements (3)");
+
+ g_strfreev (v);
+
+ v = g_strsplit ("abcXYXYXYdefXY", "XY", -1);
+ if (strcmp (v [0], "abc") != 0)
+ return FAILED ("Invalid value 8");
+
+ if (strcmp (v [1], "") != 0)
+ return FAILED ("Invalid value 9");
+
+ if (strcmp (v [2], "") != 0)
+ return FAILED ("Invalid value 10");
+
+ if (strcmp (v [3], "def") != 0)
+ return FAILED ("Invalid value 11");
+
+ if (strcmp (v [4], "") != 0)
+ return FAILED ("Invalid value 12");
+
+ if (v [5] != NULL)
+ return FAILED ("Expected only 5 elements (4)");
+
+ g_strfreev (v);
+
+ v = g_strsplit ("XYXYXYabcXYdef", "XY", -1);
+ if (strcmp (v [0], "") != 0)
+ return FAILED ("Invalid value 13");
+
+ if (strcmp (v [1], "") != 0)
+ return FAILED ("Invalid value 14");
+
+ if (strcmp (v [2], "") != 0)
+ return FAILED ("Invalid value 15");
+
+ if (strcmp (v [3], "abc") != 0)
+ return FAILED ("Invalid value 16");
+
+ if (strcmp (v [4], "def") != 0)
+ return FAILED ("Invalid value 17");
+
+ if (v [5] != NULL)
+ return FAILED ("Expected only 5 elements (5)");
+
+ g_strfreev (v);
+
return OK;
}
RESULT
+test_split_set ()
+{
+ gchar **v;
+
+ v = g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 6);
+ if (strcmp (v [0], "abc") != 0)
+ return FAILED ("Invalid value 0");
+
+ if (strcmp (v [1], "") != 0)
+ return FAILED ("Invalid value 1");
+
+ if (strcmp (v [2], "def") != 0)
+ return FAILED ("Invalid value 2");
+
+ if (strcmp (v [3], "ghi") != 0)
+ return FAILED ("Invalid value 3");
+
+ if (strcmp (v [4], "") != 0)
+ return FAILED ("Invalid value 4");
+
+ if (strcmp (v [5], "jklYmno") != 0)
+ return FAILED ("Invalid value 5");
+
+ if (v [6] != NULL)
+ return FAILED ("Expected only 6 elements (1)");
+
+ g_strfreev (v);
+
+ v = g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 3);
+ if (strcmp (v [0], "abc") != 0)
+ return FAILED ("Invalid value 6");
+
+ if (strcmp (v [1], "") != 0)
+ return FAILED ("Invalid value 7");
+
+ if (strcmp (v [2], "defXghiXYjklYmno") != 0)
+ return FAILED ("Invalid value 8");
+
+ if (v [3] != NULL)
+ return FAILED ("Expected only 3 elements (2)");
+
+ g_strfreev (v);
+
+ v = g_strsplit_set ("abcXdefYghiXjklYmnoX", "XY", 5);
+ if (strcmp (v [0], "abc") != 0)
+ return FAILED ("Invalid value 9");
+
+ if (strcmp (v [1], "def") != 0)
+ return FAILED ("Invalid value 10");
+
+ if (strcmp (v [2], "ghi") != 0)
+ return FAILED ("Invalid value 11");
+
+ if (strcmp (v [3], "jkl") != 0)
+ return FAILED ("Invalid value 12");
+
+ if (strcmp (v [4], "mnoX") != 0)
+ return FAILED ("Invalid value 13");
+
+ if (v [5] != NULL)
+ return FAILED ("Expected only 5 elements (5)");
+
+ g_strfreev (v);
+
+ v = g_strsplit_set ("abcXYXdefXY", "XY", -1);
+ if (strcmp (v [0], "abc") != 0)
+ return FAILED ("Invalid value 14");
+
+ if (strcmp (v [1], "") != 0)
+ return FAILED ("Invalid value 15");
+
+ if (strcmp (v [2], "") != 0)
+ return FAILED ("Invalid value 16");
+
+ if (strcmp (v [3], "def") != 0)
+ return FAILED ("Invalid value 17");
+
+ if (strcmp (v [4], "") != 0)
+ return FAILED ("Invalid value 18");
+
+ if (strcmp (v [5], "") != 0)
+ return FAILED ("Invalid value 19");
+
+ if (v [6] != NULL)
+ return FAILED ("Expected only 6 elements (4)");
+
+ g_strfreev (v);
+
+ v = g_strsplit_set ("XYXabcXYdef", "XY", -1);
+ if (strcmp (v [0], "") != 0)
+ return FAILED ("Invalid value 20");
+
+ if (strcmp (v [1], "") != 0)
+ return FAILED ("Invalid value 21");
+
+ if (strcmp (v [2], "") != 0)
+ return FAILED ("Invalid value 22");
+
+ if (strcmp (v [3], "abc") != 0)
+ return FAILED ("Invalid value 23");
+
+ if (strcmp (v [4], "") != 0)
+ return FAILED ("Invalid value 24");
+
+ if (strcmp (v [5], "def") != 0)
+ return FAILED ("Invalid value 25");
+
+ if (v [6] != NULL)
+ return FAILED ("Expected only 6 elements (5)");
+
+ g_strfreev (v);
+
+ return OK;
+}
+
+RESULT
test_strreverse ()
{
gchar *a = g_strdup ("onetwothree");
@@ -425,6 +625,7 @@
{"g_strfreev", test_strfreev},
{"g_strconcat", test_concat},
{"g_strsplit", test_split},
+ {"g_strsplit_set", test_split_set},
{"g_strreverse", test_strreverse},
{"g_strjoin", test_strjoin},
{"g_strchug", test_strchug},
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches