mirror of
https://github.com/vlang/v.git
synced 2025-09-08 23:07:19 -04:00
builtin: cleanup u8.repeat() and rune.repeat() (#22100)
This commit is contained in:
parent
e3ceb5881a
commit
971feb8f89
@ -599,24 +599,17 @@ pub fn (b []u8) byterune() !rune {
|
||||
|
||||
// repeat returns a new string with `count` number of copies of the byte it was called on.
|
||||
pub fn (b u8) repeat(count int) string {
|
||||
if count < 0 {
|
||||
panic('byte.repeat: count is negative: ${count}')
|
||||
} else if count == 0 {
|
||||
if count <= 0 {
|
||||
return ''
|
||||
} else if count == 1 {
|
||||
return b.ascii_str()
|
||||
}
|
||||
mut ret := unsafe { malloc_noscan(count + 1) }
|
||||
for i in 0 .. count {
|
||||
unsafe {
|
||||
ret[i] = b
|
||||
}
|
||||
}
|
||||
new_len := count
|
||||
mut bytes := unsafe { malloc_noscan(count + 1) }
|
||||
unsafe {
|
||||
ret[new_len] = 0
|
||||
C.memset(bytes, b, count)
|
||||
bytes[count] = `0`
|
||||
}
|
||||
return unsafe { ret.vstring_with_len(new_len) }
|
||||
return unsafe { bytes.vstring_with_len(count) }
|
||||
}
|
||||
|
||||
// for atomic ints, internal
|
||||
|
@ -10,9 +10,7 @@ pub fn (ra []rune) string() string {
|
||||
}
|
||||
|
||||
pub fn (c rune) repeat(count int) string {
|
||||
if count < 0 {
|
||||
panic('rune.repeat: count is negative: ${count}')
|
||||
} else if count == 0 {
|
||||
if count <= 0 {
|
||||
return ''
|
||||
} else if count == 1 {
|
||||
return c.str()
|
||||
|
@ -43,9 +43,7 @@ pub fn (ra []rune) string() string {
|
||||
|
||||
// repeat returns a new string with `count` number of copies of the rune it was called on.
|
||||
pub fn (c rune) repeat(count int) string {
|
||||
if count < 0 {
|
||||
panic('rune.repeat: count is negative: ${count}')
|
||||
} else if count == 0 {
|
||||
if count <= 0 {
|
||||
return ''
|
||||
} else if count == 1 {
|
||||
return c.str()
|
||||
|
Loading…
x
Reference in New Issue
Block a user