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
99
100
|
package bjc.everge;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Utility methods for strings.
*
* @author Ben Culkin.
*/
public class StringUtils {
public static boolean isDebug = false;
/**
* Split a string on every occurance of a string not preceeded by an escape.
*
* @param escape
* The escape that stops splitting.
* @param splat
* The string to split on.
* @param inp
* The string to split.
* @return The string split as specified above.
*/
public static String[] escapeSplit(String escape, String splat, String inp) {
/*
* Special case some stuffs.
*/
if (inp == null || inp.equals("")) {
// No input
return new String[] {inp};
}
if (!inp.contains(splat)) {
// Input does not contain any delimiters
return new String[] {inp};
}
if (escape == null || escape.equals("")) {
// No escape, so we can just split normally
return inp.split(Pattern.quote(splat));
}
List<String> ret = new ArrayList<>();
String wrk = inp;
int idx = wrk.indexOf(splat);
if (isDebug) {
System.err.printf("[DEBUG] 'hard' escapeSplit: (%s) (%s) (%s) init: %d\n",
escape, splat, inp, idx);
}
while (idx != -1) {
boolean hasEscape = wrk.regionMatches(idx - 1, escape, 0, escape.length());
while (idx != -1 && hasEscape) {
int oidx = wrk.indexOf(splat, idx + 1);
if (isDebug) {
System.err.printf("[TRACE] idx: %d, oidx: %d\n", idx, oidx);
}
idx = oidx;
hasEscape = wrk.regionMatches(idx - 1, escape, 0, escape.length());
}
if (idx == -1) {
break;
}
if (isDebug) {
System.err.printf("[TRACE] sliced string into (%s) and (%s) at %d\n",
wrk.substring(0, idx), wrk.substring(idx), idx);
}
String tmp = wrk.substring(0, idx);
ret.add(tmp);
if (!tmp.equals("") && wrk.endsWith(tmp)) {
wrk = "";
} else {
wrk = wrk.substring(idx + splat.length());
}
idx = wrk.indexOf(splat);
}
if (isDebug) {
System.err.printf("\t[TRACE] Remnant is (%s) for string (%s)\n", wrk, inp);
}
if (!wrk.equals("")) ret.add(wrk);
return ret.toArray(new String[0]);
}
}
|