Suppose you want to split a `name=value` string:
char** kvp = g_strsplit ("name=value", "=", 2);
// kvp[0] is "name", kvp[1] is "value"
That works, but what if the string is "malformed" and missses a value?
char** kvp = g_strsplit ("name=", "=", 2);
// kvp[0] is "name", kvp[1] is "="
A value of "=" makes no sense here: values returned from
g_strsplit() shouldn't contain delimeter unless max_count is specified
and it's _within_ the last string; it's also not what GLib does.
What makes sense is a value of "" (the empty string), which IS what
GLib does (so it's compatible and sensible).
Fix g_strsplit() to use the empty string when the "rest" value would
otherwise be the delimiter.
}
if (*string) {
- /* Add the rest of the string as the last element */
- add_to_vector (&vector, size, g_strdup (string));
+ if (strcmp (string, delimiter) == 0)
+ add_to_vector (&vector, size, g_strdup (""));
+ else {
+ /* Add the rest of the string as the last element */
+ add_to_vector (&vector, size, g_strdup (string));
+ }
size++;
}
g_strfreev (v);
+ v = g_strsplit ("value=", "=", 2);
+ if (strcmp (v [0], "value") != 0)
+ return FAILED ("Invalid value 18; expected 'value', got '%s'", v [0]);
+ if (strcmp (v [1], "") != 0)
+ return FAILED ("Invalid value 19; expected '', got '%s'", v [1]);
+ if (v [2] != NULL)
+ return FAILED ("Expected only 2 elements (6)");
+
+ g_strfreev (v);
+
return OK;
}