Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change clojure.string/trim so it uses same defn of whitespace as othe…

…r trim fns

Signed-off-by: Stuart Halloway <stu@cognitect.com>
  • Loading branch information...
commit d7175e84e27816ee227220023a5411386d6aa32e 1 parent 17b1835
@jafingerhut jafingerhut authored Stuart Halloway committed
Showing with 27 additions and 12 deletions.
  1. +21 −9 src/clj/clojure/string.clj
  2. +6 −3 test/clojure/test_clojure/string.clj
View
30 src/clj/clojure/string.clj
@@ -233,18 +233,30 @@ Design notes for clojure.string:
"Removes whitespace from both ends of string."
{:added "1.2"}
[^CharSequence s]
- (.. s toString trim))
+ (let [len (.length s)]
+ (loop [rindex len]
+ (if (zero? rindex)
+ ""
+ (if (Character/isWhitespace (.charAt s (dec rindex)))
+ (recur (dec rindex))
+ ;; there is at least one non-whitespace char in the string,
+ ;; so no need to check for lindex reaching len.
+ (loop [lindex 0]
+ (if (Character/isWhitespace (.charAt s lindex))
+ (recur (inc lindex))
+ (.. s (subSequence lindex rindex) toString))))))))
(defn ^String triml
"Removes whitespace from the left side of string."
{:added "1.2"}
[^CharSequence s]
- (loop [index (int 0)]
- (if (= (.length s) index)
- ""
- (if (Character/isWhitespace (.charAt s index))
- (recur (inc index))
- (.. s (subSequence index (.length s)) toString)))))
+ (let [len (.length s)]
+ (loop [index 0]
+ (if (= len index)
+ ""
+ (if (Character/isWhitespace (.charAt s index))
+ (recur (unchecked-inc index))
+ (.. s (subSequence index len) toString))))))
(defn ^String trimr
"Removes whitespace from the right side of string."
@@ -253,8 +265,8 @@ Design notes for clojure.string:
(loop [index (.length s)]
(if (zero? index)
""
- (if (Character/isWhitespace (.charAt s (dec index)))
- (recur (dec index))
+ (if (Character/isWhitespace (.charAt s (unchecked-dec index)))
+ (recur (unchecked-dec index))
(.. s (subSequence 0 index) toString)))))
(defn ^String trim-newline
View
9 test/clojure/test_clojure/string.clj
@@ -67,14 +67,17 @@
(deftest t-triml
(is (= "foo " (s/triml " foo ")))
- (is (= "" (s/triml " "))))
+ (is (= "" (s/triml " ")))
+ (is (= "bar" (s/triml "\u2002 \tbar"))))
(deftest t-trimr
(is (= " foo" (s/trimr " foo ")))
- (is (= "" (s/trimr " "))))
+ (is (= "" (s/trimr " ")))
+ (is (= "bar" (s/trimr "bar\t \u2002"))))
(deftest t-trim
- (is (= "foo" (s/trim " foo \r\n"))))
+ (is (= "foo" (s/trim " foo \r\n")))
+ (is (= "bar" (s/trim "\u2000bar\t \u2002"))))
(deftest t-upper-case
(is (= "FOOBAR" (s/upper-case "Foobar"))))
Please sign in to comment.
Something went wrong with that request. Please try again.