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 30, 2024
1 parent 4c1503a commit c616f69
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
20 changes: 18 additions & 2 deletions include/utl/parser/csv.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ inline void parse_column(cstr& s, T& arg) {
adjust_for_quote + adjust_for_cr));
}

inline void unescape_quoted_string(std::string& arg) {
auto found_at = 0;
while ((found_at = arg.find('"', found_at)) != -1) {
if (found_at < static_cast<int>(arg.size()) - 1 &&
arg[found_at + 1] == '"') {
arg.erase(found_at, 1);
++found_at; // Skip following character
}
}
}

template <typename IntType,
std::enable_if_t<std::is_integral<IntType>::value, int> = 0>
inline void parse_value(cstr& s, IntType& arg) {
Expand All @@ -71,8 +82,13 @@ inline void parse_value(cstr& s, bool& arg) {
s = s.skip_whitespace_front();
parse_arg(s, arg);
}
inline void parse_value(cstr& s, std::string& arg) { parse_arg(s, arg); }
inline void parse_value(cstr& s, cstr& arg) { parse_arg(s, arg); }
inline void parse_value(cstr& s, std::string& arg) {
parse_arg(s, arg);
unescape_quoted_string(arg);
}
inline void parse_value(cstr& s, cstr& arg) {
parse_arg(s, arg);
}

template <int Index, typename... Args>
typename std::enable_if<Index == sizeof...(Args)>::type read(
Expand Down
2 changes: 1 addition & 1 deletion include/utl/parser/csv_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct csv_range : public LineRange {
T t{};
cista::for_each_field(t, [&, i = 0u](auto& f) mutable {
if (row[i]) {
parse_arg(row[i], f.val());
parse_value(row[i], f.val());
}
++i;
});
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 c616f69

Please sign in to comment.