Skip to content

Commit

Permalink
fix: remove duplicated base64 funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Feb 13, 2025
1 parent b20b0be commit 8c43c8c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 95 deletions.
2 changes: 1 addition & 1 deletion ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ func NewBytes(src []byte) Node {
if len(src) == 0 {
panic("empty src bytes")
}
out := rt.EncodeBase64(src)
out := rt.EncodeBase64ToString(src)
return NewString(out)
}

Expand Down
46 changes: 0 additions & 46 deletions internal/base64/b64_amd64.go

This file was deleted.

44 changes: 0 additions & 44 deletions internal/base64/b64_compat.go

This file was deleted.

3 changes: 1 addition & 2 deletions internal/encoder/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/bytedance/sonic/internal/encoder/ir"
"github.com/bytedance/sonic/internal/encoder/vars"
"github.com/bytedance/sonic/internal/rt"
"github.com/bytedance/sonic/internal/base64"
)

const (
Expand Down Expand Up @@ -176,7 +175,7 @@ func Execute(b *[]byte, p unsafe.Pointer, s *vars.Stack, flags uint64, prog *ir.
buf = alg.F64toa(buf, v)
case ir.OP_bin:
v := *(*[]byte)(p)
buf = base64.EncodeBase64(buf, v)
buf = rt.EncodeBase64(buf, v)
case ir.OP_quote:
v := *(*string)(p)
buf = alg.Quote(buf, v, true)
Expand Down
19 changes: 18 additions & 1 deletion internal/rt/base64_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ func DecodeBase64(raw []byte) ([]byte, error) {
return ret[:n], nil
}

func EncodeBase64(src []byte) string {
func EncodeBase64ToString(src []byte) string {
return base64x.StdEncoding.EncodeToString(src)
}

func EncodeBase64(buf []byte, src []byte) []byte {
if len(src) == 0 {
return append(buf, '"', '"')
}
buf = append(buf, '"')
need := base64x.StdEncoding.EncodedLen(len(src))
if cap(buf) - len(buf) < need {
tmp := make([]byte, len(buf), len(buf) + need*2)
copy(tmp, buf)
buf = tmp
}
base64x.StdEncoding.Encode(buf[len(buf):cap(buf)], src)
buf = buf[:len(buf) + need]
buf = append(buf, '"')
return buf
}
19 changes: 18 additions & 1 deletion internal/rt/base64_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ func DecodeBase64(raw []byte) ([]byte, error) {
return ret[:n], nil
}

func EncodeBase64(src []byte) string {
func EncodeBase64ToString(src []byte) string {
return base64.StdEncoding.EncodeToString(src)
}

func EncodeBase64(buf []byte, src []byte) []byte {
if len(src) == 0 {
return append(buf, '"', '"')
}
buf = append(buf, '"')
need := base64.StdEncoding.EncodedLen(len(src))
if cap(buf) - len(buf) < need {
tmp := make([]byte, len(buf), len(buf) + need*2)
copy(tmp, buf)
buf = tmp
}
base64.StdEncoding.Encode(buf[len(buf):cap(buf)], src)
buf = buf[:len(buf) + need]
buf = append(buf, '"')
return buf
}

0 comments on commit 8c43c8c

Please sign in to comment.