diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index ce137f1665..4724a444f0 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1009,3 +1009,17 @@ pub fn (s string) bytes() []byte { C.memcpy(buf.data, s.str, s.len) return buf } + +// Returns a new string with a specified number of copies of the string it was called on. +pub fn (s string) repeat(count int) string { + if count <= 1 { + return s + } + ret := malloc(s.len * count + count) + C.strcpy(ret, s.str) + for count > 1 { + C.strcat(ret, s.str) + count-- + } + return string(ret) +} diff --git a/vlib/builtin/string_test.v b/vlib/builtin/string_test.v index 22bb9ff221..fe55c43713 100644 --- a/vlib/builtin/string_test.v +++ b/vlib/builtin/string_test.v @@ -459,3 +459,8 @@ fn test_ustring_count() { assert (a.count('ﷰ'.ustring())) == 2 assert (a.count('a'.ustring())) == 0 } + +fn test_repeat() { + s := 'V! ' + assert s.repeat(5) == 'V! V! V! V! V! ' +}