aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/builtin/utf8.c.v
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/old/vlib/builtin/utf8.c.v
downloadcli-tools-windows-master.tar.gz
cli-tools-windows-master.tar.bz2
cli-tools-windows-master.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/old/vlib/builtin/utf8.c.v')
-rw-r--r--v_windows/v/old/vlib/builtin/utf8.c.v79
1 files changed, 79 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/builtin/utf8.c.v b/v_windows/v/old/vlib/builtin/utf8.c.v
new file mode 100644
index 0000000..f2ead3a
--- /dev/null
+++ b/v_windows/v/old/vlib/builtin/utf8.c.v
@@ -0,0 +1,79 @@
+module builtin
+
+const (
+ cp_utf8 = 65001
+)
+
+pub fn (_str string) to_wide() &u16 {
+ $if windows {
+ unsafe {
+ num_chars := (C.MultiByteToWideChar(cp_utf8, 0, &char(_str.str), _str.len,
+ 0, 0))
+ mut wstr := &u16(malloc_noscan((num_chars + 1) * 2)) // sizeof(wchar_t)
+ if wstr != 0 {
+ C.MultiByteToWideChar(cp_utf8, 0, &char(_str.str), _str.len, wstr, num_chars)
+ C.memset(&byte(wstr) + num_chars * 2, 0, 2)
+ }
+ return wstr
+ }
+ } $else {
+ return 0
+ }
+}
+
+[unsafe]
+pub fn string_from_wide(_wstr &u16) string {
+ $if windows {
+ unsafe {
+ wstr_len := C.wcslen(_wstr)
+ return string_from_wide2(_wstr, wstr_len)
+ }
+ } $else {
+ return ''
+ }
+}
+
+[unsafe]
+pub fn string_from_wide2(_wstr &u16, len int) string {
+ $if windows {
+ unsafe {
+ num_chars := C.WideCharToMultiByte(cp_utf8, 0, _wstr, len, 0, 0, 0, 0)
+ mut str_to := malloc_noscan(num_chars + 1)
+ if str_to != 0 {
+ C.WideCharToMultiByte(cp_utf8, 0, _wstr, len, &char(str_to), num_chars,
+ 0, 0)
+ 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
+ }
+}