Class TextLine
Internally stores a mutable list of Character objects, one per character in
the line. Provides operations for inserting, cutting, and copying substrings, as well
as indentation manipulation (adding or removing leading spaces).
Lines automatically trim trailing whitespace via the internal pack() method,
which is invoked after most mutating operations. This ensures that lines never store
unnecessary trailing space characters.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddIdent(int amount) Adds indentation (leading spaces) to the beginning of this line.copySubString(int from, int until) Removes characters from the specified range and returns them as a string.voidcutFromBeginning(int charactersToCut) Removes the specified number of characters from the beginning of this line.cutSubString(int from, int until) Extracts a substring from this line, removing those characters and returning them.voidcutUntilEnd(int col) Truncates this line at the specified column, discarding all characters from that position to the end.chargetCharForLocation(int col) Returns the character at the specified column position.getChars()Returns the internal list ofCharacterobjects backing this line.intgetIdent()Returns the indentation level of this line, measured as the number of leading space characters before the first non-space character.intReturns the length of this line (number of characters, excluding trimmed trailing whitespace).getSubLine(int from, int until) Returns a newTextLinecontaining the characters from this line in the range [from,until).getSubString(int from, int until) Returns a substring of this line from columnfrom(inclusive) to columnuntil(exclusive).voidinsertCharacter(int col, char value) Inserts a single character at the specified column position.voidinsertString(int col, String value) Inserts a string at the specified column position.voidinsertTextLine(int col, TextLine textLine) Inserts all characters from anotherTextLineat the specified column.booleanisEmpty()Returns whether this line contains no characters.voidremoveCharacter(int col) Removes the character at the specified column position.voidReplaces the entire contents of this line with the given string.toString()Returns the string representation of this line by concatenating all character values.
-
Constructor Details
-
TextLine
public TextLine()Creates an empty text line with no characters. -
TextLine
Creates a text line from an existing list ofCharacterobjects.Trailing whitespace is automatically trimmed via
pack().- Parameters:
value- the list of characters to initialize this line with
-
TextLine
Creates a text line initialized with the given string.Each character in the string is converted to a
Characterobject. Trailing whitespace is automatically trimmed.- Parameters:
value- the string to initialize this line with
-
-
Method Details
-
addIdent
public void addIdent(int amount) Adds indentation (leading spaces) to the beginning of this line.If the line is empty, no indentation is added. Otherwise, the specified number of space characters are prepended to the line.
- Parameters:
amount- the number of space characters to prepend
-
copySubString
Removes characters from the specified range and returns them as a string.This is a destructive operation: the characters in the range [
from,until) are removed from this line. If the line is shorter thanuntil, it is padded with spaces before extraction. Trailing whitespace is trimmed after removal.- Parameters:
from- the start index (inclusive) of the range to extractuntil- the end index (exclusive) of the range to extract- Returns:
- the extracted characters as a string
-
cutFromBeginning
public void cutFromBeginning(int charactersToCut) Removes the specified number of characters from the beginning of this line.If
charactersToCutexceeds the line length, the entire line is cleared. IfcharactersToCutis zero, no changes are made.- Parameters:
charactersToCut- the number of leading characters to remove
-
cutSubString
Extracts a substring from this line, removing those characters and returning them.Characters in the range [
from,until) are removed from this line and returned as a string. Characters outside the range are retained. If the line is shorter thanuntil, it is padded with spaces before extraction. Trailing whitespace is trimmed after the cut.- Parameters:
from- the start index (inclusive) of the range to cutuntil- the end index (exclusive) of the range to cut- Returns:
- the cut characters as a string
-
cutUntilEnd
public void cutUntilEnd(int col) Truncates this line at the specified column, discarding all characters from that position to the end.If
colis greater than or equal to the current line length, no changes are made.- Parameters:
col- the column index at which to truncate (exclusive; characters at indices 0 throughcol - 1are kept)
-
getCharForLocation
public char getCharForLocation(int col) Returns the character at the specified column position.If the column is beyond the end of this line, a space character is returned.
- Parameters:
col- the zero-based column index- Returns:
- the character at the given column, or
' 'if out of bounds
-
getChars
Returns the internal list ofCharacterobjects backing this line.Note: the returned list is the live internal list. Modifications to the returned list will directly affect this line.
- Returns:
- the mutable list of characters in this line
-
getIdent
public int getIdent()Returns the indentation level of this line, measured as the number of leading space characters before the first non-space character.If the line is empty, returns
0.- Returns:
- the number of leading space characters
- Throws:
RuntimeException- if the line is non-empty but contains only spaces (should not occur due to trailing whitespace trimming bypack())
-
getLength
public int getLength()Returns the length of this line (number of characters, excluding trimmed trailing whitespace).- Returns:
- the number of characters in this line
-
getSubLine
Returns a newTextLinecontaining the characters from this line in the range [from,until).If
untilexceeds the line length, only the available characters are included. The returned line is an independent copy.- Parameters:
from- the start index (inclusive)until- the end index (exclusive)- Returns:
- a new
TextLinewith the specified sub-range of characters
-
getSubString
Returns a substring of this line from columnfrom(inclusive) to columnuntil(exclusive).If the requested range extends beyond the line length, space characters are used for positions past the end of the line.
- Parameters:
from- the start column (inclusive)until- the end column (exclusive)- Returns:
- the substring in the specified range
-
insertCharacter
public void insertCharacter(int col, char value) Inserts a single character at the specified column position.If the column is beyond the current line length, the line is padded with spaces up to that position. Trailing whitespace is trimmed after insertion.
- Parameters:
col- the zero-based column at which to insertvalue- the character to insert
-
insertString
Inserts a string at the specified column position.Each character in the string is inserted sequentially starting at
col. If the column is beyond the current line length, the line is padded with spaces. Trailing whitespace is trimmed after insertion.- Parameters:
col- the zero-based column at which to start insertingvalue- the string to insert
-
insertTextLine
Inserts all characters from anotherTextLineat the specified column.If the column is beyond the current line length, the line is padded with spaces. Trailing whitespace is trimmed after insertion.
- Parameters:
col- the zero-based column at which to start insertingtextLine- the text line whose characters will be inserted
-
isEmpty
public boolean isEmpty()Returns whether this line contains no characters.Because trailing whitespace is trimmed, an empty line means there are no visible characters on this line.
- Returns:
trueif the line has no characters,falseotherwise
-
removeCharacter
public void removeCharacter(int col) Removes the character at the specified column position.If the column is beyond the end of the line, no changes are made.
- Parameters:
col- the zero-based column of the character to remove
-
setValue
Replaces the entire contents of this line with the given string.The existing characters are cleared, and each character from the string is added as a new
Characterobject. Trailing whitespace is trimmed.- Parameters:
string- the new text content for this line
-
toString
Returns the string representation of this line by concatenating all character values.
-