Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
None
-
None
-
MacOS X
Description
The closure which passed to String#replaceAll() takes arguments in two patterns:
1) Object[]
2) Arguments correspond to matcher's group(0), group(1), group(2) ...
but the closure for Pattern#match(Closure) and String#eachMatch(Closure) takes:
1) List
2) Arguments correspond to matcher's group(0), group(1), group(2) ...
So there is an asymmetry about 1).
If this issue should be fixed, IMHO, to change replaceAll() to take a list like
match/eachMatch is better. Because you can write simply:
assert "ABDE" == "abcdef".replaceAll(/(..)(.)/) { it[1].toUpperCase() } // (A)
rather than:
assert "ABDE" == "abcdef".replaceAll(/(..)(.)/) { Object[] it -> it[1].toUpperCase() } //(B)
(A) is same as:
assert "ABDE" == "abcdef".replaceAll(/(..)(.)/) { List it -> it[1].toUpperCase() } // (C)
I attach small patch for 1.8-trunk and some test cases for this.
This patch leads small backward-incompatibility so that (B) would fail because
of the difference of parameter type of the closure. But current test cases
don't include this.