From 94f4dfe85b2338dde8ef249013c81ee2854d3896 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 9 Aug 2020 04:20:35 +0200 Subject: [PATCH] builtin: a temporary []byte.str2() (until runes are implemented) --- vlib/builtin/int.v | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index e254bf0dac..2fa9a2fd08 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -273,10 +273,10 @@ fn u64_to_hex(nn u64, len byte) string { mut n := nn mut buf := [256]byte buf[len] = `\0` - mut i := 0 + mut i := 0 for i=len-1; i>=0; i-- { d := byte(n & 0xF) - x := if d < 10 { d + `0` } else { d + 87 } + x := if d < 10 { d + `0` } else { d + 87 } buf[i] = x n = n >> 4 } @@ -294,7 +294,7 @@ fn u64_to_hex_no_leading_zeros(nn u64, len byte) string { mut i := 0 for i=len-1; i>=0; i-- { d := byte(n & 0xF) - x := if d < 10 { d + `0` } else { d + 87 } + x := if d < 10 { d + `0` } else { d + 87 } buf[i] = x n = n >> 4 if n == 0 { @@ -411,6 +411,18 @@ pub fn (b []byte) clone() []byte { return res } +pub fn (b []byte) str2() string { + return bytes2string(b) +} + +// TODO copy pasted from builder.v +fn bytes2string(b []byte) string { + mut copy := b.clone() + copy << `\0` + res := tos(copy.data, copy.len-1) + return res +} + // TODO generic pub fn (a []byte) contains(val byte) bool {