Skip to content

Commit e0ac2d7

Browse files
puredangerstuarthalloway
authored andcommitted
CLJ-2585 - Fix index check in nth with not-found for regex matcher
Signed-off-by: Stuart Halloway <stu@cognitect.com>
1 parent fed73d7 commit e0ac2d7

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/jvm/clojure/lang/RT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,8 @@ else if(coll instanceof RandomAccess) {
965965
}
966966
else if(coll instanceof Matcher) {
967967
Matcher m = (Matcher) coll;
968-
if(n < m.groupCount())
968+
int groups = m.groupCount();
969+
if(groups > 0 && n <= m.groupCount())
969970
return m.group(n);
970971
return notFound;
971972
}

test/clojure/test_clojure/other_functions.clj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,19 @@
348348
; Regex Support
349349
; re-matcher re-find re-matches re-groups re-seq
350350

351+
(deftest test-regex-matcher
352+
(let [matcher (re-matcher #"(\d{2})/(\d{2})/(\d{4})" "12/02/1975")]
353+
(is (= ["12/02/1975" "12" "02" "1975"] (re-find matcher)))
354+
(is (= ["12/02/1975" "12" "02" "1975"] (re-groups matcher)))
355+
(is (= "12/02/1975" (nth matcher 0) (nth matcher 0 :foo)))
356+
(is (= "12" (nth matcher 1) (nth matcher 1 :foo)))
357+
(is (= "02" (nth matcher 2) (nth matcher 2 :foo)))
358+
(is (= "1975" (nth matcher 3) (nth matcher 3 :foo)))
359+
(is (thrown? IndexOutOfBoundsException (nth matcher -1)))
360+
(is (= :foo (nth matcher -1 :foo)))
361+
(is (thrown? IndexOutOfBoundsException (nth matcher 4)))
362+
(is (= :foo (nth matcher 4 :foo)))))
363+
351364
; update
352365

353366
(deftest test-update

0 commit comments

Comments
 (0)