1
0
Fork 0

Stream buffer

This commit is contained in:
Gregory Eremin 2015-02-18 20:43:00 +07:00
parent 6b9a8bebbc
commit 57f3043281
3 changed files with 42 additions and 5 deletions

37
buffer/stream.go Normal file
View File

@ -0,0 +1,37 @@
package buffer
import (
"bytes"
"io"
"unicode/utf8"
)
type (
StreamBuffer struct {
input io.Reader
}
)
func NewStreamBuffer(input io.Reader) *StreamBuffer {
return &StreamBuffer{
input: input,
}
}
func (b *StreamBuffer) Next() rune {
var (
buf bytes.Buffer
)
for {
rbuf := make([]byte, 1)
if n, err := b.input.Read(rbuf); n != 1 || err != nil {
return 0
}
buf.Write(rbuf)
if ok := utf8.FullRune(buf.Bytes()); ok {
r, _ := utf8.DecodeRune(buf.Bytes())
return r
}
}
return 0
}

View File

@ -20,8 +20,7 @@ type (
)
// Creates a new parser
func New(b []byte, sels []string) *Parser {
buf := buffer.NewDataBuffer(b)
func New(buf buffer.Bufferer, sels []string) *Parser {
return &Parser{
lex: lexer.New(buf),
ctx: &context{

7
run.go
View File

@ -1,18 +1,19 @@
package main
import (
"io/ioutil"
"os"
"github.com/kr/pretty"
"github.com/localhots/punk/buffer"
"github.com/localhots/punk/parser"
)
func main() {
f, _ := os.Open("test.json")
b, _ := ioutil.ReadAll(f)
// b, _ := ioutil.ReadAll(f)
buf := buffer.NewStreamBuffer(f)
p := parser.New(b, []string{
p := parser.New(buf, []string{
"/prices/*",
"/bananas/[*]/weight",
})