From 56ad5d72ef25eda99e205a2ee840988ef38445f9 Mon Sep 17 00:00:00 2001 From: Enzo Date: Wed, 8 Sep 2021 12:41:08 +0200 Subject: [PATCH] builtin: add `.str` method for `i/usize` (#11441) --- vlib/builtin/int.v | 15 +++++++-------- vlib/v/tests/integer_size_test.v | 26 ++++++-------------------- 2 files changed, 13 insertions(+), 28 deletions(-) diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index 845ae401d8..1bc63f366f 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -15,14 +15,13 @@ pub fn ptr_str(ptr voidptr) string { return buf1 } -// TODO uncomment to have string representation of i/usize -// pub fn (x isize) str() string { -// return u64(x).str() -// } -// -// pub fn (x usize) str() string { -// return u64(x).str() -// } +pub fn (x isize) str() string { + return i64(x).str() +} + +pub fn (x usize) str() string { + return u64(x).str() +} pub fn (x size_t) str() string { return u64(x).str() diff --git a/vlib/v/tests/integer_size_test.v b/vlib/v/tests/integer_size_test.v index 6a019437bb..43b1a9c500 100644 --- a/vlib/v/tests/integer_size_test.v +++ b/vlib/v/tests/integer_size_test.v @@ -1,36 +1,22 @@ fn f(u usize) usize fn g(i isize) isize -// TODO refactor once `str` method is implemented - fn test_usize() { mut u := usize(3) u += u32(1) - // assert u == 4 - if u != 4 { - assert false - } + assert u == 4 u = 4 u++ - // assert u == 5 - if u != 5 { - assert false - } - // assert u.str() == '5' + assert u == 5 + assert u.str() == '5' } fn test_isize() { mut i := isize(-3) i -= int(1) - // assert i == -4 - if i != -4 { - assert false - } + assert i == -4 i = -5 i += 2 - // assert i == -3 - if i != -3 { - assert false - } - // assert i.str() == '-2' + assert i == -3 + assert i.str() == '-3' }