string: new type conversion functions

pull/3450/head
Swastik Baranwal 2020-01-14 22:35:38 +05:30 committed by Alexander Medvednikov
parent 2c8848f1bc
commit 2741c0b211
2 changed files with 171 additions and 1 deletions

View File

@ -298,6 +298,14 @@ pub fn (s string) i64() i64 {
return strconv.common_parse_int(s, 0, 64, false, false)
}
pub fn (s string) i8() i8 {
return i8(strconv.common_parse_int(s, 0, 8, false, false))
}
pub fn (s string) i16() i16 {
return i16(strconv.common_parse_int(s, 0, 16, false, false))
}
pub fn (s string) f32() f32 {
// return C.atof(charptr(s.str))
return f32(strconv.atof64(s))
@ -308,6 +316,10 @@ pub fn (s string) f64() f64 {
return strconv.atof64(s)
}
pub fn (s string) u16() u16 {
return u16(strconv.common_parse_uint(s, 0, 16, false, false))
}
pub fn (s string) u32() u32 {
return u32(strconv.common_parse_uint(s, 0, 32, false, false))
}
@ -1201,4 +1213,3 @@ pub fn (s string) repeat(count int) string {
ret[s.len * count] = 0
return string(ret)
}

View File

@ -25,3 +25,162 @@ fn test_common_atoi() {
assert s.int() == n
}
}
fn test_unsigned_cast() {
// tests for u16
// test common cases
assert "70zzz".u16() == 70
assert "2901issue".u16() == 2901
assert '0y'.u16() == 0
// test lead zeros
assert '0000012'.u16() == 12
assert '0x001F'.u16() == 31
assert '0x001f'.u16() == 31
assert '0o00011'.u16() == 9
assert '0b00001001'.u16() == 9
// tests for u32
// test common cases
assert "70zzz".u32() == 70
assert "2901issue".u32() == 2901
assert '234232w'.u32() == 234232
assert '-9009x'.u32() == 0
assert '0y'.u32() == 0
// test lead zeros
assert '0000012'.u32() == 12
assert '-0000012'.u32() == 0
assert '0x001F'.u32() == 31
assert '-0x001F'.u32() == 0
assert '0x001f'.u32() == 31
assert '0o00011'.u32() == 9
assert '0b00001001'.u32() == 9
// test underscore in string
assert '-10_000'.u32() == 0
assert '-0x00_0_f_ff'.u32() == 0
assert '10_000_000'.u32() == 10000000
for n in 0 .. 100 {
s := n.str()+"z"
assert s.u32() == n
}
// tests for u64
// test common cases
assert "70zzz".u64() == 70
assert "2901issue".u64() == 2901
assert '234232w'.u64() == 234232
assert '-9009x'.u64() == 0
assert '0y'.u64() == 0
// test lead zeros
assert '0000012'.u64() == 12
assert '-0000012'.u64() == 0
assert '0x001F'.u64() == 31
assert '-0x001F'.u64() == 0
assert '0x001f'.u64() == 31
assert '0o00011'.u64() == 9
assert '0b00001001'.u64() == 9
// test underscore in string
assert '-10_000'.u64() == 0
assert '-0x00_0_f_ff'.u64() == 0
assert '10_000_000'.u64() == 10000000
for n in 0 .. 10000 {
s := n.str()+"z"
assert s.u64() == n
}
}
fn test_signed_cast() {
// tests for i64
// test common cases
assert "70zzz".i64() == 70
assert "2901issue".i64() == 2901
assert '234232w'.i64() == 234232
assert '-9009x'.i64() == -9009
assert '0y'.i64() == 0
// test lead zeros
assert '0000012'.i64() == 12
assert '-0000012'.i64() == -12
assert '0x001F'.i64() == 31
assert '-0x001F'.i64() == -31
assert '0x001f'.i64() == 31
assert '0o00011'.i64() == 9
assert '0b00001001'.i64() == 9
// test underscore in string
assert '-10_000'.i64() == -10000
assert '-0x00_0_f_ff'.i64() == -0xfff
assert '10_000_000'.i64() == 10000000
for n in -10000 .. 100000 {
s := n.str()+"z"
assert s.i64() == n
}
// tests for i8
// test common cases
assert "70zzz".i8() == 70
assert "29issue".i8() == 29
assert '22w'.i8() == 22
assert '-90x'.i8() == -90
assert '0y'.i8() == 0
// test lead zeros
assert '0000012'.i8() == 12
assert '-0000012'.i8() == -12
assert '0x001F'.i8() == 31
assert '-0x001F'.i8() == -31
assert '0x001f'.i8() == 31
assert '0o00011'.i8() == 9
assert '0b000011'.i8() == 3
// test underscore in string
assert '-10_0'.i8() == -100
assert '-0x0_0_f'.i8() == -0xf
assert '10_0'.i8() == 100
for n in -10 .. 100 {
s := n.str()+"z"
assert s.i8() == n
}
// tests for i16
// test common cases
assert "70zzz".i16() == 70
assert "2901issue".i16() == 2901
assert '2342w'.i16() == 2342
assert '-9009x'.i16() == -9009
assert '0y'.i16() == 0
// test lead zeros
assert '0000012'.i16() == 12
assert '-0000012'.i16() == -12
assert '0x001F'.i16() == 31
assert '-0x001F'.i16() == -31
assert '0x001f'.i16() == 31
assert '0o00011'.i16() == 9
assert '0b00001001'.i16() == 9
// test underscore in string
assert '-10_0'.i16() == -100
assert '-0x00_0_fff'.i16() == -0xfff
assert '10_0'.i16() == 100
for n in -100 .. 100 {
s := n.str()+"z"
assert s.i16() == n
}
}