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:
parent
5510327d70
commit
dc2c62807a
@ -10,6 +10,7 @@ fn C.qsort(voidptr, int, int, voidptr)
|
|||||||
|
|
||||||
fn C.sprintf(a ...voidptr) byteptr
|
fn C.sprintf(a ...voidptr) byteptr
|
||||||
fn C.strlen(s byteptr) int
|
fn C.strlen(s byteptr) int
|
||||||
|
fn C.isdigit(s byteptr) bool
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -176,9 +176,29 @@ pub fn (s string) replace(rep, with string) string {
|
|||||||
return tos(b, new_len)
|
return tos(b, new_len)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
pub fn (s string) int() int {
|
pub fn (s string) int() int {
|
||||||
return strconv.parse_int(s, 0, 32)
|
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 {
|
pub fn (s string) i64() i64 {
|
||||||
|
@ -482,3 +482,13 @@ fn test_escape() {
|
|||||||
//println("\"$a")
|
//println("\"$a")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_atoi() {
|
||||||
|
assert '234232'.int() == 234232
|
||||||
|
assert '-9009'.int() == -9009
|
||||||
|
assert '0'.int() == 0
|
||||||
|
for n in -10000 .. 100000 {
|
||||||
|
s := n.str()
|
||||||
|
assert s.int() == n
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user