diff options
author | scgilardi <scgilardi@gmail.com> | 2008-07-06 14:57:06 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-07-06 14:57:06 +0000 |
commit | 223f40a545bb583cc11ceb3f5d33b7a5a51ff9b3 (patch) | |
tree | 28c3d34c156239170d4eb9c2c34ec8ccae8c10c9 | |
parent | 37044c82ac649b1360a036fff7415c1ac305af0f (diff) |
add string.clj; lib.clj: use String/format for throw-if
-rw-r--r-- | lib.clj | 17 | ||||
-rw-r--r-- | string.clj | 29 |
2 files changed, 38 insertions, 8 deletions
@@ -160,10 +160,10 @@ [root runtime system]))) (defn- throw-if - "Throws an exception with a message if pred is true. Args - after pred will be passed along to str to form the message." - [pred & args] - (if pred (throw (Exception.(apply str args))))) + "Throws an exception with a message if pred is true. See + java.util.Formatter for format string syntax." + [pred fmt & args] + (if pred (throw (Exception. (String/format fmt (to-array args)))))) (def find-resource) ; forward declaration (def load-resource) ; forward declaration @@ -175,10 +175,11 @@ (let [res (str sym ".clj") rel-path (if in (str in \/ res) res) url (find-resource rel-path)] - (throw-if (not url) "resource '" rel-path "' not found in classpath") + (throw-if (not url) "resource '%s' not found in classpath" rel-path) (load-resource url) - (throw-if (and ns (not (find-ns ns))) "namespace '" ns "' not found" - " after loading resource '" rel-path "'")) + (throw-if (and ns (not (find-ns ns))) + "namespace '%s' not found after loading resource '%s'" + ns rel-path)) (dosync (commute *libs* conj sym)) (when *verbose* @@ -242,7 +243,7 @@ (instance? URL abs-path) abs-path (instance? URI abs-path) (.toURL abs-path) (string? abs-path) (URL. abs-path))] - (throw-if (not url) "Cannot coerce " (class abs-path) " to " URL) + (throw-if (not url) "Cannot coerce %s to %s" (class abs-path) URL) (with-open reader (BufferedReader. (InputStreamReader. diff --git a/string.clj b/string.clj new file mode 100644 index 00000000..5e738961 --- /dev/null +++ b/string.clj @@ -0,0 +1,29 @@ +;; Copyright (c) Stephen C. Gilardi. All rights reserved. +;; The use and distribution terms for this software are covered by the +;; Common Public License 1.0 (http://opensource.org/licenses/cpl.php) +;; which can be found in the file CPL.TXT at the root of this distribution. +;; By using this software in any fashion, you are agreeing to be bound by +;; the terms of this license. +;; You must not remove this notice, or any other, from this software. +;; +;; string.clj +;; +;; String functions +;; +;; scgilardi (gmail) +;; Created: 6 July 2008 + +(clojure/in-ns 'string) +(clojure/refer 'clojure) + +;; until Clojure upports "..." arguments, calling String/format directly +;; is just ugly enough, and could be commonly used enough to warrant a +;; Clojure wrapper. +;; +;; (let [name "world"] (format "Hello, %s!" name)) ==> "Hello, world!" + +(defn format + "Returns a string using the specified format and arguments. See + java.util.Formatter for format string syntax." + [fmt & args] + (String/format fmt (to-array args))) |