Browse files

re-groups working; changed re-matcher interface to be more Pythonic a…

…nd return nil if no match; updated unit tests
  • Loading branch information...
1 parent f5ec46a commit fb7231abfbdf962a668914e013862ab24c5e78c4 @adamfeuer committed Apr 8, 2012
Showing with 24 additions and 16 deletions.
  1. +16 −9 clojure/core.clj
  2. +8 −7 tests/re-tests.clj
View
25 clojure/core.clj
@@ -3438,16 +3438,25 @@
s
(re/compile s)))
+(comment
(defn re-matcher
"Returns a Python MatchObject, for use, e.g. in
- re-find. If no match, returns the string itself."
+ re-find. If no match, returns a function that returns the string itself."
{:added "1.0"
:static true}
[^PatternType re s]
(let [result (.search re s)]
(if (nil? result)
(fn [] s)
result)))
+)
+(defn re-matcher
+ "Returns a Python MatchObject, for use, e.g. in
+ re-find. If no match, returns a function that returns the string itself."
+ {:added "1.0"
+ :static true}
+ [^PatternType re s]
+ (.search re s))
(def MatchObjectType (py/type (.match (re-pattern "^.*$") "foo")))
(defn re-groups
@@ -3458,14 +3467,12 @@
{:added "1.0"
:static true}
[^MatchObjectType m]
- (if (fn? m)
- (m)
+ (if (nil? m)
+ []
(let [gc (count (.groups m))]
- (if (zero? gc)
- (.group m 0)
- (loop [ret [] c 0]
- (if (<= c gc)
- (recur (conj ret (.group m c)) (inc c))
- ret))))))
+ (loop [ret [] c 0]
+ (if (<= c gc)
+ (recur (conj ret (.group m c)) (inc c))
+ ret)))))
View
15 tests/re-tests.clj
@@ -11,23 +11,24 @@
(assertions/assert-true (instance? PatternType (re-pattern "^(.*)(foo)(bar)+$"))))
(deftest re-matcher-tests
- (def MatchObjectType (py/type (.match (re-pattern "^.*$") "foo")))
+ (def MatchObjectType (py/type (.search (re-pattern "^.*$") "foo")))
(def testPattern "^(foo).(bar)$")
(def inputString "foo-bar")
(def get-matcher (re-matcher (re-pattern testPattern) inputString))
- (assertions/assert-not-nil (re-matcher (re-pattern "foo") ""))
+ (assertions/assert-nil (re-matcher (re-pattern "foo") ""))
+ (assertions/assert-equal MatchObjectType (py/type (re-matcher (re-pattern "^.*$") "foo")))
(assertions/assert-true (instance? MatchObjectType (re-matcher (re-pattern "^.*$") "foo")))
(assertions/assert-true (instance? MatchObjectType get-matcher))
(assertions/assert-equal "foo-bar" (.group get-matcher 0))
(assertions/assert-equal "foo" (.group get-matcher 1))
(assertions/assert-equal "bar" (.group get-matcher 2)))
(deftest re-groups-tests
- (assertions/assert-equal "" (re-groups (re-matcher (re-pattern "") "")))
- (assertions/assert-equal "" (re-groups (re-matcher (re-pattern "") "foo")))
- (assertions/assert-equal "" (re-groups (re-matcher (re-pattern "foo") "")))
- (assertions/assert-equal "bar" (re-groups (re-matcher (re-pattern "foo") "bar")))
- (assertions/assert-equal "foo" (re-groups (re-matcher (re-pattern "foo") "foo")))
+ (assertions/assert-equal [] [])
+ (assertions/assert-equal [""] (re-groups (re-matcher (re-pattern "") "")))
+ (assertions/assert-equal [""] (re-groups (re-matcher (re-pattern "") "foo")))
+ (assertions/assert-equal [] (re-groups (re-matcher (re-pattern "foo") "")))
+ (assertions/assert-equal ["foo"] (re-groups (re-matcher (re-pattern "foo") "foo")))
(assertions/assert-equal ["foo" "foo"] (re-groups (re-matcher (re-pattern "(foo)") "foo")))
(assertions/assert-equal ["foobar" "foo" "bar"] (re-groups (re-matcher (re-pattern "(foo)(bar)") "foobar")))
(assertions/assert-equal ["foobbbbbarrrbbbaaazzz" "foo" "bbbbbarrr" "bbbaaazzz"] (re-groups (re-matcher (re-pattern "(fo+)(b+.r*)(b*a*z*)") "foobbbbbarrrbbbaaazzz")))

0 comments on commit fb7231a

Please sign in to comment.