2019-06-23 05:21:30 +03:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// 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-07-31 10:20:40 +03:00
|
|
|
#include <float.h>
|
2019-08-24 02:48:40 +03:00
|
|
|
#include <math.h>
|
2019-07-31 10:20:40 +03:00
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (d f64) str() string {
|
2019-06-22 21:20:28 +03:00
|
|
|
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
2019-09-15 15:36:05 +03:00
|
|
|
C.sprintf(*char(buf), '%f', d)
|
|
|
|
return tos(buf, vstrlen(buf))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (d f32) str() string {
|
2019-06-22 21:20:28 +03:00
|
|
|
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
2019-09-15 15:36:05 +03:00
|
|
|
C.sprintf(*char(buf), '%f', d)
|
|
|
|
return tos(buf, vstrlen(buf))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn ptr_str(ptr voidptr) string {
|
2019-06-22 21:20:28 +03:00
|
|
|
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
2019-09-15 15:36:05 +03:00
|
|
|
C.sprintf(*char(buf), '%p', ptr)
|
|
|
|
return tos(buf, vstrlen(buf))
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-07-31 10:20:40 +03:00
|
|
|
// compare floats using C epsilon
|
2019-10-01 22:38:36 +03:00
|
|
|
// ==
|
2019-07-31 10:20:40 +03:00
|
|
|
pub fn (a f64) eq(b f64) bool {
|
2019-10-13 04:27:57 +03:00
|
|
|
$if windows {
|
|
|
|
$if tinyc {
|
|
|
|
return a -b <= 0.01
|
|
|
|
} $else {
|
|
|
|
return C.fabs(a - b) <= C.DBL_EPSILON
|
|
|
|
}
|
2019-10-14 09:03:11 +03:00
|
|
|
} $else {
|
2019-10-13 04:27:57 +03:00
|
|
|
return C.fabs(a - b) <= C.DBL_EPSILON
|
2019-10-14 09:03:11 +03:00
|
|
|
}
|
2019-07-31 10:20:40 +03:00
|
|
|
}
|
2019-09-29 18:27:53 +03:00
|
|
|
pub fn (a f32) eq(b f32) bool {
|
2019-10-13 04:27:57 +03:00
|
|
|
$if windows {
|
|
|
|
$if tinyc {
|
|
|
|
return a -b <= 0.01
|
|
|
|
} $else {
|
|
|
|
return C.fabs(a - b) <= C.DBL_EPSILON
|
|
|
|
}
|
2019-10-14 09:03:11 +03:00
|
|
|
} $else {
|
2019-10-13 04:27:57 +03:00
|
|
|
return C.fabs(a - b) <= C.DBL_EPSILON
|
|
|
|
}
|
2019-10-14 09:03:11 +03:00
|
|
|
}
|
2019-09-29 18:27:53 +03:00
|
|
|
pub fn (a f64) eqbit(b f64) bool {
|
|
|
|
return C.DEFAULT_EQUAL(a, b)
|
|
|
|
}
|
|
|
|
pub fn (a f32) eqbit(b f32) bool {
|
|
|
|
return C.DEFAULT_EQUAL(a, b)
|
|
|
|
}
|
2019-07-31 10:20:40 +03:00
|
|
|
|
2019-10-01 22:38:36 +03:00
|
|
|
// !=
|
|
|
|
fn (a f64) ne(b f64) bool {
|
|
|
|
return !a.eq(b)
|
|
|
|
}
|
|
|
|
fn (a f32) ne(b f32) bool {
|
|
|
|
return !a.eq(b)
|
|
|
|
}
|
|
|
|
pub fn (a f64) nebit(b f64) bool {
|
|
|
|
return C.DEFAULT_NOT_EQUAL(a, b)
|
|
|
|
}
|
|
|
|
pub fn (a f32) nebit(b f32) bool {
|
|
|
|
return C.DEFAULT_NOT_EQUAL(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// a < b
|
|
|
|
fn (a f64) lt(b f64) bool {
|
|
|
|
return a.ne(b) && a.ltbit(b)
|
|
|
|
}
|
|
|
|
fn (a f32) lt(b f32) bool {
|
|
|
|
return a.ne(b) && a.ltbit(b)
|
|
|
|
}
|
|
|
|
fn (a f64) ltbit(b f64) bool {
|
|
|
|
return C.DEFAULT_LT(a, b)
|
|
|
|
}
|
|
|
|
fn (a f32) ltbit(b f32) bool {
|
|
|
|
return C.DEFAULT_LT(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// a <= b
|
|
|
|
fn (a f64) le(b f64) bool {
|
|
|
|
return !a.gt(b)
|
|
|
|
}
|
|
|
|
fn (a f32) le(b f32) bool {
|
|
|
|
return !a.gt(b)
|
|
|
|
}
|
|
|
|
fn (a f64) lebit(b f64) bool {
|
|
|
|
return C.DEFAULT_LE(a, b)
|
|
|
|
}
|
|
|
|
fn (a f32) lebit(b f32) bool {
|
|
|
|
return C.DEFAULT_LE(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// a > b
|
|
|
|
fn (a f64) gt(b f64) bool {
|
|
|
|
return a.ne(b) && a.gtbit(b)
|
|
|
|
}
|
|
|
|
fn (a f32) gt(b f32) bool {
|
|
|
|
return a.ne(b) && a.gtbit(b)
|
|
|
|
}
|
|
|
|
fn (a f64) gtbit(b f64) bool {
|
|
|
|
return C.DEFAULT_GT(a, b)
|
|
|
|
}
|
|
|
|
fn (a f32) gtbit(b f32) bool {
|
|
|
|
return C.DEFAULT_GT(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// a >= b
|
|
|
|
fn (a f64) ge(b f64) bool {
|
|
|
|
return !a.lt(b)
|
|
|
|
}
|
|
|
|
fn (a f32) ge(b f32) bool {
|
|
|
|
return !a.lt(b)
|
|
|
|
}
|
|
|
|
fn (a f64) gebit(b f64) bool {
|
|
|
|
return C.DEFAULT_GE(a, b)
|
|
|
|
}
|
|
|
|
fn (a f32) gebit(b f32) bool {
|
|
|
|
return C.DEFAULT_GE(a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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-08-20 15:34:34 +03:00
|
|
|
mut buf := calloc(max)
|
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
|
|
|
|
buf[max - len - 1] = d + int(`0`)
|
|
|
|
len++
|
|
|
|
n = n / 10
|
|
|
|
}
|
|
|
|
// 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 u32) str() string {
|
|
|
|
mut n := nn
|
|
|
|
if n == u32(0) {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 16
|
|
|
|
mut buf := malloc(max)
|
|
|
|
mut len := 0
|
|
|
|
// Fill the string from the end
|
|
|
|
for n > u32(0) {
|
|
|
|
d := n % u32(10)
|
|
|
|
buf[max - len - 1] = d + u32(`0`)
|
|
|
|
len++
|
|
|
|
n = n / u32(10)
|
|
|
|
}
|
|
|
|
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-06-27 14:14:59 +03:00
|
|
|
pub fn (nn i64) str() string {
|
2019-06-28 15:19:46 +03:00
|
|
|
mut n := nn
|
2019-06-22 21:20:28 +03:00
|
|
|
if n == i64(0) {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 32
|
|
|
|
mut buf := malloc(max)
|
|
|
|
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 < i64(0) {
|
|
|
|
n = -n
|
|
|
|
is_neg = true
|
|
|
|
}
|
|
|
|
// Fill the string from the end
|
|
|
|
for n > i64(0) {
|
|
|
|
d := int(n % i64(10))
|
|
|
|
buf[max - len - 1] = d + int(`0`)
|
|
|
|
len++
|
|
|
|
n = n / i64(10)
|
|
|
|
}
|
|
|
|
// 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 {
|
|
|
|
mut n := nn
|
|
|
|
if n == u64(0) {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 32
|
|
|
|
mut buf := malloc(max)
|
|
|
|
mut len := 0
|
|
|
|
// Fill the string from the end
|
|
|
|
for n > u64(0) {
|
|
|
|
d := n % u64(10)
|
|
|
|
buf[max - len - 1] = d + u64(`0`)
|
|
|
|
len++
|
|
|
|
n = n / u64(10)
|
|
|
|
}
|
|
|
|
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-07-02 11:13:18 +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
|
2019-09-15 15:36:05 +03:00
|
|
|
count := int(C.sprintf(*char(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 {
|
2019-07-02 11:13:18 +03:00
|
|
|
len := if n >= i64(0) {
|
|
|
|
n.str().len + 3
|
|
|
|
} else {
|
|
|
|
19
|
|
|
|
}
|
|
|
|
hex := malloc(len)
|
2019-10-04 05:40:08 +03:00
|
|
|
count := int(C.sprintf(*char(hex), '0x%llx', n))
|
2019-07-02 11:13:18 +03:00
|
|
|
return tos(hex, count)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
fst_byte := int(c) >> 8 * 3 & 0xff
|
|
|
|
len := utf8_char_len(fst_byte)
|
|
|
|
mut str := string {
|
|
|
|
len: len
|
|
|
|
str: malloc(len + 1)
|
|
|
|
}
|
|
|
|
for i := 0; i < len; i++ {
|
|
|
|
str.str[i] = int(c) >> 8 * (3 - i) & 0xff
|
|
|
|
}
|
|
|
|
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-06-22 21:20:28 +03:00
|
|
|
mut str := string {
|
|
|
|
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 {
|
2019-09-19 05:22:24 +03:00
|
|
|
mut res := [byte(0)].repeat(b.len)
|
2019-07-25 19:07:16 +03:00
|
|
|
for i := 0; i < b.len; i++ {
|
2019-08-20 14:34:29 +03:00
|
|
|
res[i] = b[i]
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2019-07-25 19:07:16 +03:00
|
|
|
|