util/UriExtract: uri_get_suffix() returns std::string_view
No need to copy it to a buffer.
This commit is contained in:
@@ -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 *
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user