Skip to content

Commit 82cf585

Browse files
authoredNov 23, 2022
Precompile and reuse regular expressions (#33)
* Bump up JUnit version * Reuse precompiled regex patterns
1 parent a279af3 commit 82cf585

File tree

5 files changed

+27
-26
lines changed

5 files changed

+27
-26
lines changed
 

‎pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
<javadoc.dir>docs/javadoc</javadoc.dir>
6262

63-
<junit.version>4.12</junit.version>
63+
<junit.version>4.13.2</junit.version>
6464

6565
<maven.source.plugin>3.0.1</maven.source.plugin>
6666
<maven.javadoc.plugin>3.2.0</maven.javadoc.plugin>

‎src/main/java/io/github/cdimascio/dotenv/Dotenv.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static Dotenv load() {
4545
Set<DotenvEntry> entries();
4646

4747
/**
48-
* Returns the set of {@link DotenvEntry}s matching the the filter
48+
* Returns the set of {@link DotenvEntry}s matching the filter
4949
* @param filter the filter e.g. {@link Dotenv.Filter}
5050
* @return the set of {@link DotenvEntry}s for environment variables matching the {@link Dotenv.Filter}
5151
*/

‎src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ static class DotenvImpl implements Dotenv {
8585
private final Map<String, String> envVars;
8686
private final Set<DotenvEntry> set;
8787
private final Set<DotenvEntry> setInFile;
88-
private final Map<String, String> envVarsInFile;
8988
public DotenvImpl(List<DotenvEntry> envVars) {
90-
this.envVarsInFile = envVars.stream().collect(toMap(DotenvEntry::getKey, DotenvEntry::getValue));
91-
this.envVars = new HashMap<>(this.envVarsInFile);
92-
System.getenv().forEach(this.envVars::put);
89+
Map<String, String> envVarsInFile = envVars.stream()
90+
.collect(toMap(DotenvEntry::getKey, DotenvEntry::getValue));
91+
this.envVars = new HashMap<>(envVarsInFile);
92+
this.envVars.putAll(System.getenv());
9393

94-
this.set =this.envVars.entrySet().stream()
94+
this.set = this.envVars.entrySet().stream()
9595
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
9696
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
9797

98-
this.setInFile =this.envVarsInFile.entrySet().stream()
98+
this.setInFile = envVarsInFile.entrySet().stream()
9999
.map(it -> new DotenvEntry(it.getKey(), it.getValue()))
100100
.collect(collectingAndThen(toSet(), Collections::unmodifiableSet));
101101
}

‎src/main/java/io/github/cdimascio/dotenv/internal/DotenvParser.java

+18-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88
import java.util.List;
99
import java.util.function.Function;
10+
import java.util.function.Predicate;
1011
import java.util.regex.Matcher;
1112
import java.util.regex.Pattern;
1213

@@ -16,14 +17,18 @@
1617
* (Internal) Parses .env file
1718
*/
1819
public class DotenvParser {
20+
21+
private static final Pattern WHITE_SPACE_REGEX = Pattern.compile("^\\s*$"); // ^\s*${'$'}
22+
private static final Pattern DOTENV_ENTRY_REGEX = Pattern.compile("^\\s*([\\w.\\-]+)\\s*(=)\\s*(.*)?\\s*$"); // ^\s*([\w.\-]+)\s*(=)\s*(.*)?\s*$
23+
1924
private final DotenvReader reader;
2025
private final boolean throwIfMissing;
2126
private final boolean throwIfMalformed;
2227

23-
private final Function<String, Boolean> isWhiteSpace = s -> matches("^\\s*$", s); // ^\s*${'$'}
24-
private final Function<String, Boolean> isComment = s -> s.startsWith("#") || s.startsWith("////");
25-
private final Function<String, Boolean> isQuoted = s -> s.startsWith("\"") && s.endsWith("\"");
26-
private final Function<String, DotenvEntry> parseLine = s -> matchEntry("^\\s*([\\w.\\-]+)\\s*(=)\\s*(.*)?\\s*$", s); // ^\s*([\w.\-]+)\s*(=)\s*(.*)?\s*$
28+
private final Predicate<String> isWhiteSpace = s -> matches(WHITE_SPACE_REGEX, s);
29+
private final Predicate<String> isComment = s -> s.startsWith("#") || s.startsWith("////");
30+
private final Predicate<String> isQuoted = s -> s.startsWith("\"") && s.endsWith("\"");
31+
private final Function<String, DotenvEntry> parseLine = s -> matchEntry(DOTENV_ENTRY_REGEX, s);
2732

2833
/**
2934
* Creates a dotenv parser
@@ -46,11 +51,11 @@ public List<DotenvEntry> parse() throws DotenvException {
4651
List<DotenvEntry> entries = new ArrayList<>();
4752
for (String line : lines()) {
4853
String l = line.trim();
49-
if (isWhiteSpace.apply(l) || isComment.apply(l) || isBlank(l)) continue;
54+
if (isWhiteSpace.test(l) || isComment.test(l) || isBlank(l)) continue;
5055

5156
DotenvEntry entry = parseLine.apply(l);
5257
if (entry == null) {
53-
if (throwIfMalformed) throw new DotenvException("Malformed entry "+ l);
58+
if (throwIfMalformed) throw new DotenvException("Malformed entry " + l);
5459
continue;
5560
}
5661
String key = entry.getKey();
@@ -73,26 +78,24 @@ private List<String> lines() throws DotenvException {
7378

7479
private String normalizeValue(String value) {
7580
String tr = value.trim();
76-
return isQuoted.apply(tr)
77-
? tr.substring(1, value.length() -1)
81+
return isQuoted.test(tr)
82+
? tr.substring(1, value.length() - 1)
7883
: tr;
7984
}
8085

81-
private static boolean matches(String regex, String text) {
82-
Pattern pattern = Pattern.compile(regex);
83-
Matcher matcher = pattern.matcher(text);
86+
private static boolean matches(Pattern regex, String text) {
87+
Matcher matcher = regex.matcher(text);
8488
return matcher.matches();
8589
}
8690

87-
private static DotenvEntry matchEntry(String regex, String text) {
88-
Pattern pattern = Pattern.compile(regex);
89-
Matcher matcher = pattern.matcher(text);
91+
private static DotenvEntry matchEntry(Pattern regex, String text) {
92+
Matcher matcher = regex.matcher(text);
9093
boolean result = matcher.matches();
9194
if (!result || matcher.groupCount() < 3) return null;
9295
return new DotenvEntry(matcher.group(1), matcher.group(3));
9396
}
9497

95-
private boolean isBlank(String s) {
98+
private static boolean isBlank(String s) {
9699
return s == null || s.trim().isEmpty();
97100
}
98101
}

‎src/main/java/io/github/cdimascio/dotenv/internal/DotenvReader.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ public List<String> read() throws DotenvException, IOException {
4747
: Paths.get(location);
4848

4949
if (Files.exists(path)) {
50-
return Files
51-
.lines(path)
52-
.collect(Collectors.toList());
50+
return Files.readAllLines(path);
5351
}
5452

5553
try {

0 commit comments

Comments
 (0)
Please sign in to comment.