Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add STR_LENGTH, STR_MATCHES and STR_SUBSTRING #498

Merged
merged 7 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions docs/references/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ 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_ENDS_WITH(string, substring) | Returns true if the string ends with the substring (case-sensitive) |
| STR_FORMAT(format [,argument, ...]) | Returns a formatted string using the specified format string and arguments, using the configured locale |
| 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_TRIM(string) | Returns the given string with all leading and trailing space removed. |
| 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_FORMAT(format [,argument, ...]) | Returns a formatted string using the specified format string and arguments, using the configured locale |
| 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_TRIM(string) | Returns the given string with all leading and trailing space removed. |
| STR_UPPER(value) | Converts the given value to upper case |
| STR_LENGTH(string) | Returns the length of the string |
| STR_MATCHES(string, pattern) | Returns true if the string matches the RegEx pattern |
| STR_SUBSTRING(string, start[, end]) | Returns a substring of the given string, starting at the _start_ index and ending at the _end_ index (the end of the string if not specified) |
HSGamer marked this conversation as resolved.
Show resolved Hide resolved

### Trigonometric Functions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ public class ExpressionConfiguration {
Map.entry("STR_STARTS_WITH", new StringStartsWithFunction()),
Map.entry("STR_TRIM", new StringTrimFunction()),
Map.entry("STR_UPPER", new StringUpperFunction()),
Map.entry("STR_LENGTH", new StringLengthFunction()),
Map.entry("STR_MATCHES", new StringMatchesFunction()),
Map.entry("STR_SUBSTRING", new StringSubstringFunction()),
HSGamer marked this conversation as resolved.
Show resolved Hide resolved
// date time functions
Map.entry("DT_DATE_NEW", new DateTimeNewFunction()),
Map.entry("DT_DATE_PARSE", new DateTimeParseFunction()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
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.EvaluationException;
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 the length of the string.
*
* @author HSGamer
*/
@FunctionParameter(name = "string")
public class StringLengthFunction extends AbstractFunction {
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues)
throws EvaluationException {
return expression.convertValue(parameterValues[0].getStringValue().length());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
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.EvaluationException;
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 matches the pattern.
*
* @author HSGamer
*/
@FunctionParameter(name = "string")
@FunctionParameter(name = "pattern")
public class StringMatchesFunction extends AbstractFunction {
@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues)
throws EvaluationException {
String string = parameterValues[0].getStringValue();
String pattern = parameterValues[1].getStringValue();
return expression.convertValue(string.matches(pattern));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
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.EvaluationException;
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 a substring of a string.
*
* @author HSGamer
*/
@FunctionParameter(name = "string")
@FunctionParameter(name = "start", nonNegative = true)
@FunctionParameter(name = "end", isVarArg = true, nonNegative = true)
public class StringSubstringFunction extends AbstractFunction {
@Override
public void validatePreEvaluation(Token token, EvaluationValue... parameterValues)
throws EvaluationException {
super.validatePreEvaluation(token, parameterValues);
if (parameterValues.length > 2) {
if (parameterValues[2].getNumberValue().intValue()
HSGamer marked this conversation as resolved.
Show resolved Hide resolved
< parameterValues[1].getNumberValue().intValue()) {
throw new EvaluationException(
token, "End index must be greater than or equal to start index");
}
}
}

@Override
public EvaluationValue evaluate(
Expression expression, Token functionToken, EvaluationValue... parameterValues)
throws EvaluationException {
String string = parameterValues[0].getStringValue();
int start = parameterValues[1].getNumberValue().intValue();
String result;
if (parameterValues.length > 2) {
int end = parameterValues[2].getNumberValue().intValue();
int length = string.length();
int finalEnd = Math.min(end, length);
result = string.substring(start, finalEnd);
} else {
result = string.substring(start);
}
return expression.convertValue(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package com.ezylang.evalex.functions.string;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.ezylang.evalex.BaseEvaluationTest;
import com.ezylang.evalex.EvaluationException;
import com.ezylang.evalex.config.TestConfigurationProvider;
import com.ezylang.evalex.parser.ParseException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

Expand Down Expand Up @@ -165,4 +168,62 @@ void testTrimString(String expression, String expectedResult)
throws EvaluationException, ParseException {
assertExpressionHasExpectedResult(expression, expectedResult);
}

@ParameterizedTest
@CsvSource(
delimiter = ':',
value = {
"STR_LENGTH(\"\") : 0",
"STR_LENGTH(\"a\") : 1",
"STR_LENGTH(\"AbCdEf\") : 6",
"STR_LENGTH(\"A1b3C4/?\") : 8",
"STR_LENGTH(\"äöüß\") : 4"
})
void testLength(String expression, String expectedResult)
throws EvaluationException, ParseException {
assertExpressionHasExpectedResult(expression, expectedResult);
}

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

@ParameterizedTest
@CsvSource(
delimiter = ':',
value = {
"STR_SUBSTRING(\"\", 0, 0) : ''",
"STR_SUBSTRING(\"Hello World\", 0) : Hello World",
"STR_SUBSTRING(\"Hello World\", 6) : World",
"STR_SUBSTRING(\"Hello World\", 0, 5) : Hello",
"STR_SUBSTRING(\"Hello World\", 6, 11) : World",
"STR_SUBSTRING(\"Hello World\", 6, 6) : ''",
"STR_SUBSTRING(\"Hello World\", 0, 11) : Hello World",
"STR_SUBSTRING(\"Hello World\", 0, 12) : Hello World",
})
void testSubstring(String expression, String expectedResult)
throws EvaluationException, ParseException {
assertExpressionHasExpectedResult(expression, expectedResult);
}

@Test
void testSubstringEndLessThanStart() {
assertThatThrownBy(() -> evaluate("STR_SUBSTRING(\"Hello World\", 6, 5)"))
.isInstanceOf(EvaluationException.class)
.hasMessage("End index must be greater than or equal to start index");
}
}