From 242ba727b205bc962017fa7b4f03f04b1c8a6019 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 31 May 2022 13:33:33 +0200 Subject: [PATCH] net/HostParser: use std::string_view instead of StringView --- src/net/HostParser.cxx | 24 ++++++++++++++++++------ src/net/HostParser.hxx | 13 +++++-------- src/net/Resolver.cxx | 6 +++--- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/net/HostParser.cxx b/src/net/HostParser.cxx index ed4f77d85..3bdd71540 100644 --- a/src/net/HostParser.cxx +++ b/src/net/HostParser.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2007-2020 CM4all GmbH + * Copyright 2007-2022 CM4all GmbH * All rights reserved. * * author: Max Kellermann @@ -83,10 +83,22 @@ FindIPv6End(const char *p) noexcept return p; } +static constexpr std::string_view +SV(const char *begin, const char *end) noexcept +{ +#if __cplusplus >= 202002 && !defined(__clang__) + return {begin, end}; +#else + /* kludge for libc++ which does not yet implement the C++20 + iterator constructor */ + return {begin, std::size_t(end - begin)}; +#endif +} + ExtractHostResult ExtractHost(const char *src) noexcept { - ExtractHostResult result{nullptr, src}; + ExtractHostResult result{{}, src}; const char *hostname; if (IsValidHostnameChar(*src)) { @@ -100,7 +112,7 @@ ExtractHost(const char *src) noexcept /* found a second colon: assume it's an IPv6 address */ result.end = FindIPv6End(src + 1); - result.host = {hostname, result.end}; + result.host = SV(hostname, result.end); return result; } else /* remember the position of the first colon */ @@ -115,11 +127,11 @@ ExtractHost(const char *src) noexcept src = colon; result.end = src; - result.host = {hostname, result.end}; + result.host = SV(hostname, result.end); } else if (src[0] == ':' && src[1] == ':') { /* IPv6 address beginning with "::" */ result.end = FindIPv6End(src + 2); - result.host = {src, result.end}; + result.host = SV(src, result.end); } else if (src[0] == '[') { /* "[hostname]:port" (IPv6?) */ @@ -129,7 +141,7 @@ ExtractHost(const char *src) noexcept /* failed, return nullptr */ return result; - result.host = {hostname, end}; + result.host = SV(hostname, end); result.end = end + 1; } else { /* failed, return nullptr */ diff --git a/src/net/HostParser.hxx b/src/net/HostParser.hxx index 561beb843..3b8aecd0f 100644 --- a/src/net/HostParser.hxx +++ b/src/net/HostParser.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2007-2020 CM4all GmbH + * Copyright 2007-2022 CM4all GmbH * All rights reserved. * * author: Max Kellermann @@ -30,10 +30,9 @@ * OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef NET_HOST_PARSER_HXX -#define NET_HOST_PARSER_HXX +#pragma once -#include "util/StringView.hxx" +#include /** * Result type for ExtractHost(). @@ -44,7 +43,7 @@ struct ExtractHostResult { * * If nothing was parsed, then this is nullptr. */ - StringView host; + std::string_view host; /** * Pointer to the first character that was not parsed. On @@ -57,7 +56,7 @@ struct ExtractHostResult { const char *end; constexpr bool HasFailed() const noexcept { - return host == nullptr; + return host.data() == nullptr; } }; @@ -71,5 +70,3 @@ struct ExtractHostResult { [[gnu::pure]] ExtractHostResult ExtractHost(const char *src) noexcept; - -#endif diff --git a/src/net/Resolver.cxx b/src/net/Resolver.cxx index e4c6c48a3..19da604e0 100644 --- a/src/net/Resolver.cxx +++ b/src/net/Resolver.cxx @@ -108,11 +108,11 @@ Resolve(const char *host_and_port, int default_port, if (eh.HasFailed()) throw std::runtime_error("Failed to extract host name"); - if (eh.host.size >= sizeof(buffer)) + if (eh.host.size() >= sizeof(buffer)) throw std::runtime_error("Host name too long"); - memcpy(buffer, eh.host.data, eh.host.size); - buffer[eh.host.size] = 0; + memcpy(buffer, eh.host.data(), eh.host.size()); + buffer[eh.host.size()] = 0; host = buffer; #ifndef _WIN32