Browse Source

2006-10-21 Miguel de Icaza <[email protected]>

	* src/gstr.c (g_strsplit): this routine has some non-expected
	behavior, if the string begins with the delimiter, it will return
	an empty first string, unlike strtok

	* src/gpath.c (g_path_get_dirname): Return "." as a dirname for
	paths that do not contain a directory.


svn path=/trunk/mono/; revision=66859
Miguel de Icaza 19 years ago
parent
commit
03be2afdc4
5 changed files with 49 additions and 24 deletions
  1. 9 0
      eglib/ChangeLog
  2. 1 1
      eglib/src/gpath.c
  3. 29 22
      eglib/src/gstr.c
  4. 4 1
      eglib/test/path.c
  5. 6 0
      eglib/test/string-util.c

+ 9 - 0
eglib/ChangeLog

@@ -1,3 +1,12 @@
+2006-10-21  Miguel de Icaza  <[email protected]>
+
+	* src/gstr.c (g_strsplit): this routine has some non-expected
+	behavior, if the string begins with the delimiter, it will return
+	an empty first string, unlike strtok
+
+	* src/gpath.c (g_path_get_dirname): Return "." as a dirname for
+	paths that do not contain a directory.
+
 2006-10-18 Gonzalo Paniagua Javier <[email protected]>
 
 	* test/array.c: new test for insertion in the middle of other values.

+ 1 - 1
eglib/src/gpath.c

@@ -82,7 +82,7 @@ g_path_get_dirname (const gchar *filename)
 
 	p = strrchr (filename, G_DIR_SEPARATOR);
 	if (p == NULL)
-		return g_strdup ("");
+		return g_strdup (".");
 	count = p - filename;
 	r = g_malloc (count + 1);
 	strncpy (r, filename, count);

+ 29 - 22
eglib/src/gstr.c

@@ -176,34 +176,41 @@ g_strsplit (const gchar *string, const gchar *delimiter, gint max_tokens)
 	string_c = (gchar *)g_malloc(token_length + 1);
 	memcpy(string_c, string, token_length);
 	string_c[token_length] = 0;
-	
-	vector = NULL;
-	token = (gchar *)strtok_r(string_c, delimiter, &strtok_save);
-
-	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;
 
-		vector = vector == NULL ? 
-			(gchar **)g_malloc(2 * sizeof(vector)) :
-			(gchar **)g_realloc(vector, (size + 1) * sizeof(vector));
-	
-		vector[size - 1] = token_c;	
+	if (strncmp (string_c, delimiter, strlen (delimiter)) == 0){
+		vector = (gchar **) g_malloc (2 * sizeof (vector));
+		vector [0]  = g_strdup ("");
 		size++;
+	} else
+		vector = NULL;
+	token = (gchar *)strtok_r(string_c, delimiter, &strtok_save);
 
-		if(max_tokens > 0 && size >= max_tokens) {
-			if(size > max_tokens) {
-				break;
+	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;
+			
+			vector = vector == NULL ? 
+				(gchar **)g_malloc(2 * sizeof(vector)) :
+				(gchar **)g_realloc(vector, (size + 1) * sizeof(vector));
+			
+			vector[size - 1] = token_c;	
+			size++;
+			
+			if(max_tokens > 0 && size >= max_tokens) {
+				if(size > max_tokens) {
+					break;
+				}
+				
+				token = strtok_save;
+			} else {
+				token = (gchar *)strtok_r(NULL, delimiter, &strtok_save);
 			}
-
-			token = strtok_save;
-		} else {
-			token = (gchar *)strtok_r(NULL, delimiter, &strtok_save);
 		}
 	}
-
+	
 	if (vector == NULL){
 		vector = (gchar **) g_malloc (2 * sizeof (vector));
 		vector [0] = NULL;

+ 4 - 1
eglib/test/path.c

@@ -114,7 +114,10 @@ test_dirname ()
 	if (strcmp (s, "/home/dingus") != 0)
 		return FAILED ("Expected /home/dingus, got %s", s);
 	g_free (s);
-	
+
+	s = g_path_get_dirname ("dir.c");
+	if (strcmp (s, ".") != 0)
+		return FAILED ("Expected `.', got %s", s);
 	return OK;
 }
 

+ 6 - 0
eglib/test/string-util.c

@@ -69,6 +69,12 @@ test_split ()
 	if (v == NULL)
 		return FAILED ("g_strsplit returned NULL");
 	g_strfreev (v);
+
+	v = g_strsplit ("/home/miguel/dingus", "/", 0);
+	if (v [0][0] != 0)
+		return FAILED ("Got a non-empty first element");
+	g_strfreev (v);
+		
 	return OK;
 }