Skip to content

Commit

Permalink
Adds functions STR_STARTS_WITH and STR_ENDS_WITH (#450)
Browse files Browse the repository at this point in the history
* Adds functions STR_STARTS_WITH and STR_ENDS_WITH
  • Loading branch information
oswaldobapvicjr authored Mar 24, 2024
1 parent 051d189 commit 9ed8fd8
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 8 deletions.
12 changes: 7 additions & 5 deletions docs/references/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ Available through the _ExpressionConfiguration.StandardFunctionsDictionary_ cons

### String Functions

| Name | Description |
|---------------------------------|-----------------------------------------------------------------------|
| STR_CONTAINS(string, substring) | Returns true, if the string contains the substring (case-insensitive) |
| STR_LOWER(value) | Converts the given value to lower case |
| STR_UPPER(value) | Converts the given value to upper case |
| Name | Description |
|------------------------------------|-----------------------------------------------------------------------|
| STR_CONTAINS(string, substring) | Returns true if the string contains the substring (case-insensitive) |
| STR_ENDS_WITH(string, substring) | Returns true if the string ends with the substring (case-sensitive) |
| STR_LOWER(value) | Converts the given value to lower case |
| STR_STARTS_WITH(string, substring) | Returns true if the string starts with the substring (case-sensitive) |
| STR_UPPER(value) | Converts the given value to upper case |

### Trigonometric Functions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
import com.ezylang.evalex.functions.FunctionIfc;
import com.ezylang.evalex.functions.basic.*;
import com.ezylang.evalex.functions.datetime.*;
import com.ezylang.evalex.functions.string.StringContains;
import com.ezylang.evalex.functions.string.StringLowerFunction;
import com.ezylang.evalex.functions.string.StringUpperFunction;
import com.ezylang.evalex.functions.string.*;
import com.ezylang.evalex.functions.trigonometric.*;
import com.ezylang.evalex.operators.OperatorIfc;
import com.ezylang.evalex.operators.arithmetic.*;
Expand Down Expand Up @@ -182,7 +180,9 @@ public class ExpressionConfiguration {
Map.entry("TANR", new TanRFunction()),
// string functions
Map.entry("STR_CONTAINS", new StringContains()),
Map.entry("STR_ENDS_WITH", new StringEndsWithFunction()),
Map.entry("STR_LOWER", new StringLowerFunction()),
Map.entry("STR_STARTS_WITH", new StringStartsWithFunction()),
Map.entry("STR_UPPER", new StringUpperFunction()),
// date time functions
Map.entry("DT_DATE_NEW", new DateTimeNewFunction()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2012-2024 Udo Klimaschewski
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.ezylang.evalex.functions.string;

import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;

/**
* Returns true if the string ends with the substring (case-sensitive).
*
* @author oswaldobapvicjr
*/
@FunctionParameter(name = "string")
@FunctionParameter(name = "substring")
public class StringEndsWithFunction extends AbstractFunction {
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues) {
String string = parameterValues[0].getStringValue();
String substring = parameterValues[1].getStringValue();
return expression.convertValue(string.endsWith(substring));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2012-2024 Udo Klimaschewski
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.ezylang.evalex.functions.string;

import com.ezylang.evalex.Expression;
import com.ezylang.evalex.data.EvaluationValue;
import com.ezylang.evalex.functions.AbstractFunction;
import com.ezylang.evalex.functions.FunctionParameter;
import com.ezylang.evalex.parser.Token;

/**
* Returns true if the string starts with the substring (case-sensitive).
*
* @author oswaldobapvicjr
*/
@FunctionParameter(name = "string")
@FunctionParameter(name = "substring")
public class StringStartsWithFunction extends AbstractFunction {
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues) {
String string = parameterValues[0].getStringValue();
String substring = parameterValues[1].getStringValue();
return expression.convertValue(string.startsWith(substring));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,36 @@ void testContains(String expression, String expectedResult)
throws EvaluationException, ParseException {
assertExpressionHasExpectedResult(expression, expectedResult);
}

@ParameterizedTest
@CsvSource(
delimiter = ':',
value = {
"STR_STARTS_WITH(\"\", \"\") : true",
"STR_STARTS_WITH(\"a\", \"a\") : true",
"STR_STARTS_WITH(\"Hello World\", \"Hello\") : true",
"STR_STARTS_WITH(\"Hello World\", \"hello\") : false",
"STR_STARTS_WITH(\"Hello world\", \"text\") : false",
"STR_STARTS_WITH(\"\", \"text\") : false"
})
void testStartsWith(String expression, String expectedResult)
throws EvaluationException, ParseException {
assertExpressionHasExpectedResult(expression, expectedResult);
}

@ParameterizedTest
@CsvSource(
delimiter = ':',
value = {
"STR_ENDS_WITH(\"\", \"\") : true",
"STR_ENDS_WITH(\"a\", \"a\") : true",
"STR_ENDS_WITH(\"Hello World\", \"World\") : true",
"STR_ENDS_WITH(\"Hello World\", \"world\") : false",
"STR_ENDS_WITH(\"Hello world\", \"text\") : false",
"STR_ENDS_WITH(\"\", \"text\") : false"
})
void testEndsWith(String expression, String expectedResult)
throws EvaluationException, ParseException {
assertExpressionHasExpectedResult(expression, expectedResult);
}
}

0 comments on commit 9ed8fd8

Please sign in to comment.