2019-11-15 02:04:40 +03:00
|
|
|
module builtin
|
|
|
|
|
|
|
|
pub struct string {
|
|
|
|
pub:
|
|
|
|
str byteptr
|
|
|
|
len int
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn strlen(s byteptr) int {
|
|
|
|
mut i := 0
|
2019-11-16 21:49:55 +03:00
|
|
|
for ; s[i] != 0; i++ {}
|
2019-11-15 02:04:40 +03:00
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tos(s byteptr, len int) string {
|
|
|
|
if s == 0 {
|
|
|
|
panic('tos(): nil string')
|
|
|
|
}
|
|
|
|
return string {
|
|
|
|
str: s
|
|
|
|
len: len
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
pub fn tos2(s byteptr) string {
|
|
|
|
if s == 0 {
|
|
|
|
panic('tos2: nil string')
|
|
|
|
}
|
|
|
|
return string {
|
|
|
|
str: s
|
|
|
|
len: strlen(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tos3(s *C.char) string {
|
|
|
|
if s == 0 {
|
|
|
|
panic('tos3: nil string')
|
|
|
|
}
|
|
|
|
return string {
|
|
|
|
str: byteptr(s)
|
|
|
|
len: strlen(byteptr(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 19:35:57 +03: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 09:53:52 +03:00
|
|
|
|
2019-11-15 03:17:47 +03:00
|
|
|
/*
|
2019-11-15 02:04:40 +03:00
|
|
|
pub fn (a string) clone() string {
|
|
|
|
mut b := string {
|
|
|
|
len: a.len
|
|
|
|
str: malloc(a.len + 1)
|
|
|
|
}
|
|
|
|
for i := 0; i < a.len; i++ {
|
|
|
|
b[i] = a[i]
|
|
|
|
}
|
|
|
|
b[a.len] = `\0`
|
|
|
|
return b
|
|
|
|
}
|
2019-11-15 03:17:47 +03:00
|
|
|
*/
|