-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils-string.stanza
More file actions
63 lines (56 loc) · 1.9 KB
/
utils-string.stanza
File metadata and controls
63 lines (56 loc) · 1.9 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
defpackage utils/string :
import core
public defn hex-to-int (s: String) -> Int :
var res = 0
for i in 2 to length(s) do :
val dig = index-of("0123456789ABCDEF", s[i]) as Int
res = res * 16 + dig
res
;;; CHARACTERS
public defn trim (s:String, ok?:(Char) -> True|False) -> String :
match(index-when(ok?, s)) :
(i:Int) :
val n = length(s)
val j = for i in (n - 1) through 0 by -1 find! :
ok?(s[i])
s[i through j]
(i:False) : ""
defn split-any* (str:IndexedCollection<Char>, any:String, range:Seq<Int>) -> Seq<KeyValue<Int, String>> :
generate<KeyValue<Int, String>> :
val beg = next(range)
let loop (start = beg, i = beg) :
if empty?(range) :
if contains?(any, str[i]) :
yield(start => (to-string $ str[start to i]))
yield(i => (to-string $ str[i through i]))
loop(i + 1, i + 1)
else :
loop(start, i + 1)
else if i != start :
yield(start => (to-string $ str[start to false]))
public defn split-any (str:IndexedCollection<Char>, any:String) -> Seq<KeyValue<Int, String>> :
generate<KeyValue<Int, String>> :
let loop (start = 0, i = 0) :
if i < length(str) :
if contains?(any, str[i]) :
yield(start => (to-string $ str[start to i]))
yield(i => (to-string $ str[i through i]))
loop(i + 1, i + 1)
else :
loop(start, i + 1)
else if start < length(str) :
yield(start => (to-string $ str[start to false]))
public defn split* (str:String, s:String) -> Seq<String> :
generate<String> :
val strl = length(str)
val sl = length(s)
let loop (b = 0) :
if b < strl :
match(index-of-chars(str, b to false, s)) :
(i:Int) :
yield(str[b to i])
loop(i + sl)
(i:False) :
yield(str[b to false])
else if b > 0 :
yield("")