Mercurial > lasercutter
diff src/clojure/contrib/str_utils.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/contrib/str_utils.clj Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,103 @@ 1.4 +;;; str_utils.clj -- string utilities for Clojure 1.5 + 1.6 +;; by Stuart Sierra <mail@stuartsierra.com> 1.7 +;; April 8, 2008 1.8 + 1.9 +;; Copyright (c) Stuart Sierra, 2008. All rights reserved. The use 1.10 +;; and distribution terms for this software are covered by the Eclipse 1.11 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.12 +;; which can be found in the file epl-v10.html at the root of this 1.13 +;; distribution. By using this software in any fashion, you are 1.14 +;; agreeing to be bound by the terms of this license. You must not 1.15 +;; remove this notice, or any other, from this software. 1.16 + 1.17 +;; DEPRECATED in 1.2: Promoted to clojure.java.string. Note that 1.18 +;; many function names and semantics have changed 1.19 + 1.20 +(ns 1.21 + ^{:author "Stuart Sierra", 1.22 + :deprecated "1.2" 1.23 + :doc "String utilities for Clojure"} 1.24 + clojure.contrib.str-utils 1.25 + (:import (java.util.regex Pattern))) 1.26 + 1.27 +(defn re-split 1.28 + "Splits the string on instances of 'pattern'. Returns a sequence of 1.29 + strings. Optional 'limit' argument is the maximum number of 1.30 + splits. Like Perl's 'split'." 1.31 + ([^Pattern pattern string] (seq (. pattern (split string)))) 1.32 + ([^Pattern pattern string limit] (seq (. pattern (split string limit))))) 1.33 + 1.34 +(defn re-partition 1.35 + "Splits the string into a lazy sequence of substrings, alternating 1.36 + between substrings that match the patthern and the substrings 1.37 + between the matches. The sequence always starts with the substring 1.38 + before the first match, or an empty string if the beginning of the 1.39 + string matches. 1.40 + 1.41 + For example: (re-partition #\"[a-z]+\" \"abc123def\") 1.42 + 1.43 + Returns: (\"\" \"abc\" \"123\" \"def\")" 1.44 + [^Pattern re string] 1.45 + (let [m (re-matcher re string)] 1.46 + ((fn step [prevend] 1.47 + (lazy-seq 1.48 + (if (.find m) 1.49 + (cons (.subSequence string prevend (.start m)) 1.50 + (cons (re-groups m) 1.51 + (step (+ (.start m) (count (.group m)))))) 1.52 + (when (< prevend (.length string)) 1.53 + (list (.subSequence string prevend (.length string))))))) 1.54 + 0))) 1.55 + 1.56 +(defn re-gsub 1.57 + "Replaces all instances of 'pattern' in 'string' with 1.58 + 'replacement'. Like Ruby's 'String#gsub'. 1.59 + 1.60 + If (ifn? replacment) is true, the replacement is called with the 1.61 + match. 1.62 + " 1.63 + [^java.util.regex.Pattern regex replacement ^String string] 1.64 + (if (ifn? replacement) 1.65 + (let [parts (vec (re-partition regex string))] 1.66 + (apply str 1.67 + (reduce (fn [parts match-idx] 1.68 + (update-in parts [match-idx] replacement)) 1.69 + parts (range 1 (count parts) 2)))) 1.70 + (.. regex (matcher string) (replaceAll replacement)))) 1.71 + 1.72 +(defn re-sub 1.73 + "Replaces the first instance of 'pattern' in 'string' with 1.74 + 'replacement'. Like Ruby's 'String#sub'. 1.75 + 1.76 + If (ifn? replacement) is true, the replacement is called with 1.77 + the match. 1.78 + " 1.79 + [^Pattern regex replacement ^String string] 1.80 + (if (ifn? replacement) 1.81 + (let [m (re-matcher regex string)] 1.82 + (if (.find m) 1.83 + (str (.subSequence string 0 (.start m)) 1.84 + (replacement (re-groups m)) 1.85 + (.subSequence string (.end m) (.length string))) 1.86 + string)) 1.87 + (.. regex (matcher string) (replaceFirst replacement)))) 1.88 + 1.89 + 1.90 +(defn str-join 1.91 + "Returns a string of all elements in 'sequence', separated by 1.92 + 'separator'. Like Perl's 'join'." 1.93 + [separator sequence] 1.94 + (apply str (interpose separator sequence))) 1.95 + 1.96 + 1.97 +(defn chop 1.98 + "Removes the last character of string." 1.99 + [s] 1.100 + (subs s 0 (dec (count s)))) 1.101 + 1.102 +(defn chomp 1.103 + "Removes all trailing newline \\n or return \\r characters from 1.104 + string. Note: String.trim() is similar and faster." 1.105 + [s] 1.106 + (re-sub #"[\r\n]+$" "" s))