Skip to content

Commit

Permalink
translated re-seq; added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfeuer committed Apr 8, 2012
1 parent 926af13 commit 1c346a8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
20 changes: 7 additions & 13 deletions clojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3429,14 +3429,16 @@

(require 're)
(defn re-pattern
"Returns a compiled Python Pattern object, for use, e.g. in
"Accepts a compiled regex or a string containing a regex pattern.
Returns a compiled Python Pattern object, for use, e.g. in
re-matcher."
{:added "1.0"
:static true}
[s] (re/compile s))

(defn re-matcher
"Returns a Python MatchObject, for use, e.g. in
"Accepts a compiled regex or a string containing a regex pattern.
Returns a Python MatchObject, for use, e.g. in
re-find. If no match, returns nil."
{:added "1.0"
:static true}
Expand All @@ -3460,17 +3462,9 @@
ret)))))

(defn re-seq
"Returns a lazy sequence of successive matches of pattern in string,
using re.finditer and re.MatchObject
re-groups."
"Returns a sequence of successive matches of pattern in string,
using re.findall()."
{:added "1.0"
:static true}
[^PatternType re s]
(let [m (re-matcher re s)
i (.finditer re s)]
((fn step []
(let [item (.next i)]
(when (.has_next i))
(cons (re-groups item) (lazy-seq (step))))))))

[re s] (re/findall re s))

13 changes: 7 additions & 6 deletions tests/re-tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
(def PatternType (py/type (re/compile "")))
(assertions/assert-equal PatternType (py/type (re-pattern "")))
(assertions/assert-equal PatternType (py/type (re-pattern "foo")))
(assertions/assert-true (instance? PatternType (re-pattern "foo")))
(assertions/assert-true (instance? PatternType (re-pattern "^(.*)(foo)(bar)+$"))))
(assertions/assert-true (instance? PatternType (re-pattern "foo")))
(assertions/assert-true (instance? PatternType (re-pattern (re-pattern "foo"))))
(assertions/assert-true (instance? PatternType (re-pattern "^(.*)(foo)(bar)+$"))))

(deftest re-matcher-tests
(def MatchObjectType (py/type (.search (re-pattern "^.*$") "foo")))
Expand All @@ -23,8 +24,7 @@
(assertions/assert-equal "foo" (.group get-matcher 1))
(assertions/assert-equal "bar" (.group get-matcher 2))
(assertions/assert-not-nil (re-matcher "foo" "foobar"))
(assertions/assert-equal "foo" (.group get-matcher 1))
)
(assertions/assert-equal "foo" (.group get-matcher 1)))

(deftest re-groups-tests
(assertions/assert-equal [""] (re-groups (re-matcher (re-pattern "") "")))
Expand All @@ -37,6 +37,7 @@
(assertions/assert-equal ["foobarbaz" "foo" "barbaz" "baz"] (re-groups (re-matcher (re-pattern "(foo)(bar(baz))") "foobarbaz"))))

(deftest re-seq-tests
;(assertions/assert-equal ["123" "1" "2" "3"] (first (re-seq (re-pattern "(1)(2)(3)") "123")))
;(assertions/assert-equal 1 (count (re-seq (re-pattern "(1)(2)(3)") "123")))
(assertions/assert-equal ["1" "1" "1"] (re-seq (re-pattern "(1)") "111"))
(assertions/assert-equal [["111" "222"]] (re-seq (re-pattern "(1+)(2+)") "111222"))
(assertions/assert-equal ["121" "343" "565"] (re-seq (re-pattern "(\\d+)") "121baz343foo-565bar"))
)

0 comments on commit 1c346a8

Please sign in to comment.