helper.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package optdec
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "github.com/bytedance/sonic/internal/native"
  6. "github.com/bytedance/sonic/internal/utils"
  7. "github.com/bytedance/sonic/internal/native/types"
  8. )
  9. func SkipNumberFast(json string, start int) (int, bool) {
  10. // find the number ending, we parsed in native, it always valid
  11. pos := start
  12. for pos < len(json) && json[pos] != ']' && json[pos] != '}' && json[pos] != ',' {
  13. if json[pos] >= '0' && json[pos] <= '9' || json[pos] == '.' || json[pos] == '-' || json[pos] == '+' || json[pos] == 'e' || json[pos] == 'E' {
  14. pos += 1
  15. } else {
  16. break
  17. }
  18. }
  19. // if not found number, return false
  20. if pos == start {
  21. return pos, false
  22. }
  23. return pos, true
  24. }
  25. func isSpace(c byte) bool {
  26. return c == ' ' || c == '\t' || c == '\n' || c == '\r'
  27. }
  28. // pos is the start index of the raw
  29. func ValidNumberFast(raw string) bool {
  30. ret := utils.SkipNumber(raw, 0)
  31. if ret < 0 {
  32. return false
  33. }
  34. // check trailing chars
  35. for ret < len(raw) {
  36. return false
  37. }
  38. return true
  39. }
  40. func SkipOneFast2(json string, pos *int) (int, error) {
  41. // find the number ending, we parsed in sonic-cpp, it always valid
  42. start := native.SkipOneFast(&json, pos)
  43. if start < 0 {
  44. return -1, error_syntax(*pos, json, types.ParsingError(-start).Error())
  45. }
  46. return start, nil
  47. }
  48. func SkipOneFast(json string, pos int) (string, error) {
  49. // find the number ending, we parsed in sonic-cpp, it always valid
  50. start := native.SkipOneFast(&json, &pos)
  51. if start < 0 {
  52. // TODO: details error code
  53. return "", error_syntax(pos, json, types.ParsingError(-start).Error())
  54. }
  55. return json[start:pos], nil
  56. }
  57. func ParseI64(raw string) (int64, error) {
  58. i64, err := strconv.ParseInt(raw, 10, 64)
  59. if err != nil {
  60. return 0, err
  61. }
  62. return i64, nil
  63. }
  64. func ParseBool(raw string) (bool, error) {
  65. var b bool
  66. err := json.Unmarshal([]byte(raw), &b)
  67. if err != nil {
  68. return false, err
  69. }
  70. return b, nil
  71. }
  72. func ParseU64(raw string) (uint64, error) {
  73. u64, err := strconv.ParseUint(raw, 10, 64)
  74. if err != nil {
  75. return 0, err
  76. }
  77. return u64, nil
  78. }
  79. func ParseF64(raw string) (float64, error) {
  80. f64, err := strconv.ParseFloat(raw, 64)
  81. if err != nil {
  82. return 0, err
  83. }
  84. return f64, nil
  85. }
  86. func Unquote(raw string) (string, error) {
  87. var u string
  88. err := json.Unmarshal([]byte(raw), &u)
  89. if err != nil {
  90. return "", err
  91. }
  92. return u, nil
  93. }