Permalink
Browse files

arg order fix, rename chomp-> trim-nl, drop chop, perf

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
1 parent f4427c7 commit 723991c12a4d12ef1dc4f5ab205e6f2a741faf33 David Liebke and Stuart Halloway committed with stuarthalloway May 28, 2010
Showing with 15 additions and 23 deletions.
  1. +10 −13 src/clj/clojure/string.clj
  2. +5 −10 test/clojure/test_clojure/string.clj
View
23 src/clj/clojure/string.clj
@@ -118,26 +118,23 @@
(defn ^String triml
"Removes whitespace from the left side of string."
[^String s]
- (replace-re #"^\s+" "" s))
+ (replace s #"^\s+" ""))
(defn ^String trimr
"Removes whitespace from the right side of string."
[^String s]
- (replace-re #"\s+$" "" s))
+ (replace s #"\s+$" ""))
-(defn ^String chop
- "Removes the last character of string, does nothing on a zero-length
- string."
- [^String s]
- (let [size (count s)]
- (if (zero? size)
- s
- (subs s 0 (dec (count s))))))
-
-(defn ^String chomp
+(defn ^String trim-nl
"Removes all trailing newline \\n or return \\r characters from
string. Note: String.trim() is similar and faster."
[^String s]
- (replace-re #"[\r\n]+$" "" s))
+ (loop [offset (.length s)]
+ (if (zero? offset)
+ ""
+ (let [ch (.charAt s (dec offset))]
+ (if (or (= ch \newline) (= ch \return))
+ (recur (dec offset))
+ (.substring s 0 offset))))))
View
15 test/clojure/test_clojure/string.clj
@@ -27,16 +27,11 @@
"1" \, [1]
"1 and-a 2 and-a 3" " and-a " [1 2 3]))
-(deftest t-chop
- (is (= "fo" (s/chop "foo")))
- (is (= "") (s/chop "f"))
- (is (= "") (s/chop "")))
-
-(deftest t-chomp
- (is (= "foo" (s/chomp "foo\n")))
- (is (= "foo" (s/chomp "foo\r\n")))
- (is (= "foo" (s/chomp "foo")))
- (is (= "" (s/chomp ""))))
+(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-capitalize
(is (= "Foobar" (s/capitalize "foobar")))

0 comments on commit 723991c

Please sign in to comment.