forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_value.go
69 lines (53 loc) · 1.16 KB
/
tag_value.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
64
65
66
67
68
69
package quickfix
import (
"bytes"
"fmt"
"strconv"
)
//TagValue is a low-level FIX field abstraction
type TagValue struct {
tag Tag
value []byte
bytes []byte
}
//TagValues is a slice of TagValue
type TagValues []TagValue
func (tv *TagValue) init(tag Tag, value []byte) {
var buf bytes.Buffer
buf.WriteString(strconv.Itoa(int(tag)))
buf.WriteString("=")
buf.Write(value)
buf.WriteString("")
tv.tag = tag
tv.bytes = buf.Bytes()
tv.value = value
}
func (tv *TagValue) parse(rawFieldBytes []byte) (err error) {
sepIndex := bytes.IndexByte(rawFieldBytes, '=')
if sepIndex == -1 {
err = fmt.Errorf("tagValue.Parse: No '=' in '%s'", rawFieldBytes)
return
}
parsedTag, err := atoi(rawFieldBytes[:sepIndex])
if err != nil {
err = fmt.Errorf("tagValue.Parse: %s", err.Error())
return
}
tv.tag = Tag(parsedTag)
tv.value = rawFieldBytes[(sepIndex + 1):(len(rawFieldBytes) - 1)]
tv.bytes = rawFieldBytes
return
}
func (tv TagValue) String() string {
return string(tv.bytes)
}
func (tv TagValue) total() int {
total := 0
for _, b := range []byte(tv.bytes) {
total += int(b)
}
return total
}
func (tv TagValue) length() int {
return len(tv.bytes)
}