Skip to content

Commit e4ef87b

Browse files
committed
Don't use regex to match quoted variable names
1 parent 1f333d5 commit e4ef87b

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/main/java/ch/njol/skript/variables/FlatFileStorage.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ static byte[] decode(String hex) {
531531
/**
532532
* A regex pattern of a line in a CSV file.
533533
*/
534-
private static final Pattern CSV_LINE_PATTERN = Pattern.compile("(?<=^|,)\\s*([^\",]*|\"([^\"]|\"\")*\")\\s*(,|$)");
534+
private static final Pattern CSV_LINE_PATTERN = Pattern.compile("(?<=^|,)\\s*([^\",]*|\"([^\"]|\"\")*\")\\s*(,|$)");
535535

536536
/**
537537
* Splits the given CSV line into its values.
@@ -543,12 +543,29 @@ static byte[] decode(String hex) {
543543
*/
544544
@Nullable
545545
static String[] splitCSV(String line) {
546-
Matcher matcher = CSV_LINE_PATTERN.matcher(line);
546+
547547

548548
int lastEnd = 0;
549549
ArrayList<String> result = new ArrayList<>();
550550

551-
while (matcher.find()) {
551+
// avoid regex matching quoted var names
552+
if (line.startsWith("\"")) {
553+
// find closing quote '",'
554+
int start = 0;
555+
while (start < line.length() && (start == 0 || line.charAt(start - 1) == '"')) {
556+
start = line.indexOf("\",", start + 2);
557+
if (start == -1) {
558+
// No closing quote found
559+
return null;
560+
}
561+
}
562+
result.add(line.substring(1, start).replace("\"\"", "\""));
563+
lastEnd = start + 2;
564+
}
565+
566+
Matcher matcher = CSV_LINE_PATTERN.matcher(line);
567+
568+
while (matcher.find(lastEnd)) {
552569
if (lastEnd != matcher.start())
553570
return null; // other stuff inbetween finds
554571

0 commit comments

Comments
 (0)