Skip to content

Commit

Permalink
FHU: Add StringArgumentParser function
Browse files Browse the repository at this point in the history
Split this out so we can unittest it.

Adds a unittest to handle specific edge cases.
  • Loading branch information
Sonicadvance1 committed Oct 11, 2024
1 parent 389ad73 commit 6f096e7
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
39 changes: 39 additions & 0 deletions FEXHeaderUtils/FEXHeaderUtils/StringArgumentParser.h
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
75 changes: 75 additions & 0 deletions unittests/APITests/ArgumentParser.cpp
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) == "🐸");
}
1 change: 1 addition & 0 deletions unittests/APITests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set (TESTS
Allocator
ArgumentParser
InterruptableConditionVariable
Filesystem
)
Expand Down

0 comments on commit 6f096e7

Please sign in to comment.