blob: c020f5b402d5b617e723007232d6da4a4934eee5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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) }
}
|