diff options
author | David Liebke and Stuart Halloway <pair@clojure.com> | 2010-05-28 17:07:16 -0400 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2010-06-03 21:20:59 -0400 |
commit | b7f211356c27ba099f3dbe116539dbd9efa421df (patch) | |
tree | 35ad1f87389edba92531a0fb74f03d78dcb58f76 /src | |
parent | 07fc5303c932d9d509d81d44e1a25bd244ceb7dd (diff) |
string perf tweaks, tests #359
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/clj/clojure/string.clj | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/src/clj/clojure/string.clj b/src/clj/clojure/string.clj index 47e0b02c..3583d932 100644 --- a/src/clj/clojure/string.clj +++ b/src/clj/clojure/string.clj @@ -67,7 +67,7 @@ (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))))) + :else (throw (IllegalArgumentException. (str "Invalid match arg: " match))))) (defn ^String join @@ -118,23 +118,33 @@ (defn ^String triml "Removes whitespace from the left side of string." [^String s] - (replace s #"^\s+" "")) + (loop [index (int 0)] + (if (= (.length s) index) + "" + (if (Character/isWhitespace (.charAt s index)) + (recur (inc index)) + (.substring s index))))) (defn ^String trimr "Removes whitespace from the right side of string." [^String s] - (replace s #"\s+$" "")) + (loop [index (.length s)] + (if (zero? index) + "" + (if (Character/isWhitespace (.charAt s (dec index))) + (recur (dec index)) + (.substring s 0 index))))) -(defn ^String trim-nl +(defn ^String trim-newline "Removes all trailing newline \\n or return \\r characters from string. Note: String.trim() is similar and faster." [^String s] - (loop [offset (.length s)] - (if (zero? offset) + (loop [index (.length s)] + (if (zero? index) "" - (let [ch (.charAt s (dec offset))] + (let [ch (.charAt s (dec index))] (if (or (= ch \newline) (= ch \return)) - (recur (dec offset)) - (.substring s 0 offset)))))) + (recur (dec index)) + (.substring s 0 index)))))) |