From 19a46064e91ece0c3e9a88a6ddc8efe4cdd15032 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 15 Feb 2021 19:46:56 +0100 Subject: [PATCH] protocol/RangeArg: add methods IsWellFormed(), IsEmpty(), HasAtLeast(), Count() --- src/protocol/RangeArg.hxx | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/protocol/RangeArg.hxx b/src/protocol/RangeArg.hxx index 5f846350c..347fa0aea 100644 --- a/src/protocol/RangeArg.hxx +++ b/src/protocol/RangeArg.hxx @@ -59,9 +59,37 @@ struct RangeArg { return *this == All(); } + constexpr bool IsWellFormed() const noexcept { + return start <= end; + } + + /** + * Is this range empty? A malformed range also counts as + * "empty" for this method. + */ + constexpr bool IsEmpty() const noexcept { + return start >= end; + } + + /** + * Check if the range contains at least this number of items. + * Unlike Count(), this allows the object to be malformed. + */ + constexpr bool HasAtLeast(unsigned n) const noexcept { + return start + n <= end; + } + constexpr bool Contains(unsigned i) const noexcept { return i >= start && i < end; } + + /** + * Count the number of items covered by this range. This requires the + * object to be well-formed. + */ + constexpr unsigned Count() const noexcept { + return end - start; + } }; #endif