introduce string_view

This commit is contained in:
Chunting Gu
2021-06-17 18:58:53 +08:00
parent 751a3539ae
commit 26bb6b341a
38 changed files with 404 additions and 344 deletions
+25 -3
View File
@@ -12,10 +12,32 @@
#include "webcc/config.h"
#if WEBCC_USE_STD_STRING_VIEW
#include <string_view>
#else
#include "boost/utility/string_view.hpp"
#endif // WEBCC_USE_STD_STRING_VIEW
namespace webcc {
// -----------------------------------------------------------------------------
#if WEBCC_USE_STD_STRING_VIEW
using string_view = std::string_view;
#else
using string_view = boost::string_view;
#endif // WEBCC_USE_STD_STRING_VIEW
inline std::string ToString(string_view sv) {
#if WEBCC_USE_STD_STRING_VIEW
return std::string{ sv.begin(), sv.end() };
#else
return sv.to_string();
#endif // WEBCC_USE_STD_STRING_VIEW
}
// -----------------------------------------------------------------------------
using Strings = std::vector<std::string>;
// Regex sub-matches of the URL (usually resource ID's).
@@ -169,7 +191,7 @@ public:
};
public:
explicit Error(Code code = kOK, const std::string& message = "")
explicit Error(Code code = kOK, string_view message = "")
: code_(code), message_(message) {
}
@@ -186,9 +208,9 @@ public:
return message_;
}
void Set(Code code, const std::string& message) {
void Set(Code code, string_view message) {
code_ = code;
message_ = message;
message_ = ToString(message);
}
bool timeout() const {