BiStringBuilder
- class BiStringBuilder(original)
Bidirectionally transformed string builder.
A BistrBuilder builds a transformed version of a source string iteratively. Each builder has an immutable original string, a current string, and the in-progress modified string, with alignments between each. For example:
original: |The| |quick,| |brown| |🦊| |jumps| |over| |the| |lazy| |🐶| | | | | | | | \ \ \ \ \ \ \ \ \ \ \ current: |The| |quick,| |brown| |fox| |jumps| |over| |the| |lazy| |dog| | | | / / / modified: |the| |quick| |brown| ...
The modified string is built in pieces by calling
replace()
to change n characters of the current string into new ones in the modified string. Convenience methods likeskip()
,insert()
, anddiscard()
are implemented on top of this basic primitive.Construct a BiStringBuilder.
- Arguments
original (
AnyString()
) – Either an original string or a BiString to start from.
- BiStringBuilder.alignment
type: Alignment
- BiStringBuilder.current
type: string
- BiStringBuilder.isComplete
type: boolean
- BiStringBuilder.modified
type: string
- BiStringBuilder.original
type: string
- BiStringBuilder.position
type: number
- BiStringBuilder.remaining
type: number
- BiStringBuilder.append(bs)
Append a BiString. The original value of the BiString must match the current string being processed.
- Arguments
bs (
BiString()
) –
- BiStringBuilder.build()
Build the
BiString()
.- Returns
BiString –
- BiStringBuilder.discard(n)
Discard a portion of the original string.
- Arguments
n (
number()
) –
- BiStringBuilder.discardMatch(pattern)
Discard a substring that matches a regex.
- Arguments
pattern (
RegExp()
) – The pattern to match. Must have either the sticky flag, forcing it to match at the current position, or the global flag, finding the next match.
- Returns
boolean – Whether a match was found.
- BiStringBuilder.discardRest()
Discard the rest of the original string.
- BiStringBuilder.insert(str)
Insert a substring into the string.
- Arguments
str (
string()
) –
- BiStringBuilder.peek(n)
Peek at the next few characters.
- Arguments
n (
number()
) – The number of characters to peek at.
- Returns
string –
- BiStringBuilder.replace(n, str)
Replace the next n characters with a new string.
- Arguments
n (
number()
) –str (
AnyString()
) –
- BiStringBuilder.replaceAll(pattern, replacement)
Replace all occurences of a regex, like
String.prototype.replace()
.- Arguments
pattern (
RegExp()
) – The pattern to match. The global flag (/g) must be set to get multiple matches.replacement (
string|Replacer()
) – The replacement string or function, as inString.prototype.replace()
.
- BiStringBuilder.replaceMatch(pattern, replacement)
Replace a substring that matches a regex.
- Arguments
pattern (
RegExp()
) – The pattern to match. Must have either the sticky flag, forcing it to match at the current position, or the global flag, finding the next match.replacement (
string|Replacer()
) – The replacement string or function, as inString.prototype.replace()
.
- Returns
boolean – Whether a match was found.
- BiStringBuilder.rewind()
Reset this builder to apply another transformation.
- BiStringBuilder.skip(n)
Skip the next n characters, copying them unchanged.
- Arguments
n (
number()
) –
- BiStringBuilder.skipMatch(pattern)
Skip a substring matching a regex, copying it unchanged.
- Arguments
pattern (
RegExp()
) – The pattern to match. Must have either the sticky flag, forcing it to match at the current position, or the global flag, finding the next match.
- Returns
boolean – Whether a match was found.
- BiStringBuilder.skipRest()
Skip the rest of the string, copying it unchanged.