util/UriExtract: uri_get_suffix() returns std::string_view

No need to copy it to a buffer.
This commit is contained in:
Max Kellermann
2020-11-04 20:39:06 +01:00
parent 19dd1a25d7
commit 35a232105e
14 changed files with 72 additions and 115 deletions

View File

@@ -121,37 +121,21 @@ uri_get_path(std::string_view uri) noexcept
}
/* suffixes should be ascii only characters */
const char *
std::string_view
uri_get_suffix(const char *uri) noexcept
{
const char *suffix = std::strrchr(uri, '.');
if (suffix == nullptr || suffix == uri ||
suffix[-1] == '/' || suffix[-1] == '\\')
return nullptr;
return {};
++suffix;
if (strpbrk(suffix, "/\\") != nullptr)
return nullptr;
return {};
return suffix;
}
const char *
uri_get_suffix(const char *uri, UriSuffixBuffer &buffer) noexcept
{
const char *suffix = uri_get_suffix(uri);
if (suffix == nullptr)
return nullptr;
const char *q = std::strchr(suffix, '?');
if (q != nullptr && size_t(q - suffix) < sizeof(buffer.data)) {
memcpy(buffer.data, suffix, q - suffix);
buffer.data[q - suffix] = 0;
suffix = buffer.data;
}
return suffix;
/* remove the query string */
return StringView(suffix).Split('?').first;
}
const char *

View File

@@ -62,20 +62,9 @@ std::string_view
uri_get_path(std::string_view uri) noexcept;
gcc_pure
const char *
std::string_view
uri_get_suffix(const char *uri) noexcept;
struct UriSuffixBuffer {
char data[8];
};
/**
* Returns the file name suffix, ignoring the query string.
*/
gcc_pure
const char *
uri_get_suffix(const char *uri, UriSuffixBuffer &buffer) noexcept;
/**
* Returns the URI fragment, i.e. the portion after the '#', but
* without the '#'. If there is no '#', this function returns