Skip to content

Commit fcf1fe9

Browse files
committed
Calling evaluate with empty (or no) parameter will log a "Expression is emtpy." message
This message is retrieved via `te_parser::get_last_error_message()`. Closes #19
1 parent 918e475 commit fcf1fe9

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

tests/tetests.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,25 @@ TEST_CASE("Copy", "[copy]")
10861086
}
10871087
}
10881088

1089+
TEST_CASE("Empty", "[empty]")
1090+
{
1091+
te_parser tep;
1092+
1093+
SECTION("Empty Eval")
1094+
{
1095+
CHECK(std::isnan(tep.evaluate()));
1096+
CHECK_FALSE(tep.success());
1097+
CHECK(std::string{ "Expression is emtpy." } == tep.get_last_error_message());
1098+
}
1099+
1100+
SECTION("Empty String")
1101+
{
1102+
CHECK(std::isnan(tep.evaluate("")));
1103+
CHECK_FALSE(tep.success());
1104+
CHECK(std::string{ "Expression is emtpy." } == tep.get_last_error_message());
1105+
}
1106+
}
1107+
10891108
TEST_CASE("Inf", "[inf]")
10901109
{
10911110
te_parser tep;

tinyexpr.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2484,7 +2484,7 @@ bool te_parser::compile(const std::string_view expression)
24842484
reset_state();
24852485
if (get_list_separator() == get_decimal_separator())
24862486
{
2487-
throw std::runtime_error("List and decimal separators cannot be the same");
2487+
throw std::runtime_error("List and decimal separators cannot be the same.");
24882488
}
24892489
if (expression.empty())
24902490
{
@@ -2561,6 +2561,12 @@ te_type te_parser::evaluate()
25612561
{
25622562
try
25632563
{
2564+
if (m_expression.empty())
2565+
{
2566+
m_parseSuccess = false;
2567+
m_errorPos = 0;
2568+
m_lastErrorMessage = "Expression is emtpy.";
2569+
}
25642570
m_result = (m_compiledExpression != nullptr) ? te_eval(m_compiledExpression) : te_nan;
25652571
}
25662572
catch (const std::exception& expt)
@@ -2583,6 +2589,14 @@ te_parser::evaluate(const std::string_view expression) // NOLINT(-readability-id
25832589
{
25842590
return evaluate();
25852591
}
2592+
2593+
if (expression.empty())
2594+
{
2595+
m_parseSuccess = false;
2596+
m_errorPos = 0;
2597+
m_lastErrorMessage = "Expression is emtpy.";
2598+
}
2599+
25862600
return te_nan;
25872601
}
25882602

0 commit comments

Comments
 (0)