2019-11-15 00:04:40 +01:00
|
|
|
module builtin
|
|
|
|
|
|
|
|
pub struct string {
|
|
|
|
pub:
|
|
|
|
str byteptr
|
|
|
|
len int
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn strlen(s byteptr) int {
|
|
|
|
mut i := 0
|
2019-12-08 11:44:52 +01:00
|
|
|
for ; s[i] != 0; i++ {}
|
2019-11-15 00:04:40 +01:00
|
|
|
return i
|
2019-12-08 11:44:52 +01:00
|
|
|
}
|
2019-11-15 00:04:40 +01:00
|
|
|
|
|
|
|
pub fn tos(s byteptr, len int) string {
|
|
|
|
if s == 0 {
|
|
|
|
panic('tos(): nil string')
|
|
|
|
}
|
|
|
|
return string {
|
|
|
|
str: s
|
|
|
|
len: len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:05:33 +01:00
|
|
|
fn (s string) add(a string) string {
|
|
|
|
new_len := a.len + s.len
|
|
|
|
mut res := string {
|
|
|
|
len: new_len
|
|
|
|
str: malloc(new_len + 1)
|
|
|
|
}
|
2020-02-24 17:55:16 +01:00
|
|
|
for j in 0..s.len {
|
2019-12-16 17:05:33 +01:00
|
|
|
res[j] = s[j]
|
|
|
|
}
|
2020-02-24 17:55:16 +01:00
|
|
|
for j in 0..a.len {
|
2019-12-16 17:05:33 +01:00
|
|
|
res[s.len + j] = a[j]
|
|
|
|
}
|
|
|
|
res[new_len] = `\0`// V strings are not null terminated, but just in case
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-11-15 01:17:47 +01:00
|
|
|
/*
|
2019-11-15 00:04:40 +01:00
|
|
|
pub fn tos_clone(s byteptr) string {
|
|
|
|
if s == 0 {
|
|
|
|
panic('tos: nil string')
|
|
|
|
}
|
|
|
|
return tos2(s).clone()
|
|
|
|
}
|
2019-11-15 01:17:47 +01:00
|
|
|
*/
|
2019-11-15 00:04:40 +01:00
|
|
|
|
|
|
|
// Same as `tos`, but calculates the length. Called by `string(bytes)` casts.
|
|
|
|
// Used only internally.
|
|
|
|
pub fn tos2(s byteptr) string {
|
|
|
|
if s == 0 {
|
|
|
|
panic('tos2: nil string')
|
|
|
|
}
|
|
|
|
return string {
|
|
|
|
str: s
|
|
|
|
len: strlen(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 09:37:35 +01:00
|
|
|
pub fn tos3(s charptr) string {
|
2019-11-15 00:04:40 +01:00
|
|
|
if s == 0 {
|
|
|
|
panic('tos3: nil string')
|
|
|
|
}
|
|
|
|
return string {
|
|
|
|
str: byteptr(s)
|
|
|
|
len: strlen(byteptr(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:35:57 +01:00
|
|
|
pub fn string_eq (s1, s2 string) bool {
|
|
|
|
if s1.len != s2.len { return false }
|
|
|
|
for i in 0..s1.len {
|
|
|
|
if s1[i] != s2[i] { return false }
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
pub fn string_ne (s1, s2 string) bool {
|
|
|
|
return !string_eq(s1,s2)
|
|
|
|
}
|
2019-11-19 07:53:52 +01:00
|
|
|
|
2019-11-26 04:12:37 +01:00
|
|
|
|
|
|
|
pub fn i64_tos(buf byteptr, len int, n0 i64, base int) string {
|
|
|
|
if base < 2 { panic("base must be >= 2")}
|
|
|
|
if base > 36 { panic("base must be <= 36")}
|
|
|
|
|
|
|
|
mut b := tos(buf, len)
|
|
|
|
mut i := len-1
|
|
|
|
|
|
|
|
mut n := n0
|
|
|
|
neg := n < 0
|
|
|
|
if neg { n = -n }
|
|
|
|
|
|
|
|
b[i--] = 0
|
|
|
|
|
|
|
|
for {
|
|
|
|
c := (n%base) + 48
|
|
|
|
b[i--] = if c > 57 {c+7} else {c}
|
|
|
|
if i < 0 { panic ("buffer to small") }
|
|
|
|
n /= base
|
|
|
|
if n < 1 {break}
|
|
|
|
}
|
2020-03-29 10:08:42 +02:00
|
|
|
if neg {
|
2019-11-26 04:12:37 +01:00
|
|
|
if i < 0 { panic ("buffer to small") }
|
|
|
|
b[i--] = 45
|
|
|
|
}
|
|
|
|
offset := i+1
|
|
|
|
b.str = b.str + offset
|
|
|
|
b.len -= (offset+1)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-12-08 11:44:52 +01:00
|
|
|
pub fn i64_str(n0 i64, base int) string {
|
|
|
|
buf := malloc(80)
|
|
|
|
return i64_tos(buf, 79, n0, base)
|
|
|
|
}
|
2019-11-26 04:12:37 +01:00
|
|
|
|
2019-12-16 17:05:33 +01:00
|
|
|
pub fn ptr_str(ptr voidptr) string {
|
|
|
|
buf := [16]byte
|
|
|
|
hex := i64_tos(buf, 15, i64(ptr), 16)
|
|
|
|
res := '0x' + hex
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-11-15 00:04:40 +01:00
|
|
|
pub fn (a string) clone() string {
|
|
|
|
mut b := string {
|
|
|
|
len: a.len
|
|
|
|
str: malloc(a.len + 1)
|
|
|
|
}
|
2019-12-08 11:44:52 +01:00
|
|
|
mem_copy(b.str, a.str, a.len)
|
2019-11-15 00:04:40 +01:00
|
|
|
b[a.len] = `\0`
|
|
|
|
return b
|
|
|
|
}
|