Hello.

I wrote functions for converting tabs from/to spaces.  They are not
used anytime, so they may be irrelevant to include core.  Anyway I'd
like to show them and hear your opinions.

I want to use untabify in clojure.repl for formatting docstrings
correctly (I'll post about that later), so it's not very important
to include them in clojure.string (by adding it as private function in
clojure.repl or defining it by letfn).

It's my first contribution (I hope), so I'm anxious to make a ticket
in JIRA immediately.  Any suggestions would be appreciated.

Regards,
OGINO Masanori

Signed-off-by: OGINO Masanori <masanori.og...@gmail.com>
---
 src/clj/clojure/string.clj           |   28 ++++++++++++++++++++++++++++
 test/clojure/test_clojure/string.clj |   19 +++++++++++++++++++
 2 files changed, 47 insertions(+)

diff --git a/src/clj/clojure/string.clj b/src/clj/clojure/string.clj
index 188b518..8f5ca0a 100644
--- a/src/clj/clojure/string.clj
+++ b/src/clj/clojure/string.clj
@@ -166,6 +166,34 @@ Design notes for clojure.string:
   [^CharSequence s]
   (.. s toString toLowerCase))
 
+(defn ^String tabify
+  "Converts all n spaces (\\space, U+0020) at the beginning of lines
+  to a tab (\\t, U+0009).  By default, 8 spaces are used."
+  {:added "1.5"}
+  ([^CharSequence s]
+     (tabify s 8))
+  ([^CharSequence s n]
+     (join
+       (map (fn [[_ indent body eol]]
+              (str (replace (str indent)
+                            (join (repeat n \space)) "\t")
+                   body eol))
+            (re-seq #"(?m)^([ \t]+)?([^\n]*)(\n)?" s)))))
+
+(defn ^String untabify
+  "Converts all tabs (\\t, U+0009) at the beginning of lines to n
+  spaces (\\space, U+0020).  By default, 8 spaces are used."
+  {:added "1.5"}
+  ([^CharSequence s]
+     (untabify s 8))
+  ([^CharSequence s n]
+     (join
+       (map (fn [[_ indent body eol]]
+              (str (replace (str indent)
+                            "\t" (join (repeat n \space)))
+                   body eol))
+            (re-seq #"(?m)^([ \t]+)?([^\n]*)(\n)?" s)))))
+
 (defn split
   "Splits string on a regular expression.  Optional argument limit is
   the maximum number of splits. Not lazy. Returns vector of the splits."
diff --git a/test/clojure/test_clojure/string.clj 
b/test/clojure/test_clojure/string.clj
index d6f6469..3bdde54 100644
--- a/test/clojure/test_clojure/string.clj
+++ b/test/clojure/test_clojure/string.clj
@@ -118,3 +118,22 @@
     (is (vector? result)))
   (is (= (list "foo") (s/split-lines "foo"))))
 
+(deftest t-tabify
+  (is (= "a" (s/tabify "a")))
+  (is (= "  " (s/tabify "  ")))
+  (is (= "\t" (s/tabify "        ")))
+  (is (= "\t " (s/tabify "         ")))
+  (is (= "\ta        " (s/tabify "        a        ")))
+  (is (= "\t\n\t" (s/tabify "        \n        ")))
+  (is (= "\t\n\t\n" (s/tabify "        \n        \n")))
+  (is (= "\t" (s/tabify "  " 2))))
+
+(deftest t-untabify
+  (is (= "a" (s/untabify "a")))
+  (is (= "  " (s/untabify "  ")))
+  (is (= "        " (s/untabify "\t")))
+  (is (= "         " (s/untabify " \t")))
+  (is (= "        a\t" (s/untabify "\ta\t")))
+  (is (= "        \n        " (s/untabify "\t\n\t")))
+  (is (= "        \n        \n" (s/untabify "\t\n\t\n")))
+  (is (= "  " (s/untabify "\t" 2))))
-- 
1.7.9.5

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to