Skip to content

Commit d5b374e

Browse files
committed
#46 refactoring
1 parent a041397 commit d5b374e

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

jcp/src/main/java/com/igormaznitsa/jcp/removers/JcpCommentLineRemover.java

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@ public JcpCommentLineRemover(final Reader src, final Writer dst,
4242
@Override
4343
public Writer process() throws IOException {
4444
final int STATE_NORMAL = 0;
45-
final int STATE_INSIDE_STRING = 1;
46-
final int STATE_NEXT_SPECIAL_CHAR = 2;
47-
final int STATE_FORWARD_SLASH = 3;
48-
final int STATE_POSSIBLE_JCP = 4;
45+
final int STATE_FORWARD_SLASH = 1;
46+
final int STATE_POSSIBLE_JCP = 2;
4947

5048
final StringBuilder jcpBuffer = new StringBuilder();
5149

@@ -59,20 +57,13 @@ public Writer process() throws IOException {
5957

6058
switch (state) {
6159
case STATE_NORMAL: {
62-
switch (chr) {
63-
case '\"': {
64-
this.dstWriter.write(chr);
65-
state = STATE_INSIDE_STRING;
66-
}
67-
break;
68-
case '/': {
69-
state = STATE_FORWARD_SLASH;
70-
}
71-
break;
72-
default: {
73-
this.dstWriter.write(chr);
74-
}
75-
break;
60+
if (chr == '/') {
61+
state = STATE_FORWARD_SLASH;
62+
} else if (Character.isWhitespace(chr)) {
63+
this.dstWriter.write(chr);
64+
} else {
65+
this.dstWriter.write(chr);
66+
this.copyTillNextString();
7667
}
7768
}
7869
break;
@@ -121,27 +112,6 @@ public Writer process() throws IOException {
121112
}
122113
}
123114
break;
124-
case STATE_INSIDE_STRING: {
125-
switch (chr) {
126-
case '\\': {
127-
state = STATE_NEXT_SPECIAL_CHAR;
128-
}
129-
break;
130-
case '\"': {
131-
state = STATE_NORMAL;
132-
}
133-
break;
134-
default:
135-
break;
136-
}
137-
this.dstWriter.write(chr);
138-
}
139-
break;
140-
case STATE_NEXT_SPECIAL_CHAR: {
141-
this.dstWriter.write(chr);
142-
state = STATE_INSIDE_STRING;
143-
}
144-
break;
145115
default:
146116
throw new IllegalStateException("Unexpected state: " + state);
147117
}

jcp/src/test/java/com/igormaznitsa/jcp/removers/JcpCommentLineRemoverTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ public void testJcpDirectivesInComments() throws Exception {
7878
);
7979
}
8080

81+
@Test
82+
public void testCommentedJcpDirective() throws Exception {
83+
this.assertCommentRemove(
84+
"/** some multiline //$ //# test\nwith jcp directives*/\n//$ some\n//$$$ some two\n// hello world\n////#if DEBUG\nSystem.out.println(\"DEBUG\");\n//#else\nSystem.out.println(\"RELEASE\");\n//#endif\n// end",
85+
"/** some multiline //$ //# test\nwith jcp directives*/\n\n\n// hello world\n////#if DEBUG\nSystem.out.println(\"DEBUG\");\n\nSystem.out.println(\"RELEASE\");\n\n// end"
86+
);
87+
88+
if (this.whiteSpaced) {
89+
this.assertCommentRemove(
90+
"/** some multiline //$ //# test\nwith jcp directives*/\n// $ some\n// $$$ some two\n// hello world\n//// #if DEBUG\nSystem.out.println(\"DEBUG\");\n// # else\nSystem.out.println(\"RELEASE\");\n// #endif\n// end",
91+
"/** some multiline //$ //# test\nwith jcp directives*/\n\n\n// hello world\n//// #if DEBUG\nSystem.out.println(\"DEBUG\");\n\nSystem.out.println(\"RELEASE\");\n\n// end"
92+
);
93+
}
94+
}
95+
8196
@Test
8297
public void testLineCommentInTheEnd() throws Exception {
8398
this.assertCommentRemove(

0 commit comments

Comments
 (0)