summaryrefslogtreecommitdiff
path: root/src
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 /src
parent8efeb01d4c617283a8710481f4ac41af8b64408e (diff)
collapse the replace-first-* fns, align arg order with clojure convention
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
Diffstat (limited to 'src')
-rw-r--r--src/clj/clojure/string.clj27
1 files changed, 15 insertions, 12 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."