-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.java
More file actions
139 lines (123 loc) · 3.96 KB
/
Code.java
File metadata and controls
139 lines (123 loc) · 3.96 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
135
136
137
138
139
package Code;
import java.util.*;
import java.io.*;
public class Code{
private ArrayList<String> code;
private final String DOUBLE_SLASH;
private final String SLASH_ASTERISK;
private final String ASTERISK_SLASH;
public Code () throws Exception{
DOUBLE_SLASH = "//";
SLASH_ASTERISK = "/*";
ASTERISK_SLASH = "*/";
code = new ArrayList<>();
}
public void loadFromFile (String fileName) throws Exception{
code.clear();
BufferedReader br = new BufferedReader(new FileReader(fileName));
String s = br.readLine();
while (s != null){
code.add(s);
s = br.readLine();
}
}
public void saveToFile (String fileName) throws Exception{
BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));
for (String item : code){
bw.write(item);
bw.write("\n");
}
bw.close();
}
public void delComments (){
int position;
int newPosition;
for (int i = 0; i < code.size(); i++){
position = 0;
while ((newPosition = code.get(i).indexOf(DOUBLE_SLASH, position)) != -1){
delLine(i, newPosition);
position = newPosition + 1;
}
position = 0;
while ((newPosition = code.get(i).indexOf(SLASH_ASTERISK, position)) != -1){
position = delBlock(i, newPosition);
}
}
}
private int delBlock (int line, int position){
String res;
if (!isInString(line, position)){
int endLine = code.size() - 1;
int endPos = 0;
for (int i = line; i < code.size(); i++){
if ((endPos = code.get(i).indexOf(ASTERISK_SLASH, position + 1)) != -1 && !isInString(i, endPos)){
endLine = i;
break;
}
}
res = code.get(line).substring(0, position);
if (line == endLine){
res += code.get(line).substring(endPos + 2);
code.remove(line);
}else{
code.add(line, res);
for (int i = line + 1; i < endLine + 1; i++){
code.remove(line + 1);
}
res = code.remove(line + 1).substring(endPos + 2);
if (code.get(line).matches("^[ \t\uFEFF]*$")){
code.remove(line);
}
}
if (!res.matches("^[ \t\uFEFF]*$")){
code.add(line, res);
}
}
return position + 1;
}
private boolean delLine (int line, int position){
if (isInString(line, position)){
return false;
}
char[] chars = code.get(line).toCharArray();
char[] newChars = new char[position];
String res;
for (int i = 0; i < position; i++){
newChars[i] = chars[i];
}
code.remove(line);
res = new String(newChars);
if (!res.matches("^[ \t\uFEFF]*$")){
code.add(line, res);
}
return true;
}
private boolean isInString (int line, int position){
boolean isStr;
char[] chars = code.get(line).toCharArray();
int numS = 0;
for (int i = 0; i < position; i++){
if (chars[i] == '"'){
int numSl = 0;
isStr = true;
int j = i - 1;
while (j >= 0 && chars[j] == '\\'){
numSl++;
j--;
}
if (numSl % 2 != 0){
isStr = false;
}
if (i > 0 && i < position - 1){
if (chars[i - 1] == '\'' && chars[i + 1] == '\''){
isStr = false;
}
}
if (isStr){
numS++;
}
}
}
return numS % 2 != 0;
}
}