2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-06-23 05:21:30 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 21:20:28 +03:00
|
|
|
module builtin
|
|
|
|
|
2021-05-02 16:31:29 +03:00
|
|
|
//
|
|
|
|
// ----- value to string functions -----
|
|
|
|
//
|
|
|
|
|
2022-04-15 18:25:45 +03:00
|
|
|
// type u8 = byte
|
2022-04-15 13:43:03 +03:00
|
|
|
type byte = u8
|
2021-09-08 14:48:49 +03:00
|
|
|
type i32 = int
|
2021-07-29 06:42:22 +03:00
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// ptr_str returns the address of `ptr` as a `string`.
|
2020-03-11 02:38:11 +03:00
|
|
|
pub fn ptr_str(ptr voidptr) string {
|
|
|
|
buf1 := u64(ptr).hex()
|
|
|
|
return buf1
|
|
|
|
}
|
|
|
|
|
2022-07-21 20:45:57 +03:00
|
|
|
// pub fn nil_str(x voidptr) string {
|
|
|
|
// return 'nil'
|
|
|
|
//}
|
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// str returns string equivalent of x
|
2021-09-08 13:41:08 +03:00
|
|
|
pub fn (x isize) str() string {
|
|
|
|
return i64(x).str()
|
|
|
|
}
|
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// str returns string equivalent of x
|
2021-09-08 13:41:08 +03:00
|
|
|
pub fn (x usize) str() string {
|
|
|
|
return u64(x).str()
|
|
|
|
}
|
2021-09-08 05:53:39 +03:00
|
|
|
|
2022-04-30 12:31:23 +03:00
|
|
|
// str returns string equivalent of cptr
|
2021-04-06 18:00:42 +03:00
|
|
|
pub fn (cptr &char) str() string {
|
|
|
|
return u64(cptr).hex()
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:32:32 +03:00
|
|
|
const (
|
2021-05-02 16:31:29 +03:00
|
|
|
// digit pairs in reverse order
|
2020-12-22 10:32:32 +03:00
|
|
|
digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
|
2020-03-11 02:38:11 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// This implementation is the quickest with gcc -O2
|
2021-05-02 16:31:29 +03:00
|
|
|
// str_l returns the string representation of the integer nn with max chars.
|
2021-07-26 08:06:59 +03:00
|
|
|
[direct_array_access; inline]
|
2021-01-05 18:45:36 +03:00
|
|
|
fn (nn int) str_l(max int) string {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
2021-05-02 16:31:29 +03:00
|
|
|
mut n := i64(nn)
|
|
|
|
mut d := 0
|
|
|
|
if n == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
2021-07-26 08:06:59 +03:00
|
|
|
|
2021-05-02 16:31:29 +03:00
|
|
|
mut is_neg := false
|
|
|
|
if n < 0 {
|
|
|
|
n = -n
|
|
|
|
is_neg = true
|
|
|
|
}
|
|
|
|
mut index := max
|
2021-06-15 14:47:11 +03:00
|
|
|
mut buf := malloc_noscan(max + 1)
|
2021-04-27 01:41:42 +03:00
|
|
|
buf[index] = 0
|
|
|
|
index--
|
2021-05-02 16:31:29 +03:00
|
|
|
|
|
|
|
for n > 0 {
|
|
|
|
n1 := int(n / 100)
|
|
|
|
// calculate the digit_pairs start index
|
2021-11-29 03:48:49 +03:00
|
|
|
d = int(u32(int(n) - (n1 * 100)) << 1)
|
2021-05-02 16:31:29 +03:00
|
|
|
n = n1
|
2021-04-27 01:41:42 +03:00
|
|
|
buf[index] = digit_pairs.str[d]
|
|
|
|
index--
|
|
|
|
d++
|
|
|
|
buf[index] = digit_pairs.str[d]
|
|
|
|
index--
|
2020-07-15 22:56:50 +03:00
|
|
|
}
|
2020-03-11 02:38:11 +03:00
|
|
|
index++
|
2021-05-02 16:31:29 +03:00
|
|
|
// remove head zero
|
|
|
|
if d < 20 {
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
// Prepend - if it's negative
|
|
|
|
if is_neg {
|
|
|
|
index--
|
2020-07-15 22:56:50 +03:00
|
|
|
buf[index] = `-`
|
|
|
|
}
|
2021-05-02 16:31:29 +03:00
|
|
|
diff := max - index
|
2022-04-15 14:45:52 +03:00
|
|
|
vmemmove(buf, voidptr(buf + index), diff + 1)
|
2021-05-02 16:31:29 +03:00
|
|
|
/*
|
|
|
|
// === manual memory move for bare metal ===
|
|
|
|
mut c:= 0
|
|
|
|
for c < diff {
|
|
|
|
buf[c] = buf[c+index]
|
|
|
|
c++
|
|
|
|
}
|
|
|
|
buf[c] = 0
|
|
|
|
*/
|
|
|
|
return tos(buf, diff)
|
|
|
|
|
2021-07-26 08:06:59 +03:00
|
|
|
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
|
2020-03-11 02:38:11 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// str returns the value of the `i8` as a `string`.
|
|
|
|
// Example: assert i8(-2).str() == '-2'
|
2019-12-19 23:52:45 +03:00
|
|
|
pub fn (n i8) str() string {
|
2020-03-11 02:38:11 +03:00
|
|
|
return int(n).str_l(5)
|
2019-12-19 23:52:45 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// str returns the value of the `i16` as a `string`.
|
|
|
|
// Example: assert i16(-20).str() == '-20'
|
2019-12-19 23:52:45 +03:00
|
|
|
pub fn (n i16) str() string {
|
2020-03-11 02:38:11 +03:00
|
|
|
return int(n).str_l(7)
|
2019-12-19 23:52:45 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// str returns the value of the `u16` as a `string`.
|
|
|
|
// Example: assert u16(20).str() == '20'
|
2019-12-19 23:52:45 +03:00
|
|
|
pub fn (n u16) str() string {
|
2020-03-11 02:38:11 +03:00
|
|
|
return int(n).str_l(7)
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// str returns the value of the `int` as a `string`.
|
|
|
|
// Example: assert int(-2020).str() == '-2020'
|
2020-03-11 02:38:11 +03:00
|
|
|
pub fn (n int) str() string {
|
|
|
|
return n.str_l(12)
|
2019-12-19 23:52:45 +03:00
|
|
|
}
|
2019-12-03 21:14:17 +03:00
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// str returns the value of the `u32` as a `string`.
|
|
|
|
// Example: assert u32(20000).str() == '20000'
|
2021-07-26 08:06:59 +03:00
|
|
|
[direct_array_access; inline]
|
2019-08-12 02:58:08 +03:00
|
|
|
pub fn (nn u32) str() string {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
2021-05-02 16:31:29 +03:00
|
|
|
mut n := nn
|
|
|
|
mut d := u32(0)
|
|
|
|
if n == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 12
|
2021-06-15 14:47:11 +03:00
|
|
|
mut buf := malloc_noscan(max + 1)
|
2021-05-02 16:31:29 +03:00
|
|
|
mut index := max
|
2021-04-27 01:41:42 +03:00
|
|
|
buf[index] = 0
|
|
|
|
index--
|
2021-05-02 16:31:29 +03:00
|
|
|
for n > 0 {
|
|
|
|
n1 := n / u32(100)
|
|
|
|
d = ((n - (n1 * u32(100))) << u32(1))
|
|
|
|
n = n1
|
2021-04-27 01:41:42 +03:00
|
|
|
buf[index] = digit_pairs[d]
|
|
|
|
index--
|
|
|
|
d++
|
|
|
|
buf[index] = digit_pairs[d]
|
|
|
|
index--
|
2020-07-15 22:56:50 +03:00
|
|
|
}
|
2020-03-11 02:38:11 +03:00
|
|
|
index++
|
2021-05-02 16:31:29 +03:00
|
|
|
// remove head zero
|
|
|
|
if d < u32(20) {
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
diff := max - index
|
2022-04-15 14:45:52 +03:00
|
|
|
vmemmove(buf, voidptr(buf + index), diff + 1)
|
2021-05-02 16:31:29 +03:00
|
|
|
return tos(buf, diff)
|
|
|
|
|
2021-07-26 08:06:59 +03:00
|
|
|
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-19 23:52:45 +03:00
|
|
|
|
2021-01-12 00:58:15 +03:00
|
|
|
// str returns the value of the `int_literal` as a `string`.
|
2020-05-27 06:42:48 +03:00
|
|
|
[inline]
|
2021-01-12 00:58:15 +03:00
|
|
|
pub fn (n int_literal) str() string {
|
2020-05-27 06:42:48 +03:00
|
|
|
return i64(n).str()
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// str returns the value of the `i64` as a `string`.
|
|
|
|
// Example: assert i64(-200000).str() == '-200000'
|
2021-07-26 08:06:59 +03:00
|
|
|
[direct_array_access; inline]
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (nn i64) str() string {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
2021-05-02 16:31:29 +03:00
|
|
|
mut n := nn
|
|
|
|
mut d := i64(0)
|
|
|
|
if n == 0 {
|
|
|
|
return '0'
|
2022-10-17 19:36:46 +03:00
|
|
|
} else if n == i64(-9223372036854775807 - 1) {
|
|
|
|
// math.min_i64
|
|
|
|
return '-9223372036854775808'
|
2021-05-02 16:31:29 +03:00
|
|
|
}
|
|
|
|
max := 20
|
2021-06-15 14:47:11 +03:00
|
|
|
mut buf := malloc_noscan(max + 1)
|
2021-05-02 16:31:29 +03:00
|
|
|
mut is_neg := false
|
|
|
|
if n < 0 {
|
|
|
|
n = -n
|
|
|
|
is_neg = true
|
|
|
|
}
|
|
|
|
mut index := max
|
2021-04-27 01:41:42 +03:00
|
|
|
buf[index] = 0
|
|
|
|
index--
|
2021-05-02 16:31:29 +03:00
|
|
|
for n > 0 {
|
|
|
|
n1 := n / i64(100)
|
2021-11-29 03:48:49 +03:00
|
|
|
d = (u32(n - (n1 * i64(100))) << i64(1))
|
2021-05-02 16:31:29 +03:00
|
|
|
n = n1
|
2021-04-27 01:41:42 +03:00
|
|
|
buf[index] = digit_pairs[d]
|
|
|
|
index--
|
|
|
|
d++
|
|
|
|
buf[index] = digit_pairs[d]
|
|
|
|
index--
|
2020-07-15 22:56:50 +03:00
|
|
|
}
|
2020-03-11 02:38:11 +03:00
|
|
|
index++
|
2021-05-02 16:31:29 +03:00
|
|
|
// remove head zero
|
|
|
|
if d < i64(20) {
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
// Prepend - if it's negative
|
|
|
|
if is_neg {
|
|
|
|
index--
|
2020-07-15 22:56:50 +03:00
|
|
|
buf[index] = `-`
|
|
|
|
}
|
2021-05-02 16:31:29 +03:00
|
|
|
diff := max - index
|
2022-04-15 14:45:52 +03:00
|
|
|
vmemmove(buf, voidptr(buf + index), diff + 1)
|
2021-05-02 16:31:29 +03:00
|
|
|
return tos(buf, diff)
|
2021-07-26 08:06:59 +03:00
|
|
|
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// str returns the value of the `u64` as a `string`.
|
|
|
|
// Example: assert u64(2000000).str() == '2000000'
|
2021-07-26 08:06:59 +03:00
|
|
|
[direct_array_access; inline]
|
2019-08-12 02:58:08 +03:00
|
|
|
pub fn (nn u64) str() string {
|
2020-07-15 22:56:50 +03:00
|
|
|
unsafe {
|
2021-05-02 16:31:29 +03:00
|
|
|
mut n := nn
|
|
|
|
mut d := u64(0)
|
|
|
|
if n == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 20
|
2021-06-15 14:47:11 +03:00
|
|
|
mut buf := malloc_noscan(max + 1)
|
2021-05-02 16:31:29 +03:00
|
|
|
mut index := max
|
2021-04-27 01:41:42 +03:00
|
|
|
buf[index] = 0
|
|
|
|
index--
|
2021-05-02 16:31:29 +03:00
|
|
|
for n > 0 {
|
|
|
|
n1 := n / 100
|
|
|
|
d = ((n - (n1 * 100)) << 1)
|
|
|
|
n = n1
|
2021-04-27 01:41:42 +03:00
|
|
|
buf[index] = digit_pairs[d]
|
|
|
|
index--
|
|
|
|
d++
|
|
|
|
buf[index] = digit_pairs[d]
|
|
|
|
index--
|
2020-07-15 22:56:50 +03:00
|
|
|
}
|
2020-03-11 02:38:11 +03:00
|
|
|
index++
|
2021-05-02 16:31:29 +03:00
|
|
|
// remove head zero
|
|
|
|
if d < 20 {
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
diff := max - index
|
2022-04-15 14:45:52 +03:00
|
|
|
vmemmove(buf, voidptr(buf + index), diff + 1)
|
2021-05-02 16:31:29 +03:00
|
|
|
return tos(buf, diff)
|
2021-07-26 08:06:59 +03:00
|
|
|
// return tos(memdup(&buf[0] + index, (max - index)), (max - index))
|
2020-03-11 02:38:11 +03:00
|
|
|
}
|
2019-08-12 02:58:08 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// str returns the value of the `bool` as a `string`.
|
|
|
|
// Example: assert (2 > 1).str() == 'true'
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (b bool) str() string {
|
2019-06-22 21:20:28 +03:00
|
|
|
if b {
|
|
|
|
return 'true'
|
|
|
|
}
|
|
|
|
return 'false'
|
|
|
|
}
|
|
|
|
|
2021-05-02 16:31:29 +03:00
|
|
|
//
|
2020-03-11 02:38:11 +03:00
|
|
|
// ----- value to hex string functions -----
|
2021-05-02 16:31:29 +03:00
|
|
|
//
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// u64_to_hex converts the number `nn` to a (zero padded if necessary) hexadecimal `string`.
|
2021-07-26 08:06:59 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 14:45:52 +03:00
|
|
|
fn u64_to_hex(nn u64, len u8) string {
|
2020-08-02 00:06:08 +03:00
|
|
|
mut n := nn
|
2022-04-15 14:45:52 +03:00
|
|
|
mut buf := [17]u8{}
|
2021-04-13 11:29:33 +03:00
|
|
|
buf[len] = 0
|
2020-08-09 05:20:35 +03:00
|
|
|
mut i := 0
|
2020-12-22 10:32:32 +03:00
|
|
|
for i = len - 1; i >= 0; i-- {
|
2022-04-15 14:45:52 +03:00
|
|
|
d := u8(n & 0xF)
|
2021-08-22 11:10:24 +03:00
|
|
|
buf[i] = if d < 10 { d + `0` } else { d + 87 }
|
2020-08-02 00:06:08 +03:00
|
|
|
n = n >> 4
|
|
|
|
}
|
2021-03-15 16:55:07 +03:00
|
|
|
return unsafe { tos(memdup(&buf[0], len + 1), len) }
|
2020-08-02 00:06:08 +03:00
|
|
|
}
|
2020-04-02 18:16:17 +03:00
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// u64_to_hex_no_leading_zeros converts the number `nn` to hexadecimal `string`.
|
2021-07-26 08:06:59 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 14:45:52 +03:00
|
|
|
fn u64_to_hex_no_leading_zeros(nn u64, len u8) string {
|
2020-04-02 18:16:17 +03:00
|
|
|
mut n := nn
|
2022-04-15 14:45:52 +03:00
|
|
|
mut buf := [17]u8{}
|
2021-04-13 11:29:33 +03:00
|
|
|
buf[len] = 0
|
2020-08-02 00:06:08 +03:00
|
|
|
mut i := 0
|
2020-12-22 10:32:32 +03:00
|
|
|
for i = len - 1; i >= 0; i-- {
|
2022-04-15 14:45:52 +03:00
|
|
|
d := u8(n & 0xF)
|
2021-08-22 11:10:24 +03:00
|
|
|
buf[i] = if d < 10 { d + `0` } else { d + 87 }
|
2020-04-02 18:16:17 +03:00
|
|
|
n = n >> 4
|
2020-08-02 00:06:08 +03:00
|
|
|
if n == 0 {
|
|
|
|
break
|
2020-07-15 22:56:50 +03:00
|
|
|
}
|
2020-04-02 18:16:17 +03:00
|
|
|
}
|
2020-08-02 00:06:08 +03:00
|
|
|
res_len := len - i
|
2021-03-14 19:21:45 +03:00
|
|
|
return unsafe { tos(memdup(&buf[i], res_len + 1), res_len) }
|
2020-08-02 00:06:08 +03:00
|
|
|
}
|
2020-04-02 18:16:17 +03:00
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// hex returns the value of the `byte` as a hexadecimal `string`.
|
|
|
|
// Note that the output is zero padded for values below 16.
|
2022-04-15 14:58:56 +03:00
|
|
|
// Example: assert u8(2).hex() == '02'
|
|
|
|
// Example: assert u8(15).hex() == '0f'
|
|
|
|
// Example: assert u8(255).hex() == 'ff'
|
2022-04-15 14:45:52 +03:00
|
|
|
pub fn (nn u8) hex() string {
|
2020-08-02 00:06:08 +03:00
|
|
|
if nn == 0 {
|
|
|
|
return '00'
|
2020-07-03 19:10:10 +03:00
|
|
|
}
|
2020-08-02 00:06:08 +03:00
|
|
|
return u64_to_hex(nn, 2)
|
2020-04-02 18:16:17 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03: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 18:16:17 +03:00
|
|
|
pub fn (nn i8) hex() string {
|
2021-08-22 11:10:24 +03:00
|
|
|
if nn == 0 {
|
|
|
|
return '00'
|
|
|
|
}
|
|
|
|
return u64_to_hex(u64(nn), 2)
|
2020-04-02 18:16:17 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03: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 18:16:17 +03:00
|
|
|
pub fn (nn u16) hex() string {
|
|
|
|
if nn == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
2020-08-02 00:06:08 +03:00
|
|
|
return u64_to_hex_no_leading_zeros(nn, 4)
|
2020-04-02 18:16:17 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03: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 18:16:17 +03:00
|
|
|
pub fn (nn i16) hex() string {
|
|
|
|
return u16(nn).hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03: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 18:16:17 +03:00
|
|
|
pub fn (nn u32) hex() string {
|
|
|
|
if nn == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
2020-08-02 00:06:08 +03:00
|
|
|
return u64_to_hex_no_leading_zeros(nn, 8)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03: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 18:16:17 +03:00
|
|
|
pub fn (nn int) hex() string {
|
|
|
|
return u32(nn).hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03: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 15:14:09 +03:00
|
|
|
pub fn (n int) hex2() string {
|
|
|
|
return '0x' + n.hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03: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 02:38:11 +03:00
|
|
|
pub fn (nn u64) hex() string {
|
2020-04-02 18:16:17 +03:00
|
|
|
if nn == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
2020-08-02 00:06:08 +03:00
|
|
|
return u64_to_hex_no_leading_zeros(nn, 16)
|
2020-01-29 01:43:09 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03: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 02:38:11 +03:00
|
|
|
pub fn (nn i64) hex() string {
|
2020-04-02 18:16:17 +03:00
|
|
|
return u64(nn).hex()
|
2020-03-11 02:38:11 +03:00
|
|
|
}
|
|
|
|
|
2021-01-12 00:58:15 +03:00
|
|
|
// hex returns the value of the `int_literal` as a hexadecimal `string`.
|
2021-01-05 18:45:36 +03:00
|
|
|
// Note that the output is ***not*** zero padded.
|
2021-01-12 00:58:15 +03:00
|
|
|
pub fn (nn int_literal) hex() string {
|
2020-05-27 06:42:48 +03:00
|
|
|
return u64(nn).hex()
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// hex returns the value of the `voidptr` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
2020-05-04 14:20:18 +03:00
|
|
|
pub fn (nn voidptr) str() string {
|
2021-12-30 23:34:24 +03:00
|
|
|
return '0x' + u64(nn).hex()
|
2020-05-04 14:20:18 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// hex returns the value of the `byteptr` as a hexadecimal `string`.
|
|
|
|
// Note that the output is ***not*** zero padded.
|
2021-07-26 08:06:59 +03:00
|
|
|
// pub fn (nn byteptr) str() string {
|
2020-05-04 14:20:18 +03:00
|
|
|
pub fn (nn byteptr) str() string {
|
2021-12-30 23:34:24 +03:00
|
|
|
return '0x' + u64(nn).hex()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn charptr) str() string {
|
|
|
|
return '0x' + u64(nn).hex()
|
2020-05-04 14:20:18 +03:00
|
|
|
}
|
|
|
|
|
2022-04-15 14:45:52 +03:00
|
|
|
pub fn (nn u8) hex_full() string {
|
2021-07-26 08:06:59 +03:00
|
|
|
return u64_to_hex(u64(nn), 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn i8) hex_full() string {
|
|
|
|
return u64_to_hex(u64(nn), 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn u16) hex_full() string {
|
|
|
|
return u64_to_hex(u64(nn), 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn i16) hex_full() string {
|
|
|
|
return u64_to_hex(u64(nn), 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn u32) hex_full() string {
|
|
|
|
return u64_to_hex(u64(nn), 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn int) hex_full() string {
|
|
|
|
return u64_to_hex(u64(nn), 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn i64) hex_full() string {
|
|
|
|
return u64_to_hex(u64(nn), 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn voidptr) hex_full() string {
|
|
|
|
return u64_to_hex(u64(nn), 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn int_literal) hex_full() string {
|
|
|
|
return u64_to_hex(u64(nn), 16)
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// 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 10:22:31 +03:00
|
|
|
pub fn (nn u64) hex_full() string {
|
|
|
|
return u64_to_hex(nn, 16)
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// str returns the contents of `byte` as a zero terminated `string`.
|
2022-04-03 23:41:02 +03:00
|
|
|
// See also: [`byte.ascii_str`](#byte.ascii_str)
|
2022-04-15 14:58:56 +03:00
|
|
|
// Example: assert u8(111).str() == '111'
|
2022-04-15 14:45:52 +03:00
|
|
|
pub fn (b u8) str() string {
|
2021-01-05 21:26:48 +03:00
|
|
|
return int(b).str_l(7)
|
2020-08-26 07:50:32 +03:00
|
|
|
}
|
2020-06-01 07:48:51 +03:00
|
|
|
|
2021-01-06 21:02:04 +03:00
|
|
|
// ascii_str returns the contents of `byte` as a zero terminated ASCII `string` character.
|
2022-04-15 14:58:56 +03:00
|
|
|
// Example: assert u8(97).ascii_str() == 'a'
|
2022-04-15 14:45:52 +03:00
|
|
|
pub fn (b u8) ascii_str() string {
|
2021-01-05 20:59:51 +03:00
|
|
|
mut str := string{
|
2021-06-15 14:47:11 +03:00
|
|
|
str: unsafe { malloc_noscan(2) }
|
2021-01-05 20:59:51 +03:00
|
|
|
len: 1
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
str.str[0] = b
|
2021-04-13 11:29:33 +03:00
|
|
|
str.str[1] = 0
|
2021-01-05 20:59:51 +03:00
|
|
|
}
|
|
|
|
// println(str)
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2021-01-05 18:45:36 +03:00
|
|
|
// str_escaped returns the contents of `byte` as an escaped `string`.
|
2022-04-15 14:58:56 +03:00
|
|
|
// Example: assert u8(0).str_escaped() == r'`\0`'
|
2022-01-11 16:14:42 +03:00
|
|
|
[manualfree]
|
2022-04-15 14:45:52 +03:00
|
|
|
pub fn (b u8) str_escaped() string {
|
2020-09-18 02:12:32 +03:00
|
|
|
str := match b {
|
2022-01-11 16:14:42 +03: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`'
|
|
|
|
}
|
|
|
|
27 {
|
|
|
|
r'`\e`'
|
|
|
|
}
|
|
|
|
32...126 {
|
|
|
|
b.ascii_str()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
xx := b.hex()
|
|
|
|
yy := '0x' + xx
|
|
|
|
unsafe { xx.free() }
|
|
|
|
yy
|
|
|
|
}
|
2020-09-18 02:12:32 +03:00
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|
2021-09-26 22:34:25 +03:00
|
|
|
|
2021-11-07 14:30:40 +03:00
|
|
|
// is_capital returns `true`, if the byte is a Latin capital letter.
|
|
|
|
// Example: assert `H`.is_capital() == true
|
2022-03-31 19:32:32 +03:00
|
|
|
// Example: assert `h`.is_capital() == false
|
2021-11-07 14:30:40 +03:00
|
|
|
[inline]
|
2022-04-15 14:45:52 +03:00
|
|
|
pub fn (c u8) is_capital() bool {
|
2021-09-26 22:34:25 +03:00
|
|
|
return c >= `A` && c <= `Z`
|
|
|
|
}
|
|
|
|
|
2021-12-02 16:46:53 +03:00
|
|
|
// clone clones the byte array, and returns the newly created copy.
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn (b []u8) clone() []u8 {
|
|
|
|
mut res := []u8{len: b.len}
|
|
|
|
// mut res := make([]u8, {repeat:b.len})
|
2021-09-26 22:34:25 +03:00
|
|
|
for i in 0 .. b.len {
|
|
|
|
res[i] = b[i]
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-12-02 16:46:53 +03:00
|
|
|
// bytestr produces a string from *all* the bytes in the array.
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: the returned string will have .len equal to the array.len,
|
2021-12-02 16:46:53 +03:00
|
|
|
// even when some of the array bytes were `0`.
|
|
|
|
// If you want to get a V string, that contains only the bytes till
|
2022-04-15 14:58:56 +03:00
|
|
|
// the first `0` byte, use `tos_clone(&u8(array.data))` instead.
|
2022-04-15 14:45:52 +03:00
|
|
|
pub fn (b []u8) bytestr() string {
|
2021-09-26 22:34:25 +03:00
|
|
|
unsafe {
|
|
|
|
buf := malloc_noscan(b.len + 1)
|
|
|
|
vmemcpy(buf, b.data, b.len)
|
|
|
|
buf[b.len] = 0
|
|
|
|
return tos(buf, b.len)
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 09:32:20 +03:00
|
|
|
|
2022-01-13 23:26:17 +03:00
|
|
|
// byterune attempts to decode a sequence of bytes
|
|
|
|
// from utf8 to utf32 and return the result as a rune
|
|
|
|
// it will produce an error if there are more than
|
|
|
|
// four bytes in the array.
|
2023-03-02 16:49:50 +03:00
|
|
|
pub fn (b []u8) byterune() !rune {
|
|
|
|
r := b.utf8_to_utf32()!
|
2022-01-13 23:26:17 +03:00
|
|
|
return rune(r)
|
|
|
|
}
|
|
|
|
|
2021-09-30 09:32:20 +03:00
|
|
|
// repeat returns a new string with `count` number of copies of the byte it was called on.
|
2022-04-15 14:45:52 +03:00
|
|
|
pub fn (b u8) repeat(count int) string {
|
2021-09-30 09:32:20 +03:00
|
|
|
if count < 0 {
|
2022-11-15 16:53:13 +03:00
|
|
|
panic('byte.repeat: count is negative: ${count}')
|
2021-09-30 09:32:20 +03:00
|
|
|
} else if count == 0 {
|
|
|
|
return ''
|
|
|
|
} else if count == 1 {
|
|
|
|
return b.ascii_str()
|
|
|
|
}
|
|
|
|
mut ret := unsafe { malloc_noscan(count + 1) }
|
|
|
|
for i in 0 .. count {
|
|
|
|
unsafe {
|
|
|
|
ret[i] = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_len := count
|
|
|
|
unsafe {
|
|
|
|
ret[new_len] = 0
|
|
|
|
}
|
|
|
|
return unsafe { ret.vstring_with_len(new_len) }
|
|
|
|
}
|
2022-04-27 18:31:21 +03:00
|
|
|
|
|
|
|
// for atomic ints, internal
|
|
|
|
fn _Atomic__int_str(x int) string {
|
|
|
|
return x.str()
|
|
|
|
}
|