Skip to content
Permalink
Browse files
Add a switch to decide which regex engine should be used
  • Loading branch information
FSchumacher committed Mar 5, 2022
1 parent eb7c88c commit e746871640e67b9c5bd6b859ab2f8ef3ab917941
Showing 1 changed file with 46 additions and 14 deletions.
@@ -35,6 +35,7 @@
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;

import javax.swing.table.DefaultTableModel;

@@ -116,6 +117,8 @@ public final class CSVSaveService {

private static final String LINE_SEP = System.getProperty("line.separator"); // $NON-NLS-1$

private static boolean useJavaRegex = JMeterUtils.getPropDefault("jmeter.use_java_regex", false);

/**
* Private constructor to prevent instantiation.
*/
@@ -510,20 +513,9 @@ public static SampleSaveConfiguration getSampleSaveConfiguration(
String delim = null;

if (parts == null) {
Perl5Matcher matcher = JMeterUtils.getMatcher();
PatternMatcherInput input = new PatternMatcherInput(headerLine);
Pattern pattern = JMeterUtils.getPatternCache()
// This assumes the header names are all single words with no spaces
// word followed by 0 or more repeats of (non-word char + word)
// where the non-word char (\2) is the same
// e.g. abc|def|ghi but not abd|def~ghi
.getPattern("\\w+((\\W)\\w+)?(\\2\\w+)*(\\2\"\\w+\")*", // $NON-NLS-1$
// last entries may be quoted strings
Perl5Compiler.READ_ONLY_MASK);
if (matcher.matches(input, pattern)) {
delim = matcher.getMatch().group(2);
parts = splitHeader(headerLine, delim);// now validate the
// result
delim = extractDelimiter(headerLine);
if (delim != null) {
parts = splitHeader(headerLine, delim);// now validate the result
}
}

@@ -557,6 +549,46 @@ public static SampleSaveConfiguration getSampleSaveConfiguration(
return saveConfig;
}

private static String extractDelimiter(String headerLine) {
if (useJavaRegex) {
return extractDelimWithJavaRegex(headerLine);
}
return extractDelimWithOroRegex(headerLine);
}

private static String extractDelimWithJavaRegex(String headerLine) {
java.util.regex.Pattern pattern = java.util.regex.Pattern
// This assumes the header names are all single words with no spaces
// word followed by 0 or more repeats of (non-word char + word)
// where the non-word char (\2) is the same
// e.g. abc|def|ghi but not abd|def~ghi
.compile("\\w+((\\W)\\w+)?(\\2\\w+)*(\\2\"\\w+\")*" // $NON-NLS-1$
// last entries may be quoted strings
);
Matcher matcher = pattern.matcher(headerLine);
if (matcher.matches()) {
return matcher.group(2);
}
return null;
}

private static String extractDelimWithOroRegex(String headerLine) {
Perl5Matcher matcher = JMeterUtils.getMatcher();
PatternMatcherInput input = new PatternMatcherInput(headerLine);
Pattern pattern = JMeterUtils.getPatternCache()
// This assumes the header names are all single words with no spaces
// word followed by 0 or more repeats of (non-word char + word)
// where the non-word char (\2) is the same
// e.g. abc|def|ghi but not abd|def~ghi
.getPattern("\\w+((\\W)\\w+)?(\\2\\w+)*(\\2\"\\w+\")*", // $NON-NLS-1$
// last entries may be quoted strings
Perl5Compiler.READ_ONLY_MASK);
if (matcher.matches(input, pattern)) {
return matcher.getMatch().group(2);
}
return null;
}

private static String[] splitHeader(String headerLine, String delim) {
String[] parts = headerLine.split("\\Q" + delim);// $NON-NLS-1$
int previous = -1;

0 comments on commit e746871

Please sign in to comment.