Text-processing helpers for everyday string manipulation.
This module provides a small set of "boring but useful" text utilities:
indentation helpers, wrapping and truncation, prefix/suffix checks, simple
literal substring counting, and configurable trimming.
The functions in this module operate on Lua strings as byte sequences. They
do not perform Unicode-aware grapheme segmentation, display-width
calculation, locale-sensitive casing, or language-aware hyphenation. That
makes them a good fit for:
Removes shared leading indentation from lines in a string.
When columns is omitted, the smallest indentation shared by all
non-blank lines is removed. When columns is provided, up to that many
leading spaces or tabs are removed from each line. Blank lines do not
contribute to the inferred indentation level. Output uses \n line
endings.
Parameters
The string whose lines should be unindented.
Optional explicit number of leading columns to remove.
Returns
Prefixes each line in a string with spaces.
Blank lines are indented too. Output uses \n as the line separator.
Parameters
The string whose lines should be indented.
Number of spaces to insert at the start of each line.
Returns
Wraps text to a target width.
Wrapping prefers whitespace boundaries. breakWordThreshold controls
when word-breaking is allowed or preferred:
nil or 0: never break words
1 or 2: break only words that are themselves longer than width
3+: also prefer word-breaking when a whitespace wrap would leave at
least that many unused columns at the end of the line
When a word is broken, breakWordMarker is appended to the broken
fragment and its width counts against width. The marker defaults to
"-". Use "" to break without a marker.
Lines are measured in bytes and output uses \n line endings.
Parameters
Maximum width of each output line, measured in bytes.
Controls when word-breaking is allowed or preferred.
Marker appended to broken word fragments.
Returns
Shortens a string to a maximum width, optionally appending a marker.
The marker counts against the width budget. If the marker itself is wider
than width, the marker is returned unchanged.
Parameters
Maximum output width, measured in bytes.
Optional marker appended to truncated output.
Returns
The original or truncated string.
Counts non-overlapping literal occurrences of a substring.
This uses plain substring matching rather than Lua patterns.
Parameters
The string to search within.
The literal substring to count.
Returns
Number of non-overlapping matches.
Returns whether a string begins with the given prefix.
Parameters
Returns
true when value starts with prefix.
Returns whether a string ends with the given suffix.
Parameters
Returns
true when value ends with suffix.
Removes a prefix when it is present.
If value does not start with prefix, the original string is returned.
Parameters
Returns
The string without the prefix, or the original string.
Removes a suffix when it is present.
If value does not end with suffix, the original string is returned.
Parameters
Returns
The string without the suffix, or the original string.
Trims characters from one or both sides of a string.
When trimCharacters is omitted, ASCII whitespace characters are removed:
space, tab, line feed, carriage return, vertical tab, and form feed.
Characters are matched literally and individually rather than as Lua
patterns.
text.trim(value: string, side: ("start" | "end" | "both")?, trimCharacters: { string }?) → string
Parameters
side: ("start" | "end" | "both")?
Which side to trim: "start", "end", or "both".
Characters that should be removed while scanning inward.
Returns