skip.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package utils
  2. import (
  3. `runtime`
  4. `unsafe`
  5. `github.com/bytedance/sonic/internal/native/types`
  6. `github.com/bytedance/sonic/internal/rt`
  7. )
  8. func isDigit(c byte) bool {
  9. return c >= '0' && c <= '9'
  10. }
  11. //go:nocheckptr
  12. func SkipNumber(src string, pos int) (ret int) {
  13. sp := uintptr(rt.IndexChar(src, pos))
  14. se := uintptr(rt.IndexChar(src, len(src)))
  15. if uintptr(sp) >= se {
  16. return -int(types.ERR_EOF)
  17. }
  18. if c := *(*byte)(unsafe.Pointer(sp)); c == '-' {
  19. sp += 1
  20. }
  21. ss := sp
  22. var pointer bool
  23. var exponent bool
  24. var lastIsDigit bool
  25. var nextNeedDigit = true
  26. for ; sp < se; sp += uintptr(1) {
  27. c := *(*byte)(unsafe.Pointer(sp))
  28. if isDigit(c) {
  29. lastIsDigit = true
  30. nextNeedDigit = false
  31. continue
  32. } else if nextNeedDigit {
  33. return -int(types.ERR_INVALID_CHAR)
  34. } else if c == '.' {
  35. if !lastIsDigit || pointer || exponent || sp == ss {
  36. return -int(types.ERR_INVALID_CHAR)
  37. }
  38. pointer = true
  39. lastIsDigit = false
  40. nextNeedDigit = true
  41. continue
  42. } else if c == 'e' || c == 'E' {
  43. if !lastIsDigit || exponent {
  44. return -int(types.ERR_INVALID_CHAR)
  45. }
  46. if sp == se-1 {
  47. return -int(types.ERR_EOF)
  48. }
  49. exponent = true
  50. lastIsDigit = false
  51. nextNeedDigit = false
  52. continue
  53. } else if c == '-' || c == '+' {
  54. if prev := *(*byte)(unsafe.Pointer(sp - 1)); prev != 'e' && prev != 'E' {
  55. return -int(types.ERR_INVALID_CHAR)
  56. }
  57. lastIsDigit = false
  58. nextNeedDigit = true
  59. continue
  60. } else {
  61. break
  62. }
  63. }
  64. if nextNeedDigit {
  65. return -int(types.ERR_EOF)
  66. }
  67. runtime.KeepAlive(src)
  68. return int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr))
  69. }