forked from pxu/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveCodeComments.java
More file actions
30 lines (30 loc) · 1.14 KB
/
RemoveCodeComments.java
File metadata and controls
30 lines (30 loc) · 1.14 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
public class RemoveCodeComments {
public List<String> removeComments(String[] source) {
boolean inBlock = false;
StringBuilder newline = new StringBuilder();
List<String> ans = new ArrayList();
for (String line: source) {
int i = 0;
char[] chars = line.toCharArray();
if (!inBlock) newline = new StringBuilder();
while (i < line.length()) {
if (!inBlock && i+1 < line.length() && chars[i] == '/' && chars[i+1] == '*') {
inBlock = true;
i++;
} else if (inBlock && i+1 < line.length() && chars[i] == '*' && chars[i+1] == '/') {
inBlock = false;
i++;
} else if (!inBlock && i+1 < line.length() && chars[i] == '/' && chars[i+1] == '/') {
break;
} else if (!inBlock) {
newline.append(chars[i]);
}
i++;
}
if (!inBlock && newline.length() > 0) {
ans.add(new String(newline));
}
}
return ans;
}
}