aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/strings/strings.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/strings/strings.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/strings/strings.c.v')
-rw-r--r--v_windows/v/old/vlib/strings/strings.c.v38
1 files changed, 38 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/strings/strings.c.v b/v_windows/v/old/vlib/strings/strings.c.v
new file mode 100644
index 0000000..c020f5b
--- /dev/null
+++ b/v_windows/v/old/vlib/strings/strings.c.v
@@ -0,0 +1,38 @@
+module strings
+
+// strings.repeat - fill a string with `n` repetitions of the character `c`
+pub fn repeat(c byte, n int) string {
+ if n <= 0 {
+ return ''
+ }
+ mut bytes := unsafe { malloc_noscan(n + 1) }
+ unsafe {
+ C.memset(bytes, c, n)
+ bytes[n] = `0`
+ }
+ return unsafe { bytes.vstring_with_len(n) }
+}
+
+// strings.repeat_string - gives you `n` repetitions of the substring `s`
+// NB: strings.repeat, that repeats a single byte, is between 2x
+// and 24x faster than strings.repeat_string called for a 1 char string.
+pub fn repeat_string(s string, n int) string {
+ if n <= 0 || s.len == 0 {
+ return ''
+ }
+ slen := s.len
+ blen := slen * n
+ mut bytes := unsafe { malloc_noscan(blen + 1) }
+ for bi in 0 .. n {
+ bislen := bi * slen
+ for si in 0 .. slen {
+ unsafe {
+ bytes[bislen + si] = s[si]
+ }
+ }
+ }
+ unsafe {
+ bytes[blen] = `0`
+ }
+ return unsafe { bytes.vstring_with_len(blen) }
+}