view 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 source
1 ;;; str_utils.clj -- string utilities for Clojure
3 ;; by Stuart Sierra <mail@stuartsierra.com>
4 ;; April 8, 2008
6 ;; Copyright (c) Stuart Sierra, 2008. All rights reserved. The use
7 ;; and distribution terms for this software are covered by the Eclipse
8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
9 ;; which can be found in the file epl-v10.html at the root of this
10 ;; distribution. By using this software in any fashion, you are
11 ;; agreeing to be bound by the terms of this license. You must not
12 ;; remove this notice, or any other, from this software.
14 ;; DEPRECATED in 1.2: Promoted to clojure.java.string. Note that
15 ;; many function names and semantics have changed
17 (ns
18 ^{:author "Stuart Sierra",
19 :deprecated "1.2"
20 :doc "String utilities for Clojure"}
21 clojure.contrib.str-utils
22 (:import (java.util.regex Pattern)))
24 (defn re-split
25 "Splits the string on instances of 'pattern'. Returns a sequence of
26 strings. Optional 'limit' argument is the maximum number of
27 splits. Like Perl's 'split'."
28 ([^Pattern pattern string] (seq (. pattern (split string))))
29 ([^Pattern pattern string limit] (seq (. pattern (split string limit)))))
31 (defn re-partition
32 "Splits the string into a lazy sequence of substrings, alternating
33 between substrings that match the patthern and the substrings
34 between the matches. The sequence always starts with the substring
35 before the first match, or an empty string if the beginning of the
36 string matches.
38 For example: (re-partition #\"[a-z]+\" \"abc123def\")
40 Returns: (\"\" \"abc\" \"123\" \"def\")"
41 [^Pattern re string]
42 (let [m (re-matcher re string)]
43 ((fn step [prevend]
44 (lazy-seq
45 (if (.find m)
46 (cons (.subSequence string prevend (.start m))
47 (cons (re-groups m)
48 (step (+ (.start m) (count (.group m))))))
49 (when (< prevend (.length string))
50 (list (.subSequence string prevend (.length string)))))))
51 0)))
53 (defn re-gsub
54 "Replaces all instances of 'pattern' in 'string' with
55 'replacement'. Like Ruby's 'String#gsub'.
57 If (ifn? replacment) is true, the replacement is called with the
58 match.
59 "
60 [^java.util.regex.Pattern regex replacement ^String string]
61 (if (ifn? replacement)
62 (let [parts (vec (re-partition regex string))]
63 (apply str
64 (reduce (fn [parts match-idx]
65 (update-in parts [match-idx] replacement))
66 parts (range 1 (count parts) 2))))
67 (.. regex (matcher string) (replaceAll replacement))))
69 (defn re-sub
70 "Replaces the first instance of 'pattern' in 'string' with
71 'replacement'. Like Ruby's 'String#sub'.
73 If (ifn? replacement) is true, the replacement is called with
74 the match.
75 "
76 [^Pattern regex replacement ^String string]
77 (if (ifn? replacement)
78 (let [m (re-matcher regex string)]
79 (if (.find m)
80 (str (.subSequence string 0 (.start m))
81 (replacement (re-groups m))
82 (.subSequence string (.end m) (.length string)))
83 string))
84 (.. regex (matcher string) (replaceFirst replacement))))
87 (defn str-join
88 "Returns a string of all elements in 'sequence', separated by
89 'separator'. Like Perl's 'join'."
90 [separator sequence]
91 (apply str (interpose separator sequence)))
94 (defn chop
95 "Removes the last character of string."
96 [s]
97 (subs s 0 (dec (count s))))
99 (defn chomp
100 "Removes all trailing newline \\n or return \\r characters from
101 string. Note: String.trim() is similar and faster."
102 [s]
103 (re-sub #"[\r\n]+$" "" s))