2020-02-03 05:00:36 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 20:20:28 +02:00
|
|
|
module builtin
|
|
|
|
|
2020-03-11 00:38:11 +01:00
|
|
|
/*
|
|
|
|
// old function for reference
|
|
|
|
pub fn (nn int) str1() string {
|
2019-06-28 14:19:46 +02:00
|
|
|
mut n := nn
|
2019-06-22 20:20:28 +02:00
|
|
|
if n == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 16
|
2020-03-04 17:08:28 +01:00
|
|
|
mut buf := vcalloc(max + 1)
|
2019-06-22 20:20:28 +02:00
|
|
|
mut len := 0
|
2019-06-28 14:19:46 +02:00
|
|
|
mut is_neg := false
|
2019-06-22 20:20:28 +02:00
|
|
|
if n < 0 {
|
|
|
|
n = -n
|
|
|
|
is_neg = true
|
|
|
|
}
|
|
|
|
// Fill the string from the end
|
|
|
|
for n > 0 {
|
|
|
|
d := n % 10
|
2020-02-07 22:10:48 +01:00
|
|
|
buf[max - len - 1] = d + int(`0`)
|
2019-06-22 20:20:28 +02:00
|
|
|
len++
|
|
|
|
n = n / 10
|
|
|
|
}
|
|
|
|
// Prepend - if it's negative
|
|
|
|
if is_neg {
|
|
|
|
buf[max - len - 1] = `-`
|
|
|
|
len++
|
|
|
|
}
|
2019-12-09 14:41:07 +01:00
|
|
|
buf[max] = `\0`
|
2019-06-22 20:20:28 +02:00
|
|
|
return tos(buf + max - len, len)
|
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
*/
|
|
|
|
// ----- value to string functions -----
|
|
|
|
/*
|
|
|
|
// old function for reference
|
|
|
|
pub fn ptr_str(ptr voidptr) string {
|
|
|
|
buf := malloc(sizeof(double) * 5 + 1) // TODO
|
|
|
|
C.sprintf((buf), '%p', ptr)
|
|
|
|
return tos(buf, vstrlen(buf))
|
|
|
|
}
|
|
|
|
*/
|
2021-01-05 16:45:36 +01:00
|
|
|
// ptr_str returns the address of `ptr` as a `string`.
|
2020-03-11 00:38:11 +01:00
|
|
|
pub fn ptr_str(ptr voidptr) string {
|
|
|
|
buf1 := u64(ptr).hex()
|
|
|
|
return buf1
|
|
|
|
}
|
|
|
|
|
2020-12-22 08:32:32 +01:00
|
|
|
const (
|
|
|
|
digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
|
2020-03-11 00:38:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// This implementation is the quickest with gcc -O2
|
2021-01-05 16:45:36 +01:00
|
|
|
// str_l returns the string representation of the integer `max`.
|
2020-03-11 00:38:11 +01:00
|
|
|
[inline]
|
2021-01-05 16:45:36 +01:00
|
|
|
fn (nn int) str_l(max int) string {
|
2020-09-18 11:56:16 +02:00
|
|
|
mut n := i64(nn)
|
2020-03-11 00:38:11 +01:00
|
|
|
mut d := 0
|
|
|
|
if n == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
mut buf := malloc(max + 1)
|
|
|
|
mut is_neg := false
|
|
|
|
if n < 0 {
|
|
|
|
n = -n
|
|
|
|
is_neg = true
|
|
|
|
}
|
|
|
|
mut index := max
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
buf[index--] = `\0`
|
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
for n > 0 {
|
2020-09-18 11:56:16 +02:00
|
|
|
n1 := int(n / 100)
|
|
|
|
d = ((int(n) - (n1 * 100)) << 1)
|
2020-03-11 00:38:11 +01:00
|
|
|
n = n1
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
buf[index--] = digit_pairs.str[d++]
|
|
|
|
buf[index--] = digit_pairs.str[d]
|
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
}
|
|
|
|
index++
|
|
|
|
// remove head zero
|
|
|
|
if d < 20 {
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
// Prepend - if it's negative
|
|
|
|
if is_neg {
|
|
|
|
index--
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
buf[index] = `-`
|
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe {
|
2020-12-22 08:32:32 +01:00
|
|
|
C.memmove(buf, buf + index, (max - index) + 1)
|
|
|
|
return tos(buf, (max - index))
|
2020-07-03 18:10:10 +02:00
|
|
|
}
|
2020-12-22 08:32:32 +01:00
|
|
|
// return tos(buf + index, (max-index))
|
2020-03-11 00:38:11 +01:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// str returns the value of the `i8` as a `string`.
|
|
|
|
// Example: assert i8(-2).str() == '-2'
|
2019-12-19 21:52:45 +01:00
|
|
|
pub fn (n i8) str() string {
|
2020-03-11 00:38:11 +01:00
|
|
|
return int(n).str_l(5)
|
2019-12-19 21:52:45 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// str returns the value of the `i16` as a `string`.
|
|
|
|
// Example: assert i16(-20).str() == '-20'
|
2019-12-19 21:52:45 +01:00
|
|
|
pub fn (n i16) str() string {
|
2020-03-11 00:38:11 +01:00
|
|
|
return int(n).str_l(7)
|
2019-12-19 21:52:45 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// str returns the value of the `u16` as a `string`.
|
|
|
|
// Example: assert u16(20).str() == '20'
|
2019-12-19 21:52:45 +01:00
|
|
|
pub fn (n u16) str() string {
|
2020-03-11 00:38:11 +01:00
|
|
|
return int(n).str_l(7)
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// str returns the value of the `int` as a `string`.
|
|
|
|
// Example: assert int(-2020).str() == '-2020'
|
2020-03-11 00:38:11 +01:00
|
|
|
pub fn (n int) str() string {
|
|
|
|
return n.str_l(12)
|
2019-12-19 21:52:45 +01:00
|
|
|
}
|
2019-12-03 19:14:17 +01:00
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// str returns the value of the `u32` as a `string`.
|
|
|
|
// Example: assert u32(20000).str() == '20000'
|
2019-08-12 01:58:08 +02:00
|
|
|
pub fn (nn u32) str() string {
|
2019-12-19 21:52:45 +01:00
|
|
|
mut n := nn
|
2020-03-11 00:38:11 +01:00
|
|
|
mut d := u32(0)
|
2020-02-07 22:10:48 +01:00
|
|
|
if n == 0 {
|
2019-08-12 01:58:08 +02:00
|
|
|
return '0'
|
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
max := 12
|
|
|
|
mut buf := malloc(max + 1)
|
|
|
|
mut index := max
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
buf[index--] = `\0`
|
|
|
|
}
|
2020-02-07 22:10:48 +01:00
|
|
|
for n > 0 {
|
2020-03-11 00:38:11 +01:00
|
|
|
n1 := n / u32(100)
|
|
|
|
d = ((n - (n1 * u32(100))) << u32(1))
|
|
|
|
n = n1
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
buf[index--] = digit_pairs[d++]
|
|
|
|
buf[index--] = digit_pairs[d]
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
index++
|
|
|
|
// remove head zero
|
|
|
|
if d < u32(20) {
|
|
|
|
index++
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe {
|
2020-12-22 08:32:32 +01:00
|
|
|
C.memmove(buf, buf + index, (max - index) + 1)
|
|
|
|
return tos(buf, (max - index))
|
2020-07-03 18:10:10 +02:00
|
|
|
}
|
2020-12-22 08:32:32 +01:00
|
|
|
// return tos(buf + index, (max-index))
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-12-19 21:52:45 +01:00
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// str returns the value of the `any_int` as a `string`.
|
2020-05-27 05:42:48 +02:00
|
|
|
[inline]
|
|
|
|
pub fn (n any_int) str() string {
|
|
|
|
return i64(n).str()
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// str returns the value of the `i64` as a `string`.
|
|
|
|
// Example: assert i64(-200000).str() == '-200000'
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (nn i64) str() string {
|
2019-06-28 14:19:46 +02:00
|
|
|
mut n := nn
|
2020-03-11 00:38:11 +01:00
|
|
|
mut d := i64(0)
|
2020-02-07 22:10:48 +01:00
|
|
|
if n == 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
return '0'
|
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
max := 20
|
|
|
|
mut buf := vcalloc(max + 1)
|
2019-06-28 14:19:46 +02:00
|
|
|
mut is_neg := false
|
2020-02-07 22:10:48 +01:00
|
|
|
if n < 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
n = -n
|
|
|
|
is_neg = true
|
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
mut index := max
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
buf[index--] = `\0`
|
|
|
|
}
|
2020-02-07 14:49:14 +01:00
|
|
|
for n > 0 {
|
2020-03-11 00:38:11 +01:00
|
|
|
n1 := n / i64(100)
|
|
|
|
d = ((n - (n1 * i64(100))) << i64(1))
|
|
|
|
n = n1
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
buf[index--] = digit_pairs[d++]
|
|
|
|
buf[index--] = digit_pairs[d]
|
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
}
|
|
|
|
index++
|
|
|
|
// remove head zero
|
|
|
|
if d < i64(20) {
|
|
|
|
index++
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Prepend - if it's negative
|
|
|
|
if is_neg {
|
2020-03-11 00:38:11 +01:00
|
|
|
index--
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
buf[index] = `-`
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe {
|
2020-12-22 08:32:32 +01:00
|
|
|
C.memmove(buf, buf + index, (max - index) + 1)
|
|
|
|
return tos(buf, (max - index))
|
2020-07-03 18:10:10 +02:00
|
|
|
}
|
2020-12-22 08:32:32 +01:00
|
|
|
// return tos(buf + index, (max-index))
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// str returns the value of the `u64` as a `string`.
|
|
|
|
// Example: assert u64(2000000).str() == '2000000'
|
2019-08-12 01:58:08 +02:00
|
|
|
pub fn (nn u64) str() string {
|
2019-12-19 21:52:45 +01:00
|
|
|
mut n := nn
|
2020-05-27 05:42:48 +02:00
|
|
|
mut d := u64(0)
|
2020-02-07 14:49:14 +01:00
|
|
|
if n == 0 {
|
2019-08-12 01:58:08 +02:00
|
|
|
return '0'
|
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
max := 20
|
|
|
|
mut buf := vcalloc(max + 1)
|
|
|
|
mut index := max
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
buf[index--] = `\0`
|
|
|
|
}
|
2020-02-07 14:49:14 +01:00
|
|
|
for n > 0 {
|
2020-03-11 00:38:11 +01:00
|
|
|
n1 := n / 100
|
|
|
|
d = ((n - (n1 * 100)) << 1)
|
|
|
|
n = n1
|
2020-07-15 21:56:50 +02:00
|
|
|
unsafe {
|
|
|
|
buf[index--] = digit_pairs[d++]
|
|
|
|
buf[index--] = digit_pairs[d]
|
|
|
|
}
|
2019-08-12 01:58:08 +02:00
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
index++
|
|
|
|
// remove head zero
|
|
|
|
if d < 20 {
|
|
|
|
index++
|
|
|
|
}
|
2020-07-03 18:10:10 +02:00
|
|
|
unsafe {
|
2020-12-22 08:32:32 +01:00
|
|
|
C.memmove(buf, buf + index, (max - index) + 1)
|
|
|
|
return tos(buf, (max - index))
|
2020-07-03 18:10:10 +02:00
|
|
|
}
|
2020-12-22 08:32:32 +01:00
|
|
|
// return tos(buf + index, (max-index))
|
2019-08-12 01:58:08 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// str returns the value of the `bool` as a `string`.
|
|
|
|
// Example: assert (2 > 1).str() == 'true'
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (b bool) str() string {
|
2019-06-22 20:20:28 +02:00
|
|
|
if b {
|
|
|
|
return 'true'
|
|
|
|
}
|
|
|
|
return 'false'
|
|
|
|
}
|
|
|
|
|
2020-03-11 00:38:11 +01:00
|
|
|
// ----- value to hex string functions -----
|
|
|
|
/*
|
|
|
|
//old function for reference
|
|
|
|
pub fn (n int) hex1() string {
|
2019-12-19 21:52:45 +01:00
|
|
|
len := if n >= 0 { n.str().len + 3 } else { 11 }
|
2019-08-20 13:34:29 +02:00
|
|
|
hex := malloc(len) // 0x + \n
|
2020-02-07 14:49:14 +01:00
|
|
|
count := C.sprintf((hex), '0x%x', n)
|
2019-07-01 17:00:09 +02:00
|
|
|
return tos(hex, count)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
*/
|
2021-01-05 16:45:36 +01:00
|
|
|
// u64_to_hex converts the number `nn` to a (zero padded if necessary) hexadecimal `string`.
|
2020-08-01 23:06:08 +02:00
|
|
|
[inline]
|
|
|
|
fn u64_to_hex(nn u64, len byte) string {
|
|
|
|
mut n := nn
|
2020-08-16 04:54:05 +02:00
|
|
|
mut buf := [256]byte{}
|
2020-08-01 23:06:08 +02:00
|
|
|
buf[len] = `\0`
|
2020-08-09 04:20:35 +02:00
|
|
|
mut i := 0
|
2020-12-22 08:32:32 +01:00
|
|
|
for i = len - 1; i >= 0; i-- {
|
2020-08-01 23:06:08 +02:00
|
|
|
d := byte(n & 0xF)
|
2020-08-09 04:20:35 +02:00
|
|
|
x := if d < 10 { d + `0` } else { d + 87 }
|
2020-08-01 23:06:08 +02:00
|
|
|
buf[i] = x
|
|
|
|
n = n >> 4
|
|
|
|
}
|
|
|
|
return string{
|
|
|
|
str: memdup(buf, len + 1)
|
|
|
|
len: len
|
2020-04-02 17:16:17 +02:00
|
|
|
}
|
2020-08-01 23:06:08 +02:00
|
|
|
}
|
2020-04-02 17:16:17 +02:00
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// u64_to_hex_no_leading_zeros converts the number `nn` to hexadecimal `string`.
|
2020-08-01 23:06:08 +02:00
|
|
|
[inline]
|
|
|
|
fn u64_to_hex_no_leading_zeros(nn u64, len byte) string {
|
2020-04-02 17:16:17 +02:00
|
|
|
mut n := nn
|
2020-08-16 04:54:05 +02:00
|
|
|
mut buf := [256]byte{}
|
2020-08-01 23:06:08 +02:00
|
|
|
buf[len] = `\0`
|
|
|
|
mut i := 0
|
2020-12-22 08:32:32 +01:00
|
|
|
for i = len - 1; i >= 0; i-- {
|
2020-08-01 23:06:08 +02:00
|
|
|
d := byte(n & 0xF)
|
2020-08-09 04:20:35 +02:00
|
|
|
x := if d < 10 { d + `0` } else { d + 87 }
|
2020-08-01 23:06:08 +02:00
|
|
|
buf[i] = x
|
2020-04-02 17:16:17 +02:00
|
|
|
n = n >> 4
|
2020-08-01 23:06:08 +02:00
|
|
|
if n == 0 {
|
|
|
|
break
|
2020-07-15 21:56:50 +02:00
|
|
|
}
|
2020-04-02 17:16:17 +02:00
|
|
|
}
|
2020-08-01 23:06:08 +02:00
|
|
|
res_len := len - i
|
|
|
|
return string{
|
|
|
|
str: memdup(&buf[i], res_len + 1)
|
|
|
|
len: res_len
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 17:16:17 +02:00
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `byte` as a hexadecimal `string`.
|
|
|
|
// Note that the output is zero padded for values below 16.
|
|
|
|
// Example: assert byte(2).hex() == '02'
|
|
|
|
// Example: assert byte(15).hex() == '0f'
|
|
|
|
// Example: assert byte(255).hex() == 'ff'
|
2020-08-01 23:06:08 +02:00
|
|
|
pub fn (nn byte) hex() string {
|
|
|
|
if nn == 0 {
|
|
|
|
return '00'
|
2020-07-03 18:10:10 +02:00
|
|
|
}
|
2020-08-01 23:06:08 +02:00
|
|
|
return u64_to_hex(nn, 2)
|
2020-04-02 17:16:17 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `i8` as a hexadecimal `string`.
|
|
|
|
// Note that the output is zero padded for values below 16.
|
|
|
|
// Example: assert i8(8).hex() == '08'
|
|
|
|
// Example: assert i8(10).hex() == '0a'
|
|
|
|
// Example: assert i8(15).hex() == '0f'
|
2020-04-02 17:16:17 +02:00
|
|
|
pub fn (nn i8) hex() string {
|
|
|
|
return byte(nn).hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `u16` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
|
|
|
// Example: assert u16(2).hex() == '2'
|
|
|
|
// Example: assert u16(200).hex() == 'c8'
|
2020-04-02 17:16:17 +02:00
|
|
|
pub fn (nn u16) hex() string {
|
|
|
|
if nn == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
2020-08-01 23:06:08 +02:00
|
|
|
return u64_to_hex_no_leading_zeros(nn, 4)
|
2020-04-02 17:16:17 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `i16` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
|
|
|
// Example: assert i16(2).hex() == '2'
|
|
|
|
// Example: assert i16(200).hex() == 'c8'
|
2020-04-02 17:16:17 +02:00
|
|
|
pub fn (nn i16) hex() string {
|
|
|
|
return u16(nn).hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `u32` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
|
|
|
// Example: assert u32(2).hex() == '2'
|
|
|
|
// Example: assert u32(200).hex() == 'c8'
|
2020-04-02 17:16:17 +02:00
|
|
|
pub fn (nn u32) hex() string {
|
|
|
|
if nn == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
2020-08-01 23:06:08 +02:00
|
|
|
return u64_to_hex_no_leading_zeros(nn, 8)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `int` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
|
|
|
// Example: assert int(2).hex() == '2'
|
|
|
|
// Example: assert int(200).hex() == 'c8'
|
2020-04-02 17:16:17 +02:00
|
|
|
pub fn (nn int) hex() string {
|
|
|
|
return u32(nn).hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex2 returns the value of the `int` as a `0x`-prefixed hexadecimal `string`.
|
|
|
|
// Note that the output after `0x` is ***not*** zero padded.
|
|
|
|
// Example: assert int(8).hex2() == '0x8'
|
|
|
|
// Example: assert int(15).hex2() == '0xf'
|
|
|
|
// Example: assert int(18).hex2() == '0x12'
|
2020-06-18 14:14:09 +02:00
|
|
|
pub fn (n int) hex2() string {
|
|
|
|
return '0x' + n.hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `u64` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
|
|
|
// Example: assert u64(2).hex() == '2'
|
|
|
|
// Example: assert u64(2000).hex() == '7d0'
|
2020-03-11 00:38:11 +01:00
|
|
|
pub fn (nn u64) hex() string {
|
2020-04-02 17:16:17 +02:00
|
|
|
if nn == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
2020-08-01 23:06:08 +02:00
|
|
|
return u64_to_hex_no_leading_zeros(nn, 16)
|
2020-01-28 23:43:09 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `i64` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
|
|
|
// Example: assert i64(2).hex() == '2'
|
|
|
|
// Example: assert i64(-200).hex() == 'ffffffffffffff38'
|
|
|
|
// Example: assert i64(2021).hex() == '7e5'
|
2020-03-11 00:38:11 +01:00
|
|
|
pub fn (nn i64) hex() string {
|
2020-04-02 17:16:17 +02:00
|
|
|
return u64(nn).hex()
|
2020-03-11 00:38:11 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `any_int` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
2020-05-27 05:42:48 +02:00
|
|
|
pub fn (nn any_int) hex() string {
|
|
|
|
return u64(nn).hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `voidptr` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
2020-05-04 13:20:18 +02:00
|
|
|
pub fn (nn voidptr) str() string {
|
|
|
|
return u64(nn).hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// hex returns the value of the `byteptr` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
2020-05-04 13:20:18 +02:00
|
|
|
pub fn (nn byteptr) str() string {
|
|
|
|
return u64(nn).hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
/*
|
|
|
|
pub fn (nn byte) hex_full() string { return u64_to_hex(nn, 2) }
|
|
|
|
pub fn (nn i8) hex_full() string { return u64_to_hex(byte(nn), 2) }
|
|
|
|
pub fn (nn u16) hex_full() string { return u64_to_hex(nn, 4) }
|
|
|
|
pub fn (nn i16) hex_full() string { return u64_to_hex(u16(nn), 4) }
|
|
|
|
pub fn (nn u32) hex_full() string { return u64_to_hex(nn, 8) }
|
|
|
|
pub fn (nn int) hex_full() string { return u64_to_hex(u32(nn), 8) }
|
|
|
|
*/
|
|
|
|
// hex_full returns the value of the `u64` as a *full* 16-digit hexadecimal `string`.
|
|
|
|
// Example: assert u64(2).hex_full() == '0000000000000002'
|
|
|
|
// Example: assert u64(255).hex_full() == '00000000000000ff'
|
2020-10-25 08:22:31 +01:00
|
|
|
pub fn (nn u64) hex_full() string {
|
|
|
|
return u64_to_hex(nn, 16)
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
/*
|
|
|
|
pub fn (nn i64) hex_full() string { return u64_to_hex(u64(nn), 16) }
|
|
|
|
pub fn (nn any_int) hex_full() string { return u64_to_hex(nn, 16) }
|
|
|
|
pub fn (nn voidptr) hex_full() string { return u64_to_hex(nn, 16) }
|
|
|
|
pub fn (nn byteptr) hex_full() string { return u64_to_hex(nn, 16) }
|
|
|
|
*/
|
|
|
|
// str returns the contents of `byte` as a zero terminated `string`.
|
|
|
|
// Example: assert byte(111).str() == 'o'
|
2020-08-27 06:46:18 +02:00
|
|
|
pub fn (b byte) str() string {
|
|
|
|
// TODO
|
2020-12-22 08:32:32 +01:00
|
|
|
// return int(b).str_l(7)
|
2020-08-26 06:50:32 +02:00
|
|
|
mut str := string{
|
|
|
|
str: malloc(2)
|
|
|
|
len: 1
|
|
|
|
}
|
|
|
|
unsafe {
|
2020-08-27 06:46:18 +02:00
|
|
|
str.str[0] = b
|
2020-08-26 06:50:32 +02:00
|
|
|
str.str[1] = `\0`
|
|
|
|
}
|
2020-12-22 08:32:32 +01:00
|
|
|
// println(str)
|
2020-08-26 06:50:32 +02:00
|
|
|
return str
|
|
|
|
}
|
2020-06-01 06:48:51 +02:00
|
|
|
|
2021-01-05 18:59:51 +01:00
|
|
|
pub fn (b byte) ascii_str() string {
|
|
|
|
mut str := string{
|
|
|
|
str: malloc(2)
|
|
|
|
len: 1
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
str.str[0] = b
|
|
|
|
str.str[1] = `\0`
|
|
|
|
}
|
|
|
|
// println(str)
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:36 +01:00
|
|
|
// str_escaped returns the contents of `byte` as an escaped `string`.
|
2021-01-05 17:22:17 +01:00
|
|
|
// Example: assert byte(0).str_escaped() == r'`\0`'
|
2020-09-18 01:12:32 +02:00
|
|
|
pub fn (b byte) str_escaped() string {
|
|
|
|
str := match b {
|
2021-01-05 17:22:17 +01:00
|
|
|
0 { r'`\0`' }
|
|
|
|
7 { r'`\a`' }
|
|
|
|
8 { r'`\b`' }
|
|
|
|
9 { r'`\t`' }
|
|
|
|
10 { r'`\n`' }
|
|
|
|
11 { r'`\v`' }
|
|
|
|
12 { r'`\f`' }
|
|
|
|
13 { r'`\r`' }
|
2021-01-05 18:59:51 +01:00
|
|
|
32...126 { b.ascii_str() }
|
2020-09-18 01:12:32 +02:00
|
|
|
else { '0x' + b.hex() }
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|