fix: parse integer fields as base 10
toInt and toUint passed base 0 to strconv, which infers the base from the literal the way the Go compiler does. A zero-padded CSV field was then read as octal: 08 and 09 failed with invalid syntax and 010 silently parsed as 8. Pass base 10 at both call sites. Go-style prefixed literals such as 0x1f are no longer accepted; they were undocumented and untested, and cannot be kept without keeping the octal reading of 010 as well. Closes #182
This commit is contained in:
1 parent
0ba11f2f3b
commit
26aafe16a8
3 files changed
+90
-2
No files matched your search
@@ -139,3 +139,58 @@ func TestToInt(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToIntLeadingZeros(t *testing.T) {
|
||||
testCases := []struct {
|
||||
field string
|
||||
result int64
|
||||
}{
|
||||
{"08", 8},
|
||||
{"09", 9},
|
||||
{"010", 10},
|
||||
{"007", 7},
|
||||
{"0009", 9},
|
||||
{"-08", -8},
|
||||
{"-010", -10},
|
||||
{"0", 0},
|
||||
{"00", 0},
|
||||
{"010.9", 10},
|
||||
}
|
||||
|
||||
for _, item := range testCases {
|
||||
out, err := toInt(item.field)
|
||||
if err != nil {
|
||||
t.Errorf("toInt(%q) returned an error: %s", item.field, err)
|
||||
continue
|
||||
}
|
||||
if out != item.result {
|
||||
t.Errorf("toInt(%q) = %d, want %d", item.field, out, item.result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestToUintLeadingZeros(t *testing.T) {
|
||||
testCases := []struct {
|
||||
field string
|
||||
result uint64
|
||||
}{
|
||||
{"08", 8},
|
||||
{"09", 9},
|
||||
{"010", 10},
|
||||
{"007", 7},
|
||||
{"0009", 9},
|
||||
{"0", 0},
|
||||
{"00", 0},
|
||||
}
|
||||
|
||||
for _, item := range testCases {
|
||||
out, err := toUint(item.field)
|
||||
if err != nil {
|
||||
t.Errorf("toUint(%q) returned an error: %s", item.field, err)
|
||||
continue
|
||||
}
|
||||
if out != item.result {
|
||||
t.Errorf("toUint(%q) = %d, want %d", item.field, out, item.result)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user