Browse Source

Fix bug with copying methods for `Text`

`Copy()` and `MutableCopy()` methods for `Text` incorrectly handled
negative arguments in some cases. They were fixed and appropriate tests
were added.
pull/8/head
Anton Tarasenko 3 years ago
parent
commit
9acc036f48
  1. 8
      sources/Text/Tests/TEST_Text.uc
  2. 17
      sources/Text/Text.uc

8
sources/Text/Tests/TEST_Text.uc

@ -131,6 +131,10 @@ protected static function SubTest_TextSubCopy()
TEST_ExpectTrue( formatted.Copy(13, -10).ToFormattedString()
== "{rgb(255,0,0) E} and be reborn!");
TEST_ExpectTrue(formatted.Copy(32).ToString() == "");
TEST_ExpectTrue(formatted.Copy(-30, -1).ToString() == plainString);
TEST_ExpectTrue(formatted.Copy(-20, -1).ToString() == plainString);
TEST_ExpectTrue(formatted.Copy(-30, 5).ToString() == "");
TEST_ExpectTrue(formatted.Copy(-29, 32).ToString() == "Pre");
Issue("Part of `Text`'s contents is not properly copied (mutable).");
TEST_ExpectTrue( formatted.MutableCopy(-2, 100).ToFormattedString()
@ -139,6 +143,10 @@ protected static function SubTest_TextSubCopy()
TEST_ExpectTrue( formatted.MutableCopy(13, -10).ToFormattedString()
== "{rgb(255,0,0) E} and be reborn!");
TEST_ExpectTrue(formatted.MutableCopy(32).ToString() == "");
TEST_ExpectTrue(formatted.MutableCopy(-30, -1).ToString() == plainString);
TEST_ExpectTrue(formatted.MutableCopy(-20, -1).ToString() == plainString);
TEST_ExpectTrue(formatted.MutableCopy(-30, 5).ToString() == "");
TEST_ExpectTrue(formatted.MutableCopy(-29, 32).ToString() == "Pre");
}
protected static function SubTest_TextLowerCompleteCopy()

17
sources/Text/Text.uc

@ -270,11 +270,12 @@ public static final function Text ConstFromFormattedString(string source)
/**
* Makes an immutable copy (`class'Text'`) of the caller `Text`.
*
* If provided parameters `startPosition` and `maxLength` define a range that
* Copies characters in the range `[startIndex; startIndex + maxLength - 1]`
* If provided parameters `startIndex` and `maxLength` define a range that
* goes beyond `[0; self.GetLength() - 1]`, then intersection with a valid
* range will be used.
*
* @param startPosition Position of the first character to 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`,
@ -291,7 +292,7 @@ public final function Text Copy(
local Text copy;
local Character nextCharacter;
if (maxLength <= 0) {
maxLength = codePoints.length;
maxLength = codePoints.length - startIndex;
}
endIndex = startIndex + maxLength;
copy = Text(_.memory.Allocate(class'Text'));
@ -299,7 +300,7 @@ public final function Text Copy(
if (endIndex <= 0) {
return copy;
}
if (startIndex <= 0 && maxLength >= startIndex + codePoints.length)
if (startIndex <= 0 && startIndex + maxLength >= codePoints.length)
{
copy.codePoints = codePoints;
copy.formattingChunks = formattingChunks;
@ -322,11 +323,11 @@ public final function Text Copy(
/**
* Makes a mutable copy (`class'MutableText'`) of the caller text instance.
*
* If provided parameters `startPosition` and `maxLength` define a range that
* If provided parameters `startIndex` and `maxLength` define a range that
* goes beyond `[0; self.GetLength() - 1]`, then intersection with a valid
* range will be used.
*
* @param startPosition Position of the first character to 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`,
@ -342,7 +343,7 @@ public final function MutableText MutableCopy(
local int i, endIndex;
local MutableText copy;
if (maxLength <= 0) {
maxLength = codePoints.length;
maxLength = codePoints.length - startIndex;
}
endIndex = startIndex + maxLength;
copy = MutableText(_.memory.Allocate(class'MutableText'));
@ -350,7 +351,7 @@ public final function MutableText MutableCopy(
if (endIndex <= 0 || startIndex >= codePoints.length) {
return copy;
}
if (startIndex <= 0 && maxLength >= startIndex + codePoints.length)
if (startIndex <= 0 && startIndex + maxLength >= codePoints.length)
{
copy.codePoints = codePoints;
copy.formattingChunks = formattingChunks;

Loading…
Cancel
Save