forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fix_int.go
63 lines (50 loc) · 1.09 KB
/
fix_int.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 (
"errors"
"strconv"
)
const (
//-
asciiMinus = 45
//ascii numbers 0-9
ascii0 = 48
ascii9 = 57
)
// atoi is similar to the function in strconv, but is tuned for ints appearing in FIX field types.
func atoi(d []byte) (int, error) {
if d[0] == asciiMinus {
n, err := parseUInt(d[1:])
return (-1) * n, err
}
return parseUInt(d)
}
// parseUInt is similar to the function in strconv, but is tuned for ints appearing in FIX field types.
func parseUInt(d []byte) (n int, err error) {
if len(d) == 0 {
err = errors.New("empty bytes")
return
}
for _, dec := range d {
if dec < ascii0 || dec > ascii9 {
err = errors.New("invalid format")
return
}
n = n*10 + (int(dec) - ascii0)
}
return
}
// FIXInt is a FIX Int Value, implements FieldValue
type FIXInt int
// Int converts the FIXInt value to int
func (f FIXInt) Int() int { return int(f) }
func (f *FIXInt) Read(bytes []byte) error {
i, err := atoi(bytes)
if err != nil {
return err
}
*f = FIXInt(i)
return nil
}
func (f FIXInt) Write() []byte {
return strconv.AppendInt(nil, int64(f), 10)
}