/
field_bytes.go
63 lines (48 loc) · 1.07 KB
/
field_bytes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package quickfix
import (
"bytes"
"fmt"
"strconv"
)
type fieldBytes struct {
Tag
Data []byte
Value []byte
}
func newFieldBytes(tag Tag, value []byte) *fieldBytes {
var buf bytes.Buffer
buf.WriteString(strconv.Itoa(int(tag)))
buf.WriteString("=")
buf.Write(value)
buf.WriteString("")
return &fieldBytes{Tag: tag, Data: buf.Bytes(), Value: value}
}
func (f *fieldBytes) parseField(rawFieldBytes []byte) (err error) {
sepIndex := bytes.IndexByte(rawFieldBytes, '=')
if sepIndex == -1 {
err = fmt.Errorf("fieldBytes.Parse: No '=' in '%s'", rawFieldBytes)
return
}
parsedTag, err := Atoi(rawFieldBytes[:sepIndex])
if err != nil {
err = fmt.Errorf("fieldBytes.Parse: %s", err.Error())
return
}
f.Tag = Tag(parsedTag)
f.Value = rawFieldBytes[(sepIndex + 1):(len(rawFieldBytes) - 1)]
f.Data = rawFieldBytes
return
}
func (f *fieldBytes) String() string {
return string(f.Data)
}
func (f *fieldBytes) Total() int {
total := 0
for _, b := range []byte(f.Data) {
total += int(b)
}
return total
}
func (f *fieldBytes) Length() int {
return len(f.Data)
}