diff options
author | Alberto Cortés <alcortesm@gmail.com> | 2016-10-18 15:23:01 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-10-18 15:23:01 +0200 |
commit | 5f7d34066cc5583ee30a315e0661b5326dc548db (patch) | |
tree | c10fdbf1f42d5b51de25c6828ba6573dd28f4536 /formats/pktline/decoder.go | |
parent | 6c6a37b9128189ba4cdde8128428a36ef75d1a44 (diff) | |
download | go-git-5f7d34066cc5583ee30a315e0661b5326dc548db.tar.gz |
Substitute old pktline encoder/decoder with new pktline scanner (#84)
* replace old pktline package with new pktline scanner
* remove error checks on pktline.NewFromString
* fix deppend bug
* reduce memory garbage when pktline.NewFromStrings
* improve int to hex conversion to help gc
* make intToHex func private
* clean function names
Diffstat (limited to 'formats/pktline/decoder.go')
-rw-r--r-- | formats/pktline/decoder.go | 111 |
1 files changed, 0 insertions, 111 deletions
diff --git a/formats/pktline/decoder.go b/formats/pktline/decoder.go deleted file mode 100644 index 789ba7d..0000000 --- a/formats/pktline/decoder.go +++ /dev/null @@ -1,111 +0,0 @@ -package pktline - -import ( - "errors" - "io" - "strconv" -) - -var ( - // ErrUnderflow is triggered when a line is shorter than the described length - ErrUnderflow = errors.New("unexpected string length (underflow)") - // ErrInvalidHeader invalid pktline header - ErrInvalidHeader = errors.New("invalid header") - // ErrInvalidLen ivanlid line length found, < 0 - ErrInvalidLen = errors.New("invalid length") -) - -// Decoder implements a pkt-line format decoder -type Decoder struct { - r io.Reader -} - -// NewDecoder returns a new Decoder -func NewDecoder(r io.Reader) *Decoder { - return &Decoder{r} -} - -// ReadLine reads and return one pkt-line line from the reader -func (d *Decoder) ReadLine() (string, error) { - return d.readLine() -} - -func (d *Decoder) readLine() (string, error) { - raw := make([]byte, HeaderLength) - if _, err := io.ReadFull(d.r, raw); err != nil { - return "", err - } - - header, err := strconv.ParseInt(string(raw), 16, 16) - if err != nil { - return "", ErrInvalidHeader - } - - if header == 0 { - return "", nil - } - - exp := int(header - HeaderLength) - if exp < 0 { - return "", ErrInvalidLen - } - - line := make([]byte, exp) - if read, err := io.ReadFull(d.r, line); err != nil { - if err == io.ErrUnexpectedEOF && read < exp { - return "", ErrUnderflow - } - - return "", err - } - - return string(line), nil -} - -// ReadBlock reads and return multiple pkt-line lines, it stops at the end -// of the reader or if a flush-pkt is reached -func (d *Decoder) ReadBlock() ([]string, error) { - var o []string - - for { - line, err := d.readLine() - if err == io.EOF { - return o, nil - } - - if err != nil { - return o, err - } - - if err == nil && line == "" { - return o, nil - } - - o = append(o, line) - } -} - -// ReadAll read and returns all the lines -func (d *Decoder) ReadAll() ([]string, error) { - result, err := d.ReadBlock() - if err != nil { - return result, err - } - - for { - lines, err := d.ReadBlock() - if err == io.EOF { - return result, nil - } - - if err != nil { - return result, err - } - - if err == nil && len(lines) == 0 { - return result, nil - } - - result = append(result, lines...) - } -} |