-
Notifications
You must be signed in to change notification settings - Fork 382
/
string_imported.go
307 lines (267 loc) · 5.89 KB
/
string_imported.go
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
package goja
import (
"hash/maphash"
"io"
"math"
"reflect"
"strings"
"unicode/utf16"
"unicode/utf8"
"github.com/dop251/goja/parser"
"github.com/dop251/goja/unistring"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// Represents a string imported from Go. The idea is to delay the scanning for unicode characters and converting
// to unicodeString until necessary. This way strings that are merely passed through never get scanned which
// saves CPU and memory.
// Currently, importedString is created in 2 cases: Runtime.ToValue() for strings longer than 16 bytes and as a result
// of JSON.stringify() if it may contain unicode characters. More cases could be added in the future.
type importedString struct {
s string
u unicodeString
scanned bool
}
func (i *importedString) scan() {
i.u = unistring.Scan(i.s)
i.scanned = true
}
func (i *importedString) ensureScanned() {
if !i.scanned {
i.scan()
}
}
func (i *importedString) ToInteger() int64 {
i.ensureScanned()
if i.u != nil {
return 0
}
return asciiString(i.s).ToInteger()
}
func (i *importedString) toString() String {
return i
}
func (i *importedString) string() unistring.String {
i.ensureScanned()
if i.u != nil {
return unistring.FromUtf16(i.u)
}
return unistring.String(i.s)
}
func (i *importedString) ToString() Value {
return i
}
func (i *importedString) String() string {
return i.s
}
func (i *importedString) ToFloat() float64 {
i.ensureScanned()
if i.u != nil {
return math.NaN()
}
return asciiString(i.s).ToFloat()
}
func (i *importedString) ToNumber() Value {
i.ensureScanned()
if i.u != nil {
return i.u.ToNumber()
}
return asciiString(i.s).ToNumber()
}
func (i *importedString) ToBoolean() bool {
return len(i.s) != 0
}
func (i *importedString) ToObject(r *Runtime) *Object {
return r._newString(i, r.getStringPrototype())
}
func (i *importedString) SameAs(other Value) bool {
return i.StrictEquals(other)
}
func (i *importedString) Equals(other Value) bool {
if i.StrictEquals(other) {
return true
}
i.ensureScanned()
if i.u != nil {
return i.u.Equals(other)
}
return asciiString(i.s).Equals(other)
}
func (i *importedString) StrictEquals(other Value) bool {
switch otherStr := other.(type) {
case asciiString:
if i.u != nil {
return false
}
return i.s == string(otherStr)
case unicodeString:
i.ensureScanned()
if i.u != nil && i.u.equals(otherStr) {
return true
}
case *importedString:
return i.s == otherStr.s
}
return false
}
func (i *importedString) Export() interface{} {
return i.s
}
func (i *importedString) ExportType() reflect.Type {
return reflectTypeString
}
func (i *importedString) baseObject(r *Runtime) *Object {
i.ensureScanned()
if i.u != nil {
return i.u.baseObject(r)
}
return asciiString(i.s).baseObject(r)
}
func (i *importedString) hash(hasher *maphash.Hash) uint64 {
i.ensureScanned()
if i.u != nil {
return i.u.hash(hasher)
}
return asciiString(i.s).hash(hasher)
}
func (i *importedString) CharAt(idx int) uint16 {
i.ensureScanned()
if i.u != nil {
return i.u.CharAt(idx)
}
return asciiString(i.s).CharAt(idx)
}
func (i *importedString) Length() int {
i.ensureScanned()
if i.u != nil {
return i.u.Length()
}
return asciiString(i.s).Length()
}
func (i *importedString) Concat(v String) String {
if !i.scanned {
if v, ok := v.(*importedString); ok {
if !v.scanned {
return &importedString{s: i.s + v.s}
}
}
i.ensureScanned()
}
if i.u != nil {
return i.u.Concat(v)
}
return asciiString(i.s).Concat(v)
}
func (i *importedString) Substring(start, end int) String {
i.ensureScanned()
if i.u != nil {
return i.u.Substring(start, end)
}
return asciiString(i.s).Substring(start, end)
}
func (i *importedString) CompareTo(v String) int {
return strings.Compare(i.s, v.String())
}
func (i *importedString) Reader() io.RuneReader {
if i.scanned {
if i.u != nil {
return i.u.Reader()
}
return asciiString(i.s).Reader()
}
return strings.NewReader(i.s)
}
type stringUtf16Reader struct {
s string
pos int
second uint16
}
func (s *stringUtf16Reader) readChar() (c uint16, err error) {
if s.second != 0 {
c, s.second = s.second, 0
return
}
if s.pos < len(s.s) {
r1, size1 := utf8.DecodeRuneInString(s.s[s.pos:])
s.pos += size1
if r1 <= 0xFFFF {
c = uint16(r1)
} else {
first, second := utf16.EncodeRune(r1)
c, s.second = uint16(first), uint16(second)
}
} else {
err = io.EOF
}
return
}
func (s *stringUtf16Reader) ReadRune() (r rune, size int, err error) {
c, err := s.readChar()
if err != nil {
return
}
r = rune(c)
size = 1
return
}
func (i *importedString) utf16Reader() utf16Reader {
if i.scanned {
if i.u != nil {
return i.u.utf16Reader()
}
return asciiString(i.s).utf16Reader()
}
return &stringUtf16Reader{
s: i.s,
}
}
func (i *importedString) utf16RuneReader() io.RuneReader {
if i.scanned {
if i.u != nil {
return i.u.utf16RuneReader()
}
return asciiString(i.s).utf16RuneReader()
}
return &stringUtf16Reader{
s: i.s,
}
}
func (i *importedString) utf16Runes() []rune {
i.ensureScanned()
if i.u != nil {
return i.u.utf16Runes()
}
return asciiString(i.s).utf16Runes()
}
func (i *importedString) index(v String, start int) int {
i.ensureScanned()
if i.u != nil {
return i.u.index(v, start)
}
return asciiString(i.s).index(v, start)
}
func (i *importedString) lastIndex(v String, pos int) int {
i.ensureScanned()
if i.u != nil {
return i.u.lastIndex(v, pos)
}
return asciiString(i.s).lastIndex(v, pos)
}
func (i *importedString) toLower() String {
i.ensureScanned()
if i.u != nil {
return toLower(i.s)
}
return asciiString(i.s).toLower()
}
func (i *importedString) toUpper() String {
i.ensureScanned()
if i.u != nil {
caser := cases.Upper(language.Und)
return newStringValue(caser.String(i.s))
}
return asciiString(i.s).toUpper()
}
func (i *importedString) toTrimmedUTF8() string {
return strings.Trim(i.s, parser.WhitespaceChars)
}