Skip to content

Commit 37212bb

Browse files
committed
quotes strings with spaces now work
1 parent 1be49ac commit 37212bb

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

include/argz/argz.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ namespace argz
5252

5353
inline std::string_view parse_var(const char* c) {
5454
auto start = c;
55-
while (*c != '\0' && *c != ' ') {
55+
while (*c != '\0') {
5656
++c;
5757
}
5858
return { start, static_cast<size_t>(c - start) };
5959
}
60-
60+
6161
inline void parse(const char* c, var& v)
6262
{
6363
if (c) {
@@ -118,7 +118,7 @@ namespace argz
118118
for (int_t i = 1; i < argc; ++i) {
119119
const char* flag = argv[i];
120120
if (*flag != '-') {
121-
throw std::runtime_error("expected '-'");
121+
throw std::runtime_error("Expected '-'");
122122
}
123123
++flag;
124124

src/Main.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ inline std::vector<std::string> string_to_vector(const std::string& str)
2525
if (*c == '-') {
2626
++c;
2727
if (*c == '-') {
28-
throw std::runtime_error("unepected '-'");
28+
throw std::runtime_error("Unepected -");
2929
}
3030
else if (*c==' ') {
3131
break;
@@ -45,19 +45,24 @@ inline std::vector<std::string> string_to_vector(const std::string& str)
4545
ret.emplace_back(std::string(std::string_view{ start, static_cast<size_t>(c - start) }));
4646
}
4747
}
48-
else if (*c == ' ' || *c == '"') {
49-
while (*c == ' ' || *c == '"') {
50-
++c;
51-
}
48+
else if (*c == '"') {
49+
++c;
5250
auto start = c;
53-
while (*c != '\0' && *c != ' ' && *c != '"') {
51+
while (*c != '\0' && *c != '"') {
5452
++c;
5553
}
56-
if (c != start) {
57-
58-
ret.emplace_back(std::string(std::string_view{ start, static_cast<size_t>(c - start) }));
54+
if (*c == '\0') {
55+
throw std::runtime_error("Expected \"");
5956
}
60-
}
57+
58+
ret.emplace_back(std::string(std::string_view{ start, static_cast<size_t>(c - start) }));
59+
++c; // skip last quote
60+
}
61+
else if (*c == ' ') {
62+
while (*c == ' ') {
63+
++c;
64+
}
65+
}
6166
else {
6267
auto start = c;
6368
while (*c != ' ' && *c != '\0') {
@@ -184,10 +189,10 @@ int main(int argc, char* argv[])
184189
expect(input == std::string("./some-path-with-dashes.txt")) << "actual: " << input;
185190
};
186191

187-
/*test("quoted_path") = [&] {
192+
test("quoted_path") = [&] {
188193
parse_string(R"(program.exe -i "./../some quoted path.txt" )");
189194
expect(input == "./../some quoted path.txt") << "actual: " << input;
190-
};*/
195+
};
191196

192197
return 0;
193198
}

0 commit comments

Comments
 (0)