Skip to content

Commit

Permalink
Add cista::raw::string overload of parse_value
Browse files Browse the repository at this point in the history
This one only allocates if necessary.
  • Loading branch information
jbruechert committed May 2, 2024
1 parent 81821f8 commit c5e330b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions include/utl/parser/csv.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <tuple>
#include <vector>

#include "cista/containers/string.h"
#include "utl/parser/arg_parser.h"
#include "utl/parser/cstr.h"
#include "utl/parser/file.h"
Expand Down Expand Up @@ -66,6 +67,30 @@ inline void unescape_quoted_string(std::string& arg) {
}
}

inline cista::raw::string unescape_quoted_string(cstr& arg) {
cista::raw::string out;

if (arg.contains(R"("")")) {
std::string mut_string = arg.to_str();
std::string::size_type found_at = 0;
while ((found_at = mut_string.find('"', found_at)) != std::string::npos) {
if (found_at < mut_string.size() - 1 && mut_string[found_at + 1] == '"') {
mut_string.erase(found_at,
1); // Since the string is now one character shorter,
// found_at now points to the next character
++found_at; // Skip following character ("), we are now after the ""
} else {
++found_at; // Continue search from next character
}
}

out.set_owning(mut_string);
} else {
out.set_non_owning(arg);
}
return out;
}

template <typename IntType,
std::enable_if_t<std::is_integral<IntType>::value, int> = 0>
inline void parse_value(cstr& s, IntType& arg) {
Expand All @@ -88,6 +113,11 @@ inline void parse_value(cstr& s, std::string& arg) {
parse_arg(s, arg);
unescape_quoted_string(arg);
}
inline void parse_value(cstr& s, cista::raw::string& arg) {
cstr out;
parse_arg(s, out);
arg = unescape_quoted_string(out);
}
inline void parse_value(cstr& s, cstr& arg) {
parse_arg(s, arg);
}
Expand Down

0 comments on commit c5e330b

Please sign in to comment.