Browse Source

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.
pull/8/head
Anton Tarasenko 3 years ago
parent
commit
19fed84882
  1. 15
      sources/Text/MutableText.uc
  2. BIN
      sources/Text/Tests/TEST_Text.uc
  3. 49
      sources/Text/Text.uc

15
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;
}
@ -568,9 +568,8 @@ public final function MutableText ChangeFormatting(
*
* @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.
* 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)

BIN
sources/Text/Tests/TEST_Text.uc

Binary file not shown.

49
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`.
*/
@ -379,8 +379,8 @@ 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.
* - 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`.
*/
@ -405,8 +405,8 @@ 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.
* - 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`.
*/
@ -431,8 +431,8 @@ 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.
* - 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`.
*/
@ -457,8 +457,8 @@ 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.
* - 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 ('{<color> }')
* 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) {

Loading…
Cancel
Save