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