Browse Source

Change `MutableText::ChangeFormatting()` signature

pull/8/head
Anton Tarasenko 4 years ago
parent
commit
81c461e627
  1. 18
      sources/Text/MutableText.uc
  2. 20
      sources/Text/Tests/TEST_Text.uc

18
sources/Text/MutableText.uc

@ -427,7 +427,8 @@ public final function MutableText Replace(
* goes beyond `[0; self.GetLength() - 1]`, then intersection with a valid * goes beyond `[0; self.GetLength() - 1]`, then intersection with a valid
* range will be used. * range will be used.
* *
* @param startPosition Position of the first character to change formatting * @param newFormatting New formatting to apply.
* @param startIndex Position of the first character to change formatting
* of. By default `0`, corresponding to the very first character. * of. By default `0`, corresponding to the very first character.
* @param maxLength Max length of the segment to change formatting of. * @param maxLength Max length of the segment to change formatting of.
* By default `0`, - that and all negative values are replaces by `MaxInt`, * By default `0`, - that and all negative values are replaces by `MaxInt`,
@ -435,14 +436,17 @@ public final function MutableText Replace(
* @return Reference to the caller `MutableText` to allow for method chaining. * @return Reference to the caller `MutableText` to allow for method chaining.
*/ */
public final function MutableText ChangeFormatting( public final function MutableText ChangeFormatting(
int startIndex, Formatting newFormatting,
int maxLength, optional int startIndex,
Formatting newFormatting) optional int maxLength)
{ {
local int endIndex; local int endIndex;
if (maxLength <= 0) return self; if (startIndex >= GetLength()) {
if (startIndex >= GetLength()) return self; return self;
}
if (maxLength <= 0) {
maxLength = MaxInt;
}
endIndex = Min(startIndex + maxLength, GetLength()) - 1; endIndex = Min(startIndex + maxLength, GetLength()) - 1;
startIndex = Max(startIndex, 0); startIndex = Max(startIndex, 0);
if (startIndex > endIndex) { if (startIndex > endIndex) {

20
sources/Text/Tests/TEST_Text.uc

@ -1061,26 +1061,26 @@ protected static function SubTest_ChangeFormattingRegular()
Issue("Formatting is not changed correctly."); Issue("Formatting is not changed correctly.");
template = __().text.FromFormattedString( template = __().text.FromFormattedString(
"Normal part, {#ff0000 red part}, {#00ff00 green part}!!!"); "Normal part, {#ff0000 red part}, {#00ff00 green part}!!!");
testText = template.MutableCopy().ChangeFormatting(3, 4, greenFormatting); testText = template.MutableCopy().ChangeFormatting(greenFormatting, 3, 4);
TEST_ExpectTrue(testText.ToFormattedString() == TEST_ExpectTrue(testText.ToFormattedString() ==
("Nor{rgb(0,255,0) mal }part, {rgb(255,0,0) red part}, {rgb(0,255,0)" ("Nor{rgb(0,255,0) mal }part, {rgb(255,0,0) red part}, {rgb(0,255,0)"
@ "green part}!!!")); @ "green part}!!!"));
testText = template.MutableCopy().ChangeFormatting(12, 10, greenFormatting); testText = template.MutableCopy().ChangeFormatting(greenFormatting, 12, 10);
TEST_ExpectTrue(testText.ToFormattedString() == TEST_ExpectTrue(testText.ToFormattedString() ==
"Normal part,{rgb(0,255,0) red part,} {rgb(0,255,0) green part}!!!"); "Normal part,{rgb(0,255,0) red part,} {rgb(0,255,0) green part}!!!");
testText = template.MutableCopy().ChangeFormatting(12, 11, greenFormatting); testText = template.MutableCopy().ChangeFormatting(greenFormatting, 12, 11);
TEST_ExpectTrue(testText.ToFormattedString() == TEST_ExpectTrue(testText.ToFormattedString() ==
"Normal part,{rgb(0,255,0) red part, green part}!!!"); "Normal part,{rgb(0,255,0) red part, green part}!!!");
// This test was added because it produced `none` access errors in the // This test was added because it produced `none` access errors in the
// old implementation of `ChangeFormatting()` // old implementation of `ChangeFormatting()`
testText = template.MutableCopy().ChangeFormatting(0, 35, greenFormatting); testText = template.MutableCopy().ChangeFormatting(greenFormatting, 0, 35);
TEST_ExpectTrue(testText.ToFormattedString() == TEST_ExpectTrue(testText.ToFormattedString() ==
"{rgb(0,255,0) Normal part, red part, green part!!}!"); "{rgb(0,255,0) Normal part, red part, green part!!}!");
testText = template.MutableCopy().ChangeFormatting(3, 4, defaultFormatting); testText = template.MutableCopy().ChangeFormatting(defaultFormatting, 3, 4);
TEST_ExpectTrue(testText.ToFormattedString() == TEST_ExpectTrue(testText.ToFormattedString() ==
"Normal part, {rgb(255,0,0) red part}, {rgb(0,255,0) green part}!!!"); "Normal part, {rgb(255,0,0) red part}, {rgb(0,255,0) green part}!!!");
testText = template.MutableCopy() testText = template.MutableCopy()
.ChangeFormatting(16, 13, defaultFormatting); .ChangeFormatting(defaultFormatting, 16, 13);
TEST_ExpectTrue(testText.ToFormattedString() == TEST_ExpectTrue(testText.ToFormattedString() ==
"Normal part, {rgb(255,0,0) red} part, green {rgb(0,255,0) part}!!!"); "Normal part, {rgb(255,0,0) red} part, green {rgb(0,255,0) part}!!!");
} }
@ -1095,19 +1095,19 @@ protected static function SubTest_ChangeFormattingEdgeCases()
@ "near index boundaries."); @ "near index boundaries.");
template = __().text.FromFormattedString( template = __().text.FromFormattedString(
"Normal part, {#ff0000 red part}, {#00ff00 green part}!!!"); "Normal part, {#ff0000 red part}, {#00ff00 green part}!!!");
testText = template.MutableCopy().ChangeFormatting(33, 3, greenFormatting); testText = template.MutableCopy().ChangeFormatting(greenFormatting, 33, 3);
TEST_ExpectTrue(testText.ToFormattedString() == TEST_ExpectTrue(testText.ToFormattedString() ==
"Normal part, {rgb(255,0,0) red part}, {rgb(0,255,0) green part!!!}"); "Normal part, {rgb(255,0,0) red part}, {rgb(0,255,0) green part!!!}");
testText = template.MutableCopy().ChangeFormatting(36, 5, greenFormatting); testText = template.MutableCopy().ChangeFormatting(greenFormatting, 36, 5);
TEST_ExpectTrue(testText.ToFormattedString() == TEST_ExpectTrue(testText.ToFormattedString() ==
"Normal part, {rgb(255,0,0) red part}, {rgb(0,255,0) green part}!!!"); "Normal part, {rgb(255,0,0) red part}, {rgb(0,255,0) green part}!!!");
testText = template.MutableCopy() testText = template.MutableCopy()
.ChangeFormatting(-10, 100, defaultFormatting); .ChangeFormatting(defaultFormatting, -10, 100);
TEST_ExpectTrue(testText.ToFormattedString() == TEST_ExpectTrue(testText.ToFormattedString() ==
"Normal part, red part, green part!!!"); "Normal part, red part, green part!!!");
testText = template.MutableCopy() testText = template.MutableCopy()
.ChangeFormatting(-10, 16, greenFormatting); .ChangeFormatting(greenFormatting, -10, 16);
TEST_ExpectTrue(testText.ToFormattedString() == TEST_ExpectTrue(testText.ToFormattedString() ==
("{rgb(0,255,0) Normal} part, {rgb(255,0,0) red part}, {rgb(0,255,0)" ("{rgb(0,255,0) Normal} part, {rgb(255,0,0) red part}, {rgb(0,255,0)"
@ "green part}!!!")); @ "green part}!!!"));

Loading…
Cancel
Save