/
buf.go
131 lines (113 loc) · 3.4 KB
/
buf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2019 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package vm
import (
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/gentee/gentee/core"
)
// AddºBufBuf adds two buffers
func AddºBufBuf(left *core.Buffer, right *core.Buffer) (out *core.Buffer) {
out = core.NewBuffer()
out.Data = left.Data
out.Data = append(out.Data, right.Data...)
return out
}
// AssignAddºBufBuf appends buffer to buffer
func AssignAddºBufBuf(buf interface{}, value interface{}) (interface{}, error) {
buf.(*core.Buffer).Data = append(buf.(*core.Buffer).Data, value.(*core.Buffer).Data...)
return buf, nil
}
// AssignAddºBufChar appends rune to buffer
func AssignAddºBufChar(buf interface{}, value interface{}) (interface{}, error) {
buf.(*core.Buffer).Data = append(buf.(*core.Buffer).Data,
[]byte(string([]rune{rune(value.(int64))}))...)
return buf, nil
}
// AssignAddºBufInt appends one byte to buffer
func AssignAddºBufInt(buf interface{}, value interface{}) (interface{}, error) {
if uint64(value.(int64)) > 255 {
return nil, fmt.Errorf(ErrorText(ErrByteOut))
}
buf.(*core.Buffer).Data = append(buf.(*core.Buffer).Data, byte(value.(int64)))
return buf, nil
}
// AssignAddºBufStr appends string to buffer
func AssignAddºBufStr(buf interface{}, value interface{}) (interface{}, error) {
buf.(*core.Buffer).Data = append(buf.(*core.Buffer).Data, []byte(value.(string))...)
return buf, nil
}
// Base64ºBuf encodes buf to base64 string
func Base64ºBuf(buf *core.Buffer) string {
return base64.StdEncoding.EncodeToString(buf.Data)
}
// boolºBuf converts buf to bool
func boolºBuf(buf *core.Buffer) int64 {
if len(buf.Data) == 0 {
return 0
}
return 1
}
// bufºStr converts string to buffer
func bufºStr(value string) *core.Buffer {
b := core.NewBuffer()
b.Data = []byte(value)
return b
}
// DelºBufIntInt deletes part of the buffer
func DelºBufIntInt(buf *core.Buffer, off, length int64) (*core.Buffer, error) {
size := int64(len(buf.Data))
if off < 0 || off > size {
return buf, fmt.Errorf(ErrorText(ErrInvalidParam))
}
if length < 0 {
off += length
length = -length
}
if off < 0 {
off = 0
}
if off+length > size {
length = size - off
}
buf.Data = append(buf.Data[:off], buf.Data[off+length:]...)
return buf, nil
}
// HexºBuf encodes buf to hex string
func HexºBuf(buf *core.Buffer) string {
return hex.EncodeToString(buf.Data)
}
// InsertºBufIntBuf inserts one buf object into another one
func InsertºBufIntBuf(buf *core.Buffer, off int64, b *core.Buffer) (*core.Buffer, error) {
size := int64(len(buf.Data))
if off < 0 || off > size {
return buf, fmt.Errorf(ErrorText(ErrInvalidParam))
}
buf.Data = append(buf.Data[:off], append(b.Data, buf.Data[off:]...)...)
return buf, nil
}
// strºBuf converts buffer to string
func strºBuf(buf *core.Buffer) string {
return string(buf.Data)
}
// sysBufNil return nil buffer
func sysBufNil() *core.Buffer {
b := core.NewBuffer()
b.Data = nil
return b
}
// UnBase64ºStr decodes base64 string to buf
func UnBase64ºStr(value string) (buf *core.Buffer, err error) {
buf = core.NewBuffer()
buf.Data, err = base64.StdEncoding.DecodeString(value)
return
}
// UnHexºStr decodes hex string to the buffer
func UnHexºStr(value string) (*core.Buffer, error) {
var err error
buf := core.NewBuffer()
buf.Data, err = hex.DecodeString(value)
return buf, err
}