Commit 1be020b4 authored by Dominik Charousset's avatar Dominik Charousset

Recognize string_view in stringification_inspector

parent dae40f2a
...@@ -25,8 +25,9 @@ ...@@ -25,8 +25,9 @@
#include <vector> #include <vector>
#include "caf/atom.hpp" #include "caf/atom.hpp"
#include "caf/none.hpp"
#include "caf/error.hpp" #include "caf/error.hpp"
#include "caf/none.hpp"
#include "caf/string_view.hpp"
#include "caf/meta/type_name.hpp" #include "caf/meta/type_name.hpp"
#include "caf/meta/omittable.hpp" #include "caf/meta/omittable.hpp"
...@@ -73,18 +74,20 @@ public: ...@@ -73,18 +74,20 @@ public:
void consume(atom_value& x); void consume(atom_value& x);
void consume(const char* cstr); void consume(string_view str);
inline void consume(bool& x) { inline void consume(bool& x) {
result_ += x ? "true" : "false"; result_ += x ? "true" : "false";
} }
inline void consume(char* cstr) { inline void consume(const char* cstr) {
consume(const_cast<const char*>(cstr)); string_view tmp{cstr, strlen(cstr)};
consume(tmp);
} }
inline void consume(std::string& str) { inline void consume(char* cstr) {
consume(str.c_str()); string_view tmp{cstr, strlen(cstr)};
consume(tmp);
} }
template <class T> template <class T>
...@@ -132,6 +135,7 @@ public: ...@@ -132,6 +135,7 @@ public:
template <class T> template <class T>
enable_if_t<is_iterable<T>::value enable_if_t<is_iterable<T>::value
&& !is_inspectable<stringification_inspector, T>::value && !is_inspectable<stringification_inspector, T>::value
&& !std::is_convertible<T, string_view>::value
&& !has_to_string<T>::value> && !has_to_string<T>::value>
consume(T& xs) { consume(T& xs) {
result_ += '['; result_ += '[';
......
...@@ -39,20 +39,20 @@ void stringification_inspector::consume(atom_value& x) { ...@@ -39,20 +39,20 @@ void stringification_inspector::consume(atom_value& x) {
result_ += '\''; result_ += '\'';
} }
void stringification_inspector::consume(const char* cstr) { void stringification_inspector::consume(string_view str) {
if (cstr == nullptr || *cstr == '\0') { if (str.empty()) {
result_ += R"("")"; result_ += R"("")";
return; return;
} }
if (*cstr == '"') { if (str[0] == '"') {
// assume an already escaped string // Assume an already escaped string.
result_ += cstr; result_.insert(result_.end(), str.begin(), str.end());
return; return;
} }
// Escape string.
result_ += '"'; result_ += '"';
char c; for (char c : str) {
for(;;) { switch (c) {
switch (c = *cstr++) {
default: default:
result_ += c; result_ += c;
break; break;
...@@ -62,11 +62,8 @@ void stringification_inspector::consume(const char* cstr) { ...@@ -62,11 +62,8 @@ void stringification_inspector::consume(const char* cstr) {
case '"': case '"':
result_ += R"(\")"; result_ += R"(\")";
break; break;
case '\0':
goto end_of_string;
} }
} }
end_of_string:
result_ += '"'; result_ += '"';
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment