Skip to content

Commit

Permalink
Two sub bugs fixed; two new gsub tests have been added.
Browse files Browse the repository at this point in the history
./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)
  • Loading branch information
pkoppstein authored and nicowilliams committed Dec 9, 2015
1 parent ec583de commit ad8d1a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,21 @@ 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
| [match($re; fla)]
| 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
Expand Down
8 changes: 8 additions & 0 deletions tests/onig.test
Original file line number Diff line number Diff line change
Expand Up @@ -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, "]
[", ",", ",", ",", ",", ",", ",", ",", "]
Expand Down

0 comments on commit ad8d1a8

Please sign in to comment.