forked from charles-wangkai/old_topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArgParser.java
More file actions
67 lines (66 loc) · 1.34 KB
/
ArgParser.java
File metadata and controls
67 lines (66 loc) · 1.34 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
import java.util.ArrayList;
public class ArgParser {
public String[] parse(String input) {
ArrayList<String> result = new ArrayList<String>();
boolean valid=true;
int length=input.length();
if (length<2 || input.charAt(0)!='{' || input.charAt(length-1)!='}') {
valid=false;
}
else {
StringBuffer temp=new StringBuffer();
int pos=1;
while (pos<=length-2) {
char ch=input.charAt(pos);
if (ch==',') {
if (pos+1>length-2 || input.charAt(pos+1)!=' ') {
valid=false;
break;
}
result.add(temp.toString());
temp=new StringBuffer();
pos+=2;
}
else if (ch=='\\') {
if (pos+1<=length-2) {
char ch1=input.charAt(pos+1);
if (ch1==',' || ch1=='{' || ch1=='}') {
temp.append(ch1);
pos+=2;
}
else {
temp.append('\\');
pos++;
}
}
else {
temp.append('\\');
pos++;
}
}
else if (ch=='{' || ch=='}') {
valid=false;
break;
}
else {
temp.append(ch);
pos++;
}
}
if (valid) {
if (temp.toString().endsWith("\\")) {
valid=false;
}
else {
result.add(temp.toString());
}
}
}
if (valid) {
return result.toArray(new String[0]);
}
else {
return new String[]{"INVALID"};
}
}
}