Parser class final

A parser that processes strings containing ANSI escape codes and tracks the current Style.

Parser allows you to perform various operations on strings with ANSI sequences, such as:

  • Stripping out the escape codes using removeAll.
  • Optimizing the string to remove redundant codes using optimize.
  • Extracting a substring while preserving the active style using substring.
  • Inserting text that takes the style of the place it lands in and leaves the rest of the string as it was using insertBefore and insertAfter.
  • Retrieving the computed Style at a specific text position using stateAt and finalState.
  • Retrieving the hyperlink in force at a text position using linkAt and finalLink.
  • Analyzing a string using pieces.

Parser allows you to work with a string containing ANSI escape codes as with a regular string without ANSI escape codes:

  • length - string length without ANSI escape codes.
  • indexOf - index of the first occurrence of a pattern in the string without ANSI escape codes.
  • lastIndexOf - index of the last occurrence of a pattern in the string without ANSI escape codes.
  • contains - whether the string contains a pattern in the string without ANSI escape codes.
  • startsWith - whether the string starts with a pattern in the string without ANSI escape codes.
  • endsWith - whether the string ends with a pattern in the string without ANSI escape codes.

On a large input, prefer walking pieces with a for and taking what the loop needs as it goes: the walk parses lazily, and what it has read it keeps. prepare, length and the string methods read the whole string and keep every piece, which on megabytes of input is megabytes of parse tree.

Keeping a Text from that walk after the loop is not free either, and reading its Text.string does not make it so: see Text.string for what a kept piece holds onto for as long as it is kept.

Constructors

Parser(String input)
Creates a Parser for the given input string.
A Parser over a string that begins inside initialLink.

Properties

The hyperlink the string leaves open, or null where it leaves none.
no setterinherited
finalState Style
The final S after processing the entire string.
no setterinherited
hashCode int
The hash code for this object.
no setterinherited
The link the string is read as starting inside, where there is one.
finalinherited
initialState Style
The state the string is read as starting in.
finalinherited
input String
The string being read, escape codes and all.
finalinherited
isClosed bool
Whether the string ends in the state it began in.
no setterinherited
isParsed bool
Whether the whole string has been read, rather than as much of it as the questions asked so far needed. See prepare.
no setterinherited
length int
String length without ANSI escape codes, in UTF-16 code units.
no setterinherited
pieces Pieces<Style>
The Pieces of the string.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

contains(Pattern other, [int startIndex = 0]) bool
Whether the string contains a pattern in the string without ANSI escape codes.
inherited
endsWith(String other) bool
Whether the string ends with a pattern in the string without ANSI escape codes.
inherited
indexOf(Pattern pattern) int
Index of the first occurrence of a pattern in the string without ANSI escape codes.
inherited
insertAfter(int pos, String text) String
Inserts text at the plain text pos, behind the escape codes standing there.
inherited
insertBefore(int pos, String text) String
Inserts text at the plain text pos, in front of the escape codes standing there.
inherited
lastIndexOf(Pattern pattern) int
Index of the last occurrence of a pattern in the string without ANSI escape codes.
inherited
linkAt(int pos) OscLink?
The hyperlink open at the plain text pos, or null where none is.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
optimize({bool close = true}) String
Optimizes the string by removing consecutive escape codes.
inherited
padLeft(int width, [String padding = ' ']) String
The string with padding written before it until the text is width wide.
inherited
padRight(int width, [String padding = ' ']) String
The string with padding written after it until the text is width wide.
inherited
prepare() → void
Reads the whole string, ahead of the questions asked of it.
inherited
removeAll() String
Returns the string without ANSI escape codes.
inherited
replaceAll(String replace(EscapeCode code), {String replacePlainText(Text entity)?}) String
Replaces all EscapeCodes in the string with the result of the replace function.
inherited
showControlFunctions({String open = '[', String close = ']'}) String
Returns a string in which all EscapeCode are replaced with their identifiers.
inherited
startsWith(Pattern pattern, [int index = 0]) bool
Whether the string starts with a pattern in the string without ANSI escape codes.
inherited
stateAt(int pos) Style
Returns the S of the string at the given plain text pos.
inherited
substring(int start, {int? maxLength, bool close = true}) String
Returns a substring of the given start position while preserving the text style.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited