Permalink
Browse files

abstraction fix to split: preserve vector nature of host return value…

… #359

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
1 parent 2c9503a commit 8ac69ede24a0c481f312008dbca8bee2bc6e1e73 @stuarthalloway stuarthalloway committed May 30, 2010
Showing with 12 additions and 4 deletions.
  1. +7 −4 src/clj/clojure/string.clj
  2. +5 −0 test/clojure/test_clojure/string.clj
View
@@ -10,7 +10,8 @@
:author "Stuart Sierra"}
clojure.string
(:refer-clojure :exclude (replace reverse))
- (:import (java.util.regex Pattern)))
+ (:import (java.util.regex Pattern)
+ clojure.lang.LazilyPersistentVector))
(defn ^String reverse
"Returns s with its characters reversed."
@@ -123,10 +124,12 @@
(defn split
"Splits string on a regular expression. Optional argument limit is
- the maximum number of splits."
+ the maximum number of splits. Not lazy. Returns vector of the splits."
{:added "1.2"}
- ([^Pattern re ^String s] (seq (.split re s)))
- ([^Pattern re limit ^String s] (seq (.split re s limit))))
+ ([^String s ^Pattern re]
+ (LazilyPersistentVector/createOwning (.split re s)))
+ ([ ^String s ^Pattern re limit]
+ (LazilyPersistentVector/createOwning (.split re s limit))))
(defn ^String trim
"Removes whitespace from both ends of string."
@@ -2,6 +2,11 @@
(:require [clojure.string :as s])
(:use clojure.test))
+(deftest t-split
+ (is (= ["a" "b"] (s/split "a-b" #"-")))
+ (is (= ["a" "b-c"] (s/split "a-b-c" #"-" 2)))
+ (is (vector? (s/split "abc" #"-"))))
+
(deftest t-reverse
(is (= "tab" (s/reverse "bat"))))

0 comments on commit 8ac69ed

Please sign in to comment.