Skip to content

Commit

Permalink
Split on empty sep: fix jqlang#552 moar
Browse files Browse the repository at this point in the history
  • Loading branch information
nicowilliams committed Jan 14, 2015
1 parent d630597 commit 8b5ff40
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
22 changes: 14 additions & 8 deletions jv.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,14 +656,20 @@ jv jv_string_split(jv j, jv sep) {

assert(jv_get_refcnt(a) == 1);

for (p = jstr; p < jend; p = s + seplen) {
s = _jq_memmem(p, jend - p, sepstr, seplen);
if (s == NULL)
s = jend;
a = jv_array_append(a, jv_string_sized(p, s - p));
// Add an empty string to denote that j ends on a sep
if (s + seplen == jend)
a = jv_array_append(a, jv_string(""));
if (seplen == 0) {
int c;
while ((jstr = jvp_utf8_next(jstr, jend, &c)))
a = jv_array_append(a, jv_string_append_codepoint(jv_string(""), c));
} else {
for (p = jstr; p < jend; p = s + seplen) {
s = _jq_memmem(p, jend - p, sepstr, seplen);
if (s == NULL)
s = jend;
a = jv_array_append(a, jv_string_sized(p, s - p));
// Add an empty string to denote that j ends on a sep
if (s + seplen == jend && seplen != 0)
a = jv_array_append(a, jv_string(""));
}
}
jv_free(j);
jv_free(sep);
Expand Down
4 changes: 4 additions & 0 deletions tests/all.test
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,10 @@ sub("^(?<head>.)"; "Head=\(.head) Tail=")
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
[["a,b","c","d","e,f"],["","a,b","c","d","e,f",""]]

split("")
"abc"
["a","b","c"]

########################
[.[]|[[sub(", *";":")], [gsub(", *";":")], [scan(", *")]]]
["a,b, c, d, e,f",", a,b, c, d, e,f, "]
Expand Down

0 comments on commit 8b5ff40

Please sign in to comment.