Switch to decimals in protocol code

This commit is contained in:
Ivan Danyliuk
2018-11-10 19:58:20 +01:00
parent f99345a92e
commit dfbb2baff9
2 changed files with 5 additions and 11 deletions
+3 -9
View File
@@ -1,16 +1,10 @@
package protocol
import (
"errors"
"fmt"
"sort"
)
// default errors
var (
ErrInvalidFrame = errors.New("invalid frame")
)
// Decoder represents protocol decode.
type Decoder struct {
buffer []byte
@@ -43,16 +37,16 @@ func NewDecoderSize(size int) *Decoder {
// DecodeChunk takes a single chunk of data and decodes it.
func (d *Decoder) DecodeChunk(data string) error {
if data == "" || len(data) < 4 {
return ErrInvalidFrame
return fmt.Errorf("invalid frame: \"%s\"", data)
}
var (
offset, total int
payload []byte
)
_, err := fmt.Sscanf(data, "%x/%x|%s", &offset, &total, &payload)
_, err := fmt.Sscanf(data, "%d/%d|%s", &offset, &total, &payload)
if err != nil {
return fmt.Errorf("invalid frame: %v", err)
return fmt.Errorf("invalid frame: %v (%s)", err, data)
}
// allocate enough memory at first total read
+2 -2
View File
@@ -33,7 +33,7 @@ func (e *Encoder) EncodeReader(r io.Reader) ([]string, error) {
// futher converted to QR code frames.
func (e *Encoder) Encode(str string) ([]string, error) {
if len(str) < e.chunkLen {
return []string{str}, nil
return []string{e.frame(0, len(str), str)}, nil
}
numChunks := len(str)/e.chunkLen + 1
@@ -54,5 +54,5 @@ func (e *Encoder) Encode(str string) ([]string, error) {
}
func (e *Encoder) frame(count, total int, str string) string {
return fmt.Sprintf("%x/%x|%s", count, total, str)
return fmt.Sprintf("%d/%d|%s", count, total, str)
}