Skip to content

Commit

Permalink
tweak stringbuilder_buffer grow
Browse files Browse the repository at this point in the history
  • Loading branch information
Yu-zh committed Jan 2, 2025
1 parent 532f2e8 commit 2917ec0
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions builtin/stringbuilder_buffer.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,21 @@ fn StringBuilder::grow_if_necessary(
self : StringBuilder,
required : Int
) -> Unit {
// TODO: get rid of mut
let mut enough_space = self.data.length()
if enough_space <= 0 {
enough_space = 1
let current_len = self.data.length()
if required <= current_len {
return
}
let mut enough_space = if current_len <= 0 { 8 } else { current_len }
// double the enough_space until it larger than required
while enough_space < required {
enough_space = enough_space * 2
}
if enough_space != self.data.length() {
self.data = FixedArray::make(enough_space, Byte::default())..unsafe_blit(
0,
self.data,
0,
self.len,
)
}
self.data = FixedArray::make(enough_space, Byte::default())..unsafe_blit(
0,
self.data,
0,
self.len,
)
}

///|
Expand Down

0 comments on commit 2917ec0

Please sign in to comment.