diff options
author | Stuart Sierra <mail@stuartsierra.com> | 2009-08-19 14:04:02 -0400 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2009-08-19 14:04:02 -0400 |
commit | aae84a8b7e47622a4ca324a2828757996b592ea4 (patch) | |
tree | 459f1c2a2582f06d361df73100e7ce0dc755694f | |
parent | fda5d6a8ad310624f9efb487a2c9ed8616557ef3 (diff) |
str_utils2.clj: added repeat and reverse, with tests
-rw-r--r-- | src/clojure/contrib/str_utils2.clj | 13 | ||||
-rw-r--r-- | src/clojure/contrib/test_contrib/str_utils2.clj | 6 |
2 files changed, 18 insertions, 1 deletions
diff --git a/src/clojure/contrib/str_utils2.clj b/src/clojure/contrib/str_utils2.clj index 8529d2c4..1fbd030d 100644 --- a/src/clojure/contrib/str_utils2.clj +++ b/src/clojure/contrib/str_utils2.clj @@ -29,7 +29,8 @@ Some ideas are borrowed from http://github.com/francoisdevlin/devlinsf-clojure-utils/"} clojure.contrib.str-utils2 - (:refer-clojure :exclude (take replace drop butlast partition contains? get)) + (:refer-clojure :exclude (take replace drop butlast partition + contains? get repeat reverse)) (:import (java.util.regex Pattern))) @@ -141,6 +142,16 @@ s (.substring s (- (count s) n)))) +(defn repeat + "Returns a new String containing s repeated n times." + [#^String s n] + (apply str (clojure.core/repeat n s))) + +(defn reverse + "Returns s with its characters reversed." + [#^String s] + (.toString (.reverse (StringBuilder. s)))) + (defmulti #^{:doc "Replaces all instances of pattern in string with replacement. diff --git a/src/clojure/contrib/test_contrib/str_utils2.clj b/src/clojure/contrib/test_contrib/str_utils2.clj index ecc68e19..6d1f94d2 100644 --- a/src/clojure/contrib/test_contrib/str_utils2.clj +++ b/src/clojure/contrib/test_contrib/str_utils2.clj @@ -41,6 +41,12 @@ (is (= "foobar" (s/tail "foobar" 9))) (is (= "" (s/tail "foobar" 0)))) +(deftest t-repeat + (is (= "foofoofoo" (s/repeat "foo" 3)))) + +(deftest t-reverse + (is (= "tab" (s/reverse "bat")))) + (deftest t-replace (is (= "faabar" (s/replace "foobar" \o \a))) (is (= "barbarbar" (s/replace "foobarfoo" "foo" "bar"))) |