]> granicus.if.org Git - jq/commitdiff
Split on empty sep: fix #552 moar
authorNicolas Williams <nico@cryptonector.com>
Wed, 14 Jan 2015 00:56:53 +0000 (18:56 -0600)
committerNicolas Williams <nico@cryptonector.com>
Wed, 14 Jan 2015 07:32:11 +0000 (01:32 -0600)
jv.c
tests/all.test

diff --git a/jv.c b/jv.c
index 85830f4e970deb2f0caa184ca899668e906cd0fa..22e2eb626133bb993feb3bf12ab8ef0c1d79c6bf 100644 (file)
--- a/jv.c
+++ b/jv.c
@@ -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);
index 814d1e37246f723aa13b928c9c3df3184cdb29b7..e412c6267ada69584443dc8b444bbcec44d9d617 100644 (file)
@@ -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, "]