|
1 | 1 | package net.minecraftforge.gradle.sourcemanip; |
2 | 2 |
|
3 | | -import net.minecraftforge.gradle.common.Constants; |
4 | | - |
| 3 | +import java.io.StringReader; |
| 4 | +import java.io.StringWriter; |
5 | 5 | import java.util.regex.Matcher; |
6 | 6 | import java.util.regex.Pattern; |
7 | 7 |
|
| 8 | +import net.minecraftforge.gradle.common.Constants; |
| 9 | + |
8 | 10 | public class McpCleanup |
9 | 11 | { |
10 | | - public static final Pattern COMMENTS_COMMENTS = Pattern.compile("(?ms)\\/\\/.*?$|\\/\\*.*?\\*\\/|\\'(?:\\.|[^\\'])*'|\"(?:\\.|[^\\\"])*\""); |
11 | 12 | public static final Pattern COMMENTS_TRAILING = Pattern.compile("(?m)[ \\t]+$"); |
12 | 13 | public static final Pattern COMMENTS_NEWLINES = Pattern.compile("(?m)^(?:\\r\\n|\\r|\\n){2,}"); |
13 | 14 |
|
14 | 15 | public static String stripComments(String text) |
15 | 16 | { |
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 |
18 | 24 | { |
19 | | - if (match.group().startsWith("/")) |
| 25 | + while ((ci = in.read()) != -1) |
20 | 26 | { |
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 | + } |
22 | 107 | } |
| 108 | + out.close(); |
| 109 | + } catch (Exception e) |
| 110 | + { |
| 111 | + throw new RuntimeException(e); |
23 | 112 | } |
| 113 | + text = out.toString(); |
24 | 114 |
|
25 | 115 | text = COMMENTS_TRAILING.matcher(text).replaceAll(""); |
26 | 116 | text = COMMENTS_NEWLINES.matcher(text).replaceAll(Constants.NEWLINE); |
|
0 commit comments