primitives.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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/rt`
  24. )
  25. /** Encoder Primitives **/
  26. func encodeNil(rb *[]byte) error {
  27. *rb = append(*rb, 'n', 'u', 'l', 'l')
  28. return nil
  29. }
  30. func encodeString(buf *[]byte, val string) error {
  31. var sidx int
  32. var pbuf *rt.GoSlice
  33. var pstr *rt.GoString
  34. /* opening quote */
  35. *buf = append(*buf, '"')
  36. pbuf = (*rt.GoSlice)(unsafe.Pointer(buf))
  37. pstr = (*rt.GoString)(unsafe.Pointer(&val))
  38. /* encode with native library */
  39. for sidx < pstr.Len {
  40. sn := pstr.Len - sidx
  41. dn := pbuf.Cap - pbuf.Len
  42. sp := padd(pstr.Ptr, sidx)
  43. dp := padd(pbuf.Ptr, pbuf.Len)
  44. nb := native.Quote(sp, sn, dp, &dn, 0)
  45. /* check for errors */
  46. if pbuf.Len += dn; nb >= 0 {
  47. break
  48. }
  49. /* not enough space, grow the slice and try again */
  50. sidx += ^nb
  51. *pbuf = growslice(rt.UnpackType(byteType), *pbuf, pbuf.Cap * 2)
  52. }
  53. /* closing quote */
  54. *buf = append(*buf, '"')
  55. return nil
  56. }
  57. func encodeTypedPointer(buf *[]byte, vt *rt.GoType, vp *unsafe.Pointer, sb *_Stack, fv uint64) error {
  58. if vt == nil {
  59. return encodeNil(buf)
  60. } else if fn, err := findOrCompile(vt, (fv&(1<<bitPointerValue)) != 0); err != nil {
  61. return err
  62. } else if vt.Indirect() {
  63. rt.MoreStack(_FP_size + native.MaxFrameSize)
  64. rt.StopProf()
  65. err := fn(buf, *vp, sb, fv)
  66. rt.StartProf()
  67. return err
  68. } else {
  69. rt.MoreStack(_FP_size + native.MaxFrameSize)
  70. rt.StopProf()
  71. err := fn(buf, unsafe.Pointer(vp), sb, fv)
  72. rt.StartProf()
  73. return err
  74. }
  75. }
  76. func encodeJsonMarshaler(buf *[]byte, val json.Marshaler, opt Options) error {
  77. if ret, err := val.MarshalJSON(); err != nil {
  78. return err
  79. } else {
  80. if opt & CompactMarshaler != 0 {
  81. return compact(buf, ret)
  82. }
  83. if opt & NoValidateJSONMarshaler == 0 {
  84. if ok, s := Valid(ret); !ok {
  85. return error_marshaler(ret, s)
  86. }
  87. }
  88. *buf = append(*buf, ret...)
  89. return nil
  90. }
  91. }
  92. func encodeTextMarshaler(buf *[]byte, val encoding.TextMarshaler, opt Options) error {
  93. if ret, err := val.MarshalText(); err != nil {
  94. return err
  95. } else {
  96. if opt & NoQuoteTextMarshaler != 0 {
  97. *buf = append(*buf, ret...)
  98. return nil
  99. }
  100. return encodeString(buf, rt.Mem2Str(ret) )
  101. }
  102. }
  103. func htmlEscape(dst []byte, src []byte) []byte {
  104. var sidx int
  105. dst = append(dst, src[:0]...) // avoid check nil dst
  106. sbuf := (*rt.GoSlice)(unsafe.Pointer(&src))
  107. dbuf := (*rt.GoSlice)(unsafe.Pointer(&dst))
  108. /* grow dst if it is shorter */
  109. if cap(dst) - len(dst) < len(src) + native.BufPaddingSize {
  110. cap := len(src) * 3 / 2 + native.BufPaddingSize
  111. *dbuf = growslice(typeByte, *dbuf, cap)
  112. }
  113. for sidx < sbuf.Len {
  114. sp := padd(sbuf.Ptr, sidx)
  115. dp := padd(dbuf.Ptr, dbuf.Len)
  116. sn := sbuf.Len - sidx
  117. dn := dbuf.Cap - dbuf.Len
  118. nb := native.HTMLEscape(sp, sn, dp, &dn)
  119. /* check for errors */
  120. if dbuf.Len += dn; nb >= 0 {
  121. break
  122. }
  123. /* not enough space, grow the slice and try again */
  124. sidx += ^nb
  125. *dbuf = growslice(typeByte, *dbuf, dbuf.Cap * 2)
  126. }
  127. return dst
  128. }
  129. var (
  130. argPtrs = []bool { true, true, true, false }
  131. localPtrs = []bool{}
  132. )
  133. var (
  134. _F_assertI2I = jit.Func(assertI2I)
  135. )
  136. func asText(v unsafe.Pointer) (string, error) {
  137. text := assertI2I(_T_encoding_TextMarshaler, *(*rt.GoIface)(v))
  138. r, e := (*(*encoding.TextMarshaler)(unsafe.Pointer(&text))).MarshalText()
  139. return rt.Mem2Str(r), e
  140. }
  141. func asJson(v unsafe.Pointer) (string, error) {
  142. text := assertI2I(_T_json_Marshaler, *(*rt.GoIface)(v))
  143. r, e := (*(*json.Marshaler)(unsafe.Pointer(&text))).MarshalJSON()
  144. return rt.Mem2Str(r), e
  145. }