mirror of
https://github.com/divan/txqr.git
synced 2026-08-31 07:17:27 +08:00
Switch to decimals in protocol code
This commit is contained in:
+3
-9
@@ -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
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user