blob: 733a18ec4aa3d903ba569dabc3d8fde77c0f6c47 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package cgeo.geocaching.utils;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.eclipse.jdt.annotation.NonNull;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Wrapper around the regex {@link Matcher} class. This implementation optimizes the memory usage of the matched
* Strings.
*
*/
public class MatcherWrapper {
private final Matcher matcher;
public MatcherWrapper(@NonNull final Pattern pattern, @NonNull final String input) {
this.matcher = pattern.matcher(input);
}
/**
* see {@link Matcher#find()}
*/
public boolean find() {
return matcher.find();
}
public boolean find(final int start) {
return matcher.find(start);
}
/**
* see {@link Matcher#group(int)}
*/
public String group(final int index) {
return newString(matcher.group(index));
}
/**
* This method explicitly creates a new String instance from an already existing String. This is necessary to avoid
* huge memory leaks in our parser. If we do regular expression matching on large Strings, the returned matches are
* otherwise memory mapped substrings of the huge original String, therefore blocking the garbage collector from
* removing the huge input String.
* <p>
* Do not change this method, even if Findbugs and other tools will report a violation for that line!
*
*/
@SuppressFBWarnings("DM_STRING_CTOR")
private static String newString(final String input) {
if (input == null) {
return null;
}
return new String(input); // DON'T REMOVE THE "new String" HERE!
}
/**
* see {@link Matcher#groupCount()}
*/
public int groupCount() {
return matcher.groupCount();
}
/**
* see {@link Matcher#group()}
*/
public String group() {
return newString(matcher.group());
}
/**
* see {@link Matcher#start()}
*/
public int start() {
return matcher.start();
}
/**
* see {@link Matcher#replaceAll(String)}
*/
public String replaceAll(final String replacement) {
return newString(matcher.replaceAll(replacement));
}
/**
* see {@link Matcher#matches()}
*/
public boolean matches() {
return matcher.matches();
}
/**
* see {@link Matcher#replaceFirst(String)}
*/
public String replaceFirst(final String replacement) {
return newString(matcher.replaceFirst(replacement));
}
}
|