diff options
author | Ben Smith-Mannschott <bsmith.occs@gmail.com> | 2010-08-28 11:22:22 +0200 |
---|---|---|
committer | Stuart Sierra <mail@stuartsierra.com> | 2010-09-03 12:23:37 -0400 |
commit | fe4ed311166677cd571d23774171af1d830f7fc5 (patch) | |
tree | 70d6c519b8afa7bbee51bb8c3e243d4f3903b2e2 | |
parent | 034d3d1703d139117b38fe6a10f552e23aa48b5c (diff) |
remove deprecated clojure.contrib.string
Since clojure.contrib.string is used by other submodules, some changes
were required:
- gen-html-docs and prxml needed changes because of functions were
renamed or arguments reordered when promoted to clojure.string.
- jmx, json, miglayout, prxml and sql gained a private one-argument
implementation of as-str.
- repl-utils gained a private copy of c.c.string/partition, named
spartition.
- repl-utils replaced a call to c.c.string/substring? with a call to
the java String method '.contains' (with swapped argument order).
Signed-off-by: Stuart Sierra <mail@stuartsierra.com>
21 files changed, 73 insertions, 599 deletions
diff --git a/modules/command-line/pom.xml b/modules/command-line/pom.xml index 8e2e01c9..224311a8 100644 --- a/modules/command-line/pom.xml +++ b/modules/command-line/pom.xml @@ -12,10 +12,5 @@ </parent> <artifactId>command-line</artifactId> <dependencies> - <dependency> - <groupId>org.clojure.contrib</groupId> - <artifactId>string</artifactId> - <version>1.3.0-SNAPSHOT</version> - </dependency> </dependencies> </project>
\ No newline at end of file diff --git a/modules/command-line/src/main/clojure/clojure/contrib/command_line.clj b/modules/command-line/src/main/clojure/clojure/contrib/command_line.clj index 47ee7849..6399c4fc 100644 --- a/modules/command-line/src/main/clojure/clojure/contrib/command_line.clj +++ b/modules/command-line/src/main/clojure/clojure/contrib/command_line.clj @@ -12,7 +12,7 @@ ^{:author "Chris Houser", :doc "Process command-line arguments according to a given cmdspec"} clojure.contrib.command-line - (:use (clojure.contrib [string :only (join)]))) + (:use (clojure [string :only (join)]))) (defn make-map [args cmdspec] (let [{spec true [rest-sym] false} (group-by vector? cmdspec) diff --git a/modules/complete/pom.xml b/modules/complete/pom.xml index 8b77d067..ea44dbc0 100644 --- a/modules/complete/pom.xml +++ b/modules/complete/pom.xml @@ -317,11 +317,6 @@ </dependency> <dependency> <groupId>org.clojure.contrib</groupId> - <artifactId>string</artifactId> - <version>1.3.0-SNAPSHOT</version> - </dependency> - <dependency> - <groupId>org.clojure.contrib</groupId> <artifactId>strint</artifactId> <version>1.3.0-SNAPSHOT</version> </dependency> diff --git a/modules/gen-html-docs/pom.xml b/modules/gen-html-docs/pom.xml index c04d6e84..80cc2cd5 100644 --- a/modules/gen-html-docs/pom.xml +++ b/modules/gen-html-docs/pom.xml @@ -14,11 +14,6 @@ <dependencies> <dependency> <groupId>org.clojure.contrib</groupId> - <artifactId>string</artifactId> - <version>1.3.0-SNAPSHOT</version> - </dependency> - <dependency> - <groupId>org.clojure.contrib</groupId> <artifactId>repl-utils</artifactId> <version>1.3.0-SNAPSHOT</version> </dependency> diff --git a/modules/gen-html-docs/src/main/clojure/clojure/contrib/gen_html_docs.clj b/modules/gen-html-docs/src/main/clojure/clojure/contrib/gen_html_docs.clj index 5046c163..145c040b 100644 --- a/modules/gen-html-docs/src/main/clojure/clojure/contrib/gen_html_docs.clj +++ b/modules/gen-html-docs/src/main/clojure/clojure/contrib/gen_html_docs.clj @@ -46,7 +46,7 @@ :doc "Generates a single HTML page that contains the documentation for one or more Clojure libraries."} clojure.contrib.gen-html-docs - (:require [clojure.contrib.string :as s]) + (:require [clojure.string :as s]) (:use [clojure.contrib repl-utils def prxml]) (:import [java.lang Exception] [java.util.regex Pattern])) @@ -226,7 +226,7 @@ function toggle(targetid, linkid, textWhenOpen, textWhenClosed) (if (= 0 (count l)) [:span {:class "library-member-doc-whitespace"} " "] ; We need something here to make the blank line show up l)]) - (s/split #"\n" docs)) + (s/split docs #"\n")) "")) (defn- member-type @@ -270,7 +270,7 @@ function toggle(targetid, linkid, textWhenOpen, textWhenClosed) (defn- elide-to-one-line "Elides a string down to one line." [s] - (s/replace-re #"(\n.*)+" "..." s)) + (s/replace s #"(\n.*)+" "...")) (defn- elide-string "Returns a string that is at most the first limit characters of s" @@ -282,13 +282,9 @@ function toggle(targetid, linkid, textWhenOpen, textWhenClosed) (defn- doc-elided-src "Returns the src with the docs elided." [docs src] - (s/replace-re (re-pattern (str "\"" (Pattern/quote docs) "\"")) - (str "\"" - (elide-to-one-line docs) -;; (elide-string docs 10) -;; "..." - "\"") - src)) + (s/replace src + (re-pattern (str "\"" (Pattern/quote docs) "\"")) + (str "\"" (elide-to-one-line docs) "\""))) (defn- format-source [libid memberid v] (try diff --git a/modules/jmx/pom.xml b/modules/jmx/pom.xml index 70d24b3f..08094a48 100644 --- a/modules/jmx/pom.xml +++ b/modules/jmx/pom.xml @@ -17,10 +17,5 @@ <artifactId>def</artifactId> <version>1.3.0-SNAPSHOT</version> </dependency> - <dependency> - <groupId>org.clojure.contrib</groupId> - <artifactId>string</artifactId> - <version>1.3.0-SNAPSHOT</version> - </dependency> </dependencies> </project>
\ No newline at end of file diff --git a/modules/jmx/src/main/clojure/clojure/contrib/jmx.clj b/modules/jmx/src/main/clojure/clojure/contrib/jmx.clj index ca0232ed..c248cf0d 100644 --- a/modules/jmx/src/main/clojure/clojure/contrib/jmx.clj +++ b/modules/jmx/src/main/clojure/clojure/contrib/jmx.clj @@ -72,7 +72,6 @@ clojure.contrib.jmx (:refer-clojure :exclude [read]) (:use clojure.contrib.def - [clojure.contrib.string :only [as-str]] [clojure.stacktrace :only (root-cause)] [clojure.walk :only [postwalk]]) (:import [clojure.lang Associative] @@ -83,6 +82,12 @@ (defvar *connection* (ManagementFactory/getPlatformMBeanServer) "The connection to be used for JMX ops. Defaults to the local process.") +(defn- as-str + [x] + (if (instance? clojure.lang.Named x) + (name x) + (str x))) + (load "jmx/data") (load "jmx/client") (load "jmx/server") diff --git a/modules/json/pom.xml b/modules/json/pom.xml index 95f611ca..5cf987c4 100644 --- a/modules/json/pom.xml +++ b/modules/json/pom.xml @@ -17,10 +17,5 @@ <artifactId>pprint</artifactId> <version>1.3.0-SNAPSHOT</version> </dependency> - <dependency> - <groupId>org.clojure.contrib</groupId> - <artifactId>string</artifactId> - <version>1.3.0-SNAPSHOT</version> - </dependency> </dependencies> </project>
\ No newline at end of file diff --git a/modules/json/src/main/clojure/clojure/contrib/json.clj b/modules/json/src/main/clojure/clojure/contrib/json.clj index 69f6cc9d..457f33c9 100644 --- a/modules/json/src/main/clojure/clojure/contrib/json.clj +++ b/modules/json/src/main/clojure/clojure/contrib/json.clj @@ -17,8 +17,7 @@ To write JSON, use json-str, write-json, or write-json. To read JSON, use read-json."} clojure.contrib.json - (:use [clojure.contrib.pprint :only (write formatter-out)] - [clojure.contrib.string :only (as-str)]) + (:use [clojure.contrib.pprint :only (write formatter-out)]) (:import (java.io PrintWriter PushbackReader StringWriter StringReader Reader EOFException))) @@ -228,6 +227,12 @@ (.append sb \") (.print out (str sb)))) +(defn- as-str + [x] + (if (instance? clojure.lang.Named x) + (name x) + (str x))) + (defn- write-json-object [m ^PrintWriter out] (.print out \{) (loop [x m] diff --git a/modules/miglayout/pom.xml b/modules/miglayout/pom.xml index 22328c7e..b7e3b856 100644 --- a/modules/miglayout/pom.xml +++ b/modules/miglayout/pom.xml @@ -27,10 +27,5 @@ <artifactId>fcase</artifactId> <version>1.3.0-SNAPSHOT</version> </dependency> - <dependency> - <groupId>org.clojure.contrib</groupId> - <artifactId>string</artifactId> - <version>1.3.0-SNAPSHOT</version> - </dependency> </dependencies> </project>
\ No newline at end of file diff --git a/modules/miglayout/src/main/clojure/clojure/contrib/miglayout/internal.clj b/modules/miglayout/src/main/clojure/clojure/contrib/miglayout/internal.clj index f6e6431f..86cbac12 100644 --- a/modules/miglayout/src/main/clojure/clojure/contrib/miglayout/internal.clj +++ b/modules/miglayout/src/main/clojure/clojure/contrib/miglayout/internal.clj @@ -20,8 +20,7 @@ (:use (clojure.contrib [core :only (new-by-name)] [except :only (throwf)] - [fcase :only (fcase)] - [string :only (as-str)]))) + [fcase :only (fcase)]))) (def MigLayout "net.miginfocom.swing.MigLayout") (def LayoutCallback "net.miginfocom.layout.LayoutCallback") @@ -44,6 +43,12 @@ (throwf IllegalArgumentException "unrecognized constraint: %s (%s)" c (class c)))]) +(defn- as-str + [x] + (if (instance? clojure.lang.Named x) + (name x) + (str x))) + (defn format-constraints "Returns a string representing all the constraints for one keyword-item or component formatted for miglayout." diff --git a/modules/prxml/pom.xml b/modules/prxml/pom.xml index c3132665..b2b25fe8 100644 --- a/modules/prxml/pom.xml +++ b/modules/prxml/pom.xml @@ -12,10 +12,5 @@ </parent> <artifactId>prxml</artifactId> <dependencies> - <dependency> - <groupId>org.clojure.contrib</groupId> - <artifactId>string</artifactId> - <version>1.3.0-SNAPSHOT</version> - </dependency> </dependencies> </project>
\ No newline at end of file diff --git a/modules/prxml/src/main/clojure/clojure/contrib/prxml.clj b/modules/prxml/src/main/clojure/clojure/contrib/prxml.clj index 2c2ec761..a680c773 100644 --- a/modules/prxml/src/main/clojure/clojure/contrib/prxml.clj +++ b/modules/prxml/src/main/clojure/clojure/contrib/prxml.clj @@ -27,7 +27,7 @@ :doc "Compact syntax for generating XML. See the documentation of \"prxml\" for details."} clojure.contrib.prxml - (:use [clojure.contrib.string :only (escape as-str)])) + (:use [clojure.string :only (escape)])) (def ^{:doc "If true, empty tags will have a space before the closing />"} @@ -43,11 +43,17 @@ for details."} (def ^{:private true} print-xml) ; forward declaration (defn- escape-xml [s] - (escape {\< "<" - \> ">" - \& "&" - \' "'" - \" """} s)) + (escape s {\< "<" + \> ">" + \& "&" + \' "'" + \" """})) + +(defn- as-str + [x] + (if (instance? clojure.lang.Named x) + (name x) + (str x))) (defn- prxml-attribute [name value] (print " ") diff --git a/modules/repl-utils/pom.xml b/modules/repl-utils/pom.xml index 1377caa2..cdcbbc83 100644 --- a/modules/repl-utils/pom.xml +++ b/modules/repl-utils/pom.xml @@ -17,10 +17,5 @@ <artifactId>seq</artifactId> <version>1.3.0-SNAPSHOT</version> </dependency> - <dependency> - <groupId>org.clojure.contrib</groupId> - <artifactId>string</artifactId> - <version>1.3.0-SNAPSHOT</version> - </dependency> </dependencies> </project>
\ No newline at end of file diff --git a/modules/repl-utils/src/main/clojure/clojure/contrib/repl_utils.clj b/modules/repl-utils/src/main/clojure/clojure/contrib/repl_utils.clj index bc9787b5..39195ea0 100644 --- a/modules/repl-utils/src/main/clojure/clojure/contrib/repl_utils.clj +++ b/modules/repl-utils/src/main/clojure/clojure/contrib/repl_utils.clj @@ -18,23 +18,44 @@ (:import (java.io File LineNumberReader InputStreamReader PushbackReader) (java.lang.reflect Modifier Method Constructor) (clojure.lang RT Compiler Compiler$C)) - (:require [clojure.contrib.string :as s]) (:use [clojure.contrib.seq :only (indexed)] - [clojure.java.browse :only (browse-url)])) + [clojure.java.browse :only (browse-url)] + [clojure.string :only (join)])) ;; ---------------------------------------------------------------------- ;; Examine Java classes +(defn- spartition + "Splits the string into a lazy sequence of substrings, alternating + between substrings that match the patthern and the substrings + between the matches. The sequence always starts with the substring + before the first match, or an empty string if the beginning of the + string matches. + + For example: (spartition #\"[a-z]+\" \"abc123def\") + returns: (\"\" \"abc\" \"123\" \"def\")" + [^Pattern re ^String s] + (let [m (re-matcher re s)] + ((fn step [prevend] + (lazy-seq + (if (.find m) + (cons (.subSequence s prevend (.start m)) + (cons (re-groups m) + (step (+ (.start m) (count (.group m)))))) + (when (< prevend (.length s)) + (list (.subSequence s prevend (.length s))))))) + 0))) + (defn- sortable [t] (apply str (map (fn [[a b]] (str a (format "%04d" (Integer. b)))) - (partition 2 (concat (s/partition #"\d+" t) [0]))))) + (spartition 2 (concat (spartition #"\d+" t) [0]))))) (defn- param-str [m] - (str " (" (s/join + (str " (" (join "," (map (fn [[c i]] (if (> i 3) (str (.getSimpleName c) "*" i) - (s/join "," (replicate i (.getSimpleName c))))) + (join "," (replicate i (.getSimpleName c))))) (reduce (fn [pairs y] (let [[x i] (peek pairs)] (if (= x y) (conj (pop pairs) [y (inc i)]) @@ -138,7 +159,7 @@ str-or-pattern." [str-or-pattern] (let [matches? (if (instance? java.util.regex.Pattern str-or-pattern) #(re-find str-or-pattern (str %)) - #(s/substring? (str str-or-pattern) (str %)))] + #(.contains (str %) (str str-or-pattern)))] (mapcat (fn [ns] (filter matches? (keys (ns-publics ns)))) (all-ns)))) diff --git a/modules/sql/pom.xml b/modules/sql/pom.xml index c36f876f..baf4ecab 100644 --- a/modules/sql/pom.xml +++ b/modules/sql/pom.xml @@ -19,11 +19,6 @@ </dependency> <dependency> <groupId>org.clojure.contrib</groupId> - <artifactId>string</artifactId> - <version>1.3.0-SNAPSHOT</version> - </dependency> - <dependency> - <groupId>org.clojure.contrib</groupId> <artifactId>except</artifactId> <version>1.3.0-SNAPSHOT</version> </dependency> diff --git a/modules/sql/src/main/clojure/clojure/contrib/sql.clj b/modules/sql/src/main/clojure/clojure/contrib/sql.clj index c6946078..5bc530af 100644 --- a/modules/sql/src/main/clojure/clojure/contrib/sql.clj +++ b/modules/sql/src/main/clojure/clojure/contrib/sql.clj @@ -21,9 +21,7 @@ :see-also [["http://github.com/richhickey/clojure-contrib/blob/master/src/test/clojure/clojure/contrib/test_sql.clj" "Example code"]]} clojure.contrib.sql - (:use (clojure.contrib - [def :only (defalias)] - [string :only (as-str)]) + (:use (clojure.contrib [def :only (defalias)]) clojure.contrib.sql.internal)) (defalias find-connection find-connection*) @@ -100,6 +98,12 @@ (transaction (seq (.executeBatch stmt))))) +(defn- as-str + [x] + (if (instance? clojure.lang.Named x) + (name x) + (str x))) + (defn create-table "Creates a table on the open database connection given a table name and specs. Each spec is either a column spec: a vector containing a column diff --git a/modules/string/pom.xml b/modules/string/pom.xml deleted file mode 100644 index 8978a33f..00000000 --- a/modules/string/pom.xml +++ /dev/null @@ -1,16 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 - http://maven.apache.org/maven-v4_0_0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.clojure.contrib</groupId> - <artifactId>parent</artifactId> - <version>1.3.0-SNAPSHOT</version> - <relativePath>../parent</relativePath> - </parent> - <artifactId>string</artifactId> - <dependencies> - </dependencies> -</project>
\ No newline at end of file diff --git a/modules/string/src/main/clojure/clojure/contrib/string.clj b/modules/string/src/main/clojure/clojure/contrib/string.clj deleted file mode 100644 index 0042f710..00000000 --- a/modules/string/src/main/clojure/clojure/contrib/string.clj +++ /dev/null @@ -1,382 +0,0 @@ -;;; string.clj -- functional string utilities for Clojure - -;; by Stuart Sierra, http://stuartsierra.com/ -;; January 26, 2010 - -;; Copyright (c) Stuart Sierra, 2010. All rights reserved. The use -;; and distribution terms for this software are covered by the Eclipse -;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) -;; which can be found in the file epl-v10.html 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. - -;; DEPRECATED in 1.2: Many functions have moved to clojure.string. - -(ns ^{:author "Stuart Sierra" - :doc "This is a library of string manipulation functions. It - is intented as a replacement for clojure.contrib.string. - - You cannot (use 'clojure.contrib.string) because it defines - functions with the same names as functions in clojure.core. - Instead, do (require '[clojure.contrib.string :as s]) - or something similar. - - Goals: - 1. Be functional - 2. Most significant argument LAST, to work with ->> - 3. At least O(n) performance for Strings of length n - - Some ideas are borrowed from - http://github.com/francoisdevlin/devlinsf-clojure-utils/"} - clojure.contrib.string - (:refer-clojure :exclude (take replace drop butlast partition - contains? get repeat reverse partial)) - (:import (java.util.regex Pattern))) - - -(defmacro dochars - "bindings => [name string] - - Repeatedly executes body, with name bound to each character in - string. Does NOT handle Unicode supplementary characters (above - U+FFFF)." - [bindings & body] - (assert (vector bindings)) - (assert (= 2 (count bindings))) - ;; This seems to be the fastest way to iterate over characters. - `(let [^String s# ~(second bindings)] - (dotimes [i# (.length s#)] - (let [~(first bindings) (.charAt s# i#)] - ~@body)))) - - -(defmacro docodepoints - "bindings => [name string] - - Repeatedly executes body, with name bound to the integer code point - of each Unicode character in the string. Handles Unicode - supplementary characters (above U+FFFF) correctly." - [bindings & body] - (assert (vector bindings)) - (assert (= 2 (count bindings))) - (let [character (first bindings) - string (second bindings)] - `(let [^String s# ~string - len# (.length s#)] - (loop [i# 0] - (when (< i# len#) - (let [~character (.charAt s# i#)] - (if (Character/isHighSurrogate ~character) - (let [~character (.codePointAt s# i#)] - ~@body - (recur (+ 2 i#))) - (let [~character (int ~character)] - ~@body - (recur (inc i#)))))))))) - -(defn codepoints - "Returns a sequence of integer Unicode code points in s. Handles - Unicode supplementary characters (above U+FFFF) correctly." - [^String s] - (let [len (.length s) - f (fn thisfn [^String s i] - (when (< i len) - (let [c (.charAt s i)] - (if (Character/isHighSurrogate c) - (cons (.codePointAt s i) (thisfn s (+ 2 i))) - (cons (int c) (thisfn s (inc i)))))))] - (lazy-seq (f s 0)))) - -(defn ^String escape - "Returns a new String by applying cmap (a function or a map) to each - character in s. If cmap returns nil, the original character is - added to the output unchanged." - {:deprecated "1.2"} - [cmap ^String s] - (let [buffer (StringBuilder. (.length s))] - (dochars [c s] - (if-let [r (cmap c)] - (.append buffer r) - (.append buffer c))) - (.toString buffer))) - -(defn blank? - "True if s is nil, empty, or contains only whitespace." - {:deprecated "1.2"} - [^String s] - (every? (fn [^Character c] (Character/isWhitespace c)) s)) - -(defn ^String take - "Take first n characters from s, up to the length of s." - [n ^String s] - (if (< (count s) n) - s - (.substring s 0 n))) - -(defn ^String drop - "Drops first n characters from s. Returns an empty string if n is - greater than the length of s." - [n ^String s] - (if (< (count s) n) - "" - (.substring s n))) - -(defn ^String butlast - "Returns s without the last n characters. Returns an empty string - if n is greater than the length of s." - [n ^String s] - (if (< (count s) n) - "" - (.substring s 0 (- (count s) n)))) - -(defn ^String tail - "Returns the last n characters of s." - [n ^String s] - (if (< (count s) n) - s - (.substring s (- (count s) n)))) - -(defn ^String repeat - "Returns a new String containing s repeated n times." - [n ^String s] - (apply str (clojure.core/repeat n s))) - -(defn ^String reverse - "Returns s with its characters reversed." - {:deprecated "1.2"} - [^String s] - (.toString (.reverse (StringBuilder. s)))) - -(defn replace-str - "Replaces all instances of substring a with b in s." - {:deprecated "1.2"} - [^String a ^String b ^String s] - (.replace s a b)) - -(defn replace-char - "Replaces all instances of character a with character b in s." - {:deprecated "1.2"} - [^Character a ^Character b ^String s] - (.replace s a b)) - -(defn replace-re - "Replaces all matches of re with replacement in s." - {:deprecated "1.2"} - [re replacement ^String s] - (.replaceAll (re-matcher re s) replacement)) - -(defn replace-by - "Replaces all matches of re in s with the result of - (f (re-groups the-match))." - {:deprecated "1.2"} - [re f ^String s] - (let [m (re-matcher re s)] - (let [buffer (StringBuffer. (.length s))] - (loop [] - (if (.find m) - (do (.appendReplacement m buffer (f (re-groups m))) - (recur)) - (do (.appendTail m buffer) - (.toString buffer))))))) - -(defn replace-first-str - "Replace first occurance of substring a with b in s." - {:deprecated "1.2"} - [^String a ^String b ^String s] - (.replaceFirst (re-matcher (Pattern/quote a) s) b)) - -(defn replace-first-re - "Replace first match of re in s." - {:deprecated "1.2"} - [^Pattern re ^String replacement ^String s] - (.replaceFirst (re-matcher re s) replacement)) - -(defn replace-first-by - "Replace first match of re in s with the result of - (f (re-groups the-match))." - {:deprecated "1.2"} - [^Pattern re f ^String s] - (let [m (re-matcher re s)] - (let [buffer (StringBuffer.)] - (if (.find m) - (let [rep (f (re-groups m))] - (.appendReplacement m buffer rep) - (.appendTail m buffer) - (str buffer)))))) - -(defn partition - "Splits the string into a lazy sequence of substrings, alternating - between substrings that match the patthern and the substrings - between the matches. The sequence always starts with the substring - before the first match, or an empty string if the beginning of the - string matches. - - For example: (partition #\"[a-z]+\" \"abc123def\") - returns: (\"\" \"abc\" \"123\" \"def\")" - [^Pattern re ^String s] - (let [m (re-matcher re s)] - ((fn step [prevend] - (lazy-seq - (if (.find m) - (cons (.subSequence s prevend (.start m)) - (cons (re-groups m) - (step (+ (.start m) (count (.group m)))))) - (when (< prevend (.length s)) - (list (.subSequence s prevend (.length s))))))) - 0))) - -(defn ^String join - "Returns a string of all elements in coll, separated by - separator. Like Perl's join." - {:deprecated "1.2"} - [^String separator coll] - (apply str (interpose separator coll))) - -(defn ^String chop - "Removes the last character of string, does nothing on a zero-length - string." - [^String s] - (let [size (count s)] - (if (zero? size) - s - (subs s 0 (dec (count s)))))) - -(defn ^String chomp - "Removes all trailing newline \\n or return \\r characters from - string. Note: String.trim() is similar and faster. - Deprecated in 1.2. Use clojure.string/trim-newline" - {:deprecated "1.2"} - [^String s] - (replace-re #"[\r\n]+$" "" s)) - -(defn ^String swap-case - "Changes upper case characters to lower case and vice-versa. - Handles Unicode supplementary characters correctly. Uses the - locale-sensitive String.toUpperCase() and String.toLowerCase() - methods." - [^String s] - (let [buffer (StringBuilder. (.length s)) - ;; array to make a String from one code point - ^"[I" array (make-array Integer/TYPE 1)] - (docodepoints [c s] - (aset-int array 0 c) - (if (Character/isLowerCase c) - ;; Character.toUpperCase is not locale-sensitive, but - ;; String.toUpperCase is; so we use a String. - (.append buffer (.toUpperCase (String. array 0 1))) - (.append buffer (.toLowerCase (String. array 0 1))))) - (.toString buffer))) - -(defn ^String capitalize - "Converts first character of the string to upper-case, all other - characters to lower-case." - {:deprecated "1.2"} - [^String s] - (if (< (count s) 2) - (.toUpperCase s) - (str (.toUpperCase ^String (subs s 0 1)) - (.toLowerCase ^String (subs s 1))))) - -(defn ^String ltrim - "Removes whitespace from the left side of string. - Deprecated in 1.2. Use clojure.string/triml." - {:deprecated "1.2"} - [^String s] - (replace-re #"^\s+" "" s)) - -(defn ^String rtrim - "Removes whitespace from the right side of string. - Deprecated in 1.2. Use clojure.string/trimr." - {:deprecated "1.2"} - [^String s] - (replace-re #"\s+$" "" s)) - -(defn split-lines - "Splits s on \\n or \\r\\n." - {:deprecated "1.2"} - [^String s] - (seq (.split #"\r?\n" s))) - -;; borrowed from compojure.string, by James Reeves, EPL 1.0 -(defn ^String map-str - "Apply f to each element of coll, concatenate all results into a - String." - [f coll] - (apply str (map f coll))) - -;; borrowed from compojure.string, by James Reeves, EPL 1.0 -(defn grep - "Filters elements of coll by a regular expression. The String - representation (with str) of each element is tested with re-find." - [re coll] - (filter (fn [x] (re-find re (str x))) coll)) - -(defn as-str - "Like clojure.core/str, but if an argument is a keyword or symbol, - its name will be used instead of its literal representation. - - Example: - (str :foo :bar) ;;=> \":foo:bar\" - (as-str :foo :bar) ;;=> \"foobar\" - - Note that this does not apply to keywords or symbols nested within - data structures; they will be rendered as with str. - - Example: - (str {:foo :bar}) ;;=> \"{:foo :bar}\" - (as-str {:foo :bar}) ;;=> \"{:foo :bar}\" " - ([] "") - ([x] (if (instance? clojure.lang.Named x) - (name x) - (str x))) - ([x & ys] - ((fn [^StringBuilder sb more] - (if more - (recur (. sb (append (as-str (first more)))) (next more)) - (str sb))) - (new StringBuilder ^String (as-str x)) ys))) - - -;;; WRAPPERS - -;; The following functions are simple wrappers around java.lang.String -;; functions. They are included here for completeness, and for use -;; when mapping over a collection of strings. - -(defn ^String upper-case - "Converts string to all upper-case." - {:deprecated "1.2"} - [^String s] - (.toUpperCase s)) - -(defn ^String lower-case - "Converts string to all lower-case." - {:deprecated "1.2"} - [^String s] - (.toLowerCase s)) - -(defn split - "Splits string on a regular expression. Optional argument limit is - the maximum number of splits." - {:deprecated "1.2"} - ([^Pattern re ^String s] (seq (.split re s))) - ([^Pattern re limit ^String s] (seq (.split re s limit)))) - -(defn ^String trim - "Removes whitespace from both ends of string." - {:deprecated "1.2"} - [^String s] - (.trim s)) - -(defn ^String substring? - "True if s contains the substring." - [substring ^String s] - (.contains s substring)) - -(defn ^String get - "Gets the i'th character in string." - {:deprecated "1.2"} - [^String s i] - (.charAt s i)) - diff --git a/modules/string/src/test/clojure/clojure/contrib/test_string.clj b/modules/string/src/test/clojure/clojure/contrib/test_string.clj deleted file mode 100644 index 98f03a78..00000000 --- a/modules/string/src/test/clojure/clojure/contrib/test_string.clj +++ /dev/null @@ -1,124 +0,0 @@ -(ns clojure.contrib.test-string - (:require [clojure.contrib.string :as s]) - (:use clojure.test)) - -(deftest t-codepoints - (is (= (list 102 111 111 65536 98 97 114) - (s/codepoints "foo\uD800\uDC00bar")) - "Handles Unicode supplementary characters")) - -(deftest t-escape - (is (= "<foo&bar>" - (s/escape {\& "&" \< "<" \> ">"} "<foo&bar>"))) - (is (= " \\\"foo\\\" " - (s/escape {\" "\\\""} " \"foo\" " ))) - (is (= "faabor" (s/escape {\a \o, \o \a} "foobar")))) - -(deftest t-blank - (is (s/blank? nil)) - (is (s/blank? "")) - (is (s/blank? " ")) - (is (s/blank? " \t \n \r ")) - (is (not (s/blank? " foo ")))) - -(deftest t-take - (is (= "foo" (s/take 3 "foobar"))) - (is (= "foobar" (s/take 7 "foobar"))) - (is (= "" (s/take 0 "foo")))) - -(deftest t-drop - (is (= "bar" (s/drop 3 "foobar"))) - (is (= "" (s/drop 9 "foobar"))) - (is (= "foobar" (s/drop 0 "foobar")))) - -(deftest t-butlast - (is (= "foob" (s/butlast 2 "foobar"))) - (is (= "" (s/butlast 9 "foobar"))) - (is (= "foobar" (s/butlast 0 "foobar")))) - -(deftest t-tail - (is (= "ar" (s/tail 2 "foobar"))) - (is (= "foobar" (s/tail 9 "foobar"))) - (is (= "" (s/tail 0 "foobar")))) - -(deftest t-repeat - (is (= "foofoofoo" (s/repeat 3 "foo")))) - -(deftest t-reverse - (is (= "tab" (s/reverse "bat")))) - -(deftest t-replace - (is (= "faabar" (s/replace-char \o \a "foobar"))) - (is (= "barbarbar" (s/replace-str "foo" "bar" "foobarfoo"))) - (is (= "FOObarFOO" (s/replace-by #"foo" s/upper-case "foobarfoo")))) - -(deftest t-replace-first - (is (= "barbarfoo" (s/replace-first-re #"foo" "bar" "foobarfoo"))) - (is (= "FOObarfoo" (s/replace-first-by #"foo" s/upper-case "foobarfoo")))) - -(deftest t-partition - (is (= (list "" "abc" "123" "def") - (s/partition #"[a-z]+" "abc123def")))) - -(deftest t-join - (is (= "1,2,3" (s/join \, [1 2 3]))) - (is (= "" (s/join \, []))) - (is (= "1 and-a 2 and-a 3" (s/join " and-a " [1 2 3])))) - -(deftest t-chop - (is (= "fo" (s/chop "foo"))) - (is (= "") (s/chop "f")) - (is (= "") (s/chop ""))) - -(deftest t-chomp - (is (= "foo" (s/chomp "foo\n"))) - (is (= "foo" (s/chomp "foo\r\n"))) - (is (= "foo" (s/chomp "foo"))) - (is (= "" (s/chomp "")))) - -(deftest t-swap-case - (is (= "fOO!bAR" (s/swap-case "Foo!Bar"))) - (is (= "" (s/swap-case "")))) - -(deftest t-capitalize - (is (= "Foobar" (s/capitalize "foobar"))) - (is (= "Foobar" (s/capitalize "FOOBAR")))) - -(deftest t-ltrim - (is (= "foo " (s/ltrim " foo "))) - (is (= "" (s/ltrim " ")))) - -(deftest t-rtrim - (is (= " foo" (s/rtrim " foo "))) - (is (= "" (s/rtrim " ")))) - -(deftest t-split-lines - (is (= (list "one" "two" "three") - (s/split-lines "one\ntwo\r\nthree"))) - (is (= (list "foo") (s/split-lines "foo")))) - -(deftest t-upper-case - (is (= "FOOBAR" (s/upper-case "Foobar")))) - -(deftest t-lower-case - (is (= "foobar" (s/lower-case "FooBar")))) - -(deftest t-trim - (is (= "foo" (s/trim " foo \r\n")))) - -(deftest t-substring - (is (s/substring? "foo" "foobar")) - (is (not (s/substring? "baz" "foobar")))) - -(deftest t-get - (is (= \o (s/get "foo" 1)))) - -(deftest t-as-str - (testing "keyword to string" - (is (= "foo") (s/as-str :foo))) - (testing "symbol to string" - (is (= "foo") (s/as-str 'foo))) - (testing "string to string" - (is (= "foo") (s/as-str "foo"))) - (testing "stringifying non-namish things" - (is (= "42") (s/as-str 42)))) @@ -78,7 +78,6 @@ <module>modules/singleton</module> <module>modules/sql</module> <module>modules/stream-utils</module> - <module>modules/string</module> <module>modules/strint</module> <module>modules/swing-utils</module> <module>modules/trace</module> |