protocol/RangeArg: add methods ClipRelaxed(), CheckClip(), CheckAdjustEnd()

This commit is contained in:
Max Kellermann 2021-02-15 19:47:17 +01:00
parent ab160aa359
commit 157ddcbab1
1 changed files with 50 additions and 0 deletions

View File

@ -90,6 +90,56 @@ struct RangeArg {
constexpr unsigned Count() const noexcept {
return end - start;
}
/**
* Make sure that both start and end are within the given
* count.
*/
constexpr void ClipRelaxed(unsigned count) noexcept {
if (end > count)
end = count;
if (start > end)
start = end;
}
/**
* Check if the start index is valid and clip the end of the
* range.
*
* @return false if the start is out of range
*/
[[nodiscard]]
constexpr bool CheckClip(unsigned count) noexcept {
if (start > count)
return false;
if (end > count)
end = count;
return true;
}
/**
* Check if start and end index are valid and adjust the end if this
* is an open-ended range.
*
* @return false if start or end is out of range
*/
[[nodiscard]]
constexpr bool CheckAdjustEnd(unsigned count) noexcept {
if (start > count)
return false;
if (end > count) {
if (!IsOpenEnded())
return false;
end = count;
}
return true;
}
};
#endif