Skip to content

Commit

Permalink
trim variants differ by suffix, for sortability
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 4997e80 commit 8c0022f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
51 changes: 26 additions & 25 deletions src/clj/clojure/string.clj
Expand Up @@ -82,21 +82,6 @@
sep) sep)
(str sb))))) (str sb)))))


(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
"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))

(defn ^String capitalize (defn ^String capitalize
"Converts first character of the string to upper-case, all other "Converts first character of the string to upper-case, all other
characters to lower-case." characters to lower-case."
Expand All @@ -106,16 +91,6 @@
(str (.toUpperCase ^String (subs s 0 1)) (str (.toUpperCase ^String (subs s 0 1))
(.toLowerCase ^String (subs s 1))))) (.toLowerCase ^String (subs s 1)))))


(defn ^String ltrim
"Removes whitespace from the left side of string."
[^String s]
(replace-re #"^\s+" "" s))

(defn ^String rtrim
"Removes whitespace from the right side of string."
[^String s]
(replace-re #"\s+$" "" s))

(defn ^String upper-case (defn ^String upper-case
"Converts string to all upper-case." "Converts string to all upper-case."
[^String s] [^String s]
Expand All @@ -137,3 +112,29 @@
[^String s] [^String s]
(.trim s)) (.trim s))


(defn ^String triml
"Removes whitespace from the left side of string."
[^String s]
(replace-re #"^\s+" "" s))

(defn ^String trimr
"Removes whitespace from the right side of string."
[^String s]
(replace-re #"\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
"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))


12 changes: 6 additions & 6 deletions test/clojure/test_clojure/string.clj
Expand Up @@ -41,13 +41,13 @@
(is (= "Foobar" (s/capitalize "foobar"))) (is (= "Foobar" (s/capitalize "foobar")))
(is (= "Foobar" (s/capitalize "FOOBAR")))) (is (= "Foobar" (s/capitalize "FOOBAR"))))


(deftest t-ltrim (deftest t-triml
(is (= "foo " (s/ltrim " foo "))) (is (= "foo " (s/triml " foo ")))
(is (= "" (s/ltrim " ")))) (is (= "" (s/triml " "))))


(deftest t-rtrim (deftest t-trimr
(is (= " foo" (s/rtrim " foo "))) (is (= " foo" (s/trimr " foo ")))
(is (= "" (s/rtrim " ")))) (is (= "" (s/trimr " "))))


(deftest t-trim (deftest t-trim
(is (= "foo" (s/trim " foo \r\n")))) (is (= "foo" (s/trim " foo \r\n"))))
Expand Down

0 comments on commit 8c0022f

Please sign in to comment.