Skip to content

Commit

Permalink
Merge pull request #89 from rjcohn/master
Browse files Browse the repository at this point in the history
Allow legal variable characters to be modified
  • Loading branch information
uklimaschewski authored Jan 7, 2017
2 parents 93c835a + 673ef29 commit 44d255b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
44 changes: 40 additions & 4 deletions src/com/udojava/evalex/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,18 @@ public class Expression {
* The {@link MathContext} to use for calculations.
*/
private MathContext mc = null;

/**

/**
* The characters (other than letters and digits) allowed as the first character in a variable.
*/
private String firstVarChars = "_";

/**
* The characters (other than letters and digits) allowed as the second or subsequent characters in a variable.
*/
private String varChars = "_";

/**
* The original infix expression.
*/
private final String originalExpression;
Expand Down Expand Up @@ -662,8 +672,8 @@ public String next() {
token.append(minusSign);
pos++;
token.append(next());
} else if (Character.isLetter(ch) || (ch == '_')) {
while ((Character.isLetter(ch) || Character.isDigit(ch) || (ch == '_'))
} else if (Character.isLetter(ch) || firstVarChars.indexOf(ch) >= 0) {
while ((Character.isLetter(ch) || Character.isDigit(ch) || varChars.indexOf(ch) >= 0)
&& (pos < input.length())) {
token.append(input.charAt(pos++));
ch = pos == input.length() ? 0 : input.charAt(pos);
Expand Down Expand Up @@ -1280,6 +1290,32 @@ public Expression setRoundingMode(RoundingMode roundingMode) {
return this;
}

/**
* Sets the characters other than letters and digits that are valid as the
* first character of a variable.
*
* @param chars
* The new set of variable characters.
* @return The expression, allows to chain methods.
*/
public Expression setFirstVariableCharacters(String chars) {
this.firstVarChars = chars;
return this;
}

/**
* Sets the characters other than letters and digits that are valid as the
* second and subsequent characters of a variable.
*
* @param chars
* The new set of variable characters.
* @return The expression, allows to chain methods.
*/
public Expression setVariableCharacters(String chars) {
this.varChars = chars;
return this;
}

/**
* Adds an operator to the list of supported operators.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/com/udojava/evalex/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
TestVariables.class, TestBooleans.class, TestCustoms.class,
TestNested.class, TestVarArgs.class, TestSciNotation.class,
TestExposedComponents.class, TestCaseInsensitive.class,
TestLazyIf.class, TestGetUsedVariables.class })
TestLazyIf.class, TestGetUsedVariables.class, TestVariableCharacters.class })
public class AllTests {
}
60 changes: 60 additions & 0 deletions tests/com/udojava/evalex/TestVariableCharacters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.udojava.evalex;

import org.junit.Test;

import java.math.BigDecimal;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TestVariableCharacters {

@Test
public void testBadVarChar() {
String err = "";
try {
Expression expression = new Expression("a.b/2*PI+MIN(e,b)");
expression.eval();
} catch (Expression.ExpressionException e) {
err = e.getMessage();
}
assertEquals("Unknown operator '.' at position 2", err);
}

@Test
public void testAddedVarChar() {
String err = "";
Expression expression;

try {
expression = new Expression("a.b/2*PI+MIN(e,b)").setVariableCharacters("_");
expression.eval();
} catch (Expression.ExpressionException e) {
err = e.getMessage();
}
assertEquals("Unknown operator '.' at position 2", err);

try {
expression = new Expression("a.b/2*PI+MIN(e,b)").setVariableCharacters("_.");
expression.eval();
} catch (Expression.ExpressionException e) {
err = e.getMessage();
}
assertEquals("Unknown operator or function: a.b", err);

expression = new Expression("a.b/2*PI+MIN(e,b)").setVariableCharacters("_.");
assertEquals("5.859875", expression.with("a.b", "2").and("b", "3").eval().toPlainString());

try {
expression = new Expression(".a.b/2*PI+MIN(e,b)").setVariableCharacters("_.");
expression.eval();
} catch (Expression.ExpressionException e) {
err = e.getMessage();
}
assertEquals("Unknown operator '.' at position 1", err);

expression = new Expression("a.b/2*PI+MIN(e,b)").setVariableCharacters("_.").setFirstVariableCharacters(".");
assertEquals("5.859875", expression.with("a.b", "2").and("b", "3").eval().toPlainString());
}
}

0 comments on commit 44d255b

Please sign in to comment.