-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBraceExpansion.java
More file actions
134 lines (127 loc) · 3.26 KB
/
BraceExpansion.java
File metadata and controls
134 lines (127 loc) · 3.26 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
public class BraceExpansion {
public static String expand(String str) {
if (null == str || str.equals("") || str.trim().equals("")){
return "";
}
int o,p;
if ((o = str.indexOf('{')) == -1 || (p = str.indexOf('}')) == -1 || o > p) {
return str;
}
int join = str.indexOf("..");
StringBuilder output = new StringBuilder();
char c;
if (join != -1 && o < join && p > join) {
String st = str.substring(o+1, join);
String en = str.substring(join+2, p);
int s=-1,e=-1;
try {
s = Integer.parseInt(st);
e = Integer.parseInt(en);
StringBuilder seq = new StringBuilder();
seq.append(str.substring(0, o));
seq.append(sequence(s, e));
seq.append(str.substring(p+1));
str = seq.toString();
} catch (NumberFormatException e1) {
if (st.length() == 1 && en.length() == 1
&& Character.isLetter(st.charAt(0)) && Character.isLetter(en.charAt(0))) {
StringBuilder seq = new StringBuilder();
seq.append(str.substring(0, o));
seq.append(sequence(st.charAt(0), en.charAt(0)));
seq.append(str.substring(p+1));
str = seq.toString();
}
}
}
o = str.indexOf('{');
p = str.indexOf('}');
if (o < (join=str.indexOf(',')) && p > join){
if (p == str.length()-1)
join = -1;
if (join == -1) {
for (int i = 1; i < str.length() -1; i++) {
c = str.charAt(i);
if (c == ',')
output.append(' ');
else
output.append(c);
}
} else {
join = p+1;
String joine = str.substring(join, str.indexOf("{", 1) < 0 ? str.length() : str.indexOf("{", 1));
if (joine.length() + join == str.length()) {
output.append(expand(str.substring(1, join-1), joine, " "));
} else {
output.append(expand(str.substring(1, join-1), joine, str.substring(str.indexOf('{', 1), str.length())));
}
}
} else {
output.append(str);
}
return output.toString();
}
private static String expand(String p1, String j, String p2) {
StringBuilder output = new StringBuilder();
char c='\0', b='\0';
StringBuilder sbp1;
StringBuilder sbp2;
int i, h=0;
p2 = expand(p2);
do {
sbp1 = new StringBuilder();
while (h < p1.length() && (c = p1.charAt(h++)) != ',') {
sbp1.append(c);
}
i = 0;
do {
sbp2 = new StringBuilder();
while (i < p2.length() && (b = p2.charAt(i++)) != ' ') {
sbp2.append(b);
}
output.append(sbp1);
output.append(j);
output.append(sbp2);
if (b == ' ')
output.append(' ');
} while (i < p2.length());
if (c == ',')
output.append(' ');
} while (h < p1.length());
return output.toString();
}
private static StringBuilder sequence(int s, int e) {
StringBuilder output = new StringBuilder();
output.append('{');
while (s < e) {
output.append(s);
output.append(',');
++s;
}
output.append(s);
output.append('}');
return output;
}
private static StringBuilder sequence(char s, char e) {
StringBuilder output = new StringBuilder();
output.append('{');
if (s < e) {
while (s < e) {
if (s != '\\')
output.append((char)s);
output.append(',');
++s;
}
output.append((char)s);
} else {
while (s > e) {
if (s != '\\')
output.append((char)s);
output.append(',');
--s;
}
output.append((char)s);
}
output.append('}');
return output;
}
}