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-06-27 14:14:59 +03:00
|
|
|
pub fn (d double) str() string {
|
2019-06-22 21:20:28 +03:00
|
|
|
buf := malloc(sizeof(double) * 5 + 1)// TODO
|
|
|
|
C.sprintf(buf, '%f', d)
|
|
|
|
return tos(buf, _strlen(buf))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
C.sprintf(buf, '%f', d)
|
|
|
|
return tos(buf, _strlen(buf))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
C.sprintf(buf, '%f', d)
|
|
|
|
return tos(buf, _strlen(buf))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
C.sprintf(buf, '%p', ptr)
|
|
|
|
return tos(buf, _strlen(buf))
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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 < 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-06-27 14:14:59 +03:00
|
|
|
pub fn (nn u8) str() string {
|
2019-06-28 15:19:46 +03:00
|
|
|
mut n := nn
|
2019-06-22 21:20:28 +03:00
|
|
|
if n == u8(0) {
|
|
|
|
return '0'
|
|
|
|
}
|
|
|
|
max := 5
|
|
|
|
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 < u8(0) {
|
|
|
|
n = -n
|
|
|
|
is_neg = true
|
|
|
|
}
|
|
|
|
// Fill the string from the end
|
|
|
|
for n > u8(0) {
|
|
|
|
d := n % u8(10)
|
|
|
|
buf[max - len - 1] = d + u8(`0`)
|
|
|
|
len++
|
|
|
|
n = n / u8(10)
|
|
|
|
}
|
|
|
|
// Prepend - if it's negative
|
|
|
|
if is_neg {
|
|
|
|
buf[max - len - 1] = `-`
|
|
|
|
len++
|
|
|
|
}
|
|
|
|
return tos(buf + max - len, len)
|
|
|
|
}
|
|
|
|
|
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-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-06-22 21:20:28 +03:00
|
|
|
s := n.str()
|
2019-06-28 16:50:06 +03:00
|
|
|
hex := malloc(s.len + 3) // 0x + \n
|
2019-06-22 21:20:28 +03:00
|
|
|
C.sprintf(hex, '0x%x', n)
|
2019-06-28 16:50:06 +03:00
|
|
|
return tos(hex, s.len + 3)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 14:14:59 +03:00
|
|
|
pub fn (n i64) hex() string {
|
2019-06-22 21:20:28 +03:00
|
|
|
s := n.str()
|
2019-06-28 16:50:06 +03:00
|
|
|
hex := malloc(s.len + 3)
|
2019-06-22 21:20:28 +03:00
|
|
|
C.sprintf(hex, '0x%x', n)
|
2019-06-28 16:50:06 +03:00
|
|
|
return tos(hex, s.len + 3)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2019-06-27 14:14:59 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO
|
|
|
|
fn (c rune) str() string {
|
|
|
|
}
|
|
|
|
*/
|
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
|
|
|
|
}
|
|
|
|
|