1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

string: faster int()

This commit is contained in:
Alexander Medvednikov
2019-10-25 23:41:18 +03:00
parent 5510327d70
commit dc2c62807a
3 changed files with 31 additions and 0 deletions

View File

@ -176,9 +176,29 @@ pub fn (s string) replace(rep, with string) string {
return tos(b, new_len)
}
/*
pub fn (s string) int() int {
return strconv.parse_int(s, 0, 32)
}
*/
pub fn (s string) int() int {
mut neg := false
mut i := 0
if s[0] == `-` {
neg = true
i++
}
else if s[0] == `+` {
i++
}
mut n := 0
for C.isdigit(s[i]) {
n = 10 * n - int(s[i] - `0`)
i++
}
return if neg { n } else { -n }
}
pub fn (s string) i64() i64 {