2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 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
|
2022-04-15 14:45:52 +03:00
|
|
|
len := utf8_char_len(u8(fst_byte))
|
2020-08-27 07:46:18 +03:00
|
|
|
println('len=$len')
|
|
|
|
mut str := string{
|
|
|
|
len: len
|
2022-03-11 12:07:00 +03:00
|
|
|
str: malloc_noscan(len + 1)
|
2020-08-27 07:46:18 +03:00
|
|
|
}
|
|
|
|
for i in 0..len {
|
2022-04-15 14:45:52 +03:00
|
|
|
str.str[i] = u8(int(c)>>8 * (3 - i) & 0xff)
|
2020-08-27 07:46:18 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2021-09-30 09:32:20 +03:00
|
|
|
|
|
|
|
// repeat returns a new string with `count` number of copies of the rune it was called on.
|
|
|
|
pub fn (c rune) repeat(count int) string {
|
|
|
|
if count < 0 {
|
|
|
|
panic('rune.repeat: count is negative: $count')
|
|
|
|
} else if count == 0 {
|
|
|
|
return ''
|
|
|
|
} else if count == 1 {
|
|
|
|
return c.str()
|
|
|
|
}
|
2022-04-15 14:45:52 +03:00
|
|
|
mut buffer := [5]u8{}
|
2021-09-30 09:32:20 +03:00
|
|
|
res := unsafe { utf32_to_str_no_malloc(u32(c), &buffer[0]) }
|
|
|
|
return res.repeat(count)
|
|
|
|
}
|
2021-12-01 17:35:13 +03:00
|
|
|
|
2022-01-11 15:41:01 +03:00
|
|
|
[manualfree]
|
2022-04-15 14:45:52 +03:00
|
|
|
pub fn (c rune) bytes() []u8 {
|
|
|
|
mut res := []u8{cap: 5}
|
|
|
|
res.len = unsafe { utf32_decode_to_buffer(u32(c), &u8(res.data)) }
|
2022-01-11 15:41:01 +03:00
|
|
|
return res
|
2022-01-11 10:55:51 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 21:09:39 +03:00
|
|
|
// length_in_bytes returns the number of bytes needed to store the code point.
|
|
|
|
// Returns -1 if the data is not a valid code point.
|
2021-12-01 17:35:13 +03:00
|
|
|
pub fn (c rune) length_in_bytes() int {
|
|
|
|
code := u32(c)
|
|
|
|
if code <= 0x7F {
|
|
|
|
return 1
|
|
|
|
} else if code <= 0x7FF {
|
|
|
|
return 2
|
|
|
|
} else if 0xD800 <= code && code <= 0xDFFF {
|
|
|
|
// between min and max for surrogates
|
|
|
|
return -1
|
|
|
|
} else if code <= 0xFFFF {
|
|
|
|
return 3
|
|
|
|
} else if code <= 0x10FFFF {
|
|
|
|
// 0x10FFFF is the maximum valid unicode code point
|
|
|
|
return 4
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|