builtin: cleanup u8.repeat() and rune.repeat() (#22100)

This commit is contained in:
yuyi 2024-08-23 13:24:35 +08:00 committed by GitHub
parent e3ceb5881a
commit 971feb8f89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 18 deletions

View File

@ -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

View File

@ -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()

View File

@ -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()