blob: df9ce7097a3c3707e24edabd01a8e7a12d7f74d0 (
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
|
package bjc.utils.parserutils.splitter;
import java.util.regex.Pattern;
import bjc.funcdata.ListEx;
import bjc.functypes.ID;
import bjc.utils.ioutils.RegexStringEditor;
/**
* Splits a string into pieces around a regular expression.
*
* @author EVE
*
*/
public class SimpleTokenSplitter implements TokenSplitter {
/**
* The pattern to split on.
*/
protected Pattern spliter;
/**
* Whether or not to keep the delimiters.
*/
protected final boolean keepDelim;
/**
* Create a new simple token splitter.
*
* @param splitter
* The pattern to split around.
*
* @param keepDelims
* Whether or not delimiters should be kept.
*/
public SimpleTokenSplitter(final Pattern splitter, final boolean keepDelims) {
spliter = splitter;
keepDelim = keepDelims;
}
@Override
public ListEx<String> split(final String input) {
if (keepDelim) {
return RegexStringEditor.mapOccurances(input, spliter, ID.id(), ID.id());
}
return RegexStringEditor.mapOccurances(input, spliter, ID.id(), strang -> "");
}
@Override
public String toString() {
return String.format("SimpleTokenSplitter [spliter=%s, keepDelim=%s]", spliter,
keepDelim);
}
}
|