-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(utils/utils_str): recognize '+' as a valid numeric sign #3778
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wwwfeng
changed the title
fix(utils/utils_str): fix recognize '+' as a valid numeric sign
fix(utils/utils_str):#3776 fix recognize '+' as a valid numeric sign
Sep 13, 2024
gqcn
requested changes
Sep 19, 2024
fixed #3776 |
gqcn
changed the title
fix(utils/utils_str):#3776 fix recognize '+' as a valid numeric sign
fix(utils/utils_str): recognize '+' as a valid numeric sign
Sep 19, 2024
咦,CI的问题吗 |
Hey, is it a CI problem? |
func IsNumeric(s string) bool {
var (
dotCount = 0
length = len(s)
start = 0
)
if length == 0 {
return false
}
// -123 => 123
// +123 => 123
if s[0] == '-' || s[0] == '+' {
// + false
// - false
if length == 1 {
return false
}
start = 1
}
// .123 false
// 123. false
// . false
if s[0] == '.' || s[length-1] == '.' {
return false
}
for i := start; i < length; i++ {
if s[i] == '.' {
dotCount++
if dotCount > 1 {
return false
}
continue
}
if s[i] < '0' || s[i] > '9' {
return false
}
}
return true
} 另再添加如下测试用例 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) |
确实遗漏了一些case,似乎满足这些用例下面的变更就可以实现
// .123 false |
gqcn
approved these changes
Sep 23, 2024
Quality Gate passedIssues Measures |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
我觉得
+
号也应该作为数字类型识别,我看标准库的atoi 也有类似对于带+
符号的数字处理比如