diff --git a/vlib/builtin/utf8.c.v b/vlib/builtin/utf8.c.v new file mode 100644 index 0000000000..04b25caf66 --- /dev/null +++ b/vlib/builtin/utf8.c.v @@ -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 + } +} diff --git a/vlib/builtin/utf8.v b/vlib/builtin/utf8.v index 16ea26463b..1a2d66d900 100644 --- a/vlib/builtin/utf8.v +++ b/vlib/builtin/utf8.v @@ -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 - } -} diff --git a/vlib/math/bits/bits.v b/vlib/math/bits/bits.v index b61504a64f..c8c6264629 100644 --- a/vlib/math/bits/bits.v +++ b/vlib/math/bits/bits.v @@ -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 --- diff --git a/vlib/strconv/atoi.v b/vlib/strconv/atoi.v index b20d5c2383..a9c3bdb2db 100644 --- a/vlib/strconv/atoi.v +++ b/vlib/strconv/atoi.v @@ -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 {