forked from OpenEnroth/OpenEnroth
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request OpenEnroth#1885 from captainurist/macos_fixes_611
Write out game config nicely
- Loading branch information
Showing
9 changed files
with
458 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#include <vector> | ||
#include <string> | ||
|
||
#include "Testing/Unit/UnitTest.h" | ||
|
||
#include "Utility/String/Wrap.h" | ||
|
||
UNIT_TEST(StringWrap, BasicSentence) { | ||
std::string text = "This is a simple test"; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {"This is a", "simple", "test"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, EmptyString) { | ||
std::string text = ""; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {""}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, SingleShortWord) { | ||
std::string text = "Hello"; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {"Hello"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, SingleLongWord) { | ||
std::string text = "Supercalifragilisticexpialidocious"; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {"Supercalif", "ragilistic", "expialidoc", "ious"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, WordsLongerThanWidth) { | ||
std::string text = "Hello Supercalifragilisticexpialidocious World"; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {"Hello", "Supercalif", "ragilistic", "expialidoc", "ious World"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, ExactFitWidth) { | ||
std::string text = "1234567890 12345"; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {"1234567890", "12345"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, LeadingTrailingSpaces) { | ||
std::string text = " Hello world "; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {" Hello", "world "}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, WidthGreaterThanTextLength) { | ||
std::string text = "Hello world"; | ||
size_t width = 20; | ||
std::vector<std::string> expected = {"Hello world"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, WidthEqualToTextLength) { | ||
std::string text = "Hello world"; | ||
size_t width = 11; | ||
std::vector<std::string> expected = {"Hello world"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, WidthOne) { | ||
std::string text = "AB CD EFGH"; | ||
size_t width = 1; | ||
std::vector<std::string> expected = {"A", "B", "C", "D", "E", "F", "G", "H"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, AllSpaces) { | ||
std::string text = " "; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {" "}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, ForcedLineBreak) { | ||
std::string text = "Hello\nWorld"; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {"Hello", "World"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, ForcedLineBreakWrap) { | ||
std::string text = "This is a\ntest with forced\nline breaks."; | ||
size_t width = 15; | ||
std::vector<std::string> expected = {"This is a", "test with", "forced", "line breaks."}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, ForcedLineBreakRepeated) { | ||
std::string text = "Hello\n\nWorld"; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {"Hello", "", "World"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, NewlineWithLongWordExceedingWidth) { | ||
std::string text = "Hello\nSupercalifragilisticexpialidocious\nWorld"; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {"Hello", "Supercalif", "ragilistic", "expialidoc", "ious", "World"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, NewlineAtEndOfText) { | ||
std::string text = "This is a test\n"; | ||
size_t width = 15; | ||
std::vector<std::string> expected = {"This is a test", ""}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, NewlineAtStartOfText) { | ||
std::string text = "\nThis is a test"; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {"", "This is a", "test"}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} | ||
|
||
UNIT_TEST(StringWrap, MultipleNewlinesOnly) { | ||
std::string text = "\n\n\n"; | ||
size_t width = 10; | ||
std::vector<std::string> expected = {"", "", "", ""}; | ||
EXPECT_EQ(wrapText(text, width), expected); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "Wrap.h" | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "Ascii.h" | ||
|
||
std::vector<std::string> wrapText(std::string_view text, size_t size) { | ||
std::vector<std::string> result; | ||
|
||
size_t breakablePos = 0; | ||
size_t lineLength = 0; | ||
size_t lineStart = 0; | ||
for (size_t i = 0; i <= text.size(); ++i) { | ||
lineLength++; | ||
|
||
char c = i == text.size() ? '\n' : text[i]; // Simple hack so that we don't have to do weird ifs after the loop. | ||
if (ascii::isSpace(c)) | ||
breakablePos = i; | ||
|
||
size_t breakPos = static_cast<size_t>(-1); | ||
size_t startPos = 0; | ||
if (lineLength > size && breakablePos != 0) { | ||
/* OK to break! */ | ||
breakPos = breakablePos; | ||
startPos = breakablePos + 1; | ||
} else if (lineLength > size && breakablePos == 0) { | ||
/* Nowhere to break, so break mid-word. */ | ||
breakPos = i; | ||
startPos = i; | ||
} else if (c == '\n') { | ||
/* Forced break. */ | ||
breakPos = i; | ||
startPos = i + 1; | ||
} | ||
|
||
if (breakPos != static_cast<size_t>(-1)) { | ||
result.emplace_back(text.substr(lineStart, breakPos - lineStart)); | ||
breakablePos = 0; | ||
lineLength = i - startPos + 1; | ||
lineStart = startPos; | ||
} | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
std::vector<std::string> wrapText(std::string_view text, size_t size); |