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

builtin: make byte.hex() two digits format (#5886)

This commit is contained in:
Duarte Roso 2020-08-01 23:06:08 +02:00 committed by GitHub
parent 4f37202b72
commit 055117dc5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 96 deletions

View File

@ -268,35 +268,53 @@ pub fn (n int) hex1() string {
}
*/
pub fn (nn byte) hex() string {
if nn == 0 {
return '0'
}
[inline]
fn u64_to_hex(nn u64, len byte) string {
mut n := nn
max := 2
mut buf := malloc(max + 1)
mut index := max
unsafe {
buf[index--] = `\0`
}
for n > 0 {
d := n & 0xF
mut buf := [256]byte
buf[len] = `\0`
mut i := 0
for i=len-1; i>=0; i-- {
d := byte(n & 0xF)
x := if d < 10 { d + `0` } else { d + 87 }
buf[i] = x
n = n >> 4
unsafe {
buf[index--] = if d < 10 { d + `0` } else { d + 87 }
}
return string{
str: memdup(buf, len + 1)
len: len
}
}
[inline]
fn u64_to_hex_no_leading_zeros(nn u64, len byte) string {
mut n := nn
mut buf := [256]byte
buf[len] = `\0`
mut i := 0
for i=len-1; i>=0; i-- {
d := byte(n & 0xF)
x := if d < 10 { d + `0` } else { d + 87 }
buf[i] = x
n = n >> 4
if n == 0 {
break
}
}
//buf[index--] = `x`
//buf[index] = `0`
index++
unsafe {
return tos(buf + index, (max - index))
res_len := len - i
return string{
str: memdup(&buf[i], res_len + 1)
len: res_len
}
}
pub fn (nn byte) hex() string {
if nn == 0 {
return '00'
}
return u64_to_hex(nn, 2)
}
pub fn (nn i8) hex() string {
return byte(nn).hex()
}
@ -305,29 +323,7 @@ pub fn (nn u16) hex() string {
if nn == 0 {
return '0'
}
mut n := nn
max := 5
mut buf := malloc(max + 1)
mut index := max
unsafe {
buf[index--] = `\0`
}
for n > 0 {
d := byte(n & 0xF)
n = n >> 4
unsafe {
buf[index--] = if d < 10 { d + `0` } else { d + 87 }
}
}
//buf[index--] = `x`
//buf[index] = `0`
index++
unsafe {
return tos(buf + index, (max - index))
}
return u64_to_hex_no_leading_zeros(nn, 4)
}
pub fn (nn i16) hex() string {
@ -338,29 +334,7 @@ pub fn (nn u32) hex() string {
if nn == 0 {
return '0'
}
mut n := nn
max := 10
mut buf := malloc(max + 1)
mut index := max
unsafe {
buf[index--] = `\0`
}
for n > 0 {
d := byte(n & 0xF)
n = n >> 4
unsafe {
buf[index--] = if d < 10 { d + `0` } else { d + 87 }
}
}
//buf[index--] = `x`
//buf[index] = `0`
index++
unsafe {
return tos(buf + index, (max - index))
}
return u64_to_hex_no_leading_zeros(nn, 8)
}
pub fn (nn int) hex() string {
@ -375,31 +349,7 @@ pub fn (nn u64) hex() string {
if nn == 0 {
return '0'
}
mut n := nn
max := 18
mut buf := malloc(max + 1)
mut index := max
unsafe {
buf[index--] = `\0`
}
for n > 0 {
d := byte(n & 0xF)
n = n >> 4
unsafe {
buf[index--] = if d < 10 { d + `0` } else { d + 87 }
}
}
//buf[index--] = `x`
//buf[index] = `0`
index++
unsafe {
C.memmove(buf,buf+index, (max-index)+1 )
return tos(buf, (max-index))
}
//return tos(buf + index, (max-index))
return u64_to_hex_no_leading_zeros(nn, 16)
}
pub fn (nn i64) hex() string {

View File

@ -196,9 +196,9 @@ fn test_int_to_hex() {
// --- int to hex tests
c0 := 12
// 8Bit
assert byte(0).hex() == '0'
assert byte(c0).hex() == 'c'
assert i8(c0).hex() == 'c'
assert byte(0).hex() == '00'
assert byte(c0).hex() == '0c'
assert i8(c0).hex() == '0c'
assert byte(127).hex() == '7f'
assert i8(127).hex() == '7f'
assert byte(255).hex() == 'ff'