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))
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn ptr_str(ptr voidptr) string {
|
|
|
|
buf1 := u64(ptr).hex()
|
|
|
|
return buf1
|
|
|
|
}
|
|
|
|
|
|
|
|
const(
|
|
|
|
digit_pairs = "00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This implementation is the quickest with gcc -O2
|
|
|
|
[inline]
|
|
|
|
pub fn (nn int) str_l(max int) string {
|
|
|
|
mut n := nn
|
|
|
|
mut d := 0
|
|
|
|
if n == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
mut buf := malloc(max + 1)
|
2020-03-25 00:17:39 +01:00
|
|
|
|
2020-03-11 00:38:11 +01:00
|
|
|
mut is_neg := false
|
|
|
|
if n < 0 {
|
|
|
|
n = -n
|
|
|
|
is_neg = true
|
|
|
|
}
|
|
|
|
|
|
|
|
mut index := max
|
|
|
|
buf[index--] = `\0`
|
|
|
|
for n > 0 {
|
|
|
|
n1 := n / 100
|
|
|
|
d = ((n - (n1 * 100)) << 1)
|
|
|
|
n = n1
|
2020-03-25 00:17:39 +01:00
|
|
|
buf[index--] = digit_pairs.str[d++]
|
|
|
|
buf[index--] = digit_pairs.str[d]
|
2020-03-11 00:38:11 +01:00
|
|
|
}
|
|
|
|
index++
|
2020-03-25 00:17:39 +01:00
|
|
|
|
2020-03-11 00:38:11 +01:00
|
|
|
// remove head zero
|
|
|
|
if d < 20 {
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend - if it's negative
|
|
|
|
if is_neg {
|
|
|
|
index--
|
|
|
|
buf[index] = `-`
|
|
|
|
}
|
2020-03-25 00:17:39 +01:00
|
|
|
|
2020-03-21 16:00:58 +01:00
|
|
|
C.memmove(buf,buf+index, (max-index)+1 )
|
|
|
|
return tos(buf, (max-index))
|
|
|
|
//return tos(buf + index, (max-index))
|
2020-03-11 00:38:11 +01:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (n u16) str() string {
|
2020-03-11 00:38:11 +01:00
|
|
|
return int(n).str_l(7)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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)
|
2020-03-25 00:17:39 +01:00
|
|
|
|
2020-03-11 00:38:11 +01:00
|
|
|
mut index := max
|
|
|
|
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
|
|
|
|
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++
|
2020-03-25 00:17:39 +01:00
|
|
|
|
2020-03-11 00:38:11 +01:00
|
|
|
// remove head zero
|
|
|
|
if d < u32(20) {
|
|
|
|
index++
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-03-25 00:17:39 +01:00
|
|
|
|
2020-03-21 16:00:58 +01:00
|
|
|
C.memmove(buf,buf+index, (max-index)+1 )
|
|
|
|
return tos(buf, (max-index))
|
|
|
|
//return tos(buf + index, (max-index))
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-12-19 21:52:45 +01:00
|
|
|
|
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)
|
2020-03-25 00:17:39 +01:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
buf[index--] = digit_pairs[d++]
|
|
|
|
buf[index--] = digit_pairs[d]
|
|
|
|
}
|
|
|
|
index++
|
2020-03-25 00:17:39 +01:00
|
|
|
|
2020-03-11 00:38:11 +01:00
|
|
|
// remove head zero
|
|
|
|
if d < i64(20) {
|
|
|
|
index++
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
|
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--
|
|
|
|
buf[index] = `-`
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-03-11 00:38:11 +01:00
|
|
|
|
2020-03-21 16:00:58 +01:00
|
|
|
C.memmove(buf,buf+index, (max-index)+1 )
|
|
|
|
return tos(buf, (max-index))
|
|
|
|
//return tos(buf + index, (max-index))
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
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-03-11 00:38:11 +01:00
|
|
|
mut d := 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)
|
2020-03-25 00:17:39 +01:00
|
|
|
|
2020-03-11 00:38:11 +01:00
|
|
|
mut index := max
|
|
|
|
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
|
|
|
|
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++
|
2020-03-25 00:17:39 +01:00
|
|
|
|
2020-03-11 00:38:11 +01:00
|
|
|
// remove head zero
|
|
|
|
if d < 20 {
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
|
2020-03-21 16:00:58 +01:00
|
|
|
C.memmove(buf,buf+index, (max-index)+1 )
|
|
|
|
return tos(buf, (max-index))
|
|
|
|
//return tos(buf + index, (max-index))
|
2019-08-12 01:58:08 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2019-06-22 20:20:28 +02:00
|
|
|
|
2020-04-02 17:16:17 +02:00
|
|
|
pub fn (nn byte) hex() string {
|
|
|
|
if nn == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
|
|
|
|
mut n := nn
|
|
|
|
max := 2
|
|
|
|
mut buf := malloc(max + 1)
|
|
|
|
|
|
|
|
mut index := max
|
|
|
|
buf[index--] = `\0`
|
|
|
|
for n > 0 {
|
|
|
|
d := n & 0xF
|
|
|
|
n = n >> 4
|
|
|
|
buf[index--] = if d < 10 { d + `0` } else { d + 87 }
|
|
|
|
}
|
|
|
|
//buf[index--] = `x`
|
|
|
|
//buf[index] = `0`
|
|
|
|
index++
|
|
|
|
|
|
|
|
return tos(buf + index, (max - index))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn i8) hex() string {
|
|
|
|
return byte(nn).hex()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn u16) hex() string {
|
|
|
|
if nn == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
2020-04-26 10:39:26 +02:00
|
|
|
|
2020-04-02 17:16:17 +02:00
|
|
|
mut n := nn
|
|
|
|
max := 5
|
|
|
|
mut buf := malloc(max + 1)
|
|
|
|
|
|
|
|
mut index := max
|
|
|
|
buf[index--] = `\0`
|
|
|
|
for n > 0 {
|
|
|
|
d := n & 0xF
|
|
|
|
n = n >> 4
|
|
|
|
buf[index--] = if d < 10 { d + `0` } else { d + 87 }
|
|
|
|
}
|
|
|
|
//buf[index--] = `x`
|
|
|
|
//buf[index] = `0`
|
|
|
|
index++
|
|
|
|
|
|
|
|
return tos(buf + index, (max - index))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn i16) hex() string {
|
|
|
|
return u16(nn).hex()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (nn u32) hex() string {
|
|
|
|
if nn == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
|
|
|
|
mut n := nn
|
2020-03-11 00:38:11 +01:00
|
|
|
max := 10
|
|
|
|
mut buf := malloc(max + 1)
|
|
|
|
|
|
|
|
mut index := max
|
|
|
|
buf[index--] = `\0`
|
|
|
|
for n > 0 {
|
|
|
|
d := n & 0xF
|
|
|
|
n = n >> 4
|
|
|
|
buf[index--] = if d < 10 { d + `0` } else { d + 87 }
|
|
|
|
}
|
|
|
|
//buf[index--] = `x`
|
|
|
|
//buf[index] = `0`
|
|
|
|
index++
|
|
|
|
|
|
|
|
return tos(buf + index, (max - index))
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:16:17 +02:00
|
|
|
pub fn (nn int) hex() string {
|
|
|
|
return u32(nn).hex()
|
|
|
|
}
|
|
|
|
|
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-04-26 10:39:26 +02:00
|
|
|
|
2020-03-11 00:38:11 +01:00
|
|
|
mut n := nn
|
|
|
|
max := 18
|
|
|
|
mut buf := malloc(max + 1)
|
|
|
|
|
|
|
|
mut index := max
|
|
|
|
buf[index--] = `\0`
|
|
|
|
for n > 0 {
|
|
|
|
d := n & 0xF
|
|
|
|
n = n >> 4
|
|
|
|
buf[index--] = if d < 10 { d + `0` } else { d + 87 }
|
|
|
|
}
|
|
|
|
//buf[index--] = `x`
|
|
|
|
//buf[index] = `0`
|
|
|
|
index++
|
|
|
|
|
2020-03-21 16:00:58 +01:00
|
|
|
C.memmove(buf,buf+index, (max-index)+1 )
|
|
|
|
return tos(buf, (max-index))
|
|
|
|
//return tos(buf + index, (max-index))
|
2020-01-28 23:43:09 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// ----- utilities functions -----
|
|
|
|
|
2019-06-30 13:06:46 +02:00
|
|
|
pub fn (a []byte) contains(val byte) bool {
|
2019-06-22 20:20:28 +02:00
|
|
|
for aa in a {
|
|
|
|
if aa == val {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-26 10:39:26 +02:00
|
|
|
|
|
|
|
/*
|
2019-07-01 17:36:23 +02:00
|
|
|
pub fn (c rune) str() string {
|
2020-02-07 22:10:48 +01:00
|
|
|
fst_byte := int(c)>>8 * 3 & 0xff
|
2019-07-01 17:36:23 +02:00
|
|
|
len := utf8_char_len(fst_byte)
|
2019-12-19 21:52:45 +01:00
|
|
|
mut str := string{
|
2019-07-01 17:36:23 +02:00
|
|
|
len: len
|
|
|
|
str: malloc(len + 1)
|
|
|
|
}
|
2020-02-24 17:55:16 +01:00
|
|
|
for i in 0..len {
|
2020-02-07 22:10:48 +01:00
|
|
|
str.str[i] = int(c)>>8 * (3 - i) & 0xff
|
2019-07-01 17:36:23 +02:00
|
|
|
}
|
2020-03-10 23:21:26 +01:00
|
|
|
str.str[len] = `\0`
|
2019-07-01 17:36:23 +02:00
|
|
|
return str
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2020-04-26 10:39:26 +02:00
|
|
|
*/
|
2019-07-01 17:36:23 +02:00
|
|
|
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (c byte) str() string {
|
2019-12-19 21:52:45 +01:00
|
|
|
mut str := string{
|
2019-06-22 20:20:28 +02:00
|
|
|
len: 1
|
|
|
|
str: malloc(2)
|
|
|
|
}
|
|
|
|
str.str[0] = c
|
|
|
|
str.str[1] = `\0`
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2019-08-04 09:36:21 +02:00
|
|
|
pub fn (c byte) is_capital() bool {
|
2019-08-20 13:34:29 +02:00
|
|
|
return c >= `A` && c <= `Z`
|
|
|
|
}
|
2019-08-04 09:36:21 +02:00
|
|
|
|
2019-07-25 18:07:16 +02:00
|
|
|
pub fn (b []byte) clone() []byte {
|
2020-02-07 22:10:48 +01:00
|
|
|
mut res := [byte(0)].repeat(b.len)
|
2020-02-18 20:47:19 +01:00
|
|
|
//mut res := make([]byte, {repeat:b.len})
|
2020-02-24 17:55:16 +01:00
|
|
|
for i in 0..b.len {
|
2019-08-20 13:34:29 +02:00
|
|
|
res[i] = b[i]
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2019-07-25 18:07:16 +02:00
|
|
|
|