From 19fed848820bbda36898da328c338f3cdc5f7236 Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Mon, 6 Dec 2021 03:48:17 +0700 Subject: [PATCH] Fix issues with ranges in Text/MutableText methods Several methods such as `Copy()`, `Remove()`, `ChangeFormatting()` and several others have had similar errors related to how their input parameters defined range when specified out-of-bounds. This patch should fix them. --- sources/Text/MutableText.uc | 19 +++++------ sources/Text/Tests/TEST_Text.uc | Bin 117176 -> 116638 bytes sources/Text/Text.uc | 57 +++++++++++++++----------------- 3 files changed, 36 insertions(+), 40 deletions(-) diff --git a/sources/Text/MutableText.uc b/sources/Text/MutableText.uc index 5ebd551..aa6450c 100644 --- a/sources/Text/MutableText.uc +++ b/sources/Text/MutableText.uc @@ -528,8 +528,8 @@ public final function MutableText Replace( * @param startIndex Position of the first character to change formatting * of. By default `0`, corresponding to the very first character. * @param maxLength Max length of the segment to change formatting of. - * By default `0`, - that and all negative values are replaces by `MaxInt`, - * effectively extracting as much of a string as possible. + * By default `0` - that and all negative values mean that method should + * reformat all characters to the right of `startIndex`. * @return Reference to the caller `MutableText` to allow for method chaining. */ public final function MutableText ChangeFormatting( @@ -541,11 +541,11 @@ public final function MutableText ChangeFormatting( if (startIndex >= GetLength()) { return self; } + endIndex = startIndex + maxLength - 1; + startIndex = Max(startIndex, 0); if (maxLength <= 0) { - maxLength = MaxInt; + endIndex = GetLength() - 1; } - endIndex = Min(startIndex + maxLength, GetLength()) - 1; - startIndex = Max(startIndex, 0); if (startIndex > endIndex) { return self; } @@ -566,11 +566,10 @@ public final function MutableText ChangeFormatting( * 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. + * @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 mean that method should + * remove all characters to the right of `startIndex`. * @return Reference to the caller `MutableText` to allow for method chaining. */ public final function MutableText Remove(int startIndex, optional int maxLength) diff --git a/sources/Text/Tests/TEST_Text.uc b/sources/Text/Tests/TEST_Text.uc index c606d1c616f77781afef6f6127bf61288ab1c438..ecaf3956731c0e086ea659e4153b2b5f3dd5b46a 100644 GIT binary patch delta 65 zcmV-H0KWgYlLwxU2e4_ylRw-%lTXeLli$P$11$hClfV}@lTE}JlfMuqmt~>>Mw88_ X3X=m17PH62p68SQxI(v+umL3*uaq3e delta 228 zcmbQ&&%R?a`-aS;o2`%idCsiQpf%Y*Ms&L4dd4hP1%^z9yy@$})PGH(C? diff --git a/sources/Text/Text.uc b/sources/Text/Text.uc index 3cb1590..bf63409 100644 --- a/sources/Text/Text.uc +++ b/sources/Text/Text.uc @@ -278,8 +278,8 @@ public static final function Text ConstFromFormattedString(string source) * @param startIndex Position of the first character to copy. * By default `0`, corresponding to the very first character. * @param maxLength Max length of the extracted string. By default `0`, - * - that and all negative values are replaces by `MaxInt`, - * effectively extracting as much of a string as possible. + * - that and all negative values mean that method should extract all + * characters to the right of `startIndex`. * @return Immutable copy of the caller `Text` instance. * Guaranteed to be not `none` and have class `Text`. */ @@ -330,8 +330,8 @@ public final function Text Copy( * @param startIndex Position of the first character to copy. * By default `0`, corresponding to the very first character. * @param maxLength Max length of the extracted string. By default `0`, - * - that and all negative values are replaces by `MaxInt`, - * effectively extracting as much of a string as possible. + * - that and all negative values mean that method should extract all + * characters to the right of `startIndex`. * @return Mutable copy of the caller `Text` instance. * Guaranteed to be not `none` and have class `MutableText`. */ @@ -378,9 +378,9 @@ public final function MutableText MutableCopy( * * @param startPosition Position of the first character to copy. * By default `0`, corresponding to the very first character. - * @param maxLength Max length of the extracted string. By default `0`, - * - that and all negative values are replaces by `MaxInt`, - * effectively extracting as much of a string as possible. + * @param maxLength Max length of the extracted string. By default `0`, + * - that and all negative values mean that method should extract all + * characters to the right of `startIndex`. * @return Immutable copy of caller `Text` in a lower case. * Guaranteed to be not `none` and have class `Text`. */ @@ -404,9 +404,9 @@ public final function Text LowerCopy( * * @param startPosition Position of the first character to copy. * By default `0`, corresponding to the very first character. - * @param maxLength Max length of the extracted string. By default `0`, - * - that and all negative values are replaces by `MaxInt`, - * effectively extracting as much of a string as possible. + * @param maxLength Max length of the extracted string. By default `0`, + * - that and all negative values mean that method should extract all + * characters to the right of `startIndex`. * @return Immutable copy of caller `Text` in a upper case. * Guaranteed to be not `none` and have class `Text`. */ @@ -430,9 +430,9 @@ public final function Text UpperCopy( * * @param startPosition Position of the first character to copy. * By default `0`, corresponding to the very first character. - * @param maxLength Max length of the extracted string. By default `0`, - * - that and all negative values are replaces by `MaxInt`, - * effectively extracting as much of a string as possible. + * @param maxLength Max length of the extracted string. By default `0`, + * - that and all negative values mean that method should extract all + * characters to the right of `startIndex`. * @return Mutable copy of caller `Text` instance in lower case. * Guaranteed to be not `none` and have class `MutableText`. */ @@ -456,9 +456,9 @@ public final function MutableText LowerMutableCopy( * * @param startPosition Position of the first character to copy. * By default `0`, corresponding to the very first character. - * @param maxLength Max length of the extracted string. By default `0`, - * - that and all negative values are replaces by `MaxInt`, - * effectively extracting as much of a string as possible. + * @param maxLength Max length of the extracted string. By default `0`, + * - that and all negative values mean that method should extract all + * characters to the right of `startIndex`. * @return Mutable copy of caller `Text` instance in upper case. * Guaranteed to be not `none` and have class `MutableText`. */ @@ -1118,7 +1118,7 @@ private final function NormalizeFormatting() * plain `string`. By default `0`, corresponding to the first symbol. * @param maxLength Max length of the extracted string. By default `0`, * - that and all negative values are replaces by `MaxInt`, - * effectively extracting as much of a string as possible. + * effectively extracting as much of a `string` as possible. * @return Plain `string` representation of the caller `Text`, * i.e. `string` without any color information inside. */ @@ -1131,11 +1131,10 @@ public final function string ToString( if (maxLength <= 0) { maxLength = MaxInt; } - else if (startIndex < 0) - { + else if (startIndex < 0) { maxLength += startIndex; - startIndex = 0; } + startIndex = Max(0, startIndex); for (i = startIndex; i < codePoints.length; i += 1) { if (maxLength <= 0) { @@ -1162,7 +1161,7 @@ public final function string ToString( * colored `string`. By default `0`, corresponding to the first symbol. * @param maxLength Max length of the extracted string. By default `0`, * - that and all negative values are replaces by `MaxInt`, - * effectively extracting as much of a string as possible. + * effectively extracting as much of a `string` as possible. * NOTE: this parameter only counts actual visible symbols, ignoring * 4-byte color code sequences, so method `Len()`, applied to * the result of `ToColoredString()`, will return a bigger value @@ -1185,11 +1184,10 @@ public final function string ToColoredString( if (maxLength <= 0) { maxLength = MaxInt; } - else if (startIndex < 0) - { + else if (startIndex < 0) { maxLength += startIndex; - startIndex = 0; } + startIndex = Max(0, startIndex); // `appliedColor` will contain perfect black and so, // guaranteed to be different from any actually used color defaultColor = _.color.FixColor(defaultColor); @@ -1234,9 +1232,9 @@ public final function string ToColoredString( * * @param startIndex Position of the first symbol to extract into a * formatted `string`. By default `0`, corresponding to the first symbol. - * @param maxLength Max length of the extracted string. By default `0`, - * - that and all negative values are replaces by `MaxInt`, - * effectively extracting as much of a string as possible. + * @param maxLength Max length of the extracted `string`. + * By default `0` - that and all negative values are replaces by `MaxInt`, + * effectively extracting as much of a `string` as possible. * NOTE: this parameter only counts actual visible symbols, * ignoring formatting blocks ('{ }') * or escape sequences (i.e. '&{' is one character), @@ -1257,11 +1255,10 @@ public final function string ToFormattedString( if (maxLength <= 0) { maxLength = MaxInt; } - else if (startIndex < 0) - { + else if (startIndex < 0) { maxLength += startIndex; - startIndex = 0; } + startIndex = Max(0, startIndex); for (i = startIndex; i < codePoints.length; i += 1) { if (maxLength <= 0) {