Skip to content

Commit

Permalink
builtin: cleanup u8.repeat() and rune.repeat()
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Aug 23, 2024
1 parent 18f8999 commit 2267815
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
17 changes: 5 additions & 12 deletions vlib/builtin/int.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions vlib/builtin/js/rune.js.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 1 addition & 3 deletions vlib/builtin/rune.v
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 2267815

Please sign in to comment.