Mercurial > lasercutter
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 Clojure3 ;; by Stuart Sierra <mail@stuartsierra.com>4 ;; April 8, 20086 ;; Copyright (c) Stuart Sierra, 2008. All rights reserved. The use7 ;; and distribution terms for this software are covered by the Eclipse8 ;; 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 this10 ;; distribution. By using this software in any fashion, you are11 ;; agreeing to be bound by the terms of this license. You must not12 ;; remove this notice, or any other, from this software.14 ;; DEPRECATED in 1.2: Promoted to clojure.java.string. Note that15 ;; many function names and semantics have changed17 (ns18 ^{:author "Stuart Sierra",19 :deprecated "1.2"20 :doc "String utilities for Clojure"}21 clojure.contrib.str-utils22 (:import (java.util.regex Pattern)))24 (defn re-split25 "Splits the string on instances of 'pattern'. Returns a sequence of26 strings. Optional 'limit' argument is the maximum number of27 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-partition32 "Splits the string into a lazy sequence of substrings, alternating33 between substrings that match the patthern and the substrings34 between the matches. The sequence always starts with the substring35 before the first match, or an empty string if the beginning of the36 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-seq45 (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-gsub54 "Replaces all instances of 'pattern' in 'string' with55 'replacement'. Like Ruby's 'String#gsub'.57 If (ifn? replacment) is true, the replacement is called with the58 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 str64 (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-sub70 "Replaces the first instance of 'pattern' in 'string' with71 'replacement'. Like Ruby's 'String#sub'.73 If (ifn? replacement) is true, the replacement is called with74 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-join88 "Returns a string of all elements in 'sequence', separated by89 'separator'. Like Perl's 'join'."90 [separator sequence]91 (apply str (interpose separator sequence)))94 (defn chop95 "Removes the last character of string."96 [s]97 (subs s 0 (dec (count s))))99 (defn chomp100 "Removes all trailing newline \\n or return \\r characters from101 string. Note: String.trim() is similar and faster."102 [s]103 (re-sub #"[\r\n]+$" "" s))