Update the boolean reader

This commit is contained in:
Jean Pierre Cimalando 2020-10-05 00:00:15 +02:00
parent 5b4e673fe7
commit 86d0cf3a49
3 changed files with 33 additions and 8 deletions

View file

@ -210,16 +210,18 @@ absl::optional<ValueType> readOpcode(absl::string_view value, const Range<ValueT
absl::optional<bool> readBooleanFromOpcode(const Opcode& opcode)
{
switch (hash(opcode.value)) {
case hash("off"): // fallthrough
case hash("0"):
// Cakewalk-style booleans, case-insensitive
if (absl::EqualsIgnoreCase(opcode.value, "off"))
return false;
case hash("on"): // fallthrough
case hash("1"):
if (absl::EqualsIgnoreCase(opcode.value, "on"))
return true;
default:
return {};
}
// ARIA-style booleans? (seen in egN_dynamic=1 for example)
// TODO check this
if (auto value = readOpcode(opcode.value, Range<int64_t>::wholeRange()))
return *value != 0;
return absl::nullopt;
}
template <class ValueType>

View file

@ -8,6 +8,7 @@
#include "MathHelpers.h"
#include <initializer_list>
#include <type_traits>
#include <limits>
namespace sfz
{
@ -116,6 +117,17 @@ public:
};
}
/**
* @brief Construct a range which covers the whole numeric domain
*/
static constexpr Range<Type> wholeRange() noexcept
{
return Range<Type> {
std::numeric_limits<Type>::min(),
std::numeric_limits<Type>::max(),
};
}
private:
Type _start { static_cast<Type>(0.0) };
Type _end { static_cast<Type>(0.0) };

View file

@ -280,3 +280,14 @@ TEST_CASE("[Opcode] readOpcode")
REQUIRE( !sfz::readOpcode("garbage50.25", sfz::Range<int>(-20, 100)) );
REQUIRE( !sfz::readOpcode("garbage", sfz::Range<int>(-20, 100)) );
}
TEST_CASE("[Opcode] readBooleanFromOpcode")
{
REQUIRE(sfz::readBooleanFromOpcode({"", "1"}) == true);
REQUIRE(sfz::readBooleanFromOpcode({"", "0"}) == false);
REQUIRE(sfz::readBooleanFromOpcode({"", "777"}) == true);
REQUIRE(sfz::readBooleanFromOpcode({"", "on"}) == true);
REQUIRE(sfz::readBooleanFromOpcode({"", "off"}) == false);
REQUIRE(sfz::readBooleanFromOpcode({"", "On"}) == true);
REQUIRE(sfz::readBooleanFromOpcode({"", "oFf"}) == false);
}