stringopts.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package optdec
  2. import (
  3. "encoding/json"
  4. "math"
  5. "unsafe"
  6. "github.com/bytedance/sonic/internal/rt"
  7. )
  8. type ptrStrDecoder struct {
  9. typ *rt.GoType
  10. deref decFunc
  11. }
  12. // Pointer Value is allocated in the Caller
  13. func (d *ptrStrDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  14. if node.IsNull() {
  15. *(*unsafe.Pointer)(vp) = nil
  16. return nil
  17. }
  18. s, ok := node.AsStrRef(ctx)
  19. if !ok {
  20. return error_mismatch(node, ctx, stringType)
  21. }
  22. if s == "null" {
  23. *(*unsafe.Pointer)(vp) = nil
  24. return nil
  25. }
  26. if *(*unsafe.Pointer)(vp) == nil {
  27. *(*unsafe.Pointer)(vp) = rt.Mallocgc(d.typ.Size, d.typ, true)
  28. }
  29. return d.deref.FromDom(*(*unsafe.Pointer)(vp), node, ctx)
  30. }
  31. type boolStringDecoder struct {
  32. }
  33. func (d *boolStringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  34. if node.IsNull() {
  35. return nil
  36. }
  37. s, ok := node.AsStrRef(ctx)
  38. if !ok {
  39. return error_mismatch(node, ctx, stringType)
  40. }
  41. if s == "null" {
  42. return nil
  43. }
  44. b, err := ParseBool(s)
  45. if err != nil {
  46. return error_mismatch(node, ctx, boolType)
  47. }
  48. *(*bool)(vp) = b
  49. return nil
  50. }
  51. func parseI64(node Node, ctx *context) (int64, error, bool) {
  52. if node.IsNull() {
  53. return 0, nil, true
  54. }
  55. s, ok := node.AsStrRef(ctx)
  56. if !ok {
  57. return 0, error_mismatch(node, ctx, stringType), false
  58. }
  59. if s == "null" {
  60. return 0, nil, true
  61. }
  62. ret, err := ParseI64(s)
  63. return ret, err, false
  64. }
  65. type i8StringDecoder struct{}
  66. func (d *i8StringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  67. ret, err, null := parseI64(node, ctx)
  68. if null {
  69. return nil
  70. }
  71. if err != nil {
  72. return err
  73. }
  74. if ret > math.MaxInt8 || ret < math.MinInt8 {
  75. return error_mismatch(node, ctx, int8Type)
  76. }
  77. *(*int8)(vp) = int8(ret)
  78. return nil
  79. }
  80. type i16StringDecoder struct{}
  81. func (d *i16StringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  82. ret, err, null := parseI64(node, ctx)
  83. if null {
  84. return nil
  85. }
  86. if err != nil {
  87. return err
  88. }
  89. if ret > math.MaxInt16 || ret < math.MinInt16 {
  90. return error_mismatch(node, ctx, int16Type)
  91. }
  92. *(*int16)(vp) = int16(ret)
  93. return nil
  94. }
  95. type i32StringDecoder struct{}
  96. func (d *i32StringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  97. ret, err, null := parseI64(node, ctx)
  98. if null {
  99. return nil
  100. }
  101. if err != nil {
  102. return err
  103. }
  104. if ret > math.MaxInt32 || ret < math.MinInt32 {
  105. return error_mismatch(node, ctx, int32Type)
  106. }
  107. *(*int32)(vp) = int32(ret)
  108. return nil
  109. }
  110. type i64StringDecoder struct{}
  111. func (d *i64StringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  112. ret, err, null := parseI64(node, ctx)
  113. if null {
  114. return nil
  115. }
  116. if err != nil {
  117. return err
  118. }
  119. *(*int64)(vp) = int64(ret)
  120. return nil
  121. }
  122. func parseU64(node Node, ctx *context) (uint64, error, bool) {
  123. if node.IsNull() {
  124. return 0, nil, true
  125. }
  126. s, ok := node.AsStrRef(ctx)
  127. if !ok {
  128. return 0, error_mismatch(node, ctx, stringType), false
  129. }
  130. if s == "null" {
  131. return 0, nil, true
  132. }
  133. ret, err := ParseU64(s)
  134. return ret, err, false
  135. }
  136. type u8StringDecoder struct{}
  137. func (d *u8StringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  138. ret, err, null := parseU64(node, ctx)
  139. if null {
  140. return nil
  141. }
  142. if err != nil {
  143. return err
  144. }
  145. if ret > math.MaxUint8 {
  146. return error_mismatch(node, ctx, uint8Type)
  147. }
  148. *(*uint8)(vp) = uint8(ret)
  149. return nil
  150. }
  151. type u16StringDecoder struct{}
  152. func (d *u16StringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  153. ret, err, null := parseU64(node, ctx)
  154. if null {
  155. return nil
  156. }
  157. if err != nil {
  158. return err
  159. }
  160. if ret > math.MaxUint16 {
  161. return error_mismatch(node, ctx, uint16Type)
  162. }
  163. *(*uint16)(vp) = uint16(ret)
  164. return nil
  165. }
  166. type u32StringDecoder struct{}
  167. func (d *u32StringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  168. ret, err, null := parseU64(node, ctx)
  169. if null {
  170. return nil
  171. }
  172. if err != nil {
  173. return err
  174. }
  175. if ret > math.MaxUint32 {
  176. return error_mismatch(node, ctx, uint32Type)
  177. }
  178. *(*uint32)(vp) = uint32(ret)
  179. return nil
  180. }
  181. type u64StringDecoder struct{}
  182. func (d *u64StringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  183. ret, err, null := parseU64(node, ctx)
  184. if null {
  185. return nil
  186. }
  187. if err != nil {
  188. return err
  189. }
  190. *(*uint64)(vp) = uint64(ret)
  191. return nil
  192. }
  193. type f32StringDecoder struct{}
  194. func (d *f32StringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  195. if node.IsNull() {
  196. return nil
  197. }
  198. s, ok := node.AsStrRef(ctx)
  199. if !ok {
  200. return error_mismatch(node, ctx, stringType)
  201. }
  202. if s == "null" {
  203. return nil
  204. }
  205. ret, err := ParseF64(s)
  206. if err != nil || ret > math.MaxFloat32 || ret < -math.MaxFloat32 {
  207. return error_mismatch(node, ctx, float32Type)
  208. }
  209. *(*float32)(vp) = float32(ret)
  210. return nil
  211. }
  212. type f64StringDecoder struct{}
  213. func (d *f64StringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  214. if node.IsNull() {
  215. return nil
  216. }
  217. s, ok := node.AsStrRef(ctx)
  218. if !ok {
  219. return error_mismatch(node, ctx, stringType)
  220. }
  221. if s == "null" {
  222. return nil
  223. }
  224. ret, err := ParseF64(s)
  225. if err != nil {
  226. return error_mismatch(node, ctx, float64Type)
  227. }
  228. *(*float64)(vp) = float64(ret)
  229. return nil
  230. }
  231. /* parse string field with string options */
  232. type strStringDecoder struct{}
  233. func (d *strStringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  234. if node.IsNull() {
  235. return nil
  236. }
  237. s, ok := node.AsStrRef(ctx)
  238. if !ok {
  239. return error_mismatch(node, ctx, stringType)
  240. }
  241. if s == "null" {
  242. return nil
  243. }
  244. s, err := Unquote(s)
  245. if err != nil {
  246. return error_mismatch(node, ctx, stringType)
  247. }
  248. *(*string)(vp) = s
  249. return nil
  250. }
  251. type numberStringDecoder struct{}
  252. func (d *numberStringDecoder) FromDom(vp unsafe.Pointer, node Node, ctx *context) error {
  253. if node.IsNull() {
  254. return nil
  255. }
  256. s, ok := node.AsStrRef(ctx)
  257. if !ok {
  258. return error_mismatch(node, ctx, stringType)
  259. }
  260. if s == "null" {
  261. return nil
  262. }
  263. num, ok := node.ParseNumber(ctx)
  264. if !ok {
  265. return error_mismatch(node, ctx, jsonNumberType)
  266. }
  267. end, ok := SkipNumberFast(s, 0)
  268. // has error or trailing chars
  269. if !ok || end != len(s) {
  270. return error_mismatch(node, ctx, jsonNumberType)
  271. }
  272. *(*json.Number)(vp) = json.Number(num)
  273. return nil
  274. }