diff --git a/include/bx/bx.h b/include/bx/bx.h index fa2935d..c67d939 100644 --- a/include/bx/bx.h +++ b/include/bx/bx.h @@ -251,6 +251,10 @@ namespace bx template constexpr Ty clamp(const Ty& _a, const TypeIdentityType& _min, const TypeIdentityType& _max); + /// Returns true if value `_a` is in inclusive range [`_min`, `_max`]. + template + constexpr bool isInRange(const Ty& _a, const TypeIdentityType& _min, const TypeIdentityType& _max); + /// Returns true if value `_a` is power of 2. template constexpr bool isPowerOf2(Ty _a); diff --git a/include/bx/inline/bx.inl b/include/bx/inline/bx.inl index 759a9f2..681bc78 100644 --- a/include/bx/inline/bx.inl +++ b/include/bx/inline/bx.inl @@ -216,6 +216,12 @@ namespace bx return max(min(_a, _max), _min); } + template + inline constexpr bool isInRange(const Ty& _a, const TypeIdentityType& _min, const TypeIdentityType& _max) + { + return _min <= _a && _a <= _max; + } + template inline constexpr bool isPowerOf2(Ty _a) { diff --git a/include/bx/inline/string.inl b/include/bx/inline/string.inl index 9aaa988..cf5a811 100644 --- a/include/bx/inline/string.inl +++ b/include/bx/inline/string.inl @@ -450,11 +450,6 @@ namespace bx return StringView(m_ptr, m_len); } - inline BX_CONSTEXPR_FUNC bool isInRange(char _ch, char _from, char _to) - { - return unsigned(_ch - _from) <= unsigned(_to-_from); - } - inline BX_CONSTEXPR_FUNC bool isSpace(char _ch) { return ' ' == _ch // Space. diff --git a/tests/bx_test.cpp b/tests/bx_test.cpp index 9781707..ea06eeb 100644 --- a/tests/bx_test.cpp +++ b/tests/bx_test.cpp @@ -36,6 +36,23 @@ TEST_CASE("gcd", "[bx]") REQUIRE(9 == bx::gcd(18u, 81u) ); } +TEST_CASE("isInRange", "[bx]") +{ + REQUIRE( bx::isInRange(5, 0, 10) ); + REQUIRE( bx::isInRange(0, 0, 10) ); + REQUIRE( bx::isInRange(10, 0, 10) ); + REQUIRE(!bx::isInRange(-1, 0, 10) ); + REQUIRE(!bx::isInRange(11, 0, 10) ); + REQUIRE( bx::isInRange(uint8_t(255), 0, 255) ); + REQUIRE(!bx::isInRange(uint8_t(0), 1, 255) ); + REQUIRE( bx::isInRange('m', 'a', 'z') ); + REQUIRE(!bx::isInRange('M', 'a', 'z') ); + REQUIRE( bx::isInRange(0.5f, 0.0f, 1.0f) ); + REQUIRE(!bx::isInRange(1.5f, 0.0f, 1.0f) ); + + STATIC_REQUIRE(bx::isInRange(3, 1, 3) ); +} + TEST_CASE("lcm", "[bx]") { REQUIRE(1157 == bx::lcm(13u, 89u) );