float.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package decoder
  2. import (
  3. "strconv"
  4. "unsafe"
  5. "github.com/goccy/go-json/internal/errors"
  6. )
  7. type floatDecoder struct {
  8. op func(unsafe.Pointer, float64)
  9. structName string
  10. fieldName string
  11. }
  12. func newFloatDecoder(structName, fieldName string, op func(unsafe.Pointer, float64)) *floatDecoder {
  13. return &floatDecoder{op: op, structName: structName, fieldName: fieldName}
  14. }
  15. var (
  16. floatTable = [256]bool{
  17. '0': true,
  18. '1': true,
  19. '2': true,
  20. '3': true,
  21. '4': true,
  22. '5': true,
  23. '6': true,
  24. '7': true,
  25. '8': true,
  26. '9': true,
  27. '.': true,
  28. 'e': true,
  29. 'E': true,
  30. '+': true,
  31. '-': true,
  32. }
  33. validEndNumberChar = [256]bool{
  34. nul: true,
  35. ' ': true,
  36. '\t': true,
  37. '\r': true,
  38. '\n': true,
  39. ',': true,
  40. ':': true,
  41. '}': true,
  42. ']': true,
  43. }
  44. )
  45. func floatBytes(s *Stream) []byte {
  46. start := s.cursor
  47. for {
  48. s.cursor++
  49. if floatTable[s.char()] {
  50. continue
  51. } else if s.char() == nul {
  52. if s.read() {
  53. s.cursor-- // for retry current character
  54. continue
  55. }
  56. }
  57. break
  58. }
  59. return s.buf[start:s.cursor]
  60. }
  61. func (d *floatDecoder) decodeStreamByte(s *Stream) ([]byte, error) {
  62. for {
  63. switch s.char() {
  64. case ' ', '\n', '\t', '\r':
  65. s.cursor++
  66. continue
  67. case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  68. return floatBytes(s), nil
  69. case 'n':
  70. if err := nullBytes(s); err != nil {
  71. return nil, err
  72. }
  73. return nil, nil
  74. case nul:
  75. if s.read() {
  76. continue
  77. }
  78. goto ERROR
  79. default:
  80. goto ERROR
  81. }
  82. }
  83. ERROR:
  84. return nil, errors.ErrUnexpectedEndOfJSON("float", s.totalOffset())
  85. }
  86. func (d *floatDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
  87. for {
  88. switch buf[cursor] {
  89. case ' ', '\n', '\t', '\r':
  90. cursor++
  91. continue
  92. case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  93. start := cursor
  94. cursor++
  95. for floatTable[buf[cursor]] {
  96. cursor++
  97. }
  98. num := buf[start:cursor]
  99. return num, cursor, nil
  100. case 'n':
  101. if err := validateNull(buf, cursor); err != nil {
  102. return nil, 0, err
  103. }
  104. cursor += 4
  105. return nil, cursor, nil
  106. default:
  107. return nil, 0, errors.ErrUnexpectedEndOfJSON("float", cursor)
  108. }
  109. }
  110. }
  111. func (d *floatDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) error {
  112. bytes, err := d.decodeStreamByte(s)
  113. if err != nil {
  114. return err
  115. }
  116. if bytes == nil {
  117. return nil
  118. }
  119. str := *(*string)(unsafe.Pointer(&bytes))
  120. f64, err := strconv.ParseFloat(str, 64)
  121. if err != nil {
  122. return errors.ErrSyntax(err.Error(), s.totalOffset())
  123. }
  124. d.op(p, f64)
  125. return nil
  126. }
  127. func (d *floatDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe.Pointer) (int64, error) {
  128. buf := ctx.Buf
  129. bytes, c, err := d.decodeByte(buf, cursor)
  130. if err != nil {
  131. return 0, err
  132. }
  133. if bytes == nil {
  134. return c, nil
  135. }
  136. cursor = c
  137. if !validEndNumberChar[buf[cursor]] {
  138. return 0, errors.ErrUnexpectedEndOfJSON("float", cursor)
  139. }
  140. s := *(*string)(unsafe.Pointer(&bytes))
  141. f64, err := strconv.ParseFloat(s, 64)
  142. if err != nil {
  143. return 0, errors.ErrSyntax(err.Error(), cursor)
  144. }
  145. d.op(p, f64)
  146. return cursor, nil
  147. }