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

compiler: unsigned number properly printed and converted to string

fix: array accessing now works with unsigned numbers
This commit is contained in:
Henrixounez
2019-08-12 01:58:08 +02:00
committed by Alexander Medvednikov
parent 92cb199e8c
commit 872aa536d8
4 changed files with 85 additions and 6 deletions

View File

@@ -70,6 +70,34 @@ pub fn (nn int) str() string {
return tos(buf + max - len, len)
}
pub fn (nn u32) str() string {
mut n := nn
if n == u32(0) {
return '0'
}
max := 16
mut buf := malloc(max)
mut len := 0
mut is_neg := false
if n < u32(0) {
n = -n
is_neg = true
}
// Fill the string from the end
for n > u32(0) {
d := n % u32(10)
buf[max - len - 1] = d + u32(`0`)
len++
n = n / u32(10)
}
// Prepend - if it's negative
if is_neg {
buf[max - len - 1] = `-`
len++
}
return tos(buf + max - len, len)
}
pub fn (nn u8) str() string {
mut n := nn
if n == u8(0) {
@@ -126,6 +154,34 @@ pub fn (nn i64) str() string {
return tos(buf + max - len, len)
}
pub fn (nn u64) str() string {
mut n := nn
if n == u64(0) {
return '0'
}
max := 32
mut buf := malloc(max)
mut len := 0
mut is_neg := false
if n < u64(0) {
n = -n
is_neg = true
}
// Fill the string from the end
for n > u64(0) {
d := n % u64(10)
buf[max - len - 1] = d + u64(`0`)
len++
n = n / u64(10)
}
// Prepend - if it's negative
if is_neg {
buf[max - len - 1] = `-`
len++
}
return tos(buf + max - len, len)
}
pub fn (b bool) str() string {
if b {
return 'true'

View File

@@ -10,6 +10,26 @@ fn test_const() {
assert u == u64(1)
}
fn test_str_methods() {
assert i8(1).str() == '1'
assert i8(-1).str() == '-1'
assert i16(1).str() == '1'
assert i16(-1).str() == '-1'
assert i32(1).str() == '1'
assert i32(-1).str() == '-1'
assert i64(1).str() == '1'
assert i64(-1).str() == '-1'
assert u8(1).str() == '1'
assert u8(-1).str() == '255'
assert u16(1).str() == '1'
assert u16(-1).str() == '65535'
assert u32(1).str() == '1'
assert u32(-1).str() == '4294967295'
assert u64(1).str() == '1'
assert u64(-1).str() == '18446744073709551615'
}
/*
fn test_cmp() {
assert 1 ≠ 2