1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

fix(utils/utils_str): recognize '+' as a valid numeric sign (#3778)

This commit is contained in:
wwwfeng 2024-09-23 16:45:55 +08:00 committed by GitHub
parent 777c2e7117
commit d8e3e9d713
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 1 deletions

View File

@ -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] == '.' {

View File

@ -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)
})
}

View File

@ -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))
})
}