Commit 4dd29333ddd508e9804fabe89a146ed4f2a7e618

Authored by Henry Fredrick Schreiner
1 parent 25d4cbaf

New regexless split, same behavior

include/CLI.hpp
... ... @@ -189,7 +189,6 @@ struct EmptyError : public Error {
189 189 EmptyError(std::string name) : Error("EmptyError", name, 9) {}
190 190 };
191 191  
192   -const std::regex reg_split{R"regex((?:([a-zA-Z_]?)(?:,|$)|^)([a-zA-Z0-9_][a-zA-Z0-9_\-]*)?)regex"};
193 192 const std::regex reg_short{R"regex(-([a-zA-Z_])(.*))regex"};
194 193 const std::regex reg_long{R"regex(--([^-^=][^=]*)=?(.*))regex"};
195 194  
... ... @@ -215,20 +214,7 @@ inline bool split_long(const std::string &current, std::string &name, std::strin
215 214 return false;
216 215 }
217 216  
218   -// Splits a string into long and short names
219   -inline std::tuple<std::string, std::string> split(std::string fullname) {
220   -
221   - std::smatch match;
222   - if (std::regex_match(fullname, match, reg_split)) {
223   - std::string sname = match[1];
224   - std::string lname = match[2];
225   - if(sname == "" and lname == "")
226   - throw BadNameString("EMPTY");
227   - return std::tuple<std::string, std::string>(sname, lname);
228   - } else throw BadNameString(fullname);
229   -}
230   -
231   -// Splits a string into multiple long and short names (not implemented)
  217 +// Splits a string into multiple long and short names
232 218 inline std::vector<std::string> split_names(std::string current) {
233 219 std::vector<std::string> output;
234 220 size_t val;
... ... @@ -241,6 +227,48 @@ inline std::vector&lt;std::string&gt; split_names(std::string current) {
241 227  
242 228 }
243 229  
  230 +// Currently just throws an error if name is incorrect. May clean later.
  231 +inline void cleanup_names(const std::vector<std::string> &input) {
  232 +
  233 + for(const std::string &name : input) {
  234 + if(name.compare(0, 2, "--") == 0)
  235 + //output = output.substring(2);
  236 + throw BadNameString(name);
  237 + else if(name.compare(0, 1, std::string("-")) == 0)
  238 + throw BadNameString(name);
  239 + //output = output.substring(1);
  240 + }
  241 +}
  242 +
  243 +// Splits a string into long and short names
  244 +inline std::tuple<std::string, std::string> split(std::string fullname) {
  245 + if(fullname.find(" ") != std::string::npos)
  246 + throw BadNameString("Cannot have space in name string: "+fullname);
  247 + std::vector<std::string> output = split_names(fullname);
  248 + cleanup_names(output);
  249 +
  250 + if(output.size() > 2)
  251 + throw BadNameString(fullname);
  252 + else if (output.size() == 2) {
  253 + if(output[0].length()==0 && output[1].length()==0)
  254 + throw BadNameString("EMPTY");
  255 + else if(output[0].length()<2)
  256 + return std::tuple<std::string,std::string>(output[0], output[1]);
  257 + else if(output[1].length() < 2)
  258 + return std::tuple<std::string,std::string>(output[0], output[1]);
  259 + else
  260 + throw BadNameString(fullname);
  261 + } else {
  262 + if(output[0].length()==0)
  263 + throw BadNameString("EMPTY");
  264 + else if(output[0].length() == 1)
  265 + return std::tuple<std::string,std::string>(output[0], "");
  266 + else
  267 + return std::tuple<std::string,std::string>("", output[0]);
  268 + }
  269 +}
  270 +
  271 +
244 272 const Combiner NOTHING {0, false,false,false, {}};
245 273 const Combiner REQUIRED {1, false,true, false, {}};
246 274 const Combiner DEFAULT {1, false,false,true, {}};
... ...
tests/SmallTest.cpp
... ... @@ -33,13 +33,14 @@ TEST(Split, GoodStrings) {
33 33 }
34 34  
35 35 TEST(Split, BadStrings) {
36   - std::vector<std::string> test_fails= {"a,,boo", "a,b,c", "ssd,sfd", "-a", "", ",", "one two"};
37 36  
38   - for(std::string name : test_fails) {
39   - EXPECT_THROW(CLI::split(name), CLI::BadNameString);
40   - }
41   -
42   -
  37 + EXPECT_THROW(CLI::split("a,,boo"), CLI::BadNameString);
  38 + EXPECT_THROW(CLI::split("a,b,c"), CLI::BadNameString);
  39 + EXPECT_THROW(CLI::split("ssd,sfd"), CLI::BadNameString);
  40 + EXPECT_THROW(CLI::split("-a"), CLI::BadNameString);
  41 + EXPECT_THROW(CLI::split(""), CLI::BadNameString);
  42 + EXPECT_THROW(CLI::split(","), CLI::BadNameString);
  43 + EXPECT_THROW(CLI::split("one two"), CLI::BadNameString);
43 44 }
44 45  
45 46 TEST(Validators, FileExists) {
... ...