From 7d1769dcd3774f658f9d6e9ad25befa6b3865c33 Mon Sep 17 00:00:00 2001 From: FourteenBrush Date: Thu, 20 Jun 2024 11:28:30 +0200 Subject: [PATCH] Make SymbolLookup.indexOrThrow() more efficient --- .../mathexpressionparser/symbol/SymbolLookup.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/me/fourteendoggo/mathexpressionparser/symbol/SymbolLookup.java b/core/src/main/java/me/fourteendoggo/mathexpressionparser/symbol/SymbolLookup.java index a76d9e3..16c3e1c 100644 --- a/core/src/main/java/me/fourteendoggo/mathexpressionparser/symbol/SymbolLookup.java +++ b/core/src/main/java/me/fourteendoggo/mathexpressionparser/symbol/SymbolLookup.java @@ -70,6 +70,7 @@ public class SymbolLookup { * @throws SyntaxException when this symbol is already inserted. */ public void insert(Symbol symbol) { + // TODO: maybe have some "trusted" insert that doesn't validate symbol name again putVal(symbol, true); } @@ -202,10 +203,12 @@ char getCharacter() { } private static int indexOrThrow(char value) { - // TODO: optimize calls - Assert.isTrue(value <= MAX_RANGE_CHAR, "character %s is not allowed in a symbol name", value); - byte idx = indexLookup[value]; - Assert.isTrue(idx != INVALID_IDX, "character %s is not allowed in a symbol name", value); + int idx = -1; // dummy value to make the compiler stop complaining + + Assert.isTrue( + value <= MAX_RANGE_CHAR && (idx = indexLookup[value]) != INVALID_IDX, + "character %s is not allowed in a symbol name", value + ); return idx; }