summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Liebke and Stuart Halloway <pair@clojure.com>2010-05-28 15:07:44 -0400
committerStuart Halloway <stu@thinkrelevance.com>2010-06-03 21:20:59 -0400
commitf4427c70bd7c711b3a9c49f0a2e5f6781be707e1 (patch)
treee873cc1a35d7d8df91113be799db8a069bd4c790
parent8efeb01d4c617283a8710481f4ac41af8b64408e (diff)
collapse the replace-first-* fns, align arg order with clojure convention
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r--src/clj/clojure/string.clj27
-rw-r--r--test/clojure/test_clojure/string.clj5
2 files changed, 18 insertions, 14 deletions
diff --git a/src/clj/clojure/string.clj b/src/clj/clojure/string.clj
index d150385f..8ad33608 100644
--- a/src/clj/clojure/string.clj
+++ b/src/clj/clojure/string.clj
@@ -45,20 +45,10 @@
(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]
- (.replaceFirst (re-matcher (Pattern/quote a) s) b))
-
-(defn replace-first-re
- "Replace first match of re in s."
- [^Pattern re ^String replacement ^String s]
- (.replaceFirst (re-matcher re s) replacement))
-
-(defn replace-first-by
+(defn- replace-first-by
"Replace first match of re in s with the result of
(f (re-groups the-match))."
- [^Pattern re f ^String s]
+ [^String s ^Pattern re f]
(let [m (re-matcher re s)]
(let [buffer (StringBuffer.)]
(if (.find m)
@@ -67,6 +57,19 @@
(.appendTail m buffer)
(str buffer))))))
+(defn replace-first
+ ""
+ [^String s match replacement]
+ (cond
+ (instance? String match)
+ (.replaceFirst s (Pattern/quote ^String match) ^String replacement)
+ (instance? Pattern match)
+ (if (string? replacement)
+ (.replaceFirst (re-matcher ^Pattern match s) ^String replacement)
+ (replace-first-by s match replacement))
+ :default (throw (IllegalArgumentException. (str "Invalid match arg: " match)))))
+
+
(defn ^String join
"Returns a string of all elements in coll, separated by
separator. Like Perl's join."
diff --git a/test/clojure/test_clojure/string.clj b/test/clojure/test_clojure/string.clj
index f4e6e0b8..6a84bebe 100644
--- a/test/clojure/test_clojure/string.clj
+++ b/test/clojure/test_clojure/string.clj
@@ -11,8 +11,9 @@
(is (= "FOObarFOO" (s/replace "foobarfoo" #"foo" s/upper-case))))
(deftest t-replace-first
- (is (= "barbarfoo" (s/replace-first-re #"foo" "bar" "foobarfoo")))
- (is (= "FOObarfoo" (s/replace-first-by #"foo" s/upper-case "foobarfoo"))))
+ (is (= "barbarfoo" (s/replace-first "foobarfoo" "foo" "bar")))
+ (is (= "barbarfoo" (s/replace-first "foobarfoo" #"foo" "bar")))
+ (is (= "FOObarfoo" (s/replace-first "foobarfoo" #"foo" s/upper-case))))
(deftest t-join
(are [x coll] (= x (s/join coll))