forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.pxi
More file actions
65 lines (57 loc) · 1.7 KB
/
string.pxi
File metadata and controls
65 lines (57 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(ns pixie.string
(require pixie.string.internal :as si))
; reexport native string functions
(def substring si/substring)
(def index-of si/index-of)
(def split si/split)
(def ends-with? si/ends-with)
(def starts-with? si/starts-with)
(def trim si/trim)
(def triml si/triml)
(def trimr si/trimr)
(def capitalize si/capitalize)
(def lower-case si/lower-case)
(def upper-case si/upper-case)
(defn replace
"Replace all occurrences of x in s with r."
[s x r]
(let [offset (if (zero? (count x)) (+ 1 (count r)) (count r))]
(loop [start 0
s s]
(let [i (index-of s x start)]
(if (neg? i)
s
(recur (+ i offset) (str (substring s 0 i) r (substring s (+ i (count x))))))))))
(defn replace-first
"Replace the first occurrence of x in s with r."
[s x r]
(let [i (index-of s x)]
(if (neg? i)
s
(str (substring s 0 i) r (substring s (+ i (count x)))))))
(defn join
{:doc "Join the elements of the collection using an optional seperator."
:examples [["(require pixie.string :as s)"]
["(s/join [1 2 3])" nil "123"]
["(s/join \", \" [1 2 3])" nil "1, 2, 3"]]}
([coll] (join "" coll))
([seperator coll]
(loop [s (seq coll)
res ""]
(cond
(nil? s) res
(nil? (next s)) (str res (first s))
:else (recur (next s) (str res (first s) seperator))))))
(defn blank?
"True if s is nil, empty, or contains only whitespace."
[s]
(if s
(let [white #{\space \newline \tab \backspace \formfeed \return}
length (count s)]
(loop [index 0]
(if (= length index)
true
(if (white (nth s index))
(recur (inc index))
false))))
true))