-
Notifications
You must be signed in to change notification settings - Fork 0
/
buf.go
54 lines (43 loc) · 919 Bytes
/
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
package lib
import (
"bytes"
"errors"
"github.com/dcaiafa/nitro"
"github.com/dcaiafa/nitro/lib/core"
)
type Buffer struct {
core.WriterBase
buf *bytes.Buffer
}
func (b *Buffer) Read(buf []byte) (int, error) {
return b.buf.Read(buf)
}
func (b *Buffer) String() string {
return b.buf.String()
}
func NewBuffer(data string) *Buffer {
b := &Buffer{}
if data != "" {
b.buf = bytes.NewBufferString(data)
} else {
b.buf = new(bytes.Buffer)
}
b.WriterBase = core.NewWriterBase("buffer", b.buf)
return b
}
var errBufUsage = errors.New(`invalid usage. Expected buf(string?)`)
func buf(m *nitro.VM, args []nitro.Value, nRet int) ([]nitro.Value, error) {
if len(args) > 1 {
return nil, errBufUsage
}
init := ""
if len(args) == 1 {
initArg, ok := args[0].(nitro.String)
if !ok {
return nil, errBufUsage
}
init = initArg.String()
}
b := NewBuffer(init)
return []nitro.Value{b}, nil
}