primitives.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2021 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package encoder
  17. import (
  18. `encoding`
  19. `encoding/json`
  20. `unsafe`
  21. `github.com/bytedance/sonic/internal/jit`
  22. `github.com/bytedance/sonic/internal/native`
  23. `github.com/bytedance/sonic/internal/native/types`
  24. `github.com/bytedance/sonic/internal/rt`
  25. )
  26. /** Encoder Primitives **/
  27. func encodeNil(rb *[]byte) error {
  28. *rb = append(*rb, 'n', 'u', 'l', 'l')
  29. return nil
  30. }
  31. func encodeString(buf *[]byte, val string) error {
  32. var sidx int
  33. var pbuf *rt.GoSlice
  34. var pstr *rt.GoString
  35. /* opening quote */
  36. *buf = append(*buf, '"')
  37. pbuf = (*rt.GoSlice)(unsafe.Pointer(buf))
  38. pstr = (*rt.GoString)(unsafe.Pointer(&val))
  39. /* encode with native library */
  40. for sidx < pstr.Len {
  41. sn := pstr.Len - sidx
  42. dn := pbuf.Cap - pbuf.Len
  43. sp := padd(pstr.Ptr, sidx)
  44. dp := padd(pbuf.Ptr, pbuf.Len)
  45. nb := native.Quote(sp, sn, dp, &dn, 0)
  46. /* check for errors */
  47. if pbuf.Len += dn; nb >= 0 {
  48. break
  49. }
  50. /* not enough space, grow the slice and try again */
  51. sidx += ^nb
  52. *pbuf = growslice(rt.UnpackType(byteType), *pbuf, pbuf.Cap * 2)
  53. }
  54. /* closing quote */
  55. *buf = append(*buf, '"')
  56. return nil
  57. }
  58. func encodeTypedPointer(buf *[]byte, vt *rt.GoType, vp *unsafe.Pointer, sb *_Stack, fv uint64) error {
  59. if vt == nil {
  60. return encodeNil(buf)
  61. } else if fn, err := findOrCompile(vt, (fv&(1<<bitPointerValue)) != 0); err != nil {
  62. return err
  63. } else if vt.Indirect() {
  64. rt.MoreStack(_FP_size + native.MaxFrameSize)
  65. err := fn(buf, *vp, sb, fv)
  66. return err
  67. } else {
  68. rt.MoreStack(_FP_size + native.MaxFrameSize)
  69. err := fn(buf, unsafe.Pointer(vp), sb, fv)
  70. return err
  71. }
  72. }
  73. func encodeJsonMarshaler(buf *[]byte, val json.Marshaler, opt Options) error {
  74. if ret, err := val.MarshalJSON(); err != nil {
  75. return err
  76. } else {
  77. if opt & CompactMarshaler != 0 {
  78. return compact(buf, ret)
  79. }
  80. if opt & NoValidateJSONMarshaler == 0 {
  81. if ok, s := Valid(ret); !ok {
  82. return error_marshaler(ret, s)
  83. }
  84. }
  85. *buf = append(*buf, ret...)
  86. return nil
  87. }
  88. }
  89. func encodeTextMarshaler(buf *[]byte, val encoding.TextMarshaler, opt Options) error {
  90. if ret, err := val.MarshalText(); err != nil {
  91. return err
  92. } else {
  93. if opt & NoQuoteTextMarshaler != 0 {
  94. *buf = append(*buf, ret...)
  95. return nil
  96. }
  97. return encodeString(buf, rt.Mem2Str(ret) )
  98. }
  99. }
  100. func htmlEscape(dst []byte, src []byte) []byte {
  101. var sidx int
  102. dst = append(dst, src[:0]...) // avoid check nil dst
  103. sbuf := (*rt.GoSlice)(unsafe.Pointer(&src))
  104. dbuf := (*rt.GoSlice)(unsafe.Pointer(&dst))
  105. /* grow dst if it is shorter */
  106. if cap(dst) - len(dst) < len(src) + types.BufPaddingSize {
  107. cap := len(src) * 3 / 2 + types.BufPaddingSize
  108. *dbuf = growslice(typeByte, *dbuf, cap)
  109. }
  110. for sidx < sbuf.Len {
  111. sp := padd(sbuf.Ptr, sidx)
  112. dp := padd(dbuf.Ptr, dbuf.Len)
  113. sn := sbuf.Len - sidx
  114. dn := dbuf.Cap - dbuf.Len
  115. nb := native.HTMLEscape(sp, sn, dp, &dn)
  116. /* check for errors */
  117. if dbuf.Len += dn; nb >= 0 {
  118. break
  119. }
  120. /* not enough space, grow the slice and try again */
  121. sidx += ^nb
  122. *dbuf = growslice(typeByte, *dbuf, dbuf.Cap * 2)
  123. }
  124. return dst
  125. }
  126. var (
  127. argPtrs = []bool { true, true, true, false }
  128. localPtrs = []bool{}
  129. )
  130. var (
  131. _F_assertI2I = jit.Func(rt.AssertI2I2)
  132. )
  133. func asText(v unsafe.Pointer) (string, error) {
  134. text := rt.AssertI2I2(_T_encoding_TextMarshaler, *(*rt.GoIface)(v))
  135. r, e := (*(*encoding.TextMarshaler)(unsafe.Pointer(&text))).MarshalText()
  136. return rt.Mem2Str(r), e
  137. }
  138. func asJson(v unsafe.Pointer) (string, error) {
  139. text := rt.AssertI2I2(_T_json_Marshaler, *(*rt.GoIface)(v))
  140. r, e := (*(*json.Marshaler)(unsafe.Pointer(&text))).MarshalJSON()
  141. return rt.Mem2Str(r), e
  142. }