Skip to content

Commit

Permalink
std::strings: update StrBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 10, 2024
1 parent 40f22be commit a62fa14
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions std/internal/strings/builder.jule
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use std::unsafe
use utf8 for std::unicode::utf8

// String builder for efficient concatenation.
// See [std::strings] for documentation.
struct StrBuilder {
buf: []byte
}
Expand Down Expand Up @@ -45,15 +45,19 @@ impl StrBuilder {
self.WriteStr(str(r))
}

// Returns as string.
fn Str(self): str {
ret str(self.buf)
// Returns as string, then calls the [Clear] method.
fn Str(mut self): str {
mut buf := self.buf
self.Clear() // Clear common buffer for safety.
buf = append(buf, 0) // NULL termination.
buf = buf[:len(buf)-1]
ret unsafe::StrFromBytes(buf)
}

// Clears buffer.
// Capacity will not be changed.
// After calling this function, write calls will allocate new buffer.
fn Clear(mut self) {
self.buf = self.buf[:0]
self.buf = nil
}

// Returns length of buffer.
Expand Down
1 change: 1 addition & 0 deletions std/strings/builder.jule
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
use strings for std::internal::strings

// String builder for efficient concatenation.
// Optimized for single string building not for repeated use.
type StrBuilder: strings::StrBuilder

0 comments on commit a62fa14

Please sign in to comment.