From a0a1807e2bad73ed9e4626aefcf191396b77d206 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 31 Dec 2021 04:34:24 +0800 Subject: [PATCH] builtin: add charptr str() and change string format (#12973) --- vlib/builtin/int.v | 8 +++++-- vlib/builtin/int_test.v | 10 ++++---- ...pass_voidptr_as_interface_reference_test.v | 2 +- .../voidptr_casted_as_an_interface_test.v | 2 +- vlib/v/tests/pointers_multilevel_casts_test.v | 24 +++++++++---------- 5 files changed, 26 insertions(+), 20 deletions(-) diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index 80ed4c1d8a..4be89c2269 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -391,14 +391,18 @@ pub fn (nn int_literal) hex() string { // hex returns the value of the `voidptr` as a hexadecimal `string`. // Note that the output is ***not*** zero padded. pub fn (nn voidptr) str() string { - return u64(nn).hex() + return '0x' + u64(nn).hex() } // hex returns the value of the `byteptr` as a hexadecimal `string`. // Note that the output is ***not*** zero padded. // pub fn (nn byteptr) str() string { pub fn (nn byteptr) str() string { - return u64(nn).hex() + return '0x' + u64(nn).hex() +} + +pub fn (nn charptr) str() string { + return '0x' + u64(nn).hex() } pub fn (nn byte) hex_full() string { diff --git a/vlib/builtin/int_test.v b/vlib/builtin/int_test.v index 7a20814c3a..bfb8a4286b 100644 --- a/vlib/builtin/int_test.v +++ b/vlib/builtin/int_test.v @@ -29,12 +29,14 @@ fn test_str_methods() { assert u32(-1).str() == '4294967295' assert u64(1).str() == '1' assert u64(-1).str() == '18446744073709551615' - assert voidptr(-1).str() == 'ffffffffffffffff' - assert voidptr(1).str() == '1' + assert voidptr(-1).str() == '0xffffffffffffffff' + assert voidptr(1).str() == '0x1' assert (&byte(-1)).str() == 'ffffffffffffffff' assert (&byte(1)).str() == '1' - assert byteptr(-1).str() == 'ffffffffffffffff' - assert byteptr(1).str() == '1' + assert byteptr(-1).str() == '0xffffffffffffffff' + assert byteptr(1).str() == '0x1' + assert charptr(-1).str() == '0xffffffffffffffff' + assert charptr(1).str() == '0x1' } fn test_and_precendence() { diff --git a/vlib/v/tests/interface_edge_cases/pass_voidptr_as_interface_reference_test.v b/vlib/v/tests/interface_edge_cases/pass_voidptr_as_interface_reference_test.v index 0e332e2c74..8de1486e53 100644 --- a/vlib/v/tests/interface_edge_cases/pass_voidptr_as_interface_reference_test.v +++ b/vlib/v/tests/interface_edge_cases/pass_voidptr_as_interface_reference_test.v @@ -15,5 +15,5 @@ fn test_passing_voidptr_as_an_interface_reference() { assert f(&i) == '&IAbc(Abc{})' // a voidptr() cast is an escape hatch, that should be allowed // but perhaps it should be forced by the compiler to be in unsafe{} - assert f(unsafe { voidptr(0) }) == '&IAbc(0)' + assert f(unsafe { voidptr(0) }) == '&IAbc(0x0)' } diff --git a/vlib/v/tests/interface_edge_cases/voidptr_casted_as_an_interface_test.v b/vlib/v/tests/interface_edge_cases/voidptr_casted_as_an_interface_test.v index 2908c35c42..4a70f75c0b 100644 --- a/vlib/v/tests/interface_edge_cases/voidptr_casted_as_an_interface_test.v +++ b/vlib/v/tests/interface_edge_cases/voidptr_casted_as_an_interface_test.v @@ -17,7 +17,7 @@ fn f(i &IAbc) string { fn test_voidptr_casted_as_an_interface_reference() { mut pi := &IAbc(voidptr(0)) dump(pi) - assert f(pi) == '&IAbc(0)' + assert f(pi) == '&IAbc(0x0)' // i := IAbc(Abc{}) pi = &i diff --git a/vlib/v/tests/pointers_multilevel_casts_test.v b/vlib/v/tests/pointers_multilevel_casts_test.v index 3c55e58bf0..e7a658053e 100644 --- a/vlib/v/tests/pointers_multilevel_casts_test.v +++ b/vlib/v/tests/pointers_multilevel_casts_test.v @@ -9,10 +9,10 @@ fn test_byte_pointer_casts() { ppb := &&byte(2) pppb := &&&byte(3) ppppb := &&&&byte(4) - assert voidptr(pb).str() == '1' - assert voidptr(ppb).str() == '2' - assert voidptr(pppb).str() == '3' - assert voidptr(ppppb).str() == '4' + assert voidptr(pb).str() == '0x1' + assert voidptr(ppb).str() == '0x2' + assert voidptr(pppb).str() == '0x3' + assert voidptr(ppppb).str() == '0x4' } } @@ -22,10 +22,10 @@ fn test_char_pointer_casts() { ppc := &&char(6) pppc := &&&char(7) ppppc := &&&&char(8) - assert voidptr(pc).str() == '5' - assert voidptr(ppc).str() == '6' - assert voidptr(pppc).str() == '7' - assert voidptr(ppppc).str() == '8' + assert voidptr(pc).str() == '0x5' + assert voidptr(ppc).str() == '0x6' + assert voidptr(pppc).str() == '0x7' + assert voidptr(ppppc).str() == '0x8' } } @@ -35,10 +35,10 @@ fn test_struct_pointer_casts() { pps := &&Struct(10) ppps := &&&Struct(11) pppps := &&&&Struct(12) - assert voidptr(ps).str() == '9' - assert voidptr(pps).str() == 'a' - assert voidptr(ppps).str() == 'b' - assert voidptr(pppps).str() == 'c' + assert voidptr(ps).str() == '0x9' + assert voidptr(pps).str() == '0xa' + assert voidptr(ppps).str() == '0xb' + assert voidptr(pppps).str() == '0xc' } }