From 157ddcbab1beb8c820bf7173478cb6b95f58c388 Mon Sep 17 00:00:00 2001 From: Max Kellermann <max@musicpd.org> Date: Mon, 15 Feb 2021 19:47:17 +0100 Subject: [PATCH] protocol/RangeArg: add methods ClipRelaxed(), CheckClip(), CheckAdjustEnd() --- src/protocol/RangeArg.hxx | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/protocol/RangeArg.hxx b/src/protocol/RangeArg.hxx index 347fa0aea..13f2f6114 100644 --- a/src/protocol/RangeArg.hxx +++ b/src/protocol/RangeArg.hxx @@ -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