From 8341c357678215da2bb2e3bab52eec1a49bbfbb2 Mon Sep 17 00:00:00 2001 From: Dan Burke Date: Wed, 5 May 2021 23:24:05 -0400 Subject: [PATCH] change ParseInt() to base 10 --- cast_test.go | 32 ++++++++++++++++++++++++++++++++ caste.go | 10 +++++----- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/cast_test.go b/cast_test.go index cc4ebce..eaa2547 100644 --- a/cast_test.go +++ b/cast_test.go @@ -301,6 +301,10 @@ func TestToIntE(t *testing.T) { {true, 1, false}, {false, 0, false}, {"8", 8, false}, + {"-8", -8, false}, + {"0100", 100, false}, + {"01234", 1234, false}, + {"1234", 1234, false}, {nil, 0, false}, // errors {"test", 0, true}, @@ -346,6 +350,10 @@ func TestToInt64E(t *testing.T) { {true, 1, false}, {false, 0, false}, {"8", 8, false}, + {"-8", -8, false}, + {"0100", 100, false}, + {"01234", 1234, false}, + {"1234", 1234, false}, {nil, 0, false}, // errors {"test", 0, true}, @@ -391,6 +399,10 @@ func TestToInt32E(t *testing.T) { {true, 1, false}, {false, 0, false}, {"8", 8, false}, + {"-8", -8, false}, + {"0100", 100, false}, + {"01234", 1234, false}, + {"1234", 1234, false}, {nil, 0, false}, // errors {"test", 0, true}, @@ -436,6 +448,10 @@ func TestToInt16E(t *testing.T) { {true, 1, false}, {false, 0, false}, {"8", 8, false}, + {"-8", -8, false}, + {"0100", 100, false}, + {"01234", 1234, false}, + {"1234", 1234, false}, {nil, 0, false}, // errors {"test", 0, true}, @@ -481,6 +497,10 @@ func TestToInt8E(t *testing.T) { {true, 1, false}, {false, 0, false}, {"8", 8, false}, + {"-8", -8, false}, + {"0100", 100, false}, + {"0124", 124, false}, + {"114", 114, false}, {nil, 0, false}, // errors {"test", 0, true}, @@ -524,6 +544,12 @@ func TestToFloat64E(t *testing.T) { {float32(8), 8, false}, {float64(8.31), 8.31, false}, {"8", 8, false}, + {"-8", -8, false}, + {"0100", 100, false}, + {"01234", 1234, false}, + {"01234.4", 1234.4, false}, + {"1234", 1234, false}, + {"1234.5", 1234.5, false}, {true, 1, false}, {false, 0, false}, // errors @@ -568,6 +594,12 @@ func TestToFloat32E(t *testing.T) { {float32(8.31), 8.31, false}, {float64(8.31), 8.31, false}, {"8", 8, false}, + {"-8", -8, false}, + {"0100", 100, false}, + {"01234", 1234, false}, + {"01234.4", 1234.4, false}, + {"1234", 1234, false}, + {"1234.5", 1234.5, false}, {true, 1, false}, {false, 0, false}, // errors diff --git a/caste.go b/caste.go index 9ac1015..3cf466f 100644 --- a/caste.go +++ b/caste.go @@ -211,7 +211,7 @@ func ToInt64E(i interface{}) (int64, error) { case float32: return int64(s), nil case string: - v, err := strconv.ParseInt(s, 0, 0) + v, err := strconv.ParseInt(s, 10, 0) if err == nil { return v, nil } @@ -258,7 +258,7 @@ func ToInt32E(i interface{}) (int32, error) { case float32: return int32(s), nil case string: - v, err := strconv.ParseInt(s, 0, 0) + v, err := strconv.ParseInt(s, 10, 0) if err == nil { return int32(v), nil } @@ -305,7 +305,7 @@ func ToInt16E(i interface{}) (int16, error) { case float32: return int16(s), nil case string: - v, err := strconv.ParseInt(s, 0, 0) + v, err := strconv.ParseInt(s, 10, 0) if err == nil { return int16(v), nil } @@ -352,7 +352,7 @@ func ToInt8E(i interface{}) (int8, error) { case float32: return int8(s), nil case string: - v, err := strconv.ParseInt(s, 0, 0) + v, err := strconv.ParseInt(s, 10, 0) if err == nil { return int8(v), nil } @@ -399,7 +399,7 @@ func ToIntE(i interface{}) (int, error) { case float32: return int(s), nil case string: - v, err := strconv.ParseInt(s, 0, 0) + v, err := strconv.ParseInt(s, 10, 0) if err == nil { return int(v), nil }