Skip to content

Commit

Permalink
Unescape csv strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jbruechert committed Apr 29, 2024
1 parent 4c1503a commit 5d0f995
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions include/utl/parser/arg_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,30 @@ inline bool parse_arg(cstr& s, bool& b) {
return false;
}

inline void parse_arg(cstr& s, std::string& arg) { arg.assign(s.str, s.len); }
inline void parse_arg(cstr& s, std::string& arg) {
arg.assign(s.str, s.len);

for (int found_at = 0;
found_at != -1;
found_at = arg.find('"', found_at)) {
if (arg[found_at] == '"' && found_at < int(arg.size()) - 1) {
arg.erase(found_at, 1);
found_at++; // Skip following character
}
}
}

inline void parse_arg(cstr& s, cstr& arg) { arg.assign(s.str, s.len); }
inline void parse_arg(cstr& s, cstr& arg) {
// We can't unescape strings here, as cstrings point
// to the source data
arg.assign(s.str, s.len);
}

inline void parse_arg(cstr& s, std::filesystem::path& arg) { arg = s.str; }
inline void parse_arg(cstr& s, std::filesystem::path& arg) {
std::string path;
parse_arg(s, path);
arg = std::move(path);
}

template <typename T>
inline T parse(cstr s) {
Expand Down
2 changes: 1 addition & 1 deletion test/parser/pipe_csv_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ TEST(pipe_csv, csv_escaped_string) {
| vec();

ASSERT_TRUE(result.size() == 1);
EXPECT_TRUE(result[0].foo_.val() == R"([""asd"", ""bsd""])");
EXPECT_TRUE(result[0].foo_.val() == R"(["asd", "bsd"])");
EXPECT_TRUE(result[0].bar_.val() == "asd");
EXPECT_TRUE(result[0].baz_.val() == "xxx");
}

0 comments on commit 5d0f995

Please sign in to comment.