diff --git a/src/sfizz/utility/StringViewHelpers.h b/src/sfizz/utility/StringViewHelpers.h index b865f8a3..278833c1 100644 --- a/src/sfizz/utility/StringViewHelpers.h +++ b/src/sfizz/utility/StringViewHelpers.h @@ -179,6 +179,17 @@ bool readLeadingFloat(absl::string_view input, F* result, absl::string_view* res ++numberEnd; } + if (numberEnd < input.size() && input[numberEnd] == 'e') { + ++numberEnd; + + if (numberEnd < input.size() && + (input[numberEnd] == '+' || input[numberEnd] == '-' || absl::ascii_isdigit(input[numberEnd]))) + ++numberEnd; + + while (numberEnd < input.size() && absl::ascii_isdigit(input[numberEnd])) + ++numberEnd; + } + if (!readFloat(input.substr(0, numberEnd), result)) return false; diff --git a/tests/OpcodeT.cpp b/tests/OpcodeT.cpp index ac0d3274..5423d9af 100644 --- a/tests/OpcodeT.cpp +++ b/tests/OpcodeT.cpp @@ -498,6 +498,62 @@ TEST_CASE("[Opcode] opcode read (float)") REQUIRE( opcode.read(spec) == 10.5f ); } + SECTION("Exponential notation") + { + Opcode opcode { "", "1.05e+001" }; + OpcodeSpec spec { 0.0f, Range(0.0f, 100.0f), kEnforceLowerBound }; + REQUIRE( opcode.read(spec) == 10.5f ); + } + + SECTION("Exponential notation") + { + Opcode opcode { "", "1.05e+1" }; + OpcodeSpec spec { 0.0f, Range(0.0f, 100.0f), kEnforceLowerBound }; + REQUIRE( opcode.read(spec) == 10.5f ); + } + + SECTION("Exponential notation") + { + Opcode opcode { "", "1.05e1" }; + OpcodeSpec spec { 0.0f, Range(0.0f, 100.0f), kEnforceLowerBound }; + REQUIRE( opcode.read(spec) == 10.5f ); + } + + SECTION("Exponential notation") + { + Opcode opcode { "", "1e1" }; + OpcodeSpec spec { 0.0f, Range(0.0f, 100.0f), kEnforceLowerBound }; + REQUIRE( opcode.read(spec) == 10.0f ); + } + + SECTION("Exponential notation") + { + Opcode opcode { "", "1.5e-001" }; + OpcodeSpec spec { 0.0f, Range(0.0f, 100.0f), kEnforceLowerBound }; + REQUIRE( opcode.read(spec) == 0.15f ); + } + + SECTION("Exponential notation") + { + Opcode opcode { "", "1.5e-1" }; + OpcodeSpec spec { 0.0f, Range(0.0f, 100.0f), kEnforceLowerBound }; + REQUIRE( opcode.read(spec) == 0.15f ); + } + + SECTION("Exponential notation") + { + Opcode opcode { "", "1e-1" }; + OpcodeSpec spec { 0.0f, Range(0.0f, 100.0f), kEnforceLowerBound }; + REQUIRE( opcode.read(spec) == 0.1f ); + } + + SECTION("Exponential notation") + { + Opcode opcode { "", "6.02385e-009" }; + OpcodeSpec spec { 0.0f, Range(0.0f, 100.0f), kEnforceLowerBound }; + REQUIRE( opcode.read(spec) == 6.02385e-9f ); + } + SECTION("Text before") { Opcode opcode { "", "garbage10" };