Skip to content

Commit ec4fd41

Browse files
committed
properly merged #20
1 parent a63e785 commit ec4fd41

File tree

1 file changed

+97
-7
lines changed

1 file changed

+97
-7
lines changed

src/main/java/net/minecraftforge/gradle/sourcemanip/McpCleanup.java

Lines changed: 97 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,116 @@
11
package net.minecraftforge.gradle.sourcemanip;
22

3-
import net.minecraftforge.gradle.common.Constants;
4-
3+
import java.io.StringReader;
4+
import java.io.StringWriter;
55
import java.util.regex.Matcher;
66
import java.util.regex.Pattern;
77

8+
import net.minecraftforge.gradle.common.Constants;
9+
810
public class McpCleanup
911
{
10-
public static final Pattern COMMENTS_COMMENTS = Pattern.compile("(?ms)\\/\\/.*?$|\\/\\*.*?\\*\\/|\\'(?:\\.|[^\\'])*'|\"(?:\\.|[^\\\"])*\"");
1112
public static final Pattern COMMENTS_TRAILING = Pattern.compile("(?m)[ \\t]+$");
1213
public static final Pattern COMMENTS_NEWLINES = Pattern.compile("(?m)^(?:\\r\\n|\\r|\\n){2,}");
1314

1415
public static String stripComments(String text)
1516
{
16-
Matcher match = COMMENTS_COMMENTS.matcher(text);
17-
while (match.find())
17+
StringReader in = new StringReader(text);
18+
StringWriter out = new StringWriter(text.length());
19+
boolean inComment = false;
20+
boolean inString = false;
21+
char c;
22+
int ci;
23+
try
1824
{
19-
if (match.group().startsWith("/"))
25+
while ((ci = in.read()) != -1)
2026
{
21-
text = text.replace(match.group(), "");
27+
c = (char) ci;
28+
switch (c)
29+
{
30+
case '\\':
31+
{
32+
out.write(c);
33+
out.write(in.read());//Skip escaped chars
34+
break;
35+
}
36+
case '\"':
37+
{
38+
if (!inComment)
39+
{
40+
out.write(c);
41+
inString = !inString;
42+
}
43+
break;
44+
}
45+
case '\'':
46+
{
47+
if (!inComment)
48+
{
49+
out.write(c);
50+
out.write(in.read());
51+
out.write(in.read());
52+
}
53+
break;
54+
}
55+
case '*':
56+
{
57+
char c2 = (char) in.read();
58+
if (inComment && c2 == '/')
59+
{
60+
inComment = false;
61+
out.write(' ');//Allows int x = 3; int y = -/**/-x; to work
62+
} else
63+
{
64+
out.write(c);
65+
out.write(c2);
66+
}
67+
break;
68+
}
69+
case '/':
70+
{
71+
if (!inString)
72+
{
73+
char c2 = (char) in.read();
74+
switch (c2)
75+
{
76+
case '/':
77+
char c3 = 0;
78+
while (c3 != '\n' && c3 != '\r')
79+
{
80+
c3 = (char) in.read();
81+
}
82+
out.write(c3);//write newline
83+
break;
84+
case '*':
85+
inComment = true;
86+
break;
87+
default:
88+
out.write(c);
89+
out.write(c2);
90+
break;
91+
}
92+
} else
93+
{
94+
out.write(c);
95+
}
96+
break;
97+
}
98+
default:
99+
{
100+
if (!inComment)
101+
{
102+
out.write(c);
103+
}
104+
break;
105+
}
106+
}
22107
}
108+
out.close();
109+
} catch (Exception e)
110+
{
111+
throw new RuntimeException(e);
23112
}
113+
text = out.toString();
24114

25115
text = COMMENTS_TRAILING.matcher(text).replaceAll("");
26116
text = COMMENTS_NEWLINES.matcher(text).replaceAll(Constants.NEWLINE);

0 commit comments

Comments
 (0)