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. 19
      sources/Text/MutableText.uc
  2. BIN
      sources/Text/Tests/TEST_Text.uc
  3. 57
      sources/Text/Text.uc

19
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)

BIN
sources/Text/Tests/TEST_Text.uc

Binary file not shown.

57
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 ('{<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