optimize code

This commit is contained in:
xing 2024-06-11 02:21:57 +08:00
parent 1f5f7f465d
commit 68beb0fcbb

View File

@ -165,49 +165,47 @@ func SetBody(req *http.Request, types int, form map[string]any) (err error) {
} }
type BodyBuffer struct { type BodyBuffer struct {
pointer int Offset int
Data *[]byte Data *[]byte
}
func (b *BodyBuffer) Reset() {
*b.Data = (*b.Data)[:0]
b.pointer = 0
} }
func (b *BodyBuffer) Write(p []byte) (int, error) { func (b *BodyBuffer) Write(p []byte) (int, error) {
if b.pointer == 0 { if b.Offset == 0 {
copy(*b.Data, p) copy(*b.Data, p)
if len(p) <= len(*b.Data) { if len(p) <= len(*b.Data) {
b.pointer += len(p) b.Offset += len(p)
return len(p), nil return len(p), nil
} }
b.pointer += len(*b.Data) b.Offset += len(*b.Data)
return len(*b.Data), nil return len(*b.Data), nil
} }
if len(p)+b.pointer <= len(*b.Data) { if len(p)+b.Offset <= len(*b.Data) {
copy((*b.Data)[b.pointer:b.pointer+len(p)], p) copy((*b.Data)[b.Offset:b.Offset+len(p)], p)
b.pointer += len(p) b.Offset += len(p)
return len(p), nil return len(p), nil
} }
l := len(*b.Data) - b.pointer l := len(*b.Data) - b.Offset
if l <= 0 { if l <= 0 {
return 0, nil return 0, nil
} }
copy((*b.Data)[b.pointer:], p[:l]) copy((*b.Data)[b.Offset:], p[:l])
return l, nil return l, nil
} }
func (b *BodyBuffer) Read(p []byte) (int, error) { func (b *BodyBuffer) Read(p []byte) (int, error) {
if len(p) <= len(*b.Data) { data := (*b.Data)[b.Offset:]
copy(p, *b.Data) if len(p) <= len(data) {
*b.Data = (*b.Data)[len(p):] copy(p, data[0:len(p)])
b.Offset += len(p)
return len(p), nil return len(p), nil
} }
if len(*b.Data) <= 0 { if len(data) <= 0 {
b.Offset = 0
return 0, io.EOF return 0, io.EOF
} }
copy(p, *b.Data) copy(p, data)
return len(*b.Data), io.EOF b.Offset = 0
return len(data), io.EOF
} }
func NewBodyBuffer(bytes *[]byte) BodyBuffer { func NewBodyBuffer(bytes *[]byte) BodyBuffer {
@ -225,7 +223,10 @@ func PostClient(u string, types int, form map[string]any, a ...any) (cli *http.C
URL: parse, URL: parse,
Header: http.Header{}, Header: http.Header{},
} }
SetBody(req, types, form) err = SetBody(req, types, form)
if err != nil {
return
}
SetArgs(cli, req, a...) SetArgs(cli, req, a...)
return return
} }