From 3b44ee0c6b3a8c68c981a6a6caf38e8bcbdc72d0 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sun, 22 Aug 2021 10:01:01 -0700 Subject: [PATCH] fix(core): Fixes string wrapping for houses (#717) --- .../Tests/Utility/StringHelperTests.cs | 14 +++++++++++--- Projects/Server/Text/StringHelpers.cs | 10 +++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Projects/Server.Tests/Tests/Utility/StringHelperTests.cs b/Projects/Server.Tests/Tests/Utility/StringHelperTests.cs index 910962a6d..cab32b1b3 100644 --- a/Projects/Server.Tests/Tests/Utility/StringHelperTests.cs +++ b/Projects/Server.Tests/Tests/Utility/StringHelperTests.cs @@ -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); } diff --git a/Projects/Server/Text/StringHelpers.cs b/Projects/Server/Text/StringHelpers.cs index 315850078..7e05497c2 100644 --- a/Projects/Server/Text/StringHelpers.cs +++ b/Projects/Server/Text/StringHelpers.cs @@ -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;