util: annotate string view input lifetimes

The string-view helpers return views into their input, while `LineReader` stores one.
Annotate their inputs so Clang can warn when a returned or stored view outlives a temporary string.
This commit is contained in:
Lőrinc
2026-09-03 00:20:59 -07:00
parent 4519933391
commit 34c5dc0583

View File

@@ -160,7 +160,7 @@ std::vector<T> Split(const std::span<const char>& sp, char sep, bool include_sep
return Split<std::string>(str, separators);
}
[[nodiscard]] inline std::string_view TrimStringView(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
[[nodiscard]] inline std::string_view TrimStringView(std::string_view str LIFETIMEBOUND, std::string_view pattern = " \f\n\r\t\v")
{
std::string::size_type front = str.find_first_not_of(pattern);
if (front == std::string::npos) {
@@ -175,7 +175,7 @@ std::vector<T> Split(const std::span<const char>& sp, char sep, bool include_sep
return std::string(TrimStringView(str, pattern));
}
[[nodiscard]] inline std::string_view RemoveSuffixView(std::string_view str, std::string_view suffix)
[[nodiscard]] inline std::string_view RemoveSuffixView(std::string_view str LIFETIMEBOUND, std::string_view suffix)
{
if (str.ends_with(suffix)) {
return str.substr(0, str.size() - suffix.size());
@@ -183,7 +183,7 @@ std::vector<T> Split(const std::span<const char>& sp, char sep, bool include_sep
return str;
}
[[nodiscard]] inline std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
[[nodiscard]] inline std::string_view RemovePrefixView(std::string_view str LIFETIMEBOUND, std::string_view prefix)
{
if (str.starts_with(prefix)) {
return str.substr(prefix.size());
@@ -273,7 +273,7 @@ class LineReader
std::string_view::iterator m_it;
public:
explicit LineReader(std::string_view str, size_t max_line_length);
explicit LineReader(std::string_view str LIFETIMEBOUND, size_t max_line_length);
/**
* Returns a string from current iterator position up to (but not including) next \n