From 87c7ee01bbc0e77a7c73b667e04c69094b75ee77 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Sun, 20 Aug 2023 18:07:32 +0700 Subject: [PATCH] Add `IntoStrings()` method to `TextAPI` --- sources/Text/TextAPI.uc | 87 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/sources/Text/TextAPI.uc b/sources/Text/TextAPI.uc index 9424298..16cccdf 100644 --- a/sources/Text/TextAPI.uc +++ b/sources/Text/TextAPI.uc @@ -317,10 +317,39 @@ public final function bool IsEmpty(BaseText text) public final function string IntoString(/*take*/ BaseText toConvert) { local string result; + if (toConvert != none) { result = toConvert.ToString(); + _.memory.Free(toConvert); + } + return result; +} + +/** + * Converts given array of `BaseText`s into an array of plain `string`s, + * returning their values and deallocating passed `BaseText`. + * + * Method introduced to simplify a common use-case of converting returned + * copies of `BaseText`s into a `string`s, which required additional variable + * to store and later deallocate `BaseText` references. + * + * @param toConvert Array of `BaseText`s to convert. + * @return Array of `string`s, representing passed `BaseText`s as + * a plain string. + * Empty `string`, if `toConvert == none`. + */ +public final function array IntoStrings(/*take*/ array toConvert) { + local int i; + local array result; + + for (i = 0; i < toConvert.length; i += 1) { + if (toConvert[i] != none) { + result[result.length] = toConvert[i].ToString(); + _.memory.Free(toConvert[i]); + } else { + result[result.length] = ""; + } } - _.memory.Free(toConvert); return result; } @@ -346,6 +375,34 @@ public final function string IntoColoredString(/*take*/ BaseText toConvert) return result; } +/** + * Converts given array of `BaseText`s into an array of colored `string`s, + * returning their values and deallocating passed `BaseText`. + * + * Method introduced to simplify a common use-case of converting returned + * copies of `BaseText`s into a `string`s, which required additional variable + * to store and later deallocate `BaseText` references. + * + * @param toConvert Array of `BaseText`s to convert. + * @return Array of `string`s, representing passed `BaseText`s as + * a colored string. + * Empty `string`, if `toConvert == none`. + */ +public final function array IntoColoredStrings(/*take*/ array toConvert) { + local int i; + local array result; + + for (i = 0; i < toConvert.length; i += 1) { + if (toConvert[i] != none) { + result[result.length] = toConvert[i].ToColoredString(); + _.memory.Free(toConvert[i]); + } else { + result[result.length] = ""; + } + } + return result; +} + /** * Converts given `BaseText` into a formatted `string`, returns it's value and * deallocates passed `BaseText`. @@ -368,6 +425,34 @@ public final function string IntoFormattedString(/*take*/ BaseText toConvert) return result; } +/** + * Converts given array of `BaseText`s into an array of formatted `string`s, + * returning their values and deallocating passed `BaseText`. + * + * Method introduced to simplify a common use-case of converting returned + * copies of `BaseText`s into a `string`s, which required additional variable + * to store and later deallocate `BaseText` references. + * + * @param toConvert Array of `BaseText`s to convert. + * @return Array of `string`s, representing passed `BaseText`s as + * a formatted string. + * Empty `string`, if `toConvert == none`. + */ +public final function array IntoFormattedStrings(/*take*/ array toConvert) { + local int i; + local array result; + + for (i = 0; i < toConvert.length; i += 1) { + if (toConvert[i] != none) { + result[result.length] = toConvert[i].ToFormattedString(); + _.memory.Free(toConvert[i]); + } else { + result[result.length] = ""; + } + } + return result; +} + /** * Creates a `string` that consists only of a given character. *