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/encoder.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/encoder.go')
-rw-r--r-- | formats/pktline/encoder.go | 64 |
1 files changed, 0 insertions, 64 deletions
diff --git a/formats/pktline/encoder.go b/formats/pktline/encoder.go deleted file mode 100644 index 18efa25..0000000 --- a/formats/pktline/encoder.go +++ /dev/null @@ -1,64 +0,0 @@ -package pktline - -import ( - "errors" - "fmt" - "strings" -) - -var ( - //ErrOverflow is triggered when the line length exceed the MaxLength - ErrOverflow = errors.New("unexpected string length (overflow)") -) - -// Encoder implements a pkt-line format encoder -type Encoder struct { - lines []string -} - -// NewEncoder returns a new Encoder -func NewEncoder() *Encoder { - return &Encoder{make([]string, 0)} -} - -// AddLine encode and adds a line to the encoder -func (e *Encoder) AddLine(line string) error { - le, err := EncodeFromString(line + "\n") - if err != nil { - return err - } - - e.lines = append(e.lines, le) - return nil -} - -// AddFlush adds a flush-pkt to the encoder -func (e *Encoder) AddFlush() { - e.lines = append(e.lines, "0000") -} - -// Reader returns a string.Reader over the encoder -func (e *Encoder) Reader() *strings.Reader { - data := strings.Join(e.lines, "") - - return strings.NewReader(data) -} - -// EncodeFromString encodes a string to pkt-line format -func EncodeFromString(line string) (string, error) { - return Encode([]byte(line)) -} - -// Encode encodes a byte slice to pkt-line format -func Encode(line []byte) (string, error) { - if line == nil { - return "0000", nil - } - - l := len(line) + HeaderLength - if l > MaxLength { - return "", ErrOverflow - } - - return fmt.Sprintf("%04x%s", l, line), nil -} |