Skip to content

Commit

Permalink
Migrate a bunch of string functions from Crawlista
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed Jun 11, 2012
1 parent 35efb76 commit 204f3db
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ChangeLog.md
@@ -1,6 +1,11 @@
## Changes between ClojureWerkz Support 0.4.0 and 0.5.0

No changes yet.
### New clojurewerkz.support.string functions

Several new functions were extracted from [Crawlista](https://github.com/michaelklishin/crawlista):

`clojurewerkz.support.string/maybe-prepend`, `clojurewerkz.support.string/maybe-append`, `clojurewerkz.support.string/maybe-chopl`,
`clojurewerkz.support.string/maybe-chopr`, `clojurewerkz.support.string/hex-to-int`.



Expand Down
33 changes: 33 additions & 0 deletions src/clojure/clojurewerkz/support/string.clj
Expand Up @@ -40,3 +40,36 @@
(let [s2 (.replaceAll s camel-case-splitter " ")
xs (s/split s2 #" ")]
(s/join "_" (map #(.toLowerCase ^String %) xs))))


(defn maybe-prepend
[^String s ^String prefix]
(.toLowerCase (if (.startsWith (.toLowerCase s) (.toLowerCase prefix))
s
(str prefix s))))

(defn maybe-append
[^String s ^String suffix]
(.toLowerCase (if (.endsWith (.toLowerCase s) (.toLowerCase suffix))
s
(str s suffix))))

(defn maybe-chopl
[^String s ^String prefix]
(let [ls (.toLowerCase s)]
(if (.startsWith ls prefix)
(.replaceAll ls (str "^" prefix) "")
s)))

(defn maybe-chopr
[^String s ^String suffix]
(let [ls (.toLowerCase s)]
(if (.endsWith ls suffix)
(.replaceAll ls (str suffix "$") "")
s)))

(defn hex-to-int
[^String s]
(Long/parseLong (if (.startsWith s "0x")
(subs s 2)
s) 16))
31 changes: 31 additions & 0 deletions test/clojurewerkz/support/string_test.clj
Expand Up @@ -33,3 +33,34 @@
"PDFLoader" "pdf_loader"
"lowercase" "lowercase"
"SimpleXMLParser" "simple_xml_parser"))


(deftest test-maybe-prepend
(is (= "www.apple.com" (s/maybe-prepend "apple.com" "www.")))
(is (= "www.apple.com" (s/maybe-prepend "APPLE.com" "www.")))
(is (= "www.apple.com" (s/maybe-prepend "www.apple.com" "www."))))

(deftest test-maybe-append
(is (= "apple.com" (s/maybe-append "apple.com" ".com")))
(is (= "apple.com" (s/maybe-append "APPLE.com" ".com")))
(is (= "google.com" (s/maybe-append "google" ".com"))))

(deftest test-maybe-chopl
(is (= (s/maybe-chopl "www.google.com" "www.") "google.com"))
(is (= (s/maybe-chopl "google.com" "goo") "gle.com"))
(is (= (s/maybe-chopl "Google.COM" "google.") "com"))
(is (= (s/maybe-chopl "www.www2.megacorp.net" "www.") "www2.megacorp.net")))

(deftest test-maybe-chopr
(is (= (s/maybe-chopr "http://www.google.com/" "/") "http://www.google.com"))
(is (= (s/maybe-chopr "google.com/" ".com/") "google"))
(is (= (s/maybe-chopr "Google.COM/" "/") "google.com")))

(deftest test-hexadecimal-to-int
(are [s i] (is (= (s/hex-to-int s) i))
"0xFF00FF" 16711935
"FF00FF" 16711935
"0xFF0000" 16711680
"FF0000" 16711680
"0x001100" 4352
"001100" 4352))

0 comments on commit 204f3db

Please sign in to comment.