Skip to content

Commit

Permalink
quotes strings with spaces now work
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenberry committed Jan 23, 2023
1 parent 1be49ac commit 37212bb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
6 changes: 3 additions & 3 deletions include/argz/argz.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ namespace argz

inline std::string_view parse_var(const char* c) {
auto start = c;
while (*c != '\0' && *c != ' ') {
while (*c != '\0') {
++c;
}
return { start, static_cast<size_t>(c - start) };
}

inline void parse(const char* c, var& v)
{
if (c) {
Expand Down Expand Up @@ -118,7 +118,7 @@ namespace argz
for (int_t i = 1; i < argc; ++i) {
const char* flag = argv[i];
if (*flag != '-') {
throw std::runtime_error("expected '-'");
throw std::runtime_error("Expected '-'");
}
++flag;

Expand Down
29 changes: 17 additions & 12 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ inline std::vector<std::string> string_to_vector(const std::string& str)
if (*c == '-') {
++c;
if (*c == '-') {
throw std::runtime_error("unepected '-'");
throw std::runtime_error("Unepected -");
}
else if (*c==' ') {
break;
Expand All @@ -45,19 +45,24 @@ inline std::vector<std::string> string_to_vector(const std::string& str)
ret.emplace_back(std::string(std::string_view{ start, static_cast<size_t>(c - start) }));
}
}
else if (*c == ' ' || *c == '"') {
while (*c == ' ' || *c == '"') {
++c;
}
else if (*c == '"') {
++c;
auto start = c;
while (*c != '\0' && *c != ' ' && *c != '"') {
while (*c != '\0' && *c != '"') {
++c;
}
if (c != start) {

ret.emplace_back(std::string(std::string_view{ start, static_cast<size_t>(c - start) }));
if (*c == '\0') {
throw std::runtime_error("Expected \"");
}
}

ret.emplace_back(std::string(std::string_view{ start, static_cast<size_t>(c - start) }));
++c; // skip last quote
}
else if (*c == ' ') {
while (*c == ' ') {
++c;
}
}
else {
auto start = c;
while (*c != ' ' && *c != '\0') {
Expand Down Expand Up @@ -184,10 +189,10 @@ int main(int argc, char* argv[])
expect(input == std::string("./some-path-with-dashes.txt")) << "actual: " << input;
};

/*test("quoted_path") = [&] {
test("quoted_path") = [&] {
parse_string(R"(program.exe -i "./../some quoted path.txt" )");
expect(input == "./../some quoted path.txt") << "actual: " << input;
};*/
};

return 0;
}

0 comments on commit 37212bb

Please sign in to comment.