About
Various string utilities in the Mys programming language.
String builder example
A basic example that creates a string from numbers and strings.
from string import StringBuilder
func main():
builder = StringBuilder()
builder += "Temperature: "
builder += 5
builder += 'C'
assert builder.to_string() == "Temperature: 5 C"
String reader example
A basic example that uses the string reader class to get characters from a string.
from string import StringReader
func main():
reader = StringReader("kalle kula")
assert reader.peek() == 'k'
assert reader.get() == 'k'
assert reader.get() == 'a'
assert reader.read(3) == "lle"
assert reader.get() == ' '
assert reader.read() == "kula"
assert reader.get() == ''
assert reader.peek() == ''
assert reader.read() == ""
assert reader.read(100) == ""
reader.unget()
assert reader.available() == 1
assert reader.get() == 'a'
assert reader.get() == ''
API
class StringError(Error):
message: string
class StringBuilder:
Easily create a string from strings and numbers.
func __init__(self):
func +=(self, data: string):
Append given string.
func +=(self, data: char):
Append given character.
func +=(self, data: i8):
Append given interger.
func +=(self, data: i16):
Append given interger.
func +=(self, data: i32):
Append given interger.
func +=(self, data: i64):
Append given interger.
func +=(self, data: u8):
Append given unsigned interger.
func +=(self, data: u16):
Append given unsigned interger.
func +=(self, data: u32):
Append given unsigned interger.
func +=(self, data: u64):
Append given unsigned interger.
func to_string(self) -> string:
Returns a string of current data.
func length(self) -> i64:
Get current string length.
func clear(self):
Clear everything.
func clear_from(self, offset: i64):
Clear everything after given offset.
class StringReader:
A string reader.
func __init__(self, data: string):
func available(self) -> i64:
Returns number of available characters.
func seek(self, pos: i64):
Seek given position relative to the beginning of the string.
func tell(self) -> i64:
Tell current position in the string.
func get(self) -> char:
Get the next character. Returns '' if there are no more characters
available.
func peek(self) -> char:
Peek at the next character.
func read(self, size: i64 = -1) -> string:
Read zero or more characters. Give size as -1 to read everything.
func unget(self):
Unget last character.
func pretty(data: string) -> string:
Try to make given object dump string pretty by inserting newlines
and indentations based on brackets, parentheses, strings,
characters and commas.
func indent(text: string, prefix: string = " ") -> string:
Add given prefix to the beginning of given lines.
The prefix is added to all lines that do not consist solely of
whitespace.
func join_or(items: [string]) -> string:
Join all items in given list as "item1, item2, item3 or item4".
func join_and(items: [string]) -> string:
Join all items in given list as "item1, item2, item3 and item4".