Search results

There are no results.

std.string.StringBuffer

class pub StringBuffer

A buffer for efficiently concatenating String objects together.

When concatenating multiple String objects together, intermediate String objects are created. For example:

'foo' + 'bar' + 'baz'

This code will allocate three String objects (for the String literals), and two additional String objects. This is the result of the above expression being evaluated as follows:

('foo' + 'bar') + 'baz'

This means that the first allocated String resulting from this expression is 'foobar', which is then concatenated with 'baz', producing 'foobarbaz'.

Using a StringBuffer we can work around this, only allocating a String once we are done:

import std.string (StringBuffer)

let buffer = StringBuffer.new

buffer.push('foo')
buffer.push('bar')
buffer.push('baz')

buffer.to_string # => 'foobarbaz'

You can also create a StringBuffer and feed it values right away:

import std.string (StringBuffer)

let buffer = StringBuffer.from_array(['foo', 'bar', 'baz'])

buffer.to_string # => 'foobarbaz'

Static methods

from_array

Show source code
Hide source code
fn pub static from_array(strings: Array[String]) -> StringBuffer {
  StringBuffer(strings)
}
fn pub static from_array(strings: Array[String]) -> StringBuffer

Returns a new StringBuffer from an existing Array.

Examples

Creating a StringBuffer from an Array:

import std.string (StringBuffer)

let strings = ['foo', 'bar']

StringBuffer.from_array(strings).to_string # => 'foobar'

new

Show source code
Hide source code
fn pub static new -> StringBuffer {
  StringBuffer([])
}
fn pub static new -> StringBuffer

Returns a new empty StringBuffer.

Instance methods

into_string

Show source code
Hide source code
fn pub move into_string -> String {
  to_string
}
fn pub move into_string -> String

Moves self into a String.

push

Show source code
Hide source code
fn pub mut push(string: String) {
  @strings.push(string)
}
fn pub mut push(string: String)

Adds the given String to the buffer.

Examples

Adding a String to a StringBuffer:

import std.string (StringBuffer)

let buffer = StringBuffer.new

buffer.push('hello') # => 'hello'

size

Show source code
Hide source code
fn pub size -> Int {
  @strings.size
}
fn pub size -> Int

Returns the number of values in self.

to_string

Show source code
Hide source code
fn pub to_string -> String {
  inko_string_concat(_INKO.state, @strings.to_pointer, @strings.size)
}
fn pub to_string -> String

Generates a String using the current contents of the buffer.

Examples

Converting a StringBuffer to a String:

import std.string (StringBuffer)

let buffer = StringBuffer.new

buffer.push('hello ')
buffer.push('world')

buffer.to_string # => 'hello world'

Implemented traits

std.string.

IntoString

impl IntoString for StringBuffer
std.string.

ToString

impl ToString for StringBuffer