From 60d1ecb58a7987ab1ba6e8fe6ff4991c6b2ac420 Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Mon, 12 Jan 2015 10:44:44 -0600 Subject: [PATCH] Fix #552 --- jv.c | 11 +++++++---- tests/all.test | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/jv.c b/jv.c index cdd01630..85830f4e 100644 --- a/jv.c +++ b/jv.c @@ -648,19 +648,22 @@ jv jv_string_split(jv j, jv sep) { assert(jv_get_kind(j) == JV_KIND_STRING); assert(jv_get_kind(sep) == JV_KIND_STRING); const char *jstr = jv_string_value(j); + const char *jend = jstr + jv_string_length_bytes(jv_copy(j)); const char *sepstr = jv_string_value(sep); const char *p, *s; - int jlen = jv_string_length_bytes(jv_copy(j)); int seplen = jv_string_length_bytes(jv_copy(sep)); jv a = jv_array(); assert(jv_get_refcnt(a) == 1); - for (p = jstr; p < jstr + jlen; p = s + seplen) { - s = _jq_memmem(p, (jstr + jlen) - p, sepstr, seplen); + for (p = jstr; p < jend; p = s + seplen) { + s = _jq_memmem(p, jend - p, sepstr, seplen); if (s == NULL) - s = jstr + jlen; + 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("")); } jv_free(j); jv_free(sep); diff --git a/tests/all.test b/tests/all.test index cdb34d63..814d1e37 100644 --- a/tests/all.test +++ b/tests/all.test @@ -898,7 +898,7 @@ sub("^(?.)"; "Head=\(.head) Tail=") [.[] | split(", ")] ["a,b, c, d, e,f",", a,b, c, d, e,f, "] -[["a,b","c","d","e,f"],["","a,b","c","d","e,f"]] +[["a,b","c","d","e,f"],["","a,b","c","d","e,f",""]] ######################## [.[]|[[sub(", *";":")], [gsub(", *";":")], [scan(", *")]]]