2020-02-03 07:00:36 +03:00
|
|
|
// Copyright (c) 2019-2020 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
|
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn ptr_str(ptr voidptr) string {
|
2019-12-19 23:52:45 +03:00
|
|
|
buf := malloc(sizeof(double) * 5 + 1) // TODO
|
2020-02-07 16:49:14 +03:00
|
|
|
C.sprintf((buf), '%p', ptr)
|
2019-09-15 15:36:05 +03:00
|
|
|
return tos(buf, vstrlen(buf))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fn (nn i32) str() string {
|
|
|
|
// return i
|
|
|
|
// }
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (nn int) str() string {
|
2019-06-28 15:19:46 +03:00
|
|
|
mut n := nn
|
2019-06-22 21:20:28 +03:00
|
|
|
if n == 0 {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 16
|
2019-12-19 23:52:45 +03:00
|
|
|
mut buf := calloc(max + 1)
|
2019-06-22 21:20:28 +03:00
|
|
|
mut len := 0
|
2019-06-28 15:19:46 +03:00
|
|
|
mut is_neg := false
|
2019-06-22 21:20:28 +03:00
|
|
|
if n < 0 {
|
|
|
|
n = -n
|
|
|
|
is_neg = true
|
|
|
|
}
|
|
|
|
// Fill the string from the end
|
|
|
|
for n > 0 {
|
|
|
|
d := n % 10
|
2020-02-08 00:10:48 +03:00
|
|
|
buf[max - len - 1] = d + int(`0`)
|
2019-06-22 21:20:28 +03:00
|
|
|
len++
|
|
|
|
n = n / 10
|
|
|
|
}
|
|
|
|
// Prepend - if it's negative
|
|
|
|
if is_neg {
|
|
|
|
buf[max - len - 1] = `-`
|
|
|
|
len++
|
|
|
|
}
|
2019-12-09 16:41:07 +03:00
|
|
|
buf[max] = `\0`
|
2019-06-22 21:20:28 +03:00
|
|
|
return tos(buf + max - len, len)
|
|
|
|
}
|
|
|
|
|
2019-12-19 23:52:45 +03:00
|
|
|
pub fn (n i8) str() string {
|
2020-02-08 00:10:48 +03:00
|
|
|
return int(n).str()
|
2019-12-19 23:52:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (n i16) str() string {
|
2020-02-08 00:10:48 +03:00
|
|
|
return int(n).str()
|
2019-12-19 23:52:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (n u16) str() string {
|
2020-02-08 00:10:48 +03:00
|
|
|
return int(n).str()
|
2019-12-19 23:52:45 +03:00
|
|
|
}
|
2019-12-03 21:14:17 +03:00
|
|
|
|
2019-08-12 02:58:08 +03:00
|
|
|
pub fn (nn u32) str() string {
|
2019-12-19 23:52:45 +03:00
|
|
|
mut n := nn
|
2020-02-08 00:10:48 +03:00
|
|
|
if n == 0 {
|
2019-08-12 02:58:08 +03:00
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 16
|
|
|
|
mut buf := malloc(max)
|
|
|
|
mut len := 0
|
|
|
|
// Fill the string from the end
|
2020-02-08 00:10:48 +03:00
|
|
|
for n > 0 {
|
|
|
|
d := n % 10
|
|
|
|
buf[max - len - 1] = d + u32(`0`)
|
2019-08-12 02:58:08 +03:00
|
|
|
len++
|
2020-02-08 00:10:48 +03:00
|
|
|
n = n / 10
|
2019-08-12 02:58:08 +03:00
|
|
|
}
|
|
|
|
return tos(buf + max - len, len)
|
|
|
|
}
|
|
|
|
|
2019-09-01 22:37:22 +03:00
|
|
|
/*
|
2019-09-02 15:02:25 +03:00
|
|
|
pub fn (nn byte) str() string {
|
2019-06-28 15:19:46 +03:00
|
|
|
mut n := nn
|
2019-09-02 15:02:25 +03:00
|
|
|
if n == byte(0) {
|
2019-06-22 21:20:28 +03:00
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 5
|
|
|
|
mut buf := malloc(max)
|
|
|
|
mut len := 0
|
|
|
|
// Fill the string from the end
|
2019-09-02 15:02:25 +03:00
|
|
|
for n > byte(0) {
|
|
|
|
d := n % byte(10)
|
|
|
|
buf[max - len - 1] = d + byte(`0`)
|
2019-06-22 21:20:28 +03:00
|
|
|
len++
|
2019-09-02 15:02:25 +03:00
|
|
|
n = n / byte(10)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
return tos(buf + max - len, len)
|
|
|
|
}
|
2019-09-01 22:37:22 +03:00
|
|
|
*/
|
2019-06-22 21:20:28 +03:00
|
|
|
|
2019-12-19 23:52:45 +03:00
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (nn i64) str() string {
|
2019-06-28 15:19:46 +03:00
|
|
|
mut n := nn
|
2020-02-08 00:10:48 +03:00
|
|
|
if n == 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 32
|
|
|
|
mut buf := malloc(max)
|
|
|
|
mut len := 0
|
2019-06-28 15:19:46 +03:00
|
|
|
mut is_neg := false
|
2020-02-08 00:10:48 +03:00
|
|
|
if n < 0 {
|
2019-06-22 21:20:28 +03:00
|
|
|
n = -n
|
|
|
|
is_neg = true
|
|
|
|
}
|
|
|
|
// Fill the string from the end
|
2020-02-07 16:49:14 +03:00
|
|
|
for n > 0 {
|
|
|
|
//d := int(n % (10 as i64))
|
|
|
|
d := n % 10
|
2020-02-08 00:10:48 +03:00
|
|
|
buf[max - len - 1] = d + int(`0`)
|
2019-06-22 21:20:28 +03:00
|
|
|
len++
|
2020-02-07 16:49:14 +03:00
|
|
|
n /= 10
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
// Prepend - if it's negative
|
|
|
|
if is_neg {
|
|
|
|
buf[max - len - 1] = `-`
|
|
|
|
len++
|
|
|
|
}
|
|
|
|
return tos(buf + max - len, len)
|
|
|
|
}
|
|
|
|
|
2019-08-12 02:58:08 +03:00
|
|
|
pub fn (nn u64) str() string {
|
2019-12-19 23:52:45 +03:00
|
|
|
mut n := nn
|
2020-02-07 16:49:14 +03:00
|
|
|
if n == 0 {
|
2019-08-12 02:58:08 +03:00
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 32
|
|
|
|
mut buf := malloc(max)
|
|
|
|
mut len := 0
|
|
|
|
// Fill the string from the end
|
2020-02-07 16:49:14 +03:00
|
|
|
for n > 0 {
|
|
|
|
d := n % 10
|
2020-02-08 00:10:48 +03:00
|
|
|
buf[max - len - 1] = d + u64(`0`)
|
2019-08-12 02:58:08 +03:00
|
|
|
len++
|
2020-02-07 16:49:14 +03:00
|
|
|
n = n / (10)
|
2019-08-12 02:58:08 +03:00
|
|
|
}
|
|
|
|
return tos(buf + max - len, len)
|
|
|
|
}
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (n int) hex() string {
|
2019-12-19 23:52:45 +03:00
|
|
|
len := if n >= 0 { n.str().len + 3 } else { 11 }
|
2019-08-20 14:34:29 +03:00
|
|
|
hex := malloc(len) // 0x + \n
|
2020-02-07 16:49:14 +03:00
|
|
|
count := C.sprintf((hex), '0x%x', n)
|
2019-07-01 18:00:09 +03:00
|
|
|
return tos(hex, count)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (n i64) hex() string {
|
2020-02-07 16:49:14 +03:00
|
|
|
len := if n >= 0 { n.str().len + 3 } else { 19 }
|
2019-07-02 11:13:18 +03:00
|
|
|
hex := malloc(len)
|
2020-03-01 15:26:09 +03:00
|
|
|
count := C.sprintf(charptr(hex), '0x%'C.PRIx64, n)
|
2019-07-02 11:13:18 +03:00
|
|
|
return tos(hex, count)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-01-29 01:43:09 +03:00
|
|
|
pub fn (n u64) hex() string {
|
2020-03-01 15:26:09 +03:00
|
|
|
len := if n > 0 { n.str().len + 3 } else { 19 }
|
2020-01-29 01:43:09 +03:00
|
|
|
hex := malloc(len)
|
2020-03-01 15:26:09 +03:00
|
|
|
count := C.sprintf(charptr(hex), '0x%'C.PRIx64, n)
|
2020-01-29 01:43:09 +03:00
|
|
|
return tos(hex, count)
|
|
|
|
}
|
|
|
|
|
2019-06-30 14:06:46 +03:00
|
|
|
pub fn (a []byte) contains(val byte) bool {
|
2019-06-22 21:20:28 +03:00
|
|
|
for aa in a {
|
|
|
|
if aa == val {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-01 18:36:23 +03:00
|
|
|
pub fn (c rune) str() string {
|
2020-02-08 00:10:48 +03:00
|
|
|
fst_byte := int(c)>>8 * 3 & 0xff
|
2019-07-01 18:36:23 +03:00
|
|
|
len := utf8_char_len(fst_byte)
|
2019-12-19 23:52:45 +03:00
|
|
|
mut str := string{
|
2019-07-01 18:36:23 +03:00
|
|
|
len: len
|
|
|
|
str: malloc(len + 1)
|
|
|
|
}
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..len {
|
2020-02-08 00:10:48 +03:00
|
|
|
str.str[i] = int(c)>>8 * (3 - i) & 0xff
|
2019-07-01 18:36:23 +03:00
|
|
|
}
|
|
|
|
str[len] = `\0`
|
|
|
|
return str
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
2019-07-01 18:36:23 +03:00
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (c byte) str() string {
|
2019-12-19 23:52:45 +03:00
|
|
|
mut str := string{
|
2019-06-22 21:20:28 +03:00
|
|
|
len: 1
|
|
|
|
str: malloc(2)
|
|
|
|
}
|
|
|
|
str.str[0] = c
|
|
|
|
str.str[1] = `\0`
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
2019-08-04 10:36:21 +03:00
|
|
|
pub fn (c byte) is_capital() bool {
|
2019-08-20 14:34:29 +03:00
|
|
|
return c >= `A` && c <= `Z`
|
|
|
|
}
|
2019-08-04 10:36:21 +03:00
|
|
|
|
2019-07-25 19:07:16 +03:00
|
|
|
pub fn (b []byte) clone() []byte {
|
2020-02-08 00:10:48 +03:00
|
|
|
mut res := [byte(0)].repeat(b.len)
|
2020-02-18 22:47:19 +03:00
|
|
|
//mut res := make([]byte, {repeat:b.len})
|
2020-02-24 19:55:16 +03:00
|
|
|
for i in 0..b.len {
|
2019-08-20 14:34:29 +03:00
|
|
|
res[i] = b[i]
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2019-07-25 19:07:16 +03:00
|
|
|
|