2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
2020-08-27 07:46:18 +03:00
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
|
|
|
module builtin
|
|
|
|
|
2021-08-19 06:58:53 +03:00
|
|
|
import strings
|
|
|
|
|
2020-12-16 10:03:49 +03:00
|
|
|
// This was never working correctly, the issue is now
|
|
|
|
// fixed however the type checks in checker need to be
|
|
|
|
// updated. if you uncomment it you will see the issue
|
|
|
|
// type rune = int
|
2020-08-27 07:46:18 +03:00
|
|
|
|
|
|
|
pub fn (c rune) str() string {
|
|
|
|
return utf32_to_str(u32(c))
|
|
|
|
/*
|
|
|
|
unsafe {
|
|
|
|
fst_byte := int(c)>>8 * 3 & 0xff
|
|
|
|
len := utf8_char_len(byte(fst_byte))
|
|
|
|
println('len=$len')
|
|
|
|
mut str := string{
|
|
|
|
len: len
|
|
|
|
str: malloc(len + 1)
|
|
|
|
}
|
|
|
|
for i in 0..len {
|
|
|
|
str.str[i] = byte(int(c)>>8 * (3 - i) & 0xff)
|
|
|
|
}
|
|
|
|
str.str[len] = `\0`
|
|
|
|
println(str)
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2021-06-30 22:30:28 +03:00
|
|
|
// string converts a rune array to a string
|
2021-08-19 06:58:53 +03:00
|
|
|
[manualfree]
|
2021-06-30 22:30:28 +03:00
|
|
|
pub fn (ra []rune) string() string {
|
2021-08-19 06:58:53 +03:00
|
|
|
mut sb := strings.new_builder(ra.len)
|
2021-08-19 07:14:20 +03:00
|
|
|
sb.write_runes(ra)
|
2021-08-19 06:58:53 +03:00
|
|
|
res := sb.str()
|
|
|
|
unsafe { sb.free() }
|
2021-06-30 22:30:28 +03:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2020-08-27 07:46:18 +03:00
|
|
|
// Define this on byte as well, so that we can do `s[0].is_capital()`
|
|
|
|
pub fn (c byte) is_capital() bool {
|
|
|
|
return c >= `A` && c <= `Z`
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (b []byte) clone() []byte {
|
|
|
|
mut res := []byte{len: b.len}
|
2021-03-14 19:21:45 +03:00
|
|
|
// mut res := make([]byte, {repeat:b.len})
|
|
|
|
for i in 0 .. b.len {
|
2020-08-27 07:46:18 +03:00
|
|
|
res[i] = b[i]
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-06-30 22:30:28 +03:00
|
|
|
// TODO: remove this once runes are implemented
|
2020-08-27 07:46:18 +03:00
|
|
|
pub fn (b []byte) bytestr() string {
|
2021-03-14 19:21:45 +03:00
|
|
|
unsafe {
|
2021-06-15 14:47:11 +03:00
|
|
|
buf := malloc_noscan(b.len + 1)
|
2021-08-12 21:46:38 +03:00
|
|
|
vmemcpy(buf, b.data, b.len)
|
2021-03-14 19:21:45 +03:00
|
|
|
buf[b.len] = 0
|
|
|
|
return tos(buf, b.len)
|
|
|
|
}
|
2020-08-27 07:46:18 +03:00
|
|
|
}
|