diff --git a/internal/utils/utils_str.go b/internal/utils/utils_str.go index f5891b03d..960a12920 100644 --- a/internal/utils/utils_str.go +++ b/internal/utils/utils_str.go @@ -50,7 +50,10 @@ func IsLetter(b byte) bool { // IsNumeric checks whether the given string s is numeric. // Note that float string like "123.456" is also numeric. func IsNumeric(s string) bool { - length := len(s) + var ( + dotCount = 0 + length = len(s) + ) if length == 0 { return false } @@ -59,6 +62,7 @@ func IsNumeric(s string) bool { continue } if s[i] == '.' { + dotCount++ if i > 0 && i < len(s)-1 { continue } else { @@ -69,6 +73,9 @@ func IsNumeric(s string) bool { return false } } + if dotCount > 1 { + return false + } return true } diff --git a/text/gstr/gstr_z_unit_test.go b/text/gstr/gstr_z_unit_test.go index 5744d3ed0..71e020ea9 100644 --- a/text/gstr/gstr_z_unit_test.go +++ b/text/gstr/gstr_z_unit_test.go @@ -80,6 +80,7 @@ func Test_IsNumeric(t *testing.T) { t.Assert(gstr.IsNumeric("1a我"), false) t.Assert(gstr.IsNumeric("0123"), true) t.Assert(gstr.IsNumeric("我是中国人"), false) + t.Assert(gstr.IsNumeric("1.2.3.4"), false) }) }