Skip to content

Commit

Permalink
Merge branch 'hotfix/SOE-1069'
Browse files Browse the repository at this point in the history
  • Loading branch information
gquerret committed Sep 11, 2023
2 parents 96d1c05 + 4b1fc1c commit 941b913
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 44 deletions.
16 changes: 8 additions & 8 deletions coverage-report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>coverage-report</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>

<name>JaCoCo report</name>
<description>Fake module for code coverage report in SonarQube</description>
Expand All @@ -20,37 +20,37 @@
<dependency>
<groupId>eu.rssw.openedge.rcode</groupId>
<artifactId>rcode-reader</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>eu.rssw.sonar.openedge</groupId>
<artifactId>sonar-openedge-plugin</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>proparse</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>profiler-parser</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>database-parser</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>listing-parser</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>eu.rssw.openedge.checks</groupId>
<artifactId>openedge-checks</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion database-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>database-parser</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>

<name>OpenEdge database definition lexer and parser</name>
<description>OpenEdge dump files parser</description>
Expand Down
2 changes: 1 addition & 1 deletion listing-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>listing-parser</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>

<name>OpenEdge listing so-called parser</name>
<description>OpenEdge listing files parser</description>
Expand Down
6 changes: 3 additions & 3 deletions openedge-checks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>eu.rssw.openedge.checks</groupId>
<artifactId>openedge-checks</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>

<name>OpenEdge checks</name>
<description>OpenEdge checks</description>
Expand Down Expand Up @@ -55,12 +55,12 @@
<dependency>
<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>database-parser</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>proparse</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
Expand Down
8 changes: 4 additions & 4 deletions openedge-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>eu.rssw.sonar.openedge</groupId>
<artifactId>sonar-openedge-plugin</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
<packaging>sonar-plugin</packaging>

<name>OpenEdge plugin for SonarQube</name>
Expand Down Expand Up @@ -60,17 +60,17 @@
<dependency>
<groupId>eu.rssw.openedge.checks</groupId>
<artifactId>openedge-checks</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>listing-parser</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>profiler-parser</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>eu.rssw</groupId>
<artifactId>sonar-openedge</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
<packaging>pom</packaging>

<name>OpenEdge plugin for SonarQube</name>
Expand Down
2 changes: 1 addition & 1 deletion profiler-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>profiler-parser</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>

<name>OpenEdge profiler output lexer and parser</name>
<description>OpenEdge profiler files parser</description>
Expand Down
4 changes: 2 additions & 2 deletions proparse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>eu.rssw.openedge.parsers</groupId>
<artifactId>proparse</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>

<name>Proparse</name>
<description>ABL code parser</description>
Expand Down Expand Up @@ -55,7 +55,7 @@
<dependency>
<groupId>eu.rssw.openedge.rcode</groupId>
<artifactId>rcode-reader</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
Expand Down
100 changes: 78 additions & 22 deletions proparse/src/main/java/org/prorefactor/proparse/PreproEval.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
********************************************************************************/
package org.prorefactor.proparse;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -68,6 +69,8 @@

public class PreproEval extends PreprocessorParserBaseVisitor<Object> {
private static final Logger LOGGER = LoggerFactory.getLogger(PreproEval.class);
private static final String TILDE_STAR = "\u0005";
private static final String TILDE_DOT = "\u0006";

private final IProparseSettings settings;

Expand Down Expand Up @@ -117,7 +120,7 @@ public Object visitStringOp(StringOpContext ctx) {
Object o2 = visit(ctx.expr(1));

if (ctx.op.getType() == PreprocessorParser.MATCHES) {
return matches(o1, o2);
return matches(getString(o1), getString(o2));
} else {
return getString(o1).toLowerCase().startsWith(getString(o2).toLowerCase());
}
Expand Down Expand Up @@ -673,34 +676,87 @@ static Integer lookup(Object x, Object y, Object z) {
return 0;
}

static Boolean matches(Object y, Object z) {
String a = getString(y).toLowerCase();
String b = getString(z).toLowerCase();
// Completion conditions
if (b.length() == 1 && b.charAt(0) == '*')
return true;
if (a.length() == 0) {
return b.length() == 0;
}
if (b.length() == 0)
public static Boolean matches(String a, String b) {
if ((a == null) || (b == null))
return false;

// Match any single char
if (b.charAt(0) == '.')
// Sanitize input. Escaped stars and dots converted to non-printable character.
a = a.toLowerCase();
b = b.toLowerCase().replace("~~", "~").replace("~*", TILDE_STAR).replace("~.", TILDE_DOT);

// Empty pattern ? True if source is empty
if (b.isEmpty())
return a.isEmpty();

// First character is a dot ? Consume one character
if (b.charAt(0) == '.') {
if (a.isEmpty())
return false;
return matches(a.substring(1), b.substring(1));
}

// Match any number of chars
// First character a star ?
if (b.charAt(0) == '*') {
return matches(a, b.substring(1)) || matches(a.substring(1), b);
}
// Short-circuit if pattern is just a star
if( b.length() == 1)
return true;

// Match an escaped char
if (b.charAt(0) == '~') {
return a.charAt(0) == b.charAt(1) && matches(a.substring(1), b.substring(2));
}
// Consume the star(s) and dot(s), and text coming after that. Then recursive call with the remaining of the string
int offset1 = 1; // Offset of first non-star, non-dot character
int offsetSrc = 0; // Start offset in source string (depends on the number of dots)
while ((offset1 < b.length()) && ((b.charAt(offset1) == '*') || (b.charAt(offset1) == '.'))) {
if (b.charAt(offset1) == '.')
offsetSrc++;
offset1++;
}
int offset2 = offset1 + 1; // Offset of end of alphanumeric string
while ((offset2 < b.length()) && ((b.charAt(offset2) != '*') && (b.charAt(offset2) != '.'))) {
offset2++;
}

// Match a single specific char
return a.charAt(0) == b.charAt(0) && matches(a.substring(1), b.substring(1));
// String to be consumed after the star
String str = "";
if (offset2 > b.length()) {
if (offset1 <= b.length())
str = b.substring(offset1);
} else
str = b.substring(offset1, offset2);

// Empty string ? Then we just need to find at least enough chars for dots
if (str.isEmpty()) {
return a.length() > offsetSrc;
}
// All possible occurences of string to be consumed. We keep the remaining of the string in the list
List<String> occurences = new ArrayList<>();
while (offsetSrc < a.length()) {
int xx = a.indexOf(str, offsetSrc);
if (xx == -1) {
offsetSrc = a.length() + 1;
} else {
occurences.add(a.substring(xx + str.length()));
offsetSrc = xx + 1;
}
}

// If remaining of pattern is valid, then return true
for (String s : occurences) {
if (matches(s, b.substring(offset2)).booleanValue())
return true;
}
// No valid possibility
return false;
} else {
// First character is alphanumeric. Consume until next dot / star, then return matches of remaining string
int starPos = b.indexOf('*');
int dotPos = b.indexOf('.');
if ((starPos == -1) && (dotPos == -1)) {
return a.equals(b.replace(TILDE_STAR, "*").replace(TILDE_DOT, "."));
} else {
int endPos = (starPos != -1) && (dotPos != -1) ? Math.min(starPos, dotPos) : Math.max(starPos, dotPos);
String substr = b.substring(0, endPos).replace(TILDE_STAR, "*").replace(TILDE_DOT, ".");
return a.startsWith(substr) && matches(a.substring(substr.length()), b.substring(substr.length()));
}
}
}

static Integer numentries(Object a, Object b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package org.prorefactor.core;

import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

import java.io.File;

import org.prorefactor.core.util.UnitTestModule;
import org.prorefactor.proparse.PreproEval;
import org.prorefactor.refactor.RefactorSession;
import org.prorefactor.treeparser.ParseUnit;
import org.testng.Assert;
Expand Down Expand Up @@ -410,4 +412,18 @@ public void testQStrings() {
testVariable(unit.getTopNode(), "var81");
}

@Test
public void testMatchesFunction() {
assertTrue(PreproEval.matches("test1,test2,test3", "*test2*"));
assertTrue(PreproEval.matches("test1,test2,test3", "*test2*.."));
assertTrue(PreproEval.matches("test1,test2,test3", "*test2*....."));
assertFalse(PreproEval.matches("test1,test2,test3", "*test2*......."));
assertFalse(PreproEval.matches("test1,test2,test3", "*test2*.......*"));
assertFalse(PreproEval.matches("test1,test2,test3", "*test2.......**"));
assertFalse(PreproEval.matches("test1,test2,test3", "*test2**......."));

assertTrue(PreproEval.matches("xxxfoobarxxx", "*foo*bar*"));
assertFalse(PreproEval.matches("xxxfoobarxxx", "*foo*.*bar*"));
assertTrue(PreproEval.matches("xxxfooxbarxxx", "*foo*.*bar*"));
}
}
2 changes: 1 addition & 1 deletion rcode-reader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>eu.rssw.openedge.rcode</groupId>
<artifactId>rcode-reader</artifactId>
<version>2.22.2</version>
<version>2.22.3</version>

<name>rcode-reader</name>
<description>rcode reader</description>
Expand Down

0 comments on commit 941b913

Please sign in to comment.