From 4d5665ab77d1bf9bf57c04ba2676b1be29ec41e3 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Tue, 5 Jul 2022 17:11:18 +0700 Subject: [PATCH] Add `Prepend...()` methods for `MutableText` --- sources/Text/MutableText.uc | 132 +++++++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 3 deletions(-) diff --git a/sources/Text/MutableText.uc b/sources/Text/MutableText.uc index 81da8ab..d809106 100644 --- a/sources/Text/MutableText.uc +++ b/sources/Text/MutableText.uc @@ -199,6 +199,32 @@ public final function MutableText Append( return self; } +/** + * Prepends contents of another `BaseText` to the caller `MutableText`. + * + * @param other Instance of `BaseText`, which content method + * will prepend. Prepends nothing if passed value is `none`. + * @param defaultFormatting Formatting to apply to `other`'s character that + * do not have it specified. For example, `defaultFormatting.isColored`, + * but some of `other`'s characters do not have a color defined - + * they will be prepended with a specified color. + * @return Caller `MutableText` to allow for method chaining. + */ +public final function MutableText Prepend( + BaseText other, + optional Formatting defaultFormatting) +{ + local MutableText selfCopy; + + if (other == none) { + return self; + } + selfCopy = MutableCopy(); + Clear().Append(other, defaultFormatting).Append(selfCopy); + _.memory.Free(selfCopy); + return self; +} + /** * Appends contents of the plain `string` to the caller `MutableText`. * @@ -224,6 +250,27 @@ public final function MutableText AppendString( return self; } +/** + * Prepends contents of the plain `string` to the caller `MutableText`. + * + * @param source Plain `string` to be prepended to + * the caller `MutableText`. + * @param defaultFormatting Formatting to be used for `source`'s characters. + * By default defines 'null' formatting (no color set). + * @return Caller `MutableText` to allow for method chaining. + */ +public final function MutableText PrependString( + string source, + optional Formatting defaultFormatting) +{ + local MutableText selfCopy; + + selfCopy = MutableCopy(); + Clear().AppendString(source, defaultFormatting).Append(selfCopy); + _.memory.Free(selfCopy); + return self; +} + /** * Appends contents of the colored `string` to the caller `MutableText`. * @@ -273,18 +320,78 @@ public final function MutableText AppendColoredString( return self; } +/** + * Prepends contents of the colored `string` to the caller `MutableText`. + * + * @param source Colored `string` to be prepended to + * the caller `MutableText`. + * @param defaultFormatting Formatting to be used for `source`'s characters + * that have no color information defined. + * By default defines 'null' formatting (no color set). + * @return Caller `MutableText` to allow for method chaining. + */ +public final function MutableText PrependColoredString( + string source, + optional Formatting defaultFormatting) +{ + local MutableText selfCopy; + + selfCopy = MutableCopy(); + Clear().AppendColoredString(source, defaultFormatting).Append(selfCopy); + _.memory.Free(selfCopy); + return self; +} + /** * Appends contents of the formatted `BaseText` to the caller `MutableText`. * - * @param source `BaseText` (with formatted string contents) to be + * @param other `BaseText` (with formatted string contents) to be * appended to the caller `MutableText`. * @return Caller `MutableText` to allow for method chaining. */ public final function MutableText AppendFormatted( - BaseText source, + BaseText other, optional Formatting defaultFormatting) { - class'FormattingStringParser'.static.ParseFormatted(source, self); + local MutableText coloredOther; + + if (other == none) { + return self; + } + if (defaultFormatting.isColored) + { + coloredOther = other + .MutableCopy() + .ChangeDefaultFormatting(defaultFormatting); + class'FormattingStringParser'.static + .ParseFormatted(coloredOther, self); + _.memory.Free(coloredOther); + } + else { + class'FormattingStringParser'.static.ParseFormatted(other, self); + } + return self; +} + +/** + * Prepends contents of the formatted `BaseText` to the caller `MutableText`. + * + * @param other `BaseText` (with formatted string contents) to be + * prepended to the caller `MutableText`. + * @return Caller `MutableText` to allow for method chaining. + */ +public final function MutableText PrependFormatted( + BaseText other, + optional Formatting defaultFormatting) +{ + local MutableText selfCopy; + + if (other == none) { + return self; + } + selfCopy = MutableCopy(); + Clear().AppendFormatted(other, defaultFormatting).Append(selfCopy); + _.memory.Free(selfCopy); return self; } @@ -306,6 +413,25 @@ public final function MutableText AppendFormattedString( return self; } +/** + * Prepends contents of the formatted `string` to the caller `MutableText`. + * + * @param source Formatted `string` to be prepended to + * the caller `MutableText`. + * @return Caller `MutableText` to allow for method chaining. + */ +public final function MutableText PrependFormattedString( + string source, + optional Formatting defaultFormatting) +{ + local MutableText selfCopy; + + selfCopy = MutableCopy(); + Clear().AppendFormattedString(source, defaultFormatting).Append(selfCopy); + _.memory.Free(selfCopy); + return self; +} + /** * Unlike `Text`, `MutableText` can change it's content and therefore it's * hash code cannot depend on it. So we restore `AcediaObject`'s behavior and