-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds STR_LENGTH, STR_MATCHES and STR_SUBSTRING (#498)
- Loading branch information
Showing
6 changed files
with
218 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/ezylang/evalex/functions/string/StringLengthFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/ezylang/evalex/functions/string/StringMatchesFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/ezylang/evalex/functions/string/StringSubstringFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
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 | ||
&& parameterValues[2].getNumberValue().intValue() | ||
< 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters