summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Liebke and Stuart Halloway <pair@clojure.com>2010-05-28 13:35:41 -0400
committerStuart Halloway <stu@thinkrelevance.com>2010-06-03 21:20:58 -0400
commit8efeb01d4c617283a8710481f4ac41af8b64408e (patch)
tree81c81529b775de4bf03c6614330b615942c417bf
parent8c0022f29cc3ddc6cfb8b1cacd8839425620b18d (diff)
collapse the "all" replace-* fns, align arg order with clojure convention
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r--src/clj/clojure/string.clj38
-rw-r--r--test/clojure/test_clojure/string.clj6
2 files changed, 22 insertions, 22 deletions
diff --git a/src/clj/clojure/string.clj b/src/clj/clojure/string.clj
index 951f94a1..d150385f 100644
--- a/src/clj/clojure/string.clj
+++ b/src/clj/clojure/string.clj
@@ -17,25 +17,8 @@
[^String s]
(.toString (.reverse (StringBuilder. s))))
-(defn replace-str
- "Replaces all instances of substring a with b in s."
- [^String a ^String b ^String s]
- (.replace s a b))
-
-(defn replace-char
- "Replaces all instances of character a with character b in s."
- [^Character a ^Character b ^String s]
- (.replace s a b))
-
-(defn replace-re
- "Replaces all matches of re with replacement in s."
- [re replacement ^String s]
- (.replaceAll (re-matcher re s) replacement))
-
-(defn replace-by
- "Replaces all matches of re in s with the result of
- (f (re-groups the-match))."
- [re f ^String s]
+(defn- replace-by
+ [^String s re f]
(let [m (re-matcher re s)]
(let [buffer (StringBuffer. (.length s))]
(loop []
@@ -45,6 +28,23 @@
(do (.appendTail m buffer)
(.toString buffer)))))))
+(defn replace
+ "Replaces all instance of match with replacement in s.
+
+ match/replacement can be:
+
+ string / string
+ char / char
+ pattern / (string or function of match)"
+ [^String s match replacement]
+ (cond
+ (instance? Character match) (.replace s ^Character match ^Character replacement)
+ (instance? String match) (.replace s ^String match ^String replacement)
+ (instance? Pattern match) (if (string? replacement)
+ (.replaceAll (re-matcher ^Pattern match s) ^String replacement)
+ (replace-by s match replacement))
+ :default (throw (IllegalArgumentException. (str "Invalid match arg: " match)))))
+
(defn replace-first-str
"Replace first occurance of substring a with b in s."
[^String a ^String b ^String s]
diff --git a/test/clojure/test_clojure/string.clj b/test/clojure/test_clojure/string.clj
index ce557bbc..f4e6e0b8 100644
--- a/test/clojure/test_clojure/string.clj
+++ b/test/clojure/test_clojure/string.clj
@@ -6,9 +6,9 @@
(is (= "tab" (s/reverse "bat"))))
(deftest t-replace
- (is (= "faabar" (s/replace-char \o \a "foobar")))
- (is (= "barbarbar" (s/replace-str "foo" "bar" "foobarfoo")))
- (is (= "FOObarFOO" (s/replace-by #"foo" s/upper-case "foobarfoo"))))
+ (is (= "faabar" (s/replace "foobar" \o \a)))
+ (is (= "barbarbar" (s/replace "foobarfoo" "foo" "bar")))
+ (is (= "FOObarFOO" (s/replace "foobarfoo" #"foo" s/upper-case))))
(deftest t-replace-first
(is (= "barbarfoo" (s/replace-first-re #"foo" "bar" "foobarfoo")))