net/HostParser: use std::string_view instead of StringView

This commit is contained in:
Max Kellermann 2022-05-31 13:33:33 +02:00
parent d5db4ca0e7
commit 242ba727b2
3 changed files with 26 additions and 17 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2007-2020 CM4all GmbH
* Copyright 2007-2022 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
@ -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 */

View File

@ -1,5 +1,5 @@
/*
* Copyright 2007-2020 CM4all GmbH
* Copyright 2007-2022 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
@ -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 <string_view>
/**
* 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

View File

@ -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