fix(core): Fixes string wrapping for houses (#717)

This commit is contained in:
Kamron Batman 2021-08-22 10:01:01 -07:00 committed by GitHub
parent d9a9e90c14
commit 3b44ee0c6b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View file

@ -45,10 +45,18 @@ namespace Server.Tests
[InlineData("this is a sentence that will probably wrap around a few times because it is long", 10, 6)]
[InlineData("An Unnamed House", 10, 6)]
[InlineData("Batville", 10, 6)]
public void TestWrap(string sentence, int perLine, int maxLines)
[InlineData("Bald's Shop", 10, 6)]
[InlineData(
"Something ThatIsVeryLongAndShouldBe broken up",
10, 6,
"Something", "ThatIsVery", "LongAndSho", "uldBe", "broken up"
)]
public void TestWrap(string sentence, int perLine, int maxLines, params string[] customExpected)
{
var expected = OldWrap(sentence, perLine, maxLines);
var actual = sentence.Wrap(perLine, maxLines);
var expected = customExpected.Length > 0
? customExpected
: OldWrap(sentence, perLine, maxLines).ToArray();
var actual = sentence.Wrap(perLine, maxLines).ToArray();
Assert.Equal(expected, actual);
}

View file

@ -185,12 +185,13 @@ namespace Server
var spaceIndex = span[lineLength..].IndexOf(' ');
if (spaceIndex == -1)
{
spaceIndex = span.Length; // End of the string
spaceIndex = span.Length - lineLength; // End of the string
}
var newLineLength = lineLength + spaceIndex;
if (newLineLength == perLine || newLineLength == span.Length)
// If the previous line is exactly perLine or not too long and we are at the end
if (newLineLength == perLine || newLineLength < perLine && newLineLength == span.Length)
{
list.Add(span[..newLineLength].ToString());
if (list.Count == maxLines || newLineLength == span.Length)
@ -201,10 +202,12 @@ namespace Server
span = span[(newLineLength + 1)..];
lineLength = 0;
}
// We haven't hit perLine and are not sure if we can continue adding more words without going over
else if (newLineLength < perLine)
{
lineLength = newLineLength + 1;
}
// We already tried making the line longer, and it was too long, so fall back to the old line
else if (lineLength > 0 && lineLength <= perLine)
{
list.Add(span[..(lineLength - 1)].ToString());
@ -214,8 +217,9 @@ namespace Server
}
span = span[lineLength..];
lineLength = spaceIndex;
lineLength = 0;
}
// We have a really long single word with no spaces and have to forcibly break it up.
else
{
lineLength = newLineLength;