Skip to content

Commit d2f9e15

Browse files
committed
Merge branch 'pendo-io-parse-prim-val-fastbytes'
2 parents 24fd329 + e2a6695 commit d2f9e15

File tree

2 files changed

+125
-94
lines changed

2 files changed

+125
-94
lines changed

parser.go

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,7 @@ func GetString(data []byte, keys ...string) (val string, err error) {
394394
return string(v), nil
395395
}
396396

397-
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings
398-
out, err := Unescape(v, stackbuf[:])
399-
400-
return string(out), err
397+
return ParseString(v)
401398
}
402399

403400
// GetFloat returns the value retrieved by `Get`, cast to a float64 if possible.
@@ -414,8 +411,7 @@ func GetFloat(data []byte, keys ...string) (val float64, err error) {
414411
return 0, fmt.Errorf("Value is not a number: %s", string(v))
415412
}
416413

417-
val, err = parseFloat(&v)
418-
return
414+
return ParseFloat(v)
419415
}
420416

421417
// GetInt returns the value retrieved by `Get`, cast to a float64 if possible.
@@ -431,11 +427,7 @@ func GetInt(data []byte, keys ...string) (val int64, err error) {
431427
return 0, fmt.Errorf("Value is not a number: %s", string(v))
432428
}
433429

434-
if val, ok := parseInt(v); !ok {
435-
return 0, MalformedValueError
436-
} else {
437-
return val, nil
438-
}
430+
return ParseInt(v)
439431
}
440432

441433
// GetBoolean returns the value retrieved by `Get`, cast to a bool if possible.
@@ -452,11 +444,50 @@ func GetBoolean(data []byte, keys ...string) (val bool, err error) {
452444
return false, fmt.Errorf("Value is not a boolean: %s", string(v))
453445
}
454446

455-
if v[0] == 't' {
456-
val = true
447+
return ParseBoolean(v)
448+
}
449+
450+
// ParseBoolean parses a Boolean ValueType into a Go bool (not particularly useful, but here for completeness)
451+
func ParseBoolean(b []byte) (bool, error) {
452+
switch b[0] {
453+
case 't':
454+
return true, nil
455+
case 'f':
456+
return false, nil
457+
default:
458+
return false, MalformedValueError
459+
}
460+
}
461+
462+
// ParseString parses a String ValueType into a Go []byte (the main parsing work is unescaping the JSON string)
463+
func parseStringAsBytes(b []byte) ([]byte, error) {
464+
var stackbuf [unescapeStackBufSize]byte // stack-allocated array for allocation-free unescaping of small strings (hopefully; the Go compiler might just always kick stackbuf[:] into the heap)
465+
return Unescape(b, stackbuf[:])
466+
}
467+
468+
// ParseString parses a String ValueType into a Go string (the main parsing work is unescaping the JSON string)
469+
func ParseString(b []byte) (string, error) {
470+
if bU, err := parseStringAsBytes(b); err != nil {
471+
return "", nil
457472
} else {
458-
val = false
473+
return string(bU), nil
459474
}
475+
}
460476

461-
return
477+
// ParseNumber parses a Number ValueType into a Go float64
478+
func ParseFloat(b []byte) (float64, error) {
479+
if v, err := parseFloat(&b); err != nil {
480+
return 0, MalformedValueError
481+
} else {
482+
return v, nil
483+
}
462484
}
485+
486+
// ParseNumber parses a Number ValueType into a Go float64
487+
func ParseInt(b []byte) (int64, error) {
488+
if v, err := parseInt(b); !err {
489+
return 0, MalformedValueError
490+
} else {
491+
return v, nil
492+
}
493+
}

0 commit comments

Comments
 (0)