diff options
Diffstat (limited to 'vendor/github.com/bytedance/sonic/encoder/encoder_compat.go')
-rw-r--r-- | vendor/github.com/bytedance/sonic/encoder/encoder_compat.go | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/vendor/github.com/bytedance/sonic/encoder/encoder_compat.go b/vendor/github.com/bytedance/sonic/encoder/encoder_compat.go index 320dd9b5c..38761c453 100644 --- a/vendor/github.com/bytedance/sonic/encoder/encoder_compat.go +++ b/vendor/github.com/bytedance/sonic/encoder/encoder_compat.go @@ -28,9 +28,12 @@ import ( ) func init() { - println("WARNING: sonic only supports Go1.16~1.22 && CPU amd64, but your environment is not suitable") + println("WARNING:(encoder) sonic only supports Go1.16~1.22 && CPU amd64, but your environment is not suitable") } +// EnableFallback indicates if encoder use fallback +const EnableFallback = true + // Options is a set of encoding options. type Options uint64 @@ -188,15 +191,19 @@ func Encode(val interface{}, opts Options) ([]byte, error) { // EncodeInto is like Encode but uses a user-supplied buffer instead of allocating // a new one. func EncodeInto(buf *[]byte, val interface{}, opts Options) error { - if buf == nil { - panic("user-supplied buffer buf is nil") - } - w := bytes.NewBuffer(*buf) - enc := json.NewEncoder(w) - enc.SetEscapeHTML((opts & EscapeHTML) != 0) - err := enc.Encode(val) - *buf = w.Bytes() - return err + if buf == nil { + panic("user-supplied buffer buf is nil") + } + w := bytes.NewBuffer(*buf) + enc := json.NewEncoder(w) + enc.SetEscapeHTML((opts & EscapeHTML) != 0) + err := enc.Encode(val) + *buf = w.Bytes() + l := len(*buf) + if l > 0 && (opts & NoEncoderNewline != 0) && (*buf)[l-1] == '\n' { + *buf = (*buf)[:l-1] + } + return err } // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 |