module text
module lib/text.mu
import "text"
text provides a collection of text manipulation functions.
Imports
Functions
fields#
fields parses whitespace- and quote-aware fields from line.
Source lib/text.mu:91
fn fields(line) {
if !typing.is_str(line) {
return error("text.fields expects string line, got " + type(line))
}
fields := []
length := len(line)
i := 0
while i < length {
i = trim_spaces(line, i)
if i >= length {
break
}
ch := line[i]
if ch == "\"" {
pair := _parse_quoted_field(line, i)
if is_error(pair) {
return pair
}
field := pair[0]
next := pair[1]
append(fields, field)
i = next
continue
}
pair := _parse_unquoted_field(line, i)
if is_error(pair) {
return pair
}
field := pair[0]
next := pair[1]
append(fields, field)
i = next
}
return fields
}lines#
lines returns every line in text, keeping an empty final line if present.
Source lib/text.mu:45
fn lines(text) {
if !typing.is_str(text) {
return error("text.lines expects string text, got " + type(text))
}
lines := []
cur := ""
i := 0
while i < len(text) {
ch := text[i]
if ch == "\n" {
append(lines, cur)
cur = ""
} else if ch != "\r" {
cur = cur + ch
}
i = i + 1
}
append(lines, cur)
return lines
}pad_number#
pad_number left-pads the number's decimal text to the given width with zeros.
Source lib/text.mu:15
fn pad_number(value, width) {
return strings.pad_left(inspect(value), width, "0")
}parse_int#
parse_int parses a whole-string integer without trimming.
Source lib/text.mu:40
fn parse_int(text) {
return parse_int_from(text, 0)
}parse_int_from#
parse_int_from parses an integer starting at index start, honoring an optional leading '-'.
Source lib/text.mu:20
fn parse_int_from(line, start) {
i := start
sign := 1
if i < len(line) && line[i] == "-" {
sign = -1
i = i + 1
}
n := 0
while i < len(line) {
o := ord(line[i])
if o < ord("0") || o > ord("9") {
break
}
n = n * 10 + (o - ord("0"))
i = i + 1
}
return sign * n
}parse_key_value#
parse_key_value splits line on sep and unquotes quoted values.
Source lib/text.mu:128
fn parse_key_value(line, sep) {
if !typing.is_str(line) {
return error("text.parse_key_value expects string line, got " + type(line))
}
if !typing.is_str(sep) {
return error("text.parse_key_value expects string sep, got " + type(sep))
}
text := strings.trim(line)
marker := sep
if len(marker) == 0 {
return error("text.parse_key_value separator must not be empty")
}
total := len(text)
i := 0
while i+len(marker) <= total {
if strings.matches_at(text, marker, i) {
key := strings.trim(strings.substring(text, 0, i))
value := strings.trim(strings.substring(text, i+len(marker), total))
if len(value) >= 2 && value[0] == "\"" && value[len(value)-1] == "\"" {
unescaped := _unescape_quoted(strings.substring(value, 1, len(value)-1))
if is_error(unescaped) {
return unescaped
}
return [key, unescaped]
}
return [key, value]
}
i = i + 1
}
return [text, ""]
}trim_comment#
trim_comment removes everything starting at marker, if present.
Source lib/text.mu:67
fn trim_comment(line, marker) {
if !typing.is_str(line) {
return error("text.trim_comment expects string line, got " + type(line))
}
value := line
if !typing.is_str(marker) {
return error("text.trim_comment expects string marker, got " + type(marker))
}
sep := marker
if len(sep) == 0 {
return value
}
i := 0
limit := len(value)
while i+len(sep) <= limit {
if strings.matches_at(value, sep, i) {
return strings.substring(value, 0, i)
}
i = i + 1
}
return value
}trim_spaces#
trim_spaces skips whitespace characters starting at index i.
Source lib/text.mu:7
fn trim_spaces(line, i) {
while i < len(line) && strings.is_whitespace(line[i]) {
i = i + 1
}
return i
}Internal helpers
Underscore-prefixed names are implementation detail. They are listed so the module's source reads without surprises, not as API — they may change at any time.
| _decode_escape(ch) | decode_escape maps single-character escapes to their literal characters. |
| _parse_quoted_field(line, start) | parse_quoted_field reads a quoted field, handling escapes, and returns field+next index. |
| _parse_unquoted_field(line, start) | parse_unquoted_field reads an unquoted field until whitespace is reached. |
| _unescape_quoted(value) | unescape_quoted resolves escape sequences within a quoted string fragment. |