Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions fix_float.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package quickfix

import (
"fmt"
"regexp"
"strconv"
)

Expand All @@ -22,8 +21,10 @@ func (f *FIXFloat) Read(bytes []byte) error {
}

//strconv allows values like "+100.00", which is not allowed for FIX float types
if valid, _ := regexp.MatchString("^[0-9.-]+$", string(bytes)); !valid {
return fmt.Errorf("invalid value %v", string(bytes))
for _, b := range bytes {
if b != '.' && b != '-' && !isDecimal(b) {
return fmt.Errorf("invalid value %v", string(bytes))
}
}

*f = FIXFloat(val)
Expand All @@ -34,3 +35,7 @@ func (f *FIXFloat) Read(bytes []byte) error {
func (f FIXFloat) Write() []byte {
return []byte(strconv.FormatFloat(float64(f), 'f', -1, 64))
}

func isDecimal(b byte) bool {
return '0' <= b && b <= '9'
}
13 changes: 13 additions & 0 deletions fix_float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ func TestFloatRead(t *testing.T) {
expectError bool
}{
{[]byte("15"), 15.0, false},
{[]byte("99.9"), 99.9, false},
{[]byte("0.00"), 0.0, false},
{[]byte("-99.9"), -99.9, false},
{[]byte("-99.9.9"), 0.0, true},
{[]byte("blah"), 0.0, true},
{[]byte("1.a1"), 0.0, true},
{[]byte("+200.00"), 0.0, true},
}

Expand All @@ -46,3 +51,11 @@ func TestFloatRead(t *testing.T) {
}
}
}

func BenchmarkFloatRead(b *testing.B) {
val := []byte("15.1234")
for i := 0; i < b.N; i++ {
var field FIXFloat
field.Read(val)
}
}