Skip to content
Permalink
Browse files

regex distance is computed for Pattern.matches(), String.matches(), M…

…atcher.matches() and Matcher.find()
  • Loading branch information...
jgaleotti
jgaleotti committed Sep 18, 2019
1 parent 9835a31 commit 914118fdda809f46f0888d954dfe2c9f6e94d04b
@@ -47,6 +47,16 @@
<artifactId>mockito-core</artifactId>
</dependency>

<!-- Dependencies for Java Regex distances -->
<dependency>
<groupId>net.sf.jgrapht</groupId>
<artifactId>jgrapht</artifactId>
</dependency>
<dependency>
<groupId>dk.brics.automaton</groupId>
<artifactId>automaton</artifactId>
</dependency>

</dependencies>

<build>
@@ -1,7 +1,5 @@
package org.evomaster.client.java.instrumentation.coverage.methodreplacement;

import org.evomaster.client.java.instrumentation.heuristic.Truthness;

public class DistanceHelper {

public static final double H_REACHED_BUT_NULL = 0.05d;
@@ -0,0 +1,41 @@
package org.evomaster.client.java.instrumentation.coverage.methodreplacement;

import org.evomaster.client.java.instrumentation.heuristic.Truthness;
import org.evomaster.client.java.instrumentation.shared.ReplacementType;
import org.evomaster.client.java.instrumentation.shared.StringSpecialization;
import org.evomaster.client.java.instrumentation.shared.StringSpecializationInfo;
import org.evomaster.client.java.instrumentation.staticstate.ExecutionTracer;

import java.util.Objects;
import java.util.regex.Pattern;

public class PatternMatchingHelper {

public static boolean matches(String regex, String input, String idTemplate) {
Objects.requireNonNull(regex);
Objects.requireNonNull(input);

if (ExecutionTracer.isTaintInput(input)) {
ExecutionTracer.addStringSpecialization(input,
new StringSpecializationInfo(StringSpecialization.REGEX, regex));
}

if (idTemplate == null) {
return Pattern.matches(regex, input);
}

boolean matches = Pattern.matches(regex, input);
if (matches) {
ExecutionTracer.executedReplacedMethod(idTemplate,
ReplacementType.BOOLEAN,
new Truthness(1, 0));

} else {
int distance = RegexDistanceUtils.getStandardDistance(input.toString(), regex);
ExecutionTracer.executedReplacedMethod(idTemplate,
ReplacementType.BOOLEAN,
new Truthness(1d / (1d + distance), 1));
}
return matches;
}
}

0 comments on commit 914118f

Please sign in to comment.
You can’t perform that action at this time.