Skip to content

Commit

Permalink
fix: comparison of empty strings with like (#1583)
Browse files Browse the repository at this point in the history
* fix: comparison of empty strings with like

* perf: improve QueryHelper

* fix like/ilike with newlines
  • Loading branch information
gramian committed May 8, 2024
1 parent ab1bce0 commit 7872d1a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,82 +23,62 @@ public class QueryHelper {
protected static final char WILDCARD_ANY = '%';

public static boolean like(String currentValue, String value) {
if (currentValue == null || currentValue.isEmpty() || value == null || value.isEmpty())
if (currentValue == null || value == null || (currentValue.isEmpty() ^ value.isEmpty()))
// EMPTY/NULL PARAMETERS
return false;
else if (currentValue.isEmpty() && value.isEmpty())
return true;

value = convertForRegExp(value);
return currentValue.matches(value);
}

public static String convertForRegExp(String value) {
// TODO: NOT EFFICIENT, IT SHOULD BROWSE ONCE AND BUILD THE FINAL RESULT
for (int i = 0; i < value.length(); ) {
final char c = value.charAt(i);

final String replaceWith;
final StringBuilder sb = new StringBuilder("(?s)");

for (int i = 0; i < value.length();++i) {

final char c = value.charAt(i);
switch (c) {
case '\\':
if (i + 1 < value.length()) {
final char next = value.charAt(i + 1);
if (next == '%')
replaceWith = "" + next;
else
replaceWith = "\\\\";
} else
replaceWith = "\\\\";
break;
if (next == WILDCARD_ANY) {
sb.append(next);
++i;
break;
} else if (next == WILDCARD_ANYCHAR) {
sb.append("\\" + next);
++i;
break;
}
}
case '[':
replaceWith = "\\[";
break;
case ']':
replaceWith = "\\]";
break;
case '{':
replaceWith = "\\{";
break;
case '}':
replaceWith = "\\}";
break;
case '(':
replaceWith = "\\(";
break;
case ')':
replaceWith = "\\)";
break;
case '|':
replaceWith = "\\|";
break;
case '*':
replaceWith = "\\*";
break;
case '+':
replaceWith = "\\+";
break;
case '$':
replaceWith = "\\$";
break;
case '^':
replaceWith = "\\^";
break;
case '.':
replaceWith = "\\.";
sb.append("\\" + c);
break;
case WILDCARD_ANYCHAR:
replaceWith = ".";
sb.append(".");
break;
case WILDCARD_ANY:
replaceWith = ".*";
sb.append(".*");
break;

default:
++i;
continue;
sb.append(c);
break;
}

value = value.substring(0, i) + replaceWith + value.substring(i + 1);
i += replaceWith.length();
}
return value;
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ public void test() {
Assertions.assertTrue(op.execute(null, "FOOBAR", "foobar"));
Assertions.assertTrue(op.execute(null, "100%", "100\\%"));
Assertions.assertTrue(op.execute(null, "100%", "100%"));
Assertions.assertTrue(op.execute(null, "", ""));
Assertions.assertTrue(op.execute(null, "100?", "100\\?"));
Assertions.assertTrue(op.execute(null, "100?", "100?"));
Assertions.assertTrue(op.execute(null, "abc\ndef", "%E%"));
}

@Test
public void replaceSpecialCharacters() {
Assertions.assertEquals("\\\\\\[\\]\\{\\}\\(\\)\\|\\*\\+\\$\\^\\...*", QueryHelper.convertForRegExp("\\[]{}()|*+$^.?%"));
Assertions.assertEquals("(?s)\\\\\\[\\]\\{\\}\\(\\)\\|\\*\\+\\$\\^\\...*", QueryHelper.convertForRegExp("\\[]{}()|*+$^.?%"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ public void test() {
Assertions.assertTrue(op.execute(null, "foobar", "foobar"));
Assertions.assertTrue(op.execute(null, "100%", "100\\%"));
Assertions.assertTrue(op.execute(null, "100%", "100%"));
Assertions.assertTrue(op.execute(null, "", ""));
Assertions.assertTrue(op.execute(null, "100?", "100\\?"));
Assertions.assertTrue(op.execute(null, "100?", "100?"));
Assertions.assertTrue(op.execute(null, "abc\ndef", "%e%"));
}

@Test
public void replaceSpecialCharacters() {
Assertions.assertEquals("\\\\\\[\\]\\{\\}\\(\\)\\|\\*\\+\\$\\^\\...*", QueryHelper.convertForRegExp("\\[]{}()|*+$^.?%"));
Assertions.assertEquals("(?s)\\\\\\[\\]\\{\\}\\(\\)\\|\\*\\+\\$\\^\\...*", QueryHelper.convertForRegExp("\\[]{}()|*+$^.?%"));
}
}

0 comments on commit 7872d1a

Please sign in to comment.