|
8 | 8 |
|
9 | 9 | import java.util.ArrayList;
|
10 | 10 | import java.util.List;
|
| 11 | +import java.util.regex.Matcher; |
11 | 12 | import java.util.regex.Pattern;
|
12 | 13 |
|
13 | 14 | /**
|
@@ -36,7 +37,7 @@ public static List<FileElement> parseFileLines(String fileName, List<String> lin
|
36 | 37 | for (var i = 0; i < lines.size(); i++) {
|
37 | 38 | var line = lines.get(i);
|
38 | 39 | String content = removeComments(line);
|
39 |
| - |
| 40 | + //System.out.println(content); |
40 | 41 | if (content.isEmpty()) {
|
41 | 42 | elements.add(new VoidElement(fileName, lastLine + i, expectedIndentation));
|
42 | 43 | continue;
|
@@ -94,28 +95,48 @@ private static int count(List<FileElement> elements) {
|
94 | 95 | @Nullable
|
95 | 96 | private static String removeComments(String string) {
|
96 | 97 | StringBuilder stringBuilder = new StringBuilder();
|
97 |
| - for (char c : string.toCharArray()) { |
| 98 | + for (int i = 0; i < string.length(); i++) { |
| 99 | + char c = string.charAt(i); |
98 | 100 | if (c == '#') {
|
99 |
| - int index = string.indexOf(c); |
100 |
| - if (index + 1 >= string.length()) return ""; |
101 |
| - if (string.charAt(index + 1) != '#') { //double # escape the first # |
102 |
| - //Checking if it isn't color hex and if no double # (for escaping first #) |
103 |
| - for (int i : new int[]{3, 6, 9}) { |
104 |
| - if (index + i <= string.length() - 1 && !Color.COLOR_PATTERN.matcher(string.substring(index + 1, index + i + 1)).matches()) |
105 |
| - COMMENT_STATUS = 1; |
106 |
| - } |
107 |
| - } |
108 |
| - //set start or end of a block comment ("###" characters) |
109 |
| - if (index + 2 <= string.length() && string.substring(index, index + 2).equals("##")) { |
110 |
| - COMMENT_STATUS = COMMENT_STATUS == 2 ? 0 : 2; |
111 |
| - } |
| 101 | + switch (COMMENT_STATUS) { |
| 102 | + case 0: |
| 103 | + //System.out.println("debug color match: " + Color.COLOR_PATTERN.matcher("ffff0000").matches()); |
| 104 | + for (int j : new int[]{3, 6, 8}) { |
| 105 | + if (i + j <= string.length() - 1) { |
| 106 | + //System.out.println("debug string: " + string.substring(i + 1, i + j + 1)); |
| 107 | + if (!Color.COLOR_PATTERN.matcher(string.substring(i + 1, i + j + 1)).matches()) { |
| 108 | + //System.out.println("yolo debug 1"); |
| 109 | + COMMENT_STATUS = 1; |
| 110 | + } |
| 111 | + } else { |
| 112 | + COMMENT_STATUS = 1; |
| 113 | + break; |
| 114 | + } |
| 115 | + } |
| 116 | + break; |
| 117 | + case 1: |
| 118 | + if (string.length() <= i + 1 && string.charAt(i + 1) == '#') |
| 119 | + COMMENT_STATUS = 2; |
112 | 120 |
|
| 121 | + else if(string.charAt(i - 1) == '#') |
| 122 | + COMMENT_STATUS = 0; |
| 123 | + |
| 124 | + } |
113 | 125 | }
|
114 |
| - if (COMMENT_STATUS == 0) { |
| 126 | + /* |
| 127 | + System.out.println( |
| 128 | + "Caractere : " + c + '\n' + "Caractere numero : " + i |
| 129 | + + '\n' + "Status commentaire: " + COMMENT_STATUS + '\n' |
| 130 | + ); |
| 131 | +
|
| 132 | + */ |
| 133 | + if (COMMENT_STATUS == 0) |
115 | 134 | stringBuilder.append(c);
|
116 |
| - } |
| 135 | + |
| 136 | + |
117 | 137 | }
|
118 |
| - if (COMMENT_STATUS == 1) COMMENT_STATUS = 0; |
| 138 | + if (COMMENT_STATUS != 2) COMMENT_STATUS = 0; |
| 139 | + |
119 | 140 | return stringBuilder.toString().strip();
|
120 | 141 |
|
121 | 142 | }
|
|
0 commit comments