From f5c4671bfbad96bf346bd7e9a21fc4317b4959df Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Sat, 3 Dec 2022 17:00:20 +0530 Subject: Adds most of the tools --- v_windows/v/vlib/builtin/rune.v | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 v_windows/v/vlib/builtin/rune.v (limited to 'v_windows/v/vlib/builtin/rune.v') diff --git a/v_windows/v/vlib/builtin/rune.v b/v_windows/v/vlib/builtin/rune.v new file mode 100644 index 0000000..3364d90 --- /dev/null +++ b/v_windows/v/vlib/builtin/rune.v @@ -0,0 +1,65 @@ +// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved. +// Use of this source code is governed by an MIT license that can be found in the LICENSE file. +module builtin + +import strings + +// 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 + +pub fn (c rune) str() string { + return utf32_to_str(u32(c)) + /* + unsafe { + fst_byte := int(c)>>8 * 3 & 0xff + len := utf8_char_len(byte(fst_byte)) + println('len=$len') + mut str := string{ + len: len + str: malloc(len + 1) + } + for i in 0..len { + str.str[i] = byte(int(c)>>8 * (3 - i) & 0xff) + } + str.str[len] = `\0` + println(str) + return str + } + */ +} + +// string converts a rune array to a string +[manualfree] +pub fn (ra []rune) string() string { + mut sb := strings.new_builder(ra.len) + sb.write_runes(ra) + res := sb.str() + unsafe { sb.free() } + return res +} + +// Define this on byte as well, so that we can do `s[0].is_capital()` +pub fn (c byte) is_capital() bool { + return c >= `A` && c <= `Z` +} + +pub fn (b []byte) clone() []byte { + mut res := []byte{len: b.len} + // mut res := make([]byte, {repeat:b.len}) + for i in 0 .. b.len { + res[i] = b[i] + } + return res +} + +// TODO: remove this once runes are implemented +pub fn (b []byte) bytestr() string { + unsafe { + buf := malloc_noscan(b.len + 1) + vmemcpy(buf, b.data, b.len) + buf[b.len] = 0 + return tos(buf, b.len) + } +} -- cgit v1.2.3