aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2008-04-08 13:28:51 +0000
committerStuart Sierra <mail@stuartsierra.com>2008-04-08 13:28:51 +0000
commit727e6fd988da69430b5681b671f18f21ec014402 (patch)
tree869b9f77efcdf6765ceffa3f2b951508dfec378f
parentd866cbb65da55c2339ad413e3e3b45b6a6c1b686 (diff)
Added str-utils.clj, simple string utilities
-rw-r--r--str-utils.clj45
1 files changed, 45 insertions, 0 deletions
diff --git a/str-utils.clj b/str-utils.clj
new file mode 100644
index 00000000..288e0942
--- /dev/null
+++ b/str-utils.clj
@@ -0,0 +1,45 @@
+;;; str-utils.clj -- string utilities for Clojure
+
+;; by Stuart Sierra <mail@stuartsierra.com>
+;; April 8, 2008
+
+;; Copyright (c) 2008 Stuart Sierra. All rights reserved. The use and
+;; distribution terms for this software are covered by the Common
+;; Public License 1.0 (http://www.opensource.org/licenses/cpl1.0.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.
+
+
+(clojure/in-ns 'str-utils)
+(clojure/refer 'clojure)
+
+
+(defn re-split
+ "Splits the string on instances of 'pattern'. Returns a sequence of
+ strings. Optional 'limit' argument is the maximum number of
+ splits. Like Perl's 'split'."
+ ([#^Pattern pattern string] (seq (. pattern (split string))))
+ ([#^Pattern pattern string limit] (seq (. pattern (split string limit)))))
+
+(defn re-gsub
+ "Replaces all instances of 'pattern' in 'string' with
+ 'replacement'. Like Ruby's 'String#gsub'."
+ [#^Pattern regex replacement #^String string]
+ (.. regex (matcher string) (replaceAll replacement)))
+
+(defn re-sub
+ "Replaces the first instance of 'pattern' in 'string' with
+ 'replacement'. Like Ruby's 'String#sub'."
+ [#^Pattern regex replacement #^String string]
+ (.. regex (matcher string) (replaceFirst replacement)))
+
+(defn str-join
+ "Returns a string of all elements in 'sequence', separated by
+ 'separator'. Like Perl's 'join'."
+ [separator sequence]
+ (reduce (fn
+ ([] (str)) ; in case sequence is empty
+ ([total next] (str total separator next)))
+ (seq sequence)))