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

Fixed StringIndexOutOfBoundsException issue of left and right text functions #285

Merged
merged 1 commit into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,18 @@ private static ExprValue exprSubStr(String str, int start, int len) {
}

private static ExprValue exprRight(ExprValue str, ExprValue len) {
return new ExprStringValue(str.stringValue().substring(
str.stringValue().length() - len.integerValue()));
if (len.integerValue() <= 0) {
return new ExprStringValue("");
}
String stringValue = str.stringValue();
int left = Math.max(stringValue.length() - len.integerValue(), 0);
return new ExprStringValue(str.stringValue().substring(left));
}

private static ExprValue exprLeft(ExprValue expr, ExprValue length) {
return new ExprStringValue(expr.stringValue().substring(0, length.integerValue()));
String stringValue = expr.stringValue();
int right = length.integerValue();
return new ExprStringValue(stringValue.substring(0, Math.min(right, stringValue.length())));
}

private static ExprValue exprAscii(ExprValue expr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ void right() {
assertEquals(STRING, expression.type());
assertEquals("rbar", eval(expression).stringValue());

expression = dsl.right(DSL.literal("foo"), DSL.literal(10));
assertEquals(STRING, expression.type());
assertEquals("foo", eval(expression).value());

expression = dsl.right(DSL.literal("foo"), DSL.literal(0));
assertEquals(STRING, expression.type());
assertEquals("", eval(expression).value());

expression = dsl.right(DSL.literal(""), DSL.literal(10));
assertEquals(STRING, expression.type());
assertEquals("", eval(expression).value());

when(nullRef.type()).thenReturn(STRING);
when(missingRef.type()).thenReturn(INTEGER);
assertEquals(missingValue(), eval(dsl.right(nullRef, missingRef)));
Expand All @@ -308,6 +320,18 @@ void left() {
assertEquals(STRING, expression.type());
assertEquals("hello", eval(expression).stringValue());

expression = dsl.left(DSL.literal("hello"), DSL.literal(10));
assertEquals(STRING, expression.type());
assertEquals("hello", eval(expression).value());

expression = dsl.left(DSL.literal("hello"), DSL.literal(0));
assertEquals(STRING, expression.type());
assertEquals("", eval(expression).value());

expression = dsl.left(DSL.literal(""), DSL.literal(10));
assertEquals(STRING, expression.type());
assertEquals("", eval(expression).value());

when(nullRef.type()).thenReturn(STRING);
when(missingRef.type()).thenReturn(INTEGER);
assertEquals(missingValue(), eval(dsl.left(nullRef, missingRef)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
RIGHT('Hello World', 5) as column
RIGHT('Hello World', 20) as column
RIGHT('Hello World', 0) as column
RIGHT('', 5) as column
LEFT('Hello World', 5) as column
LEFT('Hello World', 20) as column
LEFT('Hello World', 0) as column
LEFT('', 5) as column
ASCII('hello') as column
LOCATE('world', 'helloworld') as column
LOCATE('world', 'hello') as column
Expand Down