-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathstring.glj
More file actions
334 lines (292 loc) · 11.7 KB
/
Copy pathstring.glj
File metadata and controls
334 lines (292 loc) · 11.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
; Copyright (c) Rich Hickey. 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.
(ns clojure.string
(:refer-clojure :exclude (replace reverse))
(:import (regexp *Regexp)
#_(github.com:glojurelang:glojure:pkg:lang ToString NewIllegalArgumentError Char CharAt)
(github.com:glojurelang:glojure:pkg:lang)))
(set! *warn-on-reflection* true)
(defn- check-string
[maybe-s]
(if (nil? maybe-s)
(throw (github.com:glojurelang:glojure:pkg:lang.NewIllegalArgumentError "cannot call clojure.string function on nil"))
maybe-s))
(defn ^go/string reverse
"Returns s with its characters reversed."
{:added "1.2"}
[^go/string s]
(github.com:glojurelang:glojure:pkg:lang.ReverseString
(check-string s)))
(defn ^go/string re-quote-replacement
"Given a replacement string that you wish to be a literal
replacement for a pattern match in replace or replace-first, do the
necessary escaping of special characters in the replacement."
{:added "1.5"}
[^go/string replacement]
(strings.ReplaceAll (check-string ^go/string replacement)
"$"
"$$"))
(defn- replace-by
[^go/string s re f]
(let [m (re-matcher re s)]
(if (.find m)
(let [buffer (new strings.Builder)]
(loop [found true]
(if found
(do (.appendReplacement m buffer (re-quote-replacement (f (re-groups m))))
(recur (.find m)))
(do (.appendTail m buffer)
(github.com:glojurelang:glojure:pkg:lang.ToString buffer)))))
s)))
(defn ^go/string replace
"Replaces all instance of match with replacement in s.
match/replacement can be:
string / string
char / char
pattern / (string or function of match).
See also replace-first.
The replacement is literal (i.e. none of its characters are treated
specially) for all cases above except pattern / string.
For pattern / string, $1, $2, etc. in the replacement string are
substituted with the string that matched the corresponding
parenthesized group in the pattern. If you wish your replacement
string r to be used literally, use (re-quote-replacement r) as the
replacement argument. See also documentation for
java.util.regex.Matcher's appendReplacement method.
Example:
(glojure.string/replace \"Almost Pig Latin\" #\"\\b(\\w)(\\w+)\\b\" \"$2$1ay\")
-> \"lmostAay igPay atinLay\""
{:added "1.2"}
[^go/string s match replacement]
(let [s (check-string s)]
(cond
(instance? github.com:glojurelang:glojure:pkg:lang.Char match) (strings.ReplaceAll s match replacement)
(instance? go/string match) (strings.ReplaceAll s match replacement)
(instance? *Regexp match) (if (instance? go/string replacement)
(.replaceAllString ^*Regexp match s replacement)
(replace-by s match replacement))
:else (throw (github.com:glojurelang:glojure:pkg:lang.NewIllegalArgumentError (str "Invalid match arg: " match))))))
(defn- replace-first-by
[^go/string s ^*Regexp re f]
(let [m (re-matcher re s)]
(if (.find m)
(let [buffer (new strings.Builder)
rep (re-quote-replacement (f (re-groups m)))]
(.appendReplacement m buffer rep)
(.appendTail m buffer)
(str buffer))
s)))
(defn- replace-first-char
[^go/string s ^github.com:glojurelang:glojure:pkg:lang.Char match replace]
(let [s (github.com:glojurelang:glojure:pkg:lang.ToString s)
i (strings.Index s (int match))]
(if (= -1 i)
s
(str (subs s 0 i) replace (subs s (inc i))))))
(defn- replace-first-str
[^go/string s ^go/string match ^go/string replace]
(let [^go/string s (github.com:glojurelang:glojure:pkg:lang.ToString s)
i (strings.Index s match)]
(if (= -1 i)
s
(str (subs s 0 i) replace (subs s (+ i (count match)))))))
(defn ^go/string replace-first
"Replaces the first instance of match with replacement in s.
match/replacement can be:
char / char
string / string
pattern / (string or function of match).
See also replace.
The replacement is literal (i.e. none of its characters are treated
specially) for all cases above except pattern / string.
For pattern / string, $1, $2, etc. in the replacement string are
substituted with the string that matched the corresponding
parenthesized group in the pattern. If you wish your replacement
string r to be used literally, use (re-quote-replacement r) as the
replacement argument. See also documentation for
java.util.regex.Matcher's appendReplacement method.
Example:
(glojure.string/replace-first \"swap first two words\"
#\"(\\w+)(\\s+)(\\w+)\" \"$3$2$1\")
-> \"first swap two words\""
{:added "1.2"}
[^go/string s match replacement]
(let [s (check-string s)]
(cond
(instance? github.com:glojurelang:glojure:pkg:lang.Char match)
(replace-first-char s match replacement)
(instance? go/string match)
(replace-first-str s (github.com:glojurelang:glojure:pkg:lang.ToString ^go/string match)
(github.com:glojurelang:glojure:pkg:lang.ToString ^go/string replacement))
(instance? *Regexp match)
(if (instance? go/string replacement)
(let [done (atom false)]
(.ReplaceAllStringFunc ^*Regexp match s
(fn [m]
(if @done
m
(do
(reset! done true)
(.ReplaceAllString ^*Regexp match m replacement))))))
(replace-first-by s match replacement))
:else (throw (github.com:glojurelang:glojure:pkg:lang.NewIllegalArgumentError (str "Invalid match arg: " match))))))
(defn ^go/string join
"Returns a string of all elements in coll, as returned by (seq coll),
separated by an optional separator."
{:added "1.2"}
([coll]
(apply str coll))
([separator coll]
(strings.Join (map str coll) (str separator))))
(defn ^go/string capitalize
"Converts first character of the string to upper-case, all other
characters to lower-case."
{:added "1.2"}
[s]
(let [s ^go/string (str (check-string s))]
(if (< (count s) 2)
(strings.ToUpper s)
(str (strings.ToUpper (subs s 0 1))
(strings.ToLower (subs s 1))))))
(defn ^go/string upper-case
"Converts string to all upper-case."
{:added "1.2"}
[s]
(strings.ToUpper ^go/string (str (check-string s))))
(defn ^go/string lower-case
"Converts string to all lower-case."
{:added "1.2"}
[s]
(strings.ToLower ^go/string (str (check-string s))))
(defn split
"Splits string on a regular expression. Optional argument limit is
the maximum number of parts. Not lazy. Returns vector of the parts.
Trailing empty strings are not returned - pass limit of -1 to return all."
{:added "1.2"}
([^go/string s ^regexp.*Regexp re]
(github.com:glojurelang:glojure:pkg:lang.CreateOwningLazilyPersistentVector (.split re s -1)))
([ ^go/string s ^regexp.*Regexp re limit]
(github.com:glojurelang:glojure:pkg:lang.CreateOwningLazilyPersistentVector (.split re s limit))))
(defn split-lines
"Splits s on \\n or \\r\\n. Trailing empty lines are not returned."
{:added "1.2"}
[^go/string s]
#_(split s #"\r?\n")
(split s (re-pattern "\\r?\\n")))
(defn ^go/string trim
"Removes whitespace from both ends of string."
{:added "1.2"}
[^go/string s]
(strings.TrimSpace s))
(defn ^go/string triml
"Removes whitespace from the left side of string."
{:added "1.2"}
[^go/string s]
(strings.TrimLeftFunc s unicode.IsSpace))
(defn ^go/string trimr
"Removes whitespace from the right side of string."
{:added "1.2"}
[^go/string s]
(strings.TrimRightFunc s unicode.IsSpace))
(defn ^go/string trim-newline
"Removes all trailing newline \\n or return \\r characters from
string. Similar to Perl's chomp."
{:added "1.2"}
[^go/string s]
(strings.TrimRight s "\r\n"))
(defn blank?
"True if s is nil, empty, or contains only whitespace."
{:added "1.2"}
[^go/string s]
(if s
(loop [index (int 0)]
(if (= (count s) index)
true
(if (let [c (github.com:glojurelang:glojure:pkg:lang.CharAt s index)]
(and (unicode.IsSpace c)
(not= (int c) 0x00A0)
(not= (int c) 0x2007)
(not= (int c) 0x202F)))
(recur (inc index))
false)))
true))
(defn ^go/string escape
"Return a new string, using cmap to escape each character ch
from s as follows:
If (cmap ch) is nil, append ch to the new string.
If (cmap ch) is non-nil, append (str (cmap ch)) instead."
{:added "1.2"}
[^go/string s cmap]
(when (nil? s)
(throw (github.com:glojurelang:glojure:pkg:lang.NewError "s must not be nil")))
(let [sb (new strings.Builder)]
(doseq [c s]
(let [replacement (get cmap c)]
(if replacement
(.WriteString sb (str replacement))
(.WriteRune sb c))))
(.String sb)))
(defn index-of
"Return index of value (string or char) in s, optionally searching
forward from from-index. Return nil if value not found."
{:added "1.8"}
([^go/string s value]
(let [s (check-string s)
result ^long
(if (instance? github.com:glojurelang:glojure:pkg:lang.Char value)
(strings.IndexRune s ^go/rune (go/rune ^github.com:glojurelang:glojure:pkg:lang.Char value))
(strings.Index s ^go/string value))]
(if (= result -1)
nil
(go/int64 result))))
([^go/string s value ^long from-index]
(let [from-index (min (count s) (max 0 (unchecked-int from-index)))
s (subs (check-string s) from-index)
result ^long
(if (instance? github.com:glojurelang:glojure:pkg:lang.Char value)
(strings.IndexRune s ^go/rune (go/rune ^github.com:glojurelang:glojure:pkg:lang.Char value))
(strings.Index s ^go/string value))]
(if (= result -1)
nil
(+ result from-index)))))
(defn last-index-of
"Return last index of value (string or char) in s, optionally
searching backward from from-index. Return nil if value not found."
{:added "1.8"}
([^go/string s value]
(let [s (check-string s)
result (strings.LastIndex s ^go/string (str value))]
(if (= result -1)
nil
(go/int64 result))))
([^go/string s value ^long from-index]
(let [from-index (min (count s) (max 0 (inc (unchecked-int from-index))))
s (subs (check-string s) 0 from-index)
result (strings.LastIndex s ^go/string (str value))]
(if (= result -1)
nil
(go/int64 result)))))
(defn starts-with?
"True if s starts with substr."
{:added "1.8"}
[^go/string s ^go/string substr]
(if (nil? s)
(throw (github.com:glojurelang:glojure:pkg:lang.NewError "argument to starts-with? must not be nil"))
(strings.HasPrefix (github.com:glojurelang:glojure:pkg:lang.ToString s) substr)))
(defn ends-with?
"True if s ends with substr."
{:added "1.8"}
[^go/string s ^go/string substr]
(if (nil? s)
(throw (github.com:glojurelang:glojure:pkg:lang.NewError "argument to ends-with? must not be nil"))
(strings.HasSuffix (github.com:glojurelang:glojure:pkg:lang.ToString s) substr)))
(defn includes?
"True if s includes substr."
{:added "1.8"}
[^go/string s ^go/string substr]
(strings.Contains (github.com:glojurelang:glojure:pkg:lang.ToString s) substr))