From d8e3e9d7136cb43f478c54e65d27cefa592244f9 Mon Sep 17 00:00:00 2001 From: wwwfeng <32693877+wwwfeng@users.noreply.github.com> Date: Mon, 23 Sep 2024 16:45:55 +0800 Subject: [PATCH] fix(utils/utils_str): recognize '+' as a valid numeric sign (#3778) --- internal/utils/utils_str.go | 5 ++++- internal/utils/utils_z_unit_test.go | 31 ++++++++++++++++++++++++++++ util/gconv/gconv_z_unit_time_test.go | 2 ++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/internal/utils/utils_str.go b/internal/utils/utils_str.go index d22245733..9f1089d50 100644 --- a/internal/utils/utils_str.go +++ b/internal/utils/utils_str.go @@ -58,7 +58,10 @@ func IsNumeric(s string) bool { return false } for i := 0; i < length; i++ { - if s[i] == '-' && i == 0 { + if (s[i] == '-' || s[i] == '+') && i == 0 { + if length == 1 { + return false + } continue } if s[i] == '.' { diff --git a/internal/utils/utils_z_unit_test.go b/internal/utils/utils_z_unit_test.go index 259091d5f..8c73eeac1 100644 --- a/internal/utils/utils_z_unit_test.go +++ b/internal/utils/utils_z_unit_test.go @@ -95,3 +95,34 @@ func Test_CanCallIsNil(t *testing.T) { t.Assert(utils.CanCallIsNil(reflect.ValueOf(iUnsafePointer)), true) }) } + +func Test_IsNumeric(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + t.Assert(utils.IsNumeric("12345"), true) + t.Assert(utils.IsNumeric("-12345"), true) + t.Assert(utils.IsNumeric("+12345"), true) + t.Assert(utils.IsNumeric("123.45"), true) + t.Assert(utils.IsNumeric("-123.45"), true) + t.Assert(utils.IsNumeric("+123.45"), true) + t.Assert(utils.IsNumeric("1+23"), false) + t.Assert(utils.IsNumeric("123a45"), false) + t.Assert(utils.IsNumeric("123.45.67"), false) + t.Assert(utils.IsNumeric(""), false) + t.Assert(utils.IsNumeric("1e10"), false) + t.Assert(utils.IsNumeric("123 45"), false) + t.Assert(utils.IsNumeric("!!!"), false) + t.Assert(utils.IsNumeric("-a23"), false) + t.Assert(utils.IsNumeric("+a23"), false) + t.Assert(utils.IsNumeric("1+23"), false) + t.Assert(utils.IsNumeric("1-23"), false) + t.Assert(utils.IsNumeric("123."), false) + t.Assert(utils.IsNumeric(".123"), false) + t.Assert(utils.IsNumeric("123.a"), false) + t.Assert(utils.IsNumeric("a.123"), false) + t.Assert(utils.IsNumeric("+"), false) + t.Assert(utils.IsNumeric("-"), false) + t.Assert(utils.IsNumeric("."), false) + t.Assert(utils.IsNumeric("-."), false) + t.Assert(utils.IsNumeric("+."), false) + }) +} diff --git a/util/gconv/gconv_z_unit_time_test.go b/util/gconv/gconv_z_unit_time_test.go index 67178978b..2a758fe1d 100644 --- a/util/gconv/gconv_z_unit_time_test.go +++ b/util/gconv/gconv_z_unit_time_test.go @@ -35,6 +35,8 @@ func TestDuration(t *testing.T) { t.AssertEQ(gconv.Duration(timeTimeTests), time.Duration(0)) t.AssertEQ(gconv.Duration("1m"), time.Minute) t.AssertEQ(gconv.Duration(time.Hour), time.Hour) + t.AssertEQ(gconv.Duration("-1"), time.Duration(-1)) + t.AssertEQ(gconv.Duration("+1"), time.Duration(1)) }) }