From 1cb6e0704a1c4adc4d7ca7255585b154137b018f Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Sun, 24 Jan 2021 13:57:57 +0100 Subject: [PATCH] + enh: Tinyexpr evaluation of math constants "e" and "pi" not being separated from prepending text --- src/Helpers.h | 8 -------- src/Notepad3.c | 2 +- src/tinyexpr/tinyexpr.c | 1 + src/tinyexpr/tinyexpr.h | 44 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/Helpers.h b/src/Helpers.h index 7ef52d282..c98e12ce8 100644 --- a/src/Helpers.h +++ b/src/Helpers.h @@ -720,14 +720,6 @@ inline bool IsAlphaNumericW(const WCHAR ch) { ((ch >= L'A') && (ch <= L'Z')); } -// no encoding for safe chars -inline bool IsIdentifierA(const char ch) { - return ((ch >= '0') && (ch <= '9')) || - ((ch >= 'a') && (ch <= 'z')) || - ((ch >= 'A') && (ch <= 'Z')) || - (ch >= '_'); -} - // If the character is an hexadecimal digit, get its value. inline int GetHexDigitA(const char ch) { diff --git a/src/Notepad3.c b/src/Notepad3.c index f1c7459cb..1e500fbe5 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -2131,7 +2131,7 @@ static bool _EvalTinyExpr(bool qmark) while (*pBegin && exprErr) { dExprEval = te_interp(pBegin, &exprErr); // proceed to next possible expression - while (exprErr && IsIdentifierA(*pBegin++)) {} + while (exprErr && !te_is_op(pBegin++)) {} } FreeMem(lineBuf); diff --git a/src/tinyexpr/tinyexpr.c b/src/tinyexpr/tinyexpr.c index 700632ce9..351ba9fef 100644 --- a/src/tinyexpr/tinyexpr.c +++ b/src/tinyexpr/tinyexpr.c @@ -454,6 +454,7 @@ void next_token(state *s) { } else { /* Look for an operator or special character. */ + /* keep in sync with te_isop(const char ch) defined in header */ switch (s->next++[0]) { case '+': s->type = TOK_INFIX; s->function = get_function_pointer_2d(add); break; case '-': s->type = TOK_INFIX; s->function = get_function_pointer_2d(sub); break; diff --git a/src/tinyexpr/tinyexpr.h b/src/tinyexpr/tinyexpr.h index 8e74c17d0..fad777a67 100644 --- a/src/tinyexpr/tinyexpr.h +++ b/src/tinyexpr/tinyexpr.h @@ -105,6 +105,50 @@ void te_print(const te_expr *n); /* This is safe to call on NULL pointers. */ void te_free(te_expr *n); +/* check for operator or special character. */ +inline int te_is_op(const char* const expr) { + if (!expr) + return !0; + switch (*expr) { + case 0: + case '+': + case '-': + case '*': + case -41: + case '/': + case ':': + case -9: + case '^': + case '%': + case '!': + case '<': + case '>': + case '(': + case ')': + case ',': + case ' ': + case '\t': + case '\n': + case '\r': + return !0; + case '=': + if (expr[1] == '=') + return !0; + break; + case '&': + if (expr[1] == '&') + return !0; + break; + case '|': + if (expr[1] == '|') + return !0; + break; + default: + break; + } + return 0; +} + #ifdef __cplusplus }