summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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))