]> granicus.if.org Git - jq/commitdiff
Two sub bugs fixed; two new gsub tests have been added.
authorpkoppstein <pkoppstein@gmail.com>
Mon, 7 Dec 2015 23:51:26 +0000 (18:51 -0500)
committerNicolas Williams <nico@cryptonector.com>
Wed, 9 Dec 2015 21:28:01 +0000 (15:28 -0600)
./jq --run-tests < tests/onig.test
has been run successfully:

...
Testing 'gsub("a";"b")' at line number 70
Testing 'gsub( "(.*)"; "";  "x")' at line number 74
...
22 of 22 tests passed (0 malformed)

src/builtin.jq
tests/onig.test

index 4add6813000c986b8a8bbc2333d2af94db6f50ec..53998a859c9455a639707c95e41592334b51a30e 100644 (file)
@@ -125,8 +125,8 @@ def sub($re; s):
 #
 # If s contains capture variables, then create a capture object and pipe it to s
 def sub($re; s; flags):
-  def subg: explode | select(. != 103) | implode;
-# "fla" should be flags with all occurrences of g removed; gs should be non-nil if flags has a g
+  def subg: [explode[] | select(. != 103)] | implode;
+  # "fla" should be flags with all occurrences of g removed; gs should be non-nil if flags has a g
   def sub1(fla; gs):
     def mysub:
       . as $in
@@ -134,12 +134,12 @@ def sub($re; s; flags):
       | if length == 0 then $in
         else .[0] as $edit
         | ($edit | .offset + .length) as $len
-#  # create the "capture" object:
+        # create the "capture" object:
         | reduce ( $edit | .captures | .[] | select(.name != null) | { (.name) : .string } ) as $pair
             ({}; . + $pair)
         | $in[0:$edit.offset]
           + s
-          + ($in[$len:] | if gs then mysub else . end)
+          + ($in[$len:] | if length > 0 and gs then mysub else . end)
         end ;
     mysub ;
     (flags | index("g")) as $gs
index 763bf0b2f2213c9b9bb17353b1aceb93ed3927bc..daacae9cd7098b457cf619ce6c36c1cc11c6b71f 100644 (file)
@@ -67,6 +67,14 @@ gsub("(?<d>\\d)"; ":\(.d);")
 "a1b2"
 "a:1;b:2;"
 
+gsub("a";"b")
+"aaaaa"
+"bbbbb"
+
+gsub( "(.*)"; "";  "x")
+""
+""
+
 [.[] | scan(", ")]
 ["a,b, c, d, e,f",", a,b, c, d, e,f, "]
 [", ",", ",", ",", ",", ",", ",", ",", "]