vlib: prepare atoi.v and utf8.v for -Wimpure-v

pull/7342/head
Delyan Angelov 2020-12-15 10:17:46 +02:00
parent bd07177ef0
commit d155d8d3f0
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
4 changed files with 77 additions and 80 deletions

View File

@ -0,0 +1,74 @@
module builtin
pub fn (_str string) to_wide() &u16 {
$if windows {
num_chars := (C.MultiByteToWideChar(cp_utf8, 0, charptr(_str.str), _str.len, 0, 0))
mut wstr := &u16(malloc((num_chars + 1) * 2)) // sizeof(wchar_t)
if wstr != 0 {
C.MultiByteToWideChar(cp_utf8, 0, charptr(_str.str), _str.len, wstr, num_chars)
unsafe {
C.memset(&byte(wstr) + num_chars * 2, 0, 2)
}
}
return wstr
} $else {
return 0
}
}
pub fn string_from_wide(_wstr &u16) string {
$if windows {
wstr_len := C.wcslen(_wstr)
return string_from_wide2(_wstr, wstr_len)
} $else {
return ''
}
}
pub fn string_from_wide2(_wstr &u16, len int) string {
$if windows {
num_chars := C.WideCharToMultiByte(cp_utf8, 0, _wstr, len, 0, 0, 0, 0)
mut str_to := malloc(num_chars + 1)
if str_to != 0 {
C.WideCharToMultiByte(cp_utf8, 0, _wstr, len, charptr(str_to), num_chars, 0, 0)
unsafe {
C.memset(str_to + num_chars, 0, 1)
}
}
return tos2(str_to)
} $else {
return ''
}
}
// Reads an utf8 character from standard input
pub fn utf8_getchar() int {
c := C.getchar()
len := utf8_len(byte(~c))
if c < 0 {
return 0
}
else if len == 0 {
return c
}
else if len == 1 {
return -1
}
else {
mut uc := c & ((1<<(7 - len)) - 1)
for i := 0; i + 1 < len; i++ {
c2 := C.getchar()
if c2 != -1 && (c2>>6) == 2 {
uc <<= 6
uc |= (c2 & 63)
}
else if c2 == -1 {
return 0
}
else {
return -1
}
}
return uc
}
}

View File

@ -70,51 +70,6 @@ pub fn (_rune string) utf32_code() int {
return res
}
const (
cp_utf8 = 65001
)
pub fn (_str string) to_wide() &u16 {
$if windows {
num_chars := (C.MultiByteToWideChar(cp_utf8, 0, charptr(_str.str), _str.len, 0, 0))
mut wstr := &u16(malloc((num_chars + 1) * 2)) // sizeof(wchar_t)
if wstr != 0 {
C.MultiByteToWideChar(cp_utf8, 0, charptr(_str.str), _str.len, wstr, num_chars)
unsafe {
C.memset(&byte(wstr) + num_chars * 2, 0, 2)
}
}
return wstr
} $else {
return 0
}
}
pub fn string_from_wide(_wstr &u16) string {
$if windows {
wstr_len := C.wcslen(_wstr)
return string_from_wide2(_wstr, wstr_len)
} $else {
return ''
}
}
pub fn string_from_wide2(_wstr &u16, len int) string {
$if windows {
num_chars := C.WideCharToMultiByte(cp_utf8, 0, _wstr, len, 0, 0, 0, 0)
mut str_to := malloc(num_chars + 1)
if str_to != 0 {
C.WideCharToMultiByte(cp_utf8, 0, _wstr, len, charptr(str_to), num_chars, 0, 0)
unsafe {
C.memset(str_to + num_chars, 0, 1)
}
}
return tos2(str_to)
} $else {
return ''
}
}
// Calculate length to read from the first byte
fn utf8_len(c byte) int {
mut b := 0
@ -193,35 +148,3 @@ fn utf8_str_visible_length(s string) int {
}
return l
}
// Reads an utf8 character from standard input
pub fn utf8_getchar() int {
c := C.getchar()
len := utf8_len(byte(~c))
if c < 0 {
return 0
}
else if len == 0 {
return c
}
else if len == 1 {
return -1
}
else {
mut uc := c & ((1<<(7 - len)) - 1)
for i := 0; i + 1 < len; i++ {
c2 := C.getchar()
if c2 != -1 && (c2>>6) == 2 {
uc <<= 6
uc |= (c2 & 63)
}
else if c2 == -1 {
return 0
}
else {
return -1
}
}
return uc
}
}

View File

@ -24,8 +24,8 @@ const (
const (
// save importing math mod just for these
max_u32 = u32(4294967295)
max_u64 = u64(18446744073709551615)
max_u32 = 4294967295
max_u64 = 18446744073709551615
)
// --- LeadingZeros ---

View File

@ -9,7 +9,7 @@ const (
// int_size = 32 << (~u32(0) >> 63)
// max_u64 = u64(u64(1 << 63) - 1)
int_size = 32
max_u64 = u64(C.UINT64_MAX)// as u64 // use this until we add support
max_u64 = 18446744073709551615 // as u64 // use this until we add support
)
pub fn byte_to_lower(c byte) byte {