-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_ascii.go
401 lines (348 loc) · 7.51 KB
/
string_ascii.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package sobek
import (
"hash/maphash"
"io"
"math"
"math/big"
"reflect"
"strconv"
"strings"
"github.com/grafana/sobek/unistring"
)
type asciiString string
type asciiRuneReader struct {
s asciiString
pos int
}
func (rr *asciiRuneReader) ReadRune() (r rune, size int, err error) {
if rr.pos < len(rr.s) {
r = rune(rr.s[rr.pos])
size = 1
rr.pos++
} else {
err = io.EOF
}
return
}
type asciiUtf16Reader struct {
s asciiString
pos int
}
func (rr *asciiUtf16Reader) readChar() (c uint16, err error) {
if rr.pos < len(rr.s) {
c = uint16(rr.s[rr.pos])
rr.pos++
} else {
err = io.EOF
}
return
}
func (rr *asciiUtf16Reader) ReadRune() (r rune, size int, err error) {
if rr.pos < len(rr.s) {
r = rune(rr.s[rr.pos])
rr.pos++
size = 1
} else {
err = io.EOF
}
return
}
func (s asciiString) Reader() io.RuneReader {
return &asciiRuneReader{
s: s,
}
}
func (s asciiString) utf16Reader() utf16Reader {
return &asciiUtf16Reader{
s: s,
}
}
func (s asciiString) utf16RuneReader() io.RuneReader {
return &asciiUtf16Reader{
s: s,
}
}
func (s asciiString) utf16Runes() []rune {
runes := make([]rune, len(s))
for i := 0; i < len(s); i++ {
runes[i] = rune(s[i])
}
return runes
}
// ss must be trimmed
func stringToInt(ss string) (int64, error) {
if ss == "" {
return 0, nil
}
if ss == "-0" {
return 0, strconv.ErrSyntax
}
if len(ss) > 2 {
switch ss[:2] {
case "0x", "0X":
return strconv.ParseInt(ss[2:], 16, 64)
case "0b", "0B":
return strconv.ParseInt(ss[2:], 2, 64)
case "0o", "0O":
return strconv.ParseInt(ss[2:], 8, 64)
}
}
return strconv.ParseInt(ss, 10, 64)
}
func (s asciiString) _toInt(trimmed string) (int64, error) {
return stringToInt(trimmed)
}
func isRangeErr(err error) bool {
if err, ok := err.(*strconv.NumError); ok {
return err.Err == strconv.ErrRange
}
return false
}
func (s asciiString) _toFloat(trimmed string) (float64, error) {
if trimmed == "" {
return 0, nil
}
if trimmed == "-0" {
var f float64
return -f, nil
}
// Go allows underscores in numbers, when parsed as floats, but ECMAScript expect them to be interpreted as NaN.
if strings.ContainsRune(trimmed, '_') {
return 0, strconv.ErrSyntax
}
// Hexadecimal floats are not supported by ECMAScript.
if len(trimmed) >= 2 {
var prefix string
if trimmed[0] == '-' || trimmed[0] == '+' {
prefix = trimmed[1:]
} else {
prefix = trimmed
}
if len(prefix) >= 2 && prefix[0] == '0' && (prefix[1] == 'x' || prefix[1] == 'X') {
return 0, strconv.ErrSyntax
}
}
f, err := strconv.ParseFloat(trimmed, 64)
if err == nil && math.IsInf(f, 0) {
ss := strings.ToLower(trimmed)
if strings.HasPrefix(ss, "inf") || strings.HasPrefix(ss, "-inf") || strings.HasPrefix(ss, "+inf") {
// We handle "Infinity" separately, prevent from being parsed as Infinity due to strconv.ParseFloat() permissive syntax
return 0, strconv.ErrSyntax
}
}
if isRangeErr(err) {
err = nil
}
return f, err
}
func (s asciiString) ToInteger() int64 {
ss := strings.TrimSpace(string(s))
if ss == "" {
return 0
}
if ss == "Infinity" || ss == "+Infinity" {
return math.MaxInt64
}
if ss == "-Infinity" {
return math.MinInt64
}
i, err := s._toInt(ss)
if err != nil {
f, err := s._toFloat(ss)
if err == nil {
return int64(f)
}
}
return i
}
func (s asciiString) toString() String {
return s
}
func (s asciiString) ToString() Value {
return s
}
func (s asciiString) String() string {
return string(s)
}
func (s asciiString) ToFloat() float64 {
ss := strings.TrimSpace(string(s))
if ss == "" {
return 0
}
if ss == "Infinity" || ss == "+Infinity" {
return math.Inf(1)
}
if ss == "-Infinity" {
return math.Inf(-1)
}
f, err := s._toFloat(ss)
if err != nil {
i, err := s._toInt(ss)
if err == nil {
return float64(i)
}
f = math.NaN()
}
return f
}
func (s asciiString) ToBoolean() bool {
return s != ""
}
func (s asciiString) ToNumber() Value {
ss := strings.TrimSpace(string(s))
if ss == "" {
return intToValue(0)
}
if ss == "Infinity" || ss == "+Infinity" {
return _positiveInf
}
if ss == "-Infinity" {
return _negativeInf
}
if i, err := s._toInt(ss); err == nil {
return intToValue(i)
}
if f, err := s._toFloat(ss); err == nil {
return floatToValue(f)
}
return _NaN
}
func (s asciiString) ToObject(r *Runtime) *Object {
return r._newString(s, r.getStringPrototype())
}
func (s asciiString) SameAs(other Value) bool {
return s.StrictEquals(other)
}
func (s asciiString) Equals(other Value) bool {
if s.StrictEquals(other) {
return true
}
if o, ok := other.(valueInt); ok {
if o1, e := s._toInt(strings.TrimSpace(string(s))); e == nil {
return o1 == int64(o)
}
return false
}
if o, ok := other.(valueFloat); ok {
return s.ToFloat() == float64(o)
}
if o, ok := other.(valueBool); ok {
if o1, e := s._toFloat(strings.TrimSpace(string(s))); e == nil {
return o1 == o.ToFloat()
}
return false
}
if o, ok := other.(*valueBigInt); ok {
bigInt, err := stringToBigInt(s.toTrimmedUTF8())
if err != nil {
return false
}
return bigInt.Cmp((*big.Int)(o)) == 0
}
if o, ok := other.(*Object); ok {
return s.Equals(o.toPrimitive())
}
return false
}
func (s asciiString) StrictEquals(other Value) bool {
if otherStr, ok := other.(asciiString); ok {
return s == otherStr
}
if otherStr, ok := other.(*importedString); ok {
if otherStr.u == nil {
return string(s) == otherStr.s
}
}
return false
}
func (s asciiString) baseObject(r *Runtime) *Object {
ss := r.getStringSingleton()
ss.value = s
ss.setLength()
return ss.val
}
func (s asciiString) hash(hash *maphash.Hash) uint64 {
_, _ = hash.WriteString(string(s))
h := hash.Sum64()
hash.Reset()
return h
}
func (s asciiString) CharAt(idx int) uint16 {
return uint16(s[idx])
}
func (s asciiString) Length() int {
return len(s)
}
func (s asciiString) Concat(other String) String {
a, u := devirtualizeString(other)
if u != nil {
b := make([]uint16, len(s)+len(u))
b[0] = unistring.BOM
for i := 0; i < len(s); i++ {
b[i+1] = uint16(s[i])
}
copy(b[len(s)+1:], u[1:])
return unicodeString(b)
}
return s + a
}
func (s asciiString) Substring(start, end int) String {
return s[start:end]
}
func (s asciiString) CompareTo(other String) int {
switch other := other.(type) {
case asciiString:
return strings.Compare(string(s), string(other))
case unicodeString:
return strings.Compare(string(s), other.String())
case *importedString:
return strings.Compare(string(s), other.s)
default:
panic(newTypeError("Internal bug: unknown string type: %T", other))
}
}
func (s asciiString) index(substr String, start int) int {
a, u := devirtualizeString(substr)
if u == nil {
if start > len(s) {
return -1
}
p := strings.Index(string(s[start:]), string(a))
if p >= 0 {
return p + start
}
}
return -1
}
func (s asciiString) lastIndex(substr String, pos int) int {
a, u := devirtualizeString(substr)
if u == nil {
end := pos + len(a)
var ss string
if end > len(s) {
ss = string(s)
} else {
ss = string(s[:end])
}
return strings.LastIndex(ss, string(a))
}
return -1
}
func (s asciiString) toLower() String {
return asciiString(strings.ToLower(string(s)))
}
func (s asciiString) toUpper() String {
return asciiString(strings.ToUpper(string(s)))
}
func (s asciiString) toTrimmedUTF8() string {
return strings.TrimSpace(string(s))
}
func (s asciiString) string() unistring.String {
return unistring.String(s)
}
func (s asciiString) Export() interface{} {
return string(s)
}
func (s asciiString) ExportType() reflect.Type {
return reflectTypeString
}