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 | |
parent | 07fc5303c932d9d509d81d44e1a25bd244ceb7dd (diff) |
string perf tweaks, tests #359
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
-rw-r--r-- | src/clj/clojure/string.clj | 28 | ||||
-rw-r--r-- | test/clojure/test_clojure.clj | 5 | ||||
-rw-r--r-- | test/clojure/test_clojure/string.clj | 10 |
3 files changed, 29 insertions, 14 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)))))) diff --git a/test/clojure/test_clojure.clj b/test/clojure/test_clojure.clj index 9bde9009..f2b71a3e 100644 --- a/test/clojure/test_clojure.clj +++ b/test/clojure/test_clojure.clj @@ -58,6 +58,11 @@ :rt :repl :java.io + :string + :java.javadoc + :java.shell + :transients + :def ]) (def test-namespaces diff --git a/test/clojure/test_clojure/string.clj b/test/clojure/test_clojure/string.clj index af755f55..8ee7cdee 100644 --- a/test/clojure/test_clojure/string.clj +++ b/test/clojure/test_clojure/string.clj @@ -27,11 +27,11 @@ "1" \, [1] "1 and-a 2 and-a 3" " and-a " [1 2 3])) -(deftest t-trim-nl - (is (= "foo" (s/trim-nl "foo\n"))) - (is (= "foo" (s/trim-nl "foo\r\n"))) - (is (= "foo" (s/trim-nl "foo"))) - (is (= "" (s/trim-nl "")))) +(deftest t-trim-newline + (is (= "foo" (s/trim-newline "foo\n"))) + (is (= "foo" (s/trim-newline "foo\r\n"))) + (is (= "foo" (s/trim-newline "foo"))) + (is (= "" (s/trim-newline "")))) (deftest t-capitalize (is (= "Foobar" (s/capitalize "foobar"))) |