Repository navigation
Expand file tree
/
Copy pathstrings.rv
More file actions
217 lines (160 loc) · 4.36 KB
/
Copy pathstrings.rv
File metadata and controls
217 lines (160 loc) · 4.36 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
import { ... } from "./core.rv"
import { ... } from "./numbers.rv"
import { Bool, Int, UInt, UInt8, UInt16, UInt32, bits, oftype } from "./integer.rv"
import { IndexIterator, collect, count, nth, map, mapping } from "./iterate.rv"
import { List } from "./list.rv"
import { JSObject } from "./wasm/js.rv"
export { Char, String, Regex, string, concat, compare, chars, utf8, utf16, contains?, matches }
# Chars
bundle Char(val: UInt[21])
fn Char(x: (Int | UInt)) { Char(oftype(uint 21, x)) }
fn Char(x: UInt[21]) { pack(Char, x) }
@extend
fn UInt32(Char(x)) { UInt32(x) }
@extend
fn string(Char(val)) {
ref = wasm call("wasm:js-string".fromCodePoint, widen(bits(val)): i32): ref!
return unsafeString(ref)
}
@extend
fn print(ch: Char) { print(string(ch)) }
@extend
fn show(ch: Char) {
print(`c`)
show(string(ch))
}
@extend
fn js(ch: Char) { js(string(ch)) }
# Strings
bundle String(ref: Ref)
fn unsafeString(ref: Ref) { pack(String, ref) }
fn String(ref: Ref) {
ref = wasm call("wasm:js-string".cast, ref): ref!
ref = jsref(JSObject(ref).toWellFormed())
return unsafeString(ref)
}
fn String(obj: JSObject) { String(jsref(obj)) }
@extend
fn js(String(r)) { JSObject(r) }
@extend
fn jsref(String(r)) { r }
@extend
fn show(x: String) {
print(String(js.JSON.stringify(x)))
}
bundle StringItr(s: String, i: Int64)
@extend
fn iterate(x: String) { StringItr(x, 0) }
@extend
fn next(&itr: StringItr(s, i)) {
if i >= length(utf16(s)) { return }
idx = widen(bits(Int32(i)))
cp = UInt32(wasm call("wasm:js-string".codePointAt, jsref(s), idx: i32): i32)
ch = Char(cp)
itr = StringItr(s, if cp >= 0x10000 { i + 2 } else { i + 1 })
return Some(ch)
}
@extend
fn length(s: String) { count(s) }
fn (s: String)[i] { nth(s, i) }
@extend
fn concat(a: String, b: String) {
ref = wasm call("wasm:js-string".concat, jsref(a), jsref(b)): ref!
return unsafeString(ref)
}
@extend
fn (a: String) == (b: String) {
r = wasm call("wasm:js-string".equals, jsref(a), jsref(b)): i32
return Bool(r)
}
fn compare(a: String, b: String) {
r = wasm call("wasm:js-string".compare, jsref(a), jsref(b)): i32
return Int64(Int32(r))
}
# Data views
bundle Chars(data: List)
fn chars(s: String) { Chars(collect(s)) }
fn Chars(data)[i] { data[i] }
@extend
fn length(Chars(data)) { length(data) }
@extend
fn iterate(Chars(data)) { iterate(data) }
bundle UTF8View(data: List)
fn utf8(s: String) {
bytes = []
for ch = s {
ch = UInt32(ch)
if ch <= 0x007F {
append(&bytes, UInt8(ch))
} else if ch <= 0x07FF {
append(&bytes, UInt8(0x00C0 | div(ch, 0x0040)))
append(&bytes, UInt8(0x0080 | (ch & 0x003F)))
} else if ch <= 0xFFFF {
append(&bytes, UInt8(0x00E0 | div(ch, 0x1000)))
append(&bytes, UInt8(0x0080 | (div(ch, 0x0040) & 0x003F)))
append(&bytes, UInt8(0x0080 | (ch & 0x003F)))
} else {
append(&bytes, UInt8(0x00F0 | div(ch, 0x40000)))
append(&bytes, UInt8(0x0080 | (div(ch, 0x1000) & 0x003F)))
append(&bytes, UInt8(0x0080 | (div(ch, 0x0040) & 0x003F)))
append(&bytes, UInt8(0x0080 | (ch & 0x003F)))
}
}
return UTF8View(bytes)
}
fn UTF8View(data)[i] { data[i] }
@extend
fn length(UTF8View(data)) { length(data) }
@extend
fn iterate(UTF8View(data)) { iterate(data) }
bundle UTF16View(s: String)
fn utf16(s: String) { UTF16View(s) }
@extend
fn length(UTF16View(s)) {
len = wasm call("wasm:js-string".length, jsref(s)): i32
return Int64(len)
}
fn UTF16View(s)[i] {
i = widen(bits(Int32(i - 1))) # TODO remove widen
ch = wasm call("wasm:js-string".charCodeAt, jsref(s), i: i32): i32
return UInt16(ch)
}
@extend
fn iterate(v: UTF16View) { IndexIterator(v) }
# Tags
@extend
fn string(x: Tag) { tagstring(x) }
@extend
fn show(x: Tag) {
print(`tag"`)
print(string(x))
print(`"`)
}
@extend
fn show(x: Closure) { print("λ(...)") }
@extend
fn show(x: Func) { print("Func(...)") }
# Regex
bundle Regex(r: JSObject)
# TODO flags
fn Regex(r: String) { Regex(js.RegExp(r, "vg")) }
@extend
fn js(Regex(r)) { r }
@extend
fn show(Regex(r)) {
print("r`")
print(r.source)
print("`")
}
fn contains?(s: String, Regex(r)) {
r["lastIndex"] = 0 # JS RegExps have state!
return Bool(r.test(s))
}
bundle Matches(s: String, r: Regex)
fn matches(s: String, r: Regex) { Matches(s, r) }
# TODO lambdas
fn collectStrings(xs) { map(xs, String) }
@extend
fn iterate(Matches(s, r)) {
mapping(js(s).matchAll(r), collectStrings)
}