2015-02-18 13:26:13 +00:00
|
|
|
package buffer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2015-02-18 15:12:50 +00:00
|
|
|
BytesBuffer struct {
|
2015-02-18 13:26:13 +00:00
|
|
|
input []byte
|
|
|
|
size uint64
|
|
|
|
pos uint64
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-02-18 15:12:50 +00:00
|
|
|
func NewBytesBuffer(input []byte) *BytesBuffer {
|
|
|
|
return &BytesBuffer{
|
2015-02-18 13:26:13 +00:00
|
|
|
input: input,
|
|
|
|
size: uint64(len(input)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-18 15:12:50 +00:00
|
|
|
func (b *BytesBuffer) Next() rune {
|
2015-02-18 13:26:13 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
for b.pos < b.size-1 {
|
|
|
|
buf.WriteByte(b.input[b.pos])
|
|
|
|
b.pos++
|
|
|
|
if ok := utf8.FullRune(buf.Bytes()); ok {
|
|
|
|
r, _ := utf8.DecodeRune(buf.Bytes())
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|