2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-06-23 04:21:30 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-06-22 20:20:28 +02:00
|
|
|
module builtin
|
|
|
|
|
2019-09-15 14:36:05 +02:00
|
|
|
pub fn utf8_char_len(b byte) int {
|
2021-03-01 00:18:02 +01:00
|
|
|
return ((0xe5000000 >> ((b >> 3) & 0x1e)) & 3) + 1
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert utf32 to utf8
|
|
|
|
// utf32 == Codepoint
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn utf32_to_str(code u32) string {
|
2021-02-14 19:31:42 +01:00
|
|
|
unsafe {
|
2021-06-15 13:47:11 +02:00
|
|
|
mut buffer := malloc_noscan(5)
|
2021-08-13 17:37:34 +02:00
|
|
|
res := utf32_to_str_no_malloc(code, buffer)
|
|
|
|
if res.len == 0 {
|
|
|
|
// the buffer was not used at all
|
|
|
|
free(buffer)
|
|
|
|
}
|
|
|
|
return res
|
2021-02-14 19:31:42 +01:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2021-08-13 17:37:34 +02:00
|
|
|
[manualfree; unsafe]
|
|
|
|
pub fn utf32_to_str_no_malloc(code u32, buf &byte) string {
|
2022-01-11 13:41:01 +01:00
|
|
|
unsafe {
|
|
|
|
len := utf32_decode_to_buffer(code, buf)
|
|
|
|
if len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
buf[len] = 0
|
|
|
|
return tos(buf, len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[manualfree; unsafe]
|
|
|
|
pub fn utf32_decode_to_buffer(code u32, buf &byte) int {
|
2020-07-22 19:28:53 +02:00
|
|
|
unsafe {
|
2021-08-13 17:37:34 +02:00
|
|
|
icode := int(code) // Prevents doing casts everywhere
|
2021-04-05 19:55:03 +02:00
|
|
|
mut buffer := &byte(buf)
|
2021-03-01 00:18:02 +01:00
|
|
|
if icode <= 127 {
|
|
|
|
// 0x7F
|
2020-07-22 19:28:53 +02:00
|
|
|
buffer[0] = byte(icode)
|
2022-01-11 13:41:01 +01:00
|
|
|
return 1
|
2021-03-01 00:18:02 +01:00
|
|
|
} else if icode <= 2047 {
|
|
|
|
// 0x7FF
|
|
|
|
buffer[0] = 192 | byte(icode >> 6) // 0xC0 - 110xxxxx
|
|
|
|
buffer[1] = 128 | byte(icode & 63) // 0x80 - 0x3F - 10xxxxxx
|
2022-01-11 13:41:01 +01:00
|
|
|
return 2
|
2021-03-01 00:18:02 +01:00
|
|
|
} else if icode <= 65535 {
|
|
|
|
// 0xFFFF
|
|
|
|
buffer[0] = 224 | byte(icode >> 12) // 0xE0 - 1110xxxx
|
|
|
|
buffer[1] = 128 | (byte(icode >> 6) & 63) // 0x80 - 0x3F - 10xxxxxx
|
|
|
|
buffer[2] = 128 | byte(icode & 63) // 0x80 - 0x3F - 10xxxxxx
|
2022-01-11 13:41:01 +01:00
|
|
|
return 3
|
2020-07-22 19:28:53 +02:00
|
|
|
}
|
2021-03-01 00:18:02 +01:00
|
|
|
// 0x10FFFF
|
|
|
|
else if icode <= 1114111 {
|
|
|
|
buffer[0] = 240 | byte(icode >> 18) // 0xF0 - 11110xxx
|
|
|
|
buffer[1] = 128 | (byte(icode >> 12) & 63) // 0x80 - 0x3F - 10xxxxxx
|
|
|
|
buffer[2] = 128 | (byte(icode >> 6) & 63) // 0x80 - 0x3F - 10xxxxxx
|
|
|
|
buffer[3] = 128 | byte(icode & 63) // 0x80 - 0x3F - 10xxxxxx
|
2022-01-11 13:41:01 +01:00
|
|
|
return 4
|
2020-07-22 19:28:53 +02:00
|
|
|
}
|
2019-06-27 02:33:49 +02:00
|
|
|
}
|
2022-01-11 13:41:01 +01:00
|
|
|
return 0
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert utf8 to utf32
|
2022-01-13 21:26:17 +01:00
|
|
|
// the original implementation did not check for
|
|
|
|
// valid utf8 in the string, and could result in
|
|
|
|
// values greater than the utf32 spec
|
|
|
|
// it has been replaced by `utf8_to_utf32` which
|
|
|
|
// has an optional return type.
|
|
|
|
//
|
|
|
|
// this function is left for backward compatibility
|
|
|
|
// it is used in vlib/builtin/string.v,
|
|
|
|
// and also in vlib/v/gen/c/cgen.v
|
2019-06-27 13:14:59 +02:00
|
|
|
pub fn (_rune string) utf32_code() int {
|
2022-01-13 21:26:17 +01:00
|
|
|
return int(_rune.bytes().utf8_to_utf32() or {
|
|
|
|
// error('more than one utf-8 rune found in this string')
|
|
|
|
rune(0)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert array of utf8 bytes to single utf32 value
|
|
|
|
// will error if more than 4 bytes are submitted
|
|
|
|
pub fn (_bytes []byte) utf8_to_utf32() ?rune {
|
|
|
|
if _bytes.len == 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
return 0
|
|
|
|
}
|
2022-01-13 21:26:17 +01:00
|
|
|
// return ASCII unchanged
|
|
|
|
if _bytes.len == 1 {
|
|
|
|
return rune(_bytes[0])
|
|
|
|
}
|
|
|
|
if _bytes.len > 4 {
|
|
|
|
return error('attempted to decode too many bytes, utf-8 is limited to four bytes maximum')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2022-01-13 21:26:17 +01:00
|
|
|
|
|
|
|
mut b := byte(int(_bytes[0]))
|
|
|
|
|
|
|
|
b = b << _bytes.len
|
|
|
|
mut res := rune(b)
|
|
|
|
mut shift := 6 - _bytes.len
|
|
|
|
for i := 1; i < _bytes.len; i++ {
|
|
|
|
c := rune(_bytes[i])
|
|
|
|
res = rune(res) << shift
|
2019-11-13 19:14:37 +01:00
|
|
|
res |= c & 63 // 0x3f
|
2019-06-22 20:20:28 +02:00
|
|
|
shift = 6
|
|
|
|
}
|
2022-01-13 21:26:17 +01:00
|
|
|
return res
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 03:41:24 +02:00
|
|
|
// Calculate length to read from the first byte
|
|
|
|
fn utf8_len(c byte) int {
|
2019-12-19 21:52:45 +01:00
|
|
|
mut b := 0
|
|
|
|
mut x := c
|
2020-03-27 14:57:19 +01:00
|
|
|
if (x & 240) != 0 {
|
2019-12-19 21:52:45 +01:00
|
|
|
// 0xF0
|
|
|
|
x >>= 4
|
2021-03-01 00:18:02 +01:00
|
|
|
} else {
|
2019-12-19 21:52:45 +01:00
|
|
|
b += 4
|
|
|
|
}
|
2020-03-27 14:57:19 +01:00
|
|
|
if (x & 12) != 0 {
|
2019-12-19 21:52:45 +01:00
|
|
|
// 0x0C
|
|
|
|
x >>= 2
|
2021-03-01 00:18:02 +01:00
|
|
|
} else {
|
2019-12-19 21:52:45 +01:00
|
|
|
b += 2
|
|
|
|
}
|
2020-03-27 14:57:19 +01:00
|
|
|
if (x & 2) == 0 {
|
2019-12-19 21:52:45 +01:00
|
|
|
// 0x02
|
|
|
|
b++
|
|
|
|
}
|
|
|
|
return b
|
2019-09-15 03:41:24 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:21:11 +02:00
|
|
|
// Calculate string length for in number of codepoints
|
2021-04-25 16:57:21 +02:00
|
|
|
pub fn utf8_str_len(s string) int {
|
2020-05-02 00:43:59 +02:00
|
|
|
mut l := 0
|
2021-04-25 16:57:21 +02:00
|
|
|
mut i := 0
|
|
|
|
for i < s.len {
|
2020-05-02 00:43:59 +02:00
|
|
|
l++
|
2021-04-25 16:57:21 +02:00
|
|
|
i += ((0xe5000000 >> ((unsafe { s.str[i] } >> 3) & 0x1e)) & 3) + 1
|
2020-05-02 00:43:59 +02:00
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2020-05-04 13:21:11 +02:00
|
|
|
// Calculate string length for formatting, i.e. number of "characters"
|
2021-03-01 00:18:02 +01:00
|
|
|
// This is simplified implementation. if you need specification compliant width,
|
|
|
|
// use utf8.east_asian.display_width.
|
|
|
|
pub fn utf8_str_visible_length(s string) int {
|
2020-05-04 13:21:11 +02:00
|
|
|
mut l := 0
|
|
|
|
mut ul := 1
|
2021-03-01 00:18:02 +01:00
|
|
|
for i := 0; i < s.len; i += ul {
|
|
|
|
c := unsafe { s.str[i] }
|
2021-04-25 16:57:21 +02:00
|
|
|
ul = ((0xe5000000 >> ((unsafe { s.str[i] } >> 3) & 0x1e)) & 3) + 1
|
2020-05-04 13:21:11 +02:00
|
|
|
if i + ul > s.len { // incomplete UTF-8 sequence
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
l++
|
2021-04-25 16:57:21 +02:00
|
|
|
// avoid the match if not needed
|
|
|
|
if ul == 1 {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-01 00:18:02 +01:00
|
|
|
// recognize combining characters and wide characters
|
|
|
|
match ul {
|
|
|
|
2 {
|
|
|
|
r := u64((u16(c) << 8) | unsafe { s.str[i + 1] })
|
|
|
|
if r >= 0xcc80 && r < 0xcdb0 {
|
|
|
|
// diacritical marks
|
|
|
|
l--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
3 {
|
|
|
|
r := u64((u32(c) << 16) | unsafe { (u32(s.str[i + 1]) << 8) | s.str[i + 2] })
|
|
|
|
// diacritical marks extended
|
|
|
|
// diacritical marks supplement
|
|
|
|
// diacritical marks for symbols
|
|
|
|
if (r >= 0xe1aab0 && r <= 0xe1ac7f)
|
|
|
|
|| (r >= 0xe1b780 && r <= 0xe1b87f)
|
|
|
|
|| (r >= 0xe28390 && r <= 0xe2847f)
|
|
|
|
|| (r >= 0xefb8a0 && r <= 0xefb8af) {
|
|
|
|
// diacritical marks
|
|
|
|
l--
|
|
|
|
}
|
|
|
|
// Hangru
|
|
|
|
// CJK Unified Ideographics
|
|
|
|
// Hangru
|
|
|
|
// CJK
|
|
|
|
else if (r >= 0xe18480 && r <= 0xe1859f)
|
|
|
|
|| (r >= 0xe2ba80 && r <= 0xe2bf95)
|
|
|
|
|| (r >= 0xe38080 && r <= 0xe4b77f)
|
|
|
|
|| (r >= 0xe4b880 && r <= 0xea807f)
|
|
|
|
|| (r >= 0xeaa5a0 && r <= 0xeaa79f)
|
|
|
|
|| (r >= 0xeab080 && r <= 0xed9eaf)
|
|
|
|
|| (r >= 0xefa480 && r <= 0xefac7f)
|
|
|
|
|| (r >= 0xefb8b8 && r <= 0xefb9af) {
|
|
|
|
// half marks
|
|
|
|
l++
|
|
|
|
}
|
2020-05-04 13:21:11 +02:00
|
|
|
}
|
2021-03-01 00:18:02 +01:00
|
|
|
4 {
|
|
|
|
r := u64((u32(c) << 24) | unsafe {
|
|
|
|
(u32(s.str[i + 1]) << 16) | (u32(s.str[i + 2]) << 8) | s.str[i + 3]
|
|
|
|
})
|
|
|
|
// Enclosed Ideographic Supplement
|
|
|
|
// Emoji
|
|
|
|
// CJK Unified Ideographs Extension B-G
|
|
|
|
if (r >= 0x0f9f8880 && r <= 0xf09f8a8f)
|
|
|
|
|| (r >= 0xf09f8c80 && r <= 0xf09f9c90)
|
|
|
|
|| (r >= 0xf09fa490 && r <= 0xf09fa7af)
|
2021-04-15 08:20:53 +02:00
|
|
|
|| (r >= 0xf0a08080 && r <= 0xf180807f) {
|
2021-03-01 00:18:02 +01:00
|
|
|
l++
|
|
|
|
}
|
2020-05-04 13:21:11 +02:00
|
|
|
}
|
2021-03-01 00:18:02 +01:00
|
|
|
else {}
|
2020-05-04 13:21:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|