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
+11 -13
View File
@@ -17,15 +17,15 @@ namespace webcc {
class Url {
public:
// Encode URL different components.
static std::string EncodeHost(const std::string& utf8_str);
static std::string EncodePath(const std::string& utf8_str);
static std::string EncodeQuery(const std::string& utf8_str);
static std::string EncodeFull(const std::string& utf8_str);
static std::string EncodeHost(string_view utf8_str);
static std::string EncodePath(string_view utf8_str);
static std::string EncodeQuery(string_view utf8_str);
static std::string EncodeFull(string_view utf8_str);
public:
Url() = default;
explicit Url(const std::string& str, bool encode = false);
explicit Url(string_view str, bool encode = false);
const std::string& scheme() const {
return scheme_;
@@ -47,19 +47,18 @@ public:
return query_;
}
void set_port(const std::string& port) {
port_ = port;
void set_port(string_view port) {
port_ = ToString(port);
}
// Append a piece of path.
void AppendPath(const std::string& piece, bool encode = false);
void AppendPath(string_view piece, bool encode = false);
// Append a query parameter.
void AppendQuery(const std::string& key, const std::string& value,
bool encode = false);
void AppendQuery(string_view key, string_view value, bool encode = false);
private:
void Parse(const std::string& str);
void Parse(string_view str);
void Clear();
@@ -125,12 +124,11 @@ private:
// Used by Server::Route().
class UrlRegex {
public:
explicit UrlRegex(const std::string& url) : url_(url) {
explicit UrlRegex(string_view url) : url_(url) {
}
std::regex operator()() const {
std::regex::flag_type flags = std::regex::ECMAScript | std::regex::icase;
return std::regex{ url_, flags };
}