strbuf.eh

String buffers. More...

use "strbuf.eh"

Types

type StrBuf < Any;

Functions

def StrBuf.addch(ch: Char): StrBuf;
def StrBuf.append(a: Any): StrBuf;
def StrBuf.ch(at: Int): Char;
def StrBuf.chars(from: Int, to: Int, buf: [Char], ofs: Int);
def StrBuf.delch(at: Int): StrBuf;
def StrBuf.delete(from: Int, to: Int): StrBuf;
def StrBuf.insch(at: Int, ch: Char): StrBuf;
def StrBuf.insert(at: Int, a: Any): StrBuf;
def StrBuf.len(): Int;
def StrBuf.new(): StrBuf;
def StrBuf.replace(from: Int, to: Int, by: String): StrBuf;
def StrBuf.setch(at: Int, ch: Char): StrBuf;

Description

String buffer is a mutable sequence of characters. Many of StrBuf functions return the string buffer after operation allowing to write sequences like
strbuf.append("restart").delete(0,2).insert(4, "le")
String buffers are essentially effective when you need to construct new string. Compare:
// slow
var str = ""
for (var i=0, i<100, i+=1) 
  str += i


// fast
var sb = new StrBuf()
for (var i=0, i<100, i+=1) 
  sb.append(i)

var str = sb.tostr()
In the first cycle new string is created on each iteration, wasting memory and time on memory allocation. The second cycle is more efficient in both memory and time. You may still use string concatenation for simple cases though, compiler automatically turns long concatenations into StrBuf calls.
// source string
println("sin" + "(" + a + ") = " + sin(a))

// compiler turns into
println(new StrBuf().append("sin(").append(a).append(") = ").append(sin(a)).tostr())

Type details

type StrBuf < Any;
Mutable character sequence. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed.

Function details

def StrBuf.new(): StrBuf;
Creates new empty string buffer.

def StrBuf.ch(at: Int): Char;
Returns character at the specified position of this string buffer. You may also use square brackets to get character (strbuf[at]).

def StrBuf.append(a: Any): StrBuf;
Appends text representation of given value to the end of this string buffer. The string representation is the same as returned by Any.tostr. Note that character literals are Int numbers so for example calling strbuf.append('!') actually adds string "33" to the string buffer strbuf. To add single character use addch function.

def StrBuf.addch(ch: Char): StrBuf;
Adds given character to the end of the string buffer.

def StrBuf.insert(at: Int, a: Any): StrBuf;
Inserts string representation of given value at specified index of this string buffer. The string representation is the same as returned by Any.tostr.

def StrBuf.insch(at: Int, ch: Char): StrBuf;
Inserts given character at specified index of this string buffer.

def StrBuf.replace(from: Int, to: Int, by: String): StrBuf;
Replaces substring of this string buffer by the string representation of given value. The string representation is the same as returned by Any.tostr.

def StrBuf.setch(at: Int, ch: Char): StrBuf;
Replaces character at specified index of this string buffer with given one.

def StrBuf.delete(from: Int, to: Int): StrBuf;
Removes all characters in given range from this string buffer.

def StrBuf.delch(at: Int): StrBuf;
Removes character at specified index from this string buffer.

def StrBuf.len(): Int;
Returns current number of characters in this string buffer.

def StrBuf.chars(from: Int, to: Int, buf: [Char], ofs: Int);
Copies characters from given range of this string buffer to the character array buf starting at offset ofs.