Created
November 30, 2021 14:01
-
-
Save henryw374/8f1e46bf47404fb3a8cdadadadb24d10 to your computer and use it in GitHub Desktop.
clojure safe subs substring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns com.widdindustries.safe-subs) | |
(defn subs-safe | |
"like clojure.core/subs but more forgiving" | |
([^String s start] (subs-safe s start (count s))) | |
([^String s start end] | |
(when s | |
(let [end (if (> end (count s)) | |
(count s) | |
end)] | |
(cond | |
(zero? start) (subs s start end) | |
(> start (count s)) "" | |
:else (subs s start end)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment