Two sub bugs fixed; two new gsub tests have been added.

./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)
This commit is contained in:
pkoppstein authored and Nicolas Williams committed 2015-12-09 15:28:01 -06:00
1 parent ec583deefa
commit ad8d1a8cc8
2 files changed
+12 -4

No files matched your search

+4 -4
View File
@@ -125,8 +125,8 @@ def sub($re; s):
# #
# If s contains capture variables, then create a capture object and pipe it to s # If s contains capture variables, then create a capture object and pipe it to s
def sub($re; s; flags): def sub($re; s; flags):
def subg: explode | select(. != 103) | implode; 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 # "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 sub1(fla; gs):
def mysub: def mysub:
. as $in . as $in
@@ -134,12 +134,12 @@ def sub($re; s; flags):
| if length == 0 then $in | if length == 0 then $in
else .[0] as $edit else .[0] as $edit
| ($edit | .offset + .length) as $len | ($edit | .offset + .length) as $len
# # create the "capture" object: # create the "capture" object:
| reduce ( $edit | .captures | .[] | select(.name != null) | { (.name) : .string } ) as $pair | reduce ( $edit | .captures | .[] | select(.name != null) | { (.name) : .string } ) as $pair
({}; . + $pair) ({}; . + $pair)
| $in[0:$edit.offset] | $in[0:$edit.offset]
+ s + s
+ ($in[$len:] | if gs then mysub else . end) + ($in[$len:] | if length > 0 and gs then mysub else . end)
end ; end ;
mysub ; mysub ;
(flags | index("g")) as $gs (flags | index("g")) as $gs
+8
View File
@@ -67,6 +67,14 @@ gsub("(?<d>\\d)"; ":\(.d);")
"a1b2" "a1b2"
"a:1;b:2;" "a:1;b:2;"
gsub("a";"b")
"aaaaa"
"bbbbb"
gsub( "(.*)"; ""; "x")
""
""
[.[] | scan(", ")] [.[] | scan(", ")]
["a,b, c, d, e,f",", a,b, c, d, e,f, "] ["a,b, c, d, e,f",", a,b, c, d, e,f, "]
[", ",", ",", ",", ",", ",", ",", ",", "] [", ",", ",", ",", ",", ",", ",", ",", "]