forked from FEX-Emu/FEX
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FHU: Add StringArgumentParser function
Split this out so we can unittest it. Adds a unittest to handle specific edge cases.
- Loading branch information
1 parent
389ad73
commit 6f096e7
Showing
3 changed files
with
115 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
#include <FEXCore/fextl/vector.h> | ||
#include <FEXCore/fextl/fmt.h> | ||
|
||
#include <algorithm> | ||
#include <string_view> | ||
|
||
namespace FHU { | ||
|
||
/** | ||
* @brief Parses a string of arguments, returning a vector of string_views. | ||
* | ||
* @param ArgumentString The string of arguments to parse | ||
* | ||
* @return The array of parsed arguments | ||
*/ | ||
static inline fextl::vector<std::string_view> ParseArgumentsFromString(const std::string_view ArgumentString) { | ||
fextl::vector<std::string_view> Arguments; | ||
|
||
auto Begin = ArgumentString.begin(); | ||
auto ArgEnd = Begin; | ||
const auto End = ArgumentString.end(); | ||
while (ArgEnd != End && Begin != End) { | ||
// The end of an argument ends with a space or the end of the interpreter line. | ||
ArgEnd = std::find(Begin, End, ' '); | ||
|
||
if (Begin != ArgEnd) { | ||
const auto View = std::string_view(Begin, ArgEnd - Begin); | ||
if (!View.empty()) { | ||
Arguments.emplace_back(View); | ||
} | ||
} | ||
|
||
Begin = ArgEnd + 1; | ||
} | ||
|
||
return Arguments; | ||
} | ||
} // namespace FHU |
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,75 @@ | ||
#include <catch2/catch_test_macros.hpp> | ||
|
||
#include <FEXHeaderUtils/StringArgumentParser.h> | ||
|
||
TEST_CASE("Basic") { | ||
const auto ArgString = "Test a b c"; | ||
auto Args = FHU::ParseArgumentsFromString(ArgString); | ||
REQUIRE(Args.size() == 4); | ||
CHECK(Args.at(0) == "Test"); | ||
CHECK(Args.at(1) == "a"); | ||
CHECK(Args.at(2) == "b"); | ||
CHECK(Args.at(3) == "c"); | ||
} | ||
|
||
TEST_CASE("Basic - Empty") { | ||
const auto ArgString = ""; | ||
auto Args = FHU::ParseArgumentsFromString(ArgString); | ||
REQUIRE(Args.size() == 0); | ||
} | ||
|
||
TEST_CASE("Basic - Empty spaces") { | ||
const auto ArgString = " "; | ||
auto Args = FHU::ParseArgumentsFromString(ArgString); | ||
REQUIRE(Args.size() == 0); | ||
} | ||
|
||
TEST_CASE("Basic - Space at start") { | ||
const auto ArgString = " Test a b c"; | ||
auto Args = FHU::ParseArgumentsFromString(ArgString); | ||
REQUIRE(Args.size() == 4); | ||
CHECK(Args.at(0) == "Test"); | ||
CHECK(Args.at(1) == "a"); | ||
CHECK(Args.at(2) == "b"); | ||
CHECK(Args.at(3) == "c"); | ||
} | ||
|
||
TEST_CASE("Basic - Bonus spaces between args") { | ||
const auto ArgString = "Test a b c"; | ||
auto Args = FHU::ParseArgumentsFromString(ArgString); | ||
REQUIRE(Args.size() == 4); | ||
CHECK(Args.at(0) == "Test"); | ||
CHECK(Args.at(1) == "a"); | ||
CHECK(Args.at(2) == "b"); | ||
CHECK(Args.at(3) == "c"); | ||
} | ||
|
||
TEST_CASE("Basic - non printable") { | ||
const auto ArgString = "Test a b \x01c"; | ||
auto Args = FHU::ParseArgumentsFromString(ArgString); | ||
REQUIRE(Args.size() == 4); | ||
CHECK(Args.at(0) == "Test"); | ||
CHECK(Args.at(1) == "a"); | ||
CHECK(Args.at(2) == "b"); | ||
CHECK(Args.at(3) == "\x01c"); | ||
} | ||
|
||
TEST_CASE("Basic - Emoji") { | ||
const auto ArgString = "Test a b 🐸"; | ||
auto Args = FHU::ParseArgumentsFromString(ArgString); | ||
REQUIRE(Args.size() == 4); | ||
CHECK(Args.at(0) == "Test"); | ||
CHECK(Args.at(1) == "a"); | ||
CHECK(Args.at(2) == "b"); | ||
CHECK(Args.at(3) == "🐸"); | ||
} | ||
|
||
TEST_CASE("Basic - space at the end") { | ||
const auto ArgString = "Test a b 🐸 "; | ||
auto Args = FHU::ParseArgumentsFromString(ArgString); | ||
REQUIRE(Args.size() == 4); | ||
CHECK(Args.at(0) == "Test"); | ||
CHECK(Args.at(1) == "a"); | ||
CHECK(Args.at(2) == "b"); | ||
CHECK(Args.at(3) == "🐸"); | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
set (TESTS | ||
Allocator | ||
ArgumentParser | ||
InterruptableConditionVariable | ||
Filesystem | ||
) | ||
|