Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cista::raw::string overload of parse_value #19

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/utl/parser/arg_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <filesystem>
#include <string>

#include "cista/containers/string.h"
felixguendling marked this conversation as resolved.
Show resolved Hide resolved

#include "utl/parser/cstr.h"
#include "utl/verify.h"

Expand Down Expand Up @@ -131,6 +133,10 @@ inline void parse_arg(cstr& s, std::string& arg) { arg.assign(s.str, s.len); }

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

inline void parse_arg(cstr& s, cista::raw::generic_string& arg) {
arg.set_non_owning(s.str, s.len);
}

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

template <typename T>
Expand Down
12 changes: 10 additions & 2 deletions include/utl/parser/csv.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <tuple>
#include <vector>

#include "cista/containers/string.h"
felixguendling marked this conversation as resolved.
Show resolved Hide resolved

#include "utl/parser/arg_parser.h"
#include "utl/parser/cstr.h"
#include "utl/parser/file.h"
Expand Down Expand Up @@ -53,9 +55,11 @@ inline void parse_column(cstr& s, T& arg) {
adjust_for_quote + adjust_for_cr));
}

inline void unescape_quoted_string(std::string& arg) {
template <typename StringType>
inline void unescape_quoted_string(StringType& arg) {
std::string::size_type found_at = 0;
while ((found_at = arg.find('"', found_at)) != std::string::npos) {
while ((found_at = std::string_view(arg).find('"', found_at)) !=
std::string::npos) {
if (found_at < arg.size() - 1 && arg[found_at + 1] == '"') {
arg.erase(found_at, 1); // Since the string is now one character shorter,
// found_at now points to the next character
Expand Down Expand Up @@ -88,6 +92,10 @@ 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::generic_string& arg) {
parse_arg(s, arg);
unescape_quoted_string(arg);
}
inline void parse_value(cstr& s, cstr& arg) {
parse_arg(s, arg);
}
Expand Down
Loading