From 4c96ca4e62cf9e4ab37bde8472fffdb857edf64f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 31 Oct 2019 21:50:11 +0300 Subject: [PATCH] string: remove strconv dep --- vlib/builtin/string.v | 54 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 54d271f577..9d90eeb5ed 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -4,6 +4,8 @@ module builtin +//import strconv + /* NB: A V string should be/is immutable from the point of view of V user programs after it is first created. A V string is @@ -41,7 +43,6 @@ NB: A V string should be/is immutable from the point of view of when used with modules using C functions (for example os and so on). */ -//import strconv pub struct string { //mut: @@ -202,8 +203,23 @@ pub fn (s string) int() int { pub fn (s string) i64() i64 { + mut neg := false + mut i := 0 + if s[0] == `-` { + neg = true + i++ + } + else if s[0] == `+` { + i++ + } + mut n := i64(0) + for C.isdigit(s[i]) { + n = i64(10) * n - i64(s[i] - `0`) + i++ + } + return if neg { n } else { -n } //return strconv.parse_int(s, 0, 64) - return C.atoll(*char(s.str)) + //return C.atoll(*char(s.str)) } pub fn (s string) f32() f32 { @@ -215,12 +231,42 @@ pub fn (s string) f64() f64 { } pub fn (s string) u32() u32 { - return C.atol(*char(s.str)) + mut neg := false + mut i := 0 + if s[0] == `-` { + neg = true + i++ + } + else if s[0] == `+` { + i++ + } + mut n := u32(0) + for C.isdigit(s[i]) { + n = u32(10) * n - u32(s[i] - `0`) + i++ + } + return if neg { n } else { -n } + //return C.atol(*char(s.str)) //return strconv.parse_uint(s, 0, 32) } pub fn (s string) u64() u64 { - return C.atoll(*char(s.str)) + mut neg := false + mut i := 0 + if s[0] == `-` { + neg = true + i++ + } + else if s[0] == `+` { + i++ + } + mut n := u64(0) + for C.isdigit(s[i]) { + n = u64(10) * n - u64(s[i] - `0`) + i++ + } + return if neg { n } else { -n } + //return C.atoll(*char(s.str)) //return strconv.parse_uint(s, 0, 64) }