Skip to content

Commit

Permalink
arg order fix, rename chomp-> trim-nl, drop chop, perf
Browse files Browse the repository at this point in the history
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information
David Liebke and Stuart Halloway authored and stuarthalloway committed Jun 4, 2010
1 parent f4427c7 commit 723991c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
23 changes: 10 additions & 13 deletions src/clj/clojure/string.clj
Expand Up @@ -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))))))


15 changes: 5 additions & 10 deletions test/clojure/test_clojure/string.clj
Expand Up @@ -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")))
Expand Down

0 comments on commit 723991c

Please sign in to comment.