mirror of
https://github.com/nghttp2/nghttp2.git
synced 2026-03-27 16:29:17 +08:00
Refactor StringRef
This commit is contained in:
21
src/http2.cc
21
src/http2.cc
@@ -690,7 +690,7 @@ StringRef rewrite_location_uri(BlockAllocator &balloc, const StringRef &uri,
|
||||
|
||||
*p = '\0';
|
||||
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
int parse_http_status_code(const StringRef &src) {
|
||||
@@ -1672,7 +1672,7 @@ int construct_push_component(BlockAllocator &balloc, StringRef &scheme,
|
||||
}
|
||||
*p = '\0';
|
||||
|
||||
authority = StringRef{std::begin(iov), p};
|
||||
authority = StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
if (u.field_set & (1 << UF_PATH)) {
|
||||
@@ -1751,12 +1751,12 @@ StringRef path_join(BlockAllocator &balloc, const StringRef &base_path,
|
||||
p = std::copy(std::begin(base_query), std::end(base_query), p);
|
||||
}
|
||||
*p = '\0';
|
||||
return StringRef{std::begin(res), p};
|
||||
return StringRef{std::span{std::begin(res), p}};
|
||||
}
|
||||
*p++ = '?';
|
||||
p = std::copy(std::begin(rel_query), std::end(rel_query), p);
|
||||
*p = '\0';
|
||||
return StringRef{std::begin(res), p};
|
||||
return StringRef{std::span{std::begin(res), p}};
|
||||
}
|
||||
|
||||
auto first = std::begin(rel_path);
|
||||
@@ -1818,7 +1818,7 @@ StringRef path_join(BlockAllocator &balloc, const StringRef &base_path,
|
||||
p = std::copy(std::begin(rel_query), std::end(rel_query), p);
|
||||
}
|
||||
*p = '\0';
|
||||
return StringRef{std::begin(res), p};
|
||||
return StringRef{std::span{std::begin(res), p}};
|
||||
}
|
||||
|
||||
StringRef normalize_path(BlockAllocator &balloc, const StringRef &path,
|
||||
@@ -1865,7 +1865,7 @@ StringRef normalize_path(BlockAllocator &balloc, const StringRef &path,
|
||||
*p = '\0';
|
||||
|
||||
return path_join(balloc, StringRef{}, StringRef{},
|
||||
StringRef{std::begin(result), p}, query);
|
||||
StringRef{std::span{std::begin(result), p}}, query);
|
||||
}
|
||||
|
||||
StringRef normalize_path_colon(BlockAllocator &balloc, const StringRef &path,
|
||||
@@ -1912,7 +1912,7 @@ StringRef normalize_path_colon(BlockAllocator &balloc, const StringRef &path,
|
||||
*p = '\0';
|
||||
|
||||
return path_join(balloc, StringRef{}, StringRef{},
|
||||
StringRef{std::begin(result), p}, query);
|
||||
StringRef{std::span{std::begin(result), p}}, query);
|
||||
}
|
||||
|
||||
std::string normalize_path(const StringRef &path, const StringRef &query) {
|
||||
@@ -1942,7 +1942,7 @@ StringRef copy_lower(BlockAllocator &balloc, const StringRef &src) {
|
||||
p = std::copy(std::begin(src), std::end(src), p);
|
||||
*p = '\0';
|
||||
util::inp_strlower(std::begin(iov), p);
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
bool contains_trailers(const StringRef &s) {
|
||||
@@ -1977,12 +1977,13 @@ StringRef make_websocket_accept_token(uint8_t *dest, const StringRef &key) {
|
||||
std::copy_n(magic, str_size(magic), p);
|
||||
|
||||
std::array<uint8_t, 20> h;
|
||||
if (util::sha1(h.data(), StringRef{std::begin(s), std::end(s)}) != 0) {
|
||||
if (util::sha1(h.data(), StringRef{std::span{std::begin(s), std::end(s)}}) !=
|
||||
0) {
|
||||
return StringRef{};
|
||||
}
|
||||
|
||||
auto end = base64::encode(std::begin(h), std::end(h), dest);
|
||||
return StringRef{dest, end};
|
||||
return StringRef{std::span{dest, end}};
|
||||
}
|
||||
|
||||
bool legacy_http1(int major, int minor) {
|
||||
|
||||
@@ -3927,7 +3927,7 @@ int process_options(Config *config,
|
||||
auto gen = util::make_mt19937();
|
||||
p = util::random_alpha_digit(p, p + SHRPX_OBFUSCATED_NODE_LENGTH, gen);
|
||||
*p = '\0';
|
||||
fwdconf.by_obfuscated = StringRef{std::begin(iov), p};
|
||||
fwdconf.by_obfuscated = StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
if (config->http2.upstream.debug.frame_debug) {
|
||||
|
||||
@@ -290,7 +290,7 @@ int APIDownstreamConnection::error_method_not_allowed() {
|
||||
*p = '\0';
|
||||
|
||||
resp.fs.add_header_token(StringRef::from_lit("allow"),
|
||||
StringRef{std::begin(iov), p}, false, -1);
|
||||
StringRef{std::span{std::begin(iov), p}}, false, -1);
|
||||
return send_reply(405, APIStatusCode::FAILURE);
|
||||
}
|
||||
|
||||
@@ -388,8 +388,8 @@ int APIDownstreamConnection::handle_backendconfig() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto opt = StringRef{first, eq};
|
||||
auto optval = StringRef{eq + 1, eol};
|
||||
auto opt = StringRef{std::span{first, eq}};
|
||||
auto optval = StringRef{std::span{eq + 1, eol}};
|
||||
|
||||
auto optid = option_lookup_token(opt.c_str(), opt.size());
|
||||
|
||||
|
||||
@@ -498,7 +498,7 @@ ClientHandler::ClientHandler(Worker *worker, int fd, SSL *ssl,
|
||||
worker_->get_randgen());
|
||||
*p = '\0';
|
||||
|
||||
forwarded_for_ = StringRef{std::begin(buf), p};
|
||||
forwarded_for_ = StringRef{std::span{std::begin(buf), p}};
|
||||
} else {
|
||||
init_forwarded_for(family, ipaddr_);
|
||||
}
|
||||
@@ -517,7 +517,7 @@ void ClientHandler::init_forwarded_for(int family, const StringRef &ipaddr) {
|
||||
*p++ = ']';
|
||||
*p = '\0';
|
||||
|
||||
forwarded_for_ = StringRef{std::begin(buf), p};
|
||||
forwarded_for_ = StringRef{std::span{std::begin(buf), p}};
|
||||
} else {
|
||||
// family == AF_INET or family == AF_UNIX
|
||||
forwarded_for_ = ipaddr;
|
||||
@@ -802,8 +802,7 @@ uint32_t ClientHandler::get_affinity_cookie(Downstream *downstream,
|
||||
|
||||
auto d = std::uniform_int_distribution<uint32_t>(1);
|
||||
auto rh = d(worker_->get_randgen());
|
||||
h = util::hash32(StringRef{reinterpret_cast<uint8_t *>(&rh),
|
||||
reinterpret_cast<uint8_t *>(&rh) + sizeof(rh)});
|
||||
h = util::hash32(StringRef{reinterpret_cast<char *>(&rh), sizeof(rh)});
|
||||
|
||||
downstream->renew_affinity_cookie(h);
|
||||
|
||||
@@ -963,8 +962,7 @@ DownstreamAddr *ClientHandler::get_downstream_addr_strict_affinity(
|
||||
} else {
|
||||
auto d = std::uniform_int_distribution<uint32_t>(1);
|
||||
auto rh = d(worker_->get_randgen());
|
||||
h = util::hash32(StringRef{reinterpret_cast<uint8_t *>(&rh),
|
||||
reinterpret_cast<uint8_t *>(&rh) + sizeof(rh)});
|
||||
h = util::hash32(StringRef{reinterpret_cast<char *>(&rh), sizeof(rh)});
|
||||
}
|
||||
|
||||
// Client is not bound to a particular backend, or the bound backend
|
||||
@@ -1482,9 +1480,10 @@ int ClientHandler::proxy_protocol_read() {
|
||||
|
||||
rb_.drain(end + 2 - rb_.pos());
|
||||
|
||||
ipaddr_ =
|
||||
make_string_ref(balloc_, StringRef{src_addr, src_addr + src_addrlen});
|
||||
port_ = make_string_ref(balloc_, StringRef{src_port, src_port + src_portlen});
|
||||
ipaddr_ = make_string_ref(
|
||||
balloc_, StringRef{src_addr, static_cast<size_t>(src_addrlen)});
|
||||
port_ = make_string_ref(
|
||||
balloc_, StringRef{src_port, static_cast<size_t>(src_portlen)});
|
||||
|
||||
if (LOG_ENABLED(INFO)) {
|
||||
CLOG(INFO, this) << "PROXY-protocol-v1: Finished, " << (rb_.pos() - first)
|
||||
|
||||
@@ -380,7 +380,7 @@ HeaderRefs::value_type parse_header(BlockAllocator &balloc,
|
||||
*p = '\0';
|
||||
|
||||
auto nv =
|
||||
HeaderRef(StringRef{std::begin(name_iov), p},
|
||||
HeaderRef(StringRef{std::span{std::begin(name_iov), p}},
|
||||
make_string_ref(balloc, StringRef{value, std::end(optarg)}));
|
||||
|
||||
if (!nghttp2_check_header_name(nv.name.byte(), nv.name.size()) ||
|
||||
@@ -784,7 +784,7 @@ std::vector<LogFragment> parse_log_format(BlockAllocator &balloc,
|
||||
std::transform(std::begin(iov), p, std::begin(iov),
|
||||
[](auto c) { return c == '_' ? '-' : c; });
|
||||
*p = '\0';
|
||||
res.emplace_back(type, StringRef{std::begin(iov), p});
|
||||
res.emplace_back(type, StringRef{std::span{std::begin(iov), p}});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1235,7 +1235,7 @@ int parse_mapping(Config *config, DownstreamAddrConfig &addr,
|
||||
util::inp_strlower(std::begin(iov), p);
|
||||
*p++ = '/';
|
||||
*p = '\0';
|
||||
pattern = StringRef{std::begin(iov), p};
|
||||
pattern = StringRef{std::span{std::begin(iov), p}};
|
||||
} else {
|
||||
auto path = http2::normalize_path_colon(
|
||||
downstreamconf.balloc, StringRef{slash, std::end(raw_pattern)},
|
||||
@@ -1247,7 +1247,7 @@ int parse_mapping(Config *config, DownstreamAddrConfig &addr,
|
||||
util::inp_strlower(std::begin(iov), p);
|
||||
p = std::copy(std::begin(path), std::end(path), p);
|
||||
*p = '\0';
|
||||
pattern = StringRef{std::begin(iov), p};
|
||||
pattern = StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
auto it = pattern_addr_indexer.find(pattern);
|
||||
if (it != std::end(pattern_addr_indexer)) {
|
||||
@@ -1382,7 +1382,7 @@ int parse_mapping(Config *config, DownstreamAddrConfig &addr,
|
||||
auto p = std::reverse_copy(std::begin(host), std::end(host),
|
||||
std::begin(iov));
|
||||
*p = '\0';
|
||||
auto rev_host = StringRef{std::begin(iov), p};
|
||||
auto rev_host = StringRef{std::span{std::begin(iov), p}};
|
||||
|
||||
rw_router.add_route(rev_host, wildcard_patterns.size() - 1);
|
||||
} else {
|
||||
|
||||
@@ -371,7 +371,7 @@ StringRef Downstream::assemble_request_cookie() {
|
||||
p -= 2;
|
||||
}
|
||||
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
uint32_t Downstream::find_affinity_cookie(const StringRef &name) {
|
||||
@@ -478,7 +478,7 @@ StringRef alloc_header_name(BlockAllocator &balloc, const StringRef &name) {
|
||||
util::inp_strlower(std::begin(iov), p);
|
||||
*p = '\0';
|
||||
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ StringRef create_error_html(BlockAllocator &balloc, unsigned int http_status) {
|
||||
const auto &error_pages = httpconf.error_pages;
|
||||
for (const auto &page : error_pages) {
|
||||
if (page.http_status == 0 || page.http_status == http_status) {
|
||||
return StringRef{std::begin(page.content), std::end(page.content)};
|
||||
return StringRef{std::span{page.content}};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ StringRef create_forwarded(BlockAllocator &balloc, int params,
|
||||
--p;
|
||||
*p = '\0';
|
||||
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
std::string colorizeHeaders(const char *hdrs) {
|
||||
@@ -201,7 +201,7 @@ StringRef create_affinity_cookie(BlockAllocator &balloc, const StringRef &name,
|
||||
p = std::copy(std::begin(SECURE), std::end(SECURE), p);
|
||||
}
|
||||
*p = '\0';
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
bool require_cookie_secure_attribute(SessionAffinityCookieSecure secure,
|
||||
@@ -264,7 +264,7 @@ StringRef create_altsvc_header_value(BlockAllocator &balloc,
|
||||
|
||||
assert(static_cast<size_t>(p - std::begin(iov)) == len);
|
||||
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
bool check_http_scheme(const StringRef &scheme, bool encrypted) {
|
||||
|
||||
@@ -446,8 +446,8 @@ int Http2DownstreamConnection::push_request_headers() {
|
||||
p = http::create_via_header_value(p, req.http_major, req.http_minor);
|
||||
*p = '\0';
|
||||
|
||||
nva.push_back(
|
||||
http2::make_nv_ls_nocopy("via", StringRef{std::begin(iov), p}));
|
||||
nva.push_back(http2::make_nv_ls_nocopy(
|
||||
"via", StringRef{std::span{std::begin(iov), p}}));
|
||||
}
|
||||
|
||||
auto te = req.fs.header(http2::HD_TE);
|
||||
|
||||
@@ -1851,8 +1851,8 @@ int Http2Upstream::on_downstream_header_complete(Downstream *downstream) {
|
||||
p = http::create_via_header_value(p, resp.http_major, resp.http_minor);
|
||||
*p = '\0';
|
||||
|
||||
nva.push_back(
|
||||
http2::make_nv_ls_nocopy("via", StringRef{std::begin(iov), p}));
|
||||
nva.push_back(http2::make_nv_ls_nocopy(
|
||||
"via", StringRef{std::span{std::begin(iov), p}}));
|
||||
}
|
||||
|
||||
for (auto &p : httpconf.add_response_headers) {
|
||||
|
||||
@@ -1412,8 +1412,8 @@ int Http3Upstream::on_downstream_header_complete(Downstream *downstream) {
|
||||
p = http::create_via_header_value(p, resp.http_major, resp.http_minor);
|
||||
*p = '\0';
|
||||
|
||||
nva.push_back(
|
||||
http3::make_nv_ls_nocopy("via", StringRef{std::begin(iov), p}));
|
||||
nva.push_back(http3::make_nv_ls_nocopy(
|
||||
"via", StringRef{std::span{std::begin(iov), p}}));
|
||||
}
|
||||
|
||||
for (auto &p : httpconf.add_response_headers) {
|
||||
|
||||
@@ -567,7 +567,7 @@ int HttpDownstreamConnection::push_request_headers() {
|
||||
auto p =
|
||||
base64::encode(std::begin(nonce), std::end(nonce), std::begin(iov));
|
||||
*p = '\0';
|
||||
auto key = StringRef{std::begin(iov), p};
|
||||
auto key = StringRef{std::span{std::begin(iov), p}};
|
||||
downstream_->set_ws_key(key);
|
||||
|
||||
buf->append("Sec-Websocket-Key: ");
|
||||
|
||||
@@ -286,7 +286,7 @@ void rewrite_request_host_path_from_uri(BlockAllocator &balloc, Request &req,
|
||||
}
|
||||
*p = '\0';
|
||||
|
||||
req.authority = StringRef{std::begin(iovec), p};
|
||||
req.authority = StringRef{std::span{std::begin(iovec), p}};
|
||||
} else {
|
||||
req.authority = authority;
|
||||
}
|
||||
@@ -1074,7 +1074,7 @@ void HttpsUpstream::error_reply(unsigned int status_code) {
|
||||
output->append("\r\nServer: ");
|
||||
output->append(get_config()->http.server_name);
|
||||
output->append("\r\nContent-Length: ");
|
||||
std::array<uint8_t, NGHTTP2_MAX_UINT64_DIGITS> intbuf;
|
||||
std::array<char, NGHTTP2_MAX_UINT64_DIGITS> intbuf;
|
||||
output->append(StringRef{std::begin(intbuf),
|
||||
util::utos(std::begin(intbuf), html.size())});
|
||||
output->append("\r\nDate: ");
|
||||
|
||||
@@ -569,7 +569,7 @@ StringRef construct_absolute_request_uri(BlockAllocator &balloc,
|
||||
p = std::copy(std::begin(req.path), std::end(req.path), p);
|
||||
*p = '\0';
|
||||
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
||||
@@ -50,8 +50,7 @@ LogConfig::LogConfig()
|
||||
errorlog_tty(false) {
|
||||
auto tid = std::this_thread::get_id();
|
||||
auto tid_hash =
|
||||
util::hash32(StringRef{reinterpret_cast<uint8_t *>(&tid),
|
||||
reinterpret_cast<uint8_t *>(&tid) + sizeof(tid)});
|
||||
util::hash32(StringRef{reinterpret_cast<char *>(&tid), sizeof(tid)});
|
||||
thread_id = util::format_hex(std::span{&tid_hash, 1});
|
||||
}
|
||||
|
||||
|
||||
@@ -213,7 +213,7 @@ int servername_callback(SSL *ssl, int *al, void *arg) {
|
||||
return SSL_TLSEXT_ERR_NOACK;
|
||||
}
|
||||
|
||||
std::array<uint8_t, NI_MAXHOST> buf;
|
||||
std::array<char, NI_MAXHOST> buf;
|
||||
|
||||
auto end_buf = std::copy_n(rawhost, len, std::begin(buf));
|
||||
|
||||
@@ -1924,7 +1924,7 @@ int check_cert(SSL *ssl, const DownstreamAddr *addr, const Address *raddr) {
|
||||
CertLookupTree::CertLookupTree() {}
|
||||
|
||||
ssize_t CertLookupTree::add_cert(const StringRef &hostname, size_t idx) {
|
||||
std::array<uint8_t, NI_MAXHOST> buf;
|
||||
std::array<char, NI_MAXHOST> buf;
|
||||
|
||||
// NI_MAXHOST includes terminal NULL byte
|
||||
if (hostname.empty() || hostname.size() + 1 > buf.size()) {
|
||||
@@ -1976,7 +1976,7 @@ ssize_t CertLookupTree::add_cert(const StringRef &hostname, size_t idx) {
|
||||
}
|
||||
|
||||
ssize_t CertLookupTree::lookup(const StringRef &hostname) {
|
||||
std::array<uint8_t, NI_MAXHOST> buf;
|
||||
std::array<char, NI_MAXHOST> buf;
|
||||
|
||||
// NI_MAXHOST includes terminal NULL byte
|
||||
if (hostname.empty() || hostname.size() + 1 > buf.size()) {
|
||||
@@ -2025,9 +2025,7 @@ ssize_t CertLookupTree::lookup(const StringRef &hostname) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto prefixlen =
|
||||
wprefix.prefix.size() +
|
||||
(reinterpret_cast<const uint8_t *>(&rev_host[0]) - &buf[0]);
|
||||
auto prefixlen = wprefix.prefix.size() + (&rev_host[0] - &buf[0]);
|
||||
|
||||
// Breaking a tie with longer suffix
|
||||
if (prefixlen < best_prefixlen) {
|
||||
@@ -2050,7 +2048,7 @@ void CertLookupTree::dump() const {
|
||||
int cert_lookup_tree_add_ssl_ctx(
|
||||
CertLookupTree *lt, std::vector<std::vector<SSL_CTX *>> &indexed_ssl_ctx,
|
||||
SSL_CTX *ssl_ctx) {
|
||||
std::array<uint8_t, NI_MAXHOST> buf;
|
||||
std::array<char, NI_MAXHOST> buf;
|
||||
|
||||
auto cert = SSL_CTX_get0_certificate(ssl_ctx);
|
||||
auto altnames = static_cast<GENERAL_NAMES *>(
|
||||
|
||||
@@ -1299,7 +1299,7 @@ size_t match_downstream_addr_group_host(
|
||||
auto ep = std::copy(std::begin(host) + 1, std::end(host),
|
||||
std::begin(rev_host_src));
|
||||
std::reverse(std::begin(rev_host_src), ep);
|
||||
auto rev_host = StringRef{std::begin(rev_host_src), ep};
|
||||
auto rev_host = StringRef{std::span{std::begin(rev_host_src), ep}};
|
||||
|
||||
ssize_t best_group = -1;
|
||||
const RNode *last_node = nullptr;
|
||||
@@ -1400,7 +1400,7 @@ size_t match_downstream_addr_group(
|
||||
auto ep = std::copy(std::begin(host), std::end(host), std::begin(low_host));
|
||||
*ep = '\0';
|
||||
util::inp_strlower(std::begin(low_host), ep);
|
||||
host = StringRef{std::begin(low_host), ep};
|
||||
host = StringRef{std::span{std::begin(low_host), ep}};
|
||||
}
|
||||
return match_downstream_addr_group_host(routerconf, host, path, groups,
|
||||
catch_all, balloc);
|
||||
|
||||
123
src/template.h
123
src/template.h
@@ -287,8 +287,8 @@ public:
|
||||
const_reverse_iterator rend() const { return const_reverse_iterator{base}; }
|
||||
const_reverse_iterator crend() const { return const_reverse_iterator{base}; }
|
||||
|
||||
const char *c_str() const { return base; }
|
||||
size_type size() const { return len; }
|
||||
constexpr const char *c_str() const noexcept { return base; }
|
||||
constexpr size_type size() const noexcept { return len; }
|
||||
bool empty() const { return len == 0; }
|
||||
const_reference operator[](size_type pos) const { return *(base + pos); }
|
||||
|
||||
@@ -376,31 +376,30 @@ public:
|
||||
using const_iterator = const_pointer;
|
||||
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
||||
|
||||
constexpr StringRef() : base(""), len(0) {}
|
||||
explicit StringRef(const std::string &s) : base(s.c_str()), len(s.size()) {}
|
||||
explicit StringRef(const ImmutableString &s)
|
||||
constexpr StringRef() noexcept : base(""), len(0) {}
|
||||
constexpr StringRef(const StringRef &other) noexcept = default;
|
||||
constexpr StringRef(std::nullptr_t) = delete;
|
||||
constexpr explicit StringRef(const std::string &s)
|
||||
: base(s.c_str()), len(s.size()) {}
|
||||
StringRef(const char *s) : base(s), len(strlen(s)) {}
|
||||
constexpr explicit StringRef(const ImmutableString &s)
|
||||
: base(s.c_str()), len(s.size()) {}
|
||||
constexpr StringRef(const char *s) : base(s), len(traits_type::length(s)) {}
|
||||
constexpr StringRef(const char *s, size_t n) : base(s), len(n) {}
|
||||
template <typename CharT>
|
||||
constexpr StringRef(const CharT *s, size_t n)
|
||||
explicit StringRef(const uint8_t *s, size_t n)
|
||||
: base(reinterpret_cast<const char *>(s)), len(n) {}
|
||||
template <typename InputIt>
|
||||
StringRef(InputIt first, InputIt last)
|
||||
: base(reinterpret_cast<const char *>(&*first)),
|
||||
len(std::distance(first, last)) {}
|
||||
template <typename InputIt>
|
||||
StringRef(InputIt *first, InputIt *last)
|
||||
: base(reinterpret_cast<const char *>(first)),
|
||||
len(std::distance(first, last)) {}
|
||||
template <typename T, size_t N = std::dynamic_extent>
|
||||
explicit StringRef(std::span<T, N> s)
|
||||
template <std::contiguous_iterator InputIt,
|
||||
typename = std::enable_if_t<
|
||||
std::is_same_v<std::iter_value_t<InputIt>, char>>>
|
||||
constexpr StringRef(InputIt first, InputIt last)
|
||||
: base(std::to_address(first)), len(std::distance(first, last)) {}
|
||||
constexpr StringRef(std::span<const char> s)
|
||||
: base(s.data()), len(s.size_bytes()) {}
|
||||
explicit StringRef(std::span<const uint8_t> s)
|
||||
: base(reinterpret_cast<const char *>(s.data())), len(s.size_bytes()) {}
|
||||
template <typename CharT, size_t N>
|
||||
constexpr static StringRef from_lit(const CharT (&s)[N]) {
|
||||
template <size_t N> static constexpr StringRef from_lit(const char (&s)[N]) {
|
||||
return StringRef{s, N - 1};
|
||||
}
|
||||
static StringRef from_maybe_nullptr(const char *s) {
|
||||
static constexpr StringRef from_maybe_nullptr(const char *s) noexcept {
|
||||
if (s == nullptr) {
|
||||
return StringRef();
|
||||
}
|
||||
@@ -408,25 +407,31 @@ public:
|
||||
return StringRef(s);
|
||||
}
|
||||
|
||||
constexpr const_iterator begin() const { return base; };
|
||||
constexpr const_iterator cbegin() const { return base; };
|
||||
constexpr StringRef &operator=(const StringRef &other) noexcept = default;
|
||||
|
||||
constexpr const_iterator end() const { return base + len; };
|
||||
constexpr const_iterator cend() const { return base + len; };
|
||||
constexpr const_iterator begin() const noexcept { return base; };
|
||||
constexpr const_iterator cbegin() const noexcept { return base; };
|
||||
|
||||
const_reverse_iterator rbegin() const {
|
||||
constexpr const_iterator end() const noexcept { return base + len; };
|
||||
constexpr const_iterator cend() const noexcept { return base + len; };
|
||||
|
||||
constexpr const_reverse_iterator rbegin() const noexcept {
|
||||
return const_reverse_iterator{base + len};
|
||||
}
|
||||
const_reverse_iterator crbegin() const {
|
||||
constexpr const_reverse_iterator crbegin() const noexcept {
|
||||
return const_reverse_iterator{base + len};
|
||||
}
|
||||
|
||||
const_reverse_iterator rend() const { return const_reverse_iterator{base}; }
|
||||
const_reverse_iterator crend() const { return const_reverse_iterator{base}; }
|
||||
constexpr const_reverse_iterator rend() const noexcept {
|
||||
return const_reverse_iterator{base};
|
||||
}
|
||||
constexpr const_reverse_iterator crend() const noexcept {
|
||||
return const_reverse_iterator{base};
|
||||
}
|
||||
|
||||
constexpr const char *c_str() const { return base; }
|
||||
constexpr size_type size() const { return len; }
|
||||
constexpr bool empty() const { return len == 0; }
|
||||
constexpr const char *c_str() const noexcept { return base; }
|
||||
constexpr size_type size() const noexcept { return len; }
|
||||
[[nodiscard]] constexpr bool empty() const noexcept { return len == 0; }
|
||||
constexpr const_reference operator[](size_type pos) const {
|
||||
return *(base + pos);
|
||||
}
|
||||
@@ -449,62 +454,42 @@ private:
|
||||
size_type len;
|
||||
};
|
||||
|
||||
inline bool operator==(const StringRef &lhs, const StringRef &rhs) {
|
||||
inline constexpr bool operator==(const StringRef &lhs,
|
||||
const StringRef &rhs) noexcept {
|
||||
return lhs.size() == rhs.size() &&
|
||||
std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
|
||||
}
|
||||
|
||||
inline bool operator==(const StringRef &lhs, const std::string &rhs) {
|
||||
inline constexpr bool operator==(const StringRef &lhs, const std::string &rhs) {
|
||||
return lhs.size() == rhs.size() &&
|
||||
std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
|
||||
}
|
||||
|
||||
inline bool operator==(const std::string &lhs, const StringRef &rhs) {
|
||||
return rhs == lhs;
|
||||
}
|
||||
|
||||
inline bool operator==(const StringRef &lhs, const char *rhs) {
|
||||
return lhs.size() == strlen(rhs) &&
|
||||
inline constexpr bool operator==(const StringRef &lhs,
|
||||
const char *rhs) noexcept {
|
||||
return lhs.size() == std::char_traits<char>::length(rhs) &&
|
||||
std::equal(std::begin(lhs), std::end(lhs), rhs);
|
||||
}
|
||||
|
||||
inline bool operator==(const StringRef &lhs, const ImmutableString &rhs) {
|
||||
inline constexpr bool operator==(const StringRef &lhs,
|
||||
const ImmutableString &rhs) noexcept {
|
||||
return lhs.size() == rhs.size() &&
|
||||
std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs));
|
||||
}
|
||||
|
||||
inline bool operator==(const ImmutableString &lhs, const StringRef &rhs) {
|
||||
return rhs == lhs;
|
||||
#ifndef __APPLE__
|
||||
inline constexpr std::strong_ordering
|
||||
operator<=>(const StringRef &lhs, const StringRef &rhs) noexcept {
|
||||
return std::lexicographical_compare_three_way(std::begin(lhs), std::end(lhs),
|
||||
std::begin(rhs), std::end(rhs));
|
||||
}
|
||||
|
||||
inline bool operator==(const char *lhs, const StringRef &rhs) {
|
||||
return rhs == lhs;
|
||||
}
|
||||
|
||||
inline bool operator!=(const StringRef &lhs, const StringRef &rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
inline bool operator!=(const StringRef &lhs, const std::string &rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
inline bool operator!=(const std::string &lhs, const StringRef &rhs) {
|
||||
return !(rhs == lhs);
|
||||
}
|
||||
|
||||
inline bool operator!=(const StringRef &lhs, const char *rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
inline bool operator!=(const char *lhs, const StringRef &rhs) {
|
||||
return !(rhs == lhs);
|
||||
}
|
||||
|
||||
inline bool operator<(const StringRef &lhs, const StringRef &rhs) {
|
||||
#else // __APPLE__
|
||||
inline constexpr bool operator<(const StringRef &lhs,
|
||||
const StringRef &rhs) noexcept {
|
||||
return std::lexicographical_compare(std::begin(lhs), std::end(lhs),
|
||||
std::begin(rhs), std::end(rhs));
|
||||
}
|
||||
#endif // __APPLE__
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &o, const StringRef &s) {
|
||||
return o.write(s.c_str(), s.size());
|
||||
|
||||
10
src/util.cc
10
src/util.cc
@@ -195,7 +195,7 @@ StringRef percent_encode_token(BlockAllocator &balloc,
|
||||
|
||||
*p = '\0';
|
||||
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
size_t percent_encode_tokenlen(const StringRef &target) {
|
||||
@@ -241,7 +241,7 @@ StringRef quote_string(BlockAllocator &balloc, const StringRef &target) {
|
||||
|
||||
*p = '\0';
|
||||
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
size_t quote_stringlen(const StringRef &target) {
|
||||
@@ -511,7 +511,7 @@ StringRef format_hex(BlockAllocator &balloc, std::span<const uint8_t> s) {
|
||||
|
||||
*p = '\0';
|
||||
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
void to_token68(std::string &base64str) {
|
||||
@@ -554,7 +554,7 @@ StringRef to_base64(BlockAllocator &balloc, const StringRef &token68str) {
|
||||
|
||||
*p = '\0';
|
||||
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
namespace {
|
||||
@@ -1609,7 +1609,7 @@ StringRef percent_decode(BlockAllocator &balloc, const StringRef &src) {
|
||||
*p++ = *first;
|
||||
}
|
||||
*p = '\0';
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
// Returns x**y
|
||||
|
||||
@@ -469,7 +469,7 @@ StringRef make_string_ref_uint(BlockAllocator &balloc, T n) {
|
||||
auto p = std::begin(iov);
|
||||
p = util::utos(p, n);
|
||||
*p = '\0';
|
||||
return StringRef{std::begin(iov), p};
|
||||
return StringRef{std::span{std::begin(iov), p}};
|
||||
}
|
||||
|
||||
template <typename T> std::string utos_unit(T n) {
|
||||
@@ -807,7 +807,7 @@ StringRef make_hostport(OutputIt first, const StringRef &host, uint16_t port) {
|
||||
|
||||
*p = '\0';
|
||||
|
||||
return StringRef{first, p};
|
||||
return StringRef{std::span{first, p}};
|
||||
}
|
||||
|
||||
// Creates "host:port" string using given |host| and |port|. If
|
||||
@@ -838,7 +838,7 @@ StringRef make_http_hostport(OutputIt first, const StringRef &host,
|
||||
|
||||
*p = '\0';
|
||||
|
||||
return StringRef{first, p};
|
||||
return StringRef{std::span{first, p}};
|
||||
}
|
||||
|
||||
// hexdump dumps |data| of length |datalen| in the format similar to
|
||||
|
||||
Reference in New Issue
Block a user