From c72642e68570ed63523cb3a47138df81b55b0a50 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 6 Dec 2021 03:27:38 +0700 Subject: [PATCH] Add `Remove()` method for `MutableText` --- sources/Text/MutableText.uc | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/sources/Text/MutableText.uc b/sources/Text/MutableText.uc index 8d0dbc8..5924f9b 100644 --- a/sources/Text/MutableText.uc +++ b/sources/Text/MutableText.uc @@ -558,6 +558,58 @@ public final function MutableText ChangeFormatting( return self; } +/** + * Removes all characters in range, specified as + * `[startIndex; startIndex + maxLength - 1]`. + * + * If provided parameters `startPosition` and `maxLength` define a range that + * goes beyond `[0; self.GetLength() - 1]`, then intersection with a valid + * range will be used. + * + * @param startIndex Position of the first character to get removed. + * @param maxLength Max length of the segment to get removed. + * By default `0` - that and all negative values are replaces by `MaxInt`, + * effectively removing as much characters to the right of `startIndex` + * as possible. + * @return Reference to the caller `MutableText` to allow for method chaining. + */ +public final function MutableText Remove(int startIndex, optional int maxLength) +{ + local int index; + local int endIndex; + local Text selfCopy; + if (startIndex >= GetLength()) { + return self; + } + endIndex = startIndex + maxLength - 1; + startIndex = Max(startIndex, 0); + if (maxLength <= 0) { + endIndex = GetLength() - 1; + } + if (startIndex > endIndex) { + return self; + } + if (startIndex == 0 && endIndex == GetLength() - 1) + { + Clear(); + return self; + } + selfCopy = Copy(); + Clear(); + while (index < selfCopy.GetLength()) + { + if (index >= startIndex && index <= endIndex) { + index = endIndex + 1; + } + else + { + AppendCharacter(selfCopy.GetCharacter(index)); + index += 1; + } + } + selfCopy.FreeSelf(); + return self; +} defaultproperties { CODEPOINT_NEWLINE = 10