Skip to content

Commit

Permalink
Add cista::raw::string overload of parse_value (#19)
Browse files Browse the repository at this point in the history
* Add cista::raw::string overload of parse_value

This one only allocates if necessary.

* Use cista::raw::generic_string::erase

---------

Co-authored-by: Felix Gündling <[email protected]>
  • Loading branch information
jbruechert and felixguendling authored May 3, 2024
1 parent 81821f8 commit 55ad01b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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"

#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"

#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

0 comments on commit 55ad01b

Please sign in to comment.