refactor(api): 添加 parseIntVal 函数解析字符串为整数
- 实现 parseIntVal 函数,尝试将字符串转换成整数 - 失败或空字符串时返回 -1 作为默认值 - 遍历字符判断并累积转换为整数 - 简化字符串到整数的转换逻辑,提高代码复用性
This commit is contained in:
parent
da0c764646
commit
2ed9a0ce55
|
|
@ -1574,3 +1574,18 @@ func pickFirst(perm map[string]interface{}, filters map[string]interface{}, keys
|
|||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// parseIntVal 尝试将字符串解析为整数,失败返回-1
|
||||
func parseIntVal(s string) int {
|
||||
if s == "" {
|
||||
return -1
|
||||
}
|
||||
n := 0
|
||||
for _, c := range s {
|
||||
if c < '0' || c > '9' {
|
||||
return -1
|
||||
}
|
||||
n = n*10 + int(c-'0')
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue