MultipleLineText

Package: Libraries.Containers · Inherits from: Libraries.Language.Object

The MultipleLineText class is used to distribute text across multiple lines. This is done for both efficiency and to make certain kinds of operations easier. For example, inserting or deleting characters into very large strings of text will be much more efficient in a MultipleLineText than in the base text type.

Example Code

use Libraries.Containers.MultipleLineText

MultipleLineText multiLineText
multiLineText:Initialize("Hello world! ")

text extraText = "This is extra text."
multiLineText = multiLineText:Concatenate(extraText)

extraText = multiLineText:GetLineSeparator() + "This text is on a second line."
multiLineText = multiLineText:Concatenate(extraText)

output multiLineText:GetText()
output "The text contains " + multiLineText:GetLineCount() + " lines."

Actions

Balance()

This action will create a new MultipleLineText containing the balanced contents of this tree. This is typically handled automatically by the tree itself during other operations which return new MultipleLineTexts.

Returns

Libraries.Containers.MultipleLineText: A new MultipleLineText which is balanced and contains the same text as this MultipleLineText. use Libraries.Containers.MultipleLineText MultipleLineText tree tree:Initialize("Hello world!") boolean shouldRebalance = tree:NeedsBalancing() output "shouldRebalance is " + shouldRebalance if shouldRebalance tree:Balance() end

CharacterIndexFromLineIndex(integer index)

This action gets the index of the first character in the line at the given line index.

Parameters

  • index integer

Returns

integer

Concatenate(text value)

The Concatenate action creates a new MultipleLineText where the provided text is appended to the end of this tree's text. This tree will not change as a result of this action.

Parameters

  • value text: The text to append onto the end of this tree's text.

Returns

Libraries.Containers.MultipleLineText: A new MultipleLineText which contains the concatenation of this tree's text and the parameter text.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree
tree:Initialize("Hello ")

MultipleLineText result = tree:Concatenate("world!")
output result:GetText()

Concatenate(Libraries.Containers.MultipleLineText textTree)

The Concatenate action creates a new MultipleLineText where the provided tree is appended to the end of this tree. All text in the parameter tree will appear after the text from this tree inside the returned value. This tree will not change as a result of this action.

Parameters

Returns

Libraries.Containers.MultipleLineText: A new MultipleLineText which contains the concatenation of the two trees.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree1
tree1:Initialize("Hello ")

MultipleLineText tree2
tree2:Initialize("world!")

MultipleLineText result = tree1:Concatenate(tree2)
output result:GetText()

Delete(integer startIndex, integer endIndex)

The Delete action creates a new MultipleLineText which is a copy of this tree, then deletes all characters between the two given indices. The first index is inclusive (the character at the first index will be deleted) and the second index is exclusive (the character at the second index is not deleted). This action creates a new MultipleLineText, and has no effect on this tree.

Parameters

  • startIndex integer: The beginning of the range of characters to delete. The character at this index is deleted.
  • endIndex integer: The end of the range of characters to delete. A character at this index is not deleted.

Returns

Libraries.Containers.MultipleLineText: A new MultipleLineText containing this tree's text, minus the deleted portion.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree
tree:Initialize("Hello world!")

MultipleLineText deleteTree = tree:Delete(5, tree:GetSize() - 1)
output deleteTree:GetText()

DisableTextWrapping()

This action returns how many characters can be stored in each leaf of the MultipleLineText.

Returns

Libraries.Containers.MultipleLineText: How many characters can be stored in each leaf.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText defaultTree
defaultTree:Initialize("Hello world! What a lovely day!")
output "The default tree stores text in " + defaultTree:GetLeafSize() + " character chunks."

MultipleLineText newTree
newTree:SetLeafSize(10)
newTree:Initialize("Hello world! What a lovely day!")
output "The new tree stores text in " + newTree:GetLeafSize() + " character chunks."

GetCharacter(integer index)

This action retrieves a character at the given index from the tree.

Parameters

  • index integer: The index to retrieve a character from.

Returns

text: A single character located at the given index.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree
tree:Initialize("Hello world!")
text character = tree:GetCharacter(1)
output "The character at index 1 is '" + character + "'."

GetDefaultLineSeparator()

The GetDefaultLineSeparator action returns the default character(s) used by the MultipleLineText to signify the end of a line of text. This is the same line separating characters that are used by the running operating system.

Returns

text: The default characters used to indicate the end of a line of text.

GetLeafSize()

This action returns how many characters can be stored in each leaf of the MultipleLineText.

Returns

number: How many characters can be stored in each leaf.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText defaultTree
defaultTree:Initialize("Hello world! What a lovely day!")
output "The default tree stores text in " + defaultTree:GetLeafSize() + " character chunks."

MultipleLineText newTree
newTree:SetLeafSize(10)
newTree:Initialize("Hello world! What a lovely day!")
output "The new tree stores text in " + newTree:GetLeafSize() + " character chunks."

GetLine(integer index)

Parameters

  • index integer

Returns

Libraries.Containers.MultipleLineText

GetLineCount()

Returns

integer

GetLineSeparator()

The GetLineSeparator action returns the text which is used to designate the end of a line in the MultipleLineText. By default, this is the same as the value used by the operating system.

Returns

text: The character(s) used to designate the end of a line.

GetLines()

Returns

Libraries.Containers.Array<Libraries.Containers.MultipleLineText>

GetLines(Libraries.Containers.Array<Libraries.Containers.MultipleLineText> array)

Parameters

Returns

Libraries.Containers.Array<Libraries.Containers.MultipleLineText>

GetSize()

This action returns the length of the text stored in the MultipleLineText.

Returns

integer: The length of text in this MultipleLineText.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree
tree:Initialize("Hello world!")
output "The length of the text in the tree is " + tree:GetSize()

GetText(integer startIndex, integer endIndex)

This action returns the text between the two indices. The first index is inclusive (the character at the first index is part of the returned text) and the second index is exclusive (the character at the second index is not part of the returned text).

Parameters

  • startIndex integer: The beginning of the range of characters to return. This index is included in the returned value.
  • endIndex integer: The end of the range of characters to return. This index is not included in the returned value.

Returns

text: The text between the two indices.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree
tree:Initialize("Hello world!")
output tree:GetText(1, 10)

GetText()

This action returns all of the text stored in the MultipleLineText.

Returns

text: The text stored in this MultipleLineText.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree
tree:Initialize("Hello world!")
output tree:GetText()

GetWidth()

Returns

number

Initialize(text value, number leafSize)

The Initialize action constructs the MultipleLineText from a starting text value. The MultipleLineText will store the given value, which may be retrieved or modified using the MultipleLineText's other actions. The provided integer parameter will be used to set the maximum size of each leaf, in characters.

Parameters

  • value text: The value to be stored in this MultipleLineText.
  • leafSize number: How many characters should be stored in each leaf of the tree.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText defaultTree
defaultTree:Initialize("Hello world! What a lovely day!")
output "The default tree stores text in " + defaultTree:GetLeafSize() + " character chunks."

MultipleLineText newTree
newTree:Initialize("Hello world! What a lovely day!", 10)
output "The new tree stores text in " + newTree:GetLeafSize() + " character chunks."

Initialize(text value)

The Initialize action constructs the MultipleLineText from a starting text value. The MultipleLineText will store the given value, which may be retrieved or modified using the MultipleLineText's other actions.

Parameters

  • value text: The value to be stored in this MultipleLineText.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree
tree:Initialize("Hello world! What a lovely day!")
output tree:GetText()

Insert(integer index, text value)

The Insert action creates a new MultipleLineText and inserts the given text value into the tree at the provided index. The character that was previously located at that index will be positioned behind the newly inserted text. This action creates a new MultipleLineText, and has no effect on this tree.

Parameters

  • index integer: The index to begin inserting text at in the new tree.
  • value text: The text to be inserted into the new tree.

Returns

Libraries.Containers.MultipleLineText: A new MultipleLineText containing the inserted text.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree
tree:Initialize("Hello world!")

MultipleLineText insertTree = tree:Insert(11, " of tomorrow")
output insertTree:GetText()

LineIndexFromCharacterIndex(integer index)

This action gets the index of the line which contains the character at the given character index position in the text.

Parameters

  • index integer

Returns

integer

NeedsBalancing()

This action returns true is the tree is imbalanced, or false if it is considered balanced. This is typically handled automatically by the tree itself.

Returns

boolean: Whether or not the tree is currently imbalanced.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree
tree:Initialize("Hello world!")
boolean shouldRebalance = tree:NeedsBalancing()
output "shouldRebalance is " + shouldRebalance

if shouldRebalance
    tree:Balance()
end

SetLeafSize(integer size)

This action sets how many characters should be stored in each leaf of the MultipleLineText.

Parameters

  • size integer: How many characters to store in each leaf.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText defaultTree
defaultTree:Initialize("Hello world! What a lovely day!")
output "The default tree stores text in " + defaultTree:GetLeafSize() + " character chunks."

MultipleLineText newTree
newTree:SetLeafSize(10)
newTree:Initialize("Hello world! What a lovely day!")
output "The new tree stores text in " + newTree:GetLeafSize() + " character chunks."

SetLine(integer index, text value)

The Insert action creates a new MultipleLineText and inserts the given text value into the tree at the provided index. The character that was previously located at that index will be positioned behind the newly inserted text. This action creates a new MultipleLineText, and has no effect on this tree.

Parameters

  • index integer: The index to begin inserting text at in the new tree.
  • value text: The text to be inserted into the new tree.

Returns

Libraries.Containers.MultipleLineText: A new MultipleLineText containing the inserted text.

Example

use Libraries.Containers.MultipleLineText

MultipleLineText tree
tree:Initialize("Hello world!")

MultipleLineText insertTree = tree:Insert(11, " of tomorrow")
output insertTree:GetText()

SetLineSeparator(text separator)

The SetLineSeparator action sets which character(s) should be used to signify the end of a line.

Parameters

  • separator text: The character(s) to be used to mark the end of a line of text.

Inherited Actions

3 actions from Libraries.Language.Object