package jx
import (
"io"
"github.com/go-faster/errors"
)
type rawReader struct {
buf []byte
captured bool
orig io .Reader
}
func (r *rawReader ) Read (p []byte ) (n int , err error ) {
if r .captured {
r .buf = append ([]byte (nil ), r .buf ...)
r .captured = false
}
n , err = r .orig .Read (p )
if n > 0 {
r .buf = append (r .buf , p [:n ]...)
}
return n , err
}
func (d *Decoder ) Raw () (Raw , error ) {
start := d .head
if orig := d .reader ; orig != nil {
rr := &rawReader {
buf : d .buf [start :d .tail ],
captured : true ,
orig : orig ,
}
d .reader = rr
defer func () {
d .reader = orig
}()
if err := d .Skip (); err != nil {
return nil , errors .Wrap (err , "skip" )
}
unread := d .tail - d .head
raw := rr .buf
raw = raw [:len (raw )-unread ]
return raw , nil
}
if err := d .Skip (); err != nil {
return nil , errors .Wrap (err , "skip" )
}
return d .buf [start :d .head ], nil
}
func (d *Decoder ) RawAppend (buf Raw ) (Raw , error ) {
raw , err := d .Raw ()
if err != nil {
return nil , err
}
return append (buf , raw ...), err
}
type Raw []byte
func (r Raw ) Type () Type {
d := Decoder {buf : r , tail : len (r )}
return d .Next ()
}
func (r Raw ) String () string { return string (r ) }
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .