From 4dd29333ddd508e9804fabe89a146ed4f2a7e618 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Tue, 31 Jan 2017 17:45:33 -0500 Subject: [PATCH] New regexless split, same behavior --- include/CLI.hpp | 58 +++++++++++++++++++++++++++++++++++++++++++--------------- tests/SmallTest.cpp | 13 +++++++------ 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/include/CLI.hpp b/include/CLI.hpp index 54675ff..502abe6 100644 --- a/include/CLI.hpp +++ b/include/CLI.hpp @@ -189,7 +189,6 @@ struct EmptyError : public Error { EmptyError(std::string name) : Error("EmptyError", name, 9) {} }; -const std::regex reg_split{R"regex((?:([a-zA-Z_]?)(?:,|$)|^)([a-zA-Z0-9_][a-zA-Z0-9_\-]*)?)regex"}; const std::regex reg_short{R"regex(-([a-zA-Z_])(.*))regex"}; const std::regex reg_long{R"regex(--([^-^=][^=]*)=?(.*))regex"}; @@ -215,20 +214,7 @@ inline bool split_long(const std::string ¤t, std::string &name, std::strin return false; } -// Splits a string into long and short names -inline std::tuple split(std::string fullname) { - - std::smatch match; - if (std::regex_match(fullname, match, reg_split)) { - std::string sname = match[1]; - std::string lname = match[2]; - if(sname == "" and lname == "") - throw BadNameString("EMPTY"); - return std::tuple(sname, lname); - } else throw BadNameString(fullname); -} - -// Splits a string into multiple long and short names (not implemented) +// Splits a string into multiple long and short names inline std::vector split_names(std::string current) { std::vector output; size_t val; @@ -241,6 +227,48 @@ inline std::vector split_names(std::string current) { } +// Currently just throws an error if name is incorrect. May clean later. +inline void cleanup_names(const std::vector &input) { + + for(const std::string &name : input) { + if(name.compare(0, 2, "--") == 0) + //output = output.substring(2); + throw BadNameString(name); + else if(name.compare(0, 1, std::string("-")) == 0) + throw BadNameString(name); + //output = output.substring(1); + } +} + +// Splits a string into long and short names +inline std::tuple split(std::string fullname) { + if(fullname.find(" ") != std::string::npos) + throw BadNameString("Cannot have space in name string: "+fullname); + std::vector output = split_names(fullname); + cleanup_names(output); + + if(output.size() > 2) + throw BadNameString(fullname); + else if (output.size() == 2) { + if(output[0].length()==0 && output[1].length()==0) + throw BadNameString("EMPTY"); + else if(output[0].length()<2) + return std::tuple(output[0], output[1]); + else if(output[1].length() < 2) + return std::tuple(output[0], output[1]); + else + throw BadNameString(fullname); + } else { + if(output[0].length()==0) + throw BadNameString("EMPTY"); + else if(output[0].length() == 1) + return std::tuple(output[0], ""); + else + return std::tuple("", output[0]); + } +} + + const Combiner NOTHING {0, false,false,false, {}}; const Combiner REQUIRED {1, false,true, false, {}}; const Combiner DEFAULT {1, false,false,true, {}}; diff --git a/tests/SmallTest.cpp b/tests/SmallTest.cpp index a36f332..092c434 100644 --- a/tests/SmallTest.cpp +++ b/tests/SmallTest.cpp @@ -33,13 +33,14 @@ TEST(Split, GoodStrings) { } TEST(Split, BadStrings) { - std::vector test_fails= {"a,,boo", "a,b,c", "ssd,sfd", "-a", "", ",", "one two"}; - for(std::string name : test_fails) { - EXPECT_THROW(CLI::split(name), CLI::BadNameString); - } - - + EXPECT_THROW(CLI::split("a,,boo"), CLI::BadNameString); + EXPECT_THROW(CLI::split("a,b,c"), CLI::BadNameString); + EXPECT_THROW(CLI::split("ssd,sfd"), CLI::BadNameString); + EXPECT_THROW(CLI::split("-a"), CLI::BadNameString); + EXPECT_THROW(CLI::split(""), CLI::BadNameString); + EXPECT_THROW(CLI::split(","), CLI::BadNameString); + EXPECT_THROW(CLI::split("one two"), CLI::BadNameString); } TEST(Validators, FileExists) { -- libgit2 0.21.4