-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing that no braces regex would be triggered by keywords in comments,
see issue #34.
- Loading branch information
Max Rohde
committed
Feb 1, 2018
1 parent
257badb
commit 3bf3d29
Showing
4 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/delight/nashornsandbox/internal/RemoveComments.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package delight.nashornsandbox.internal; | ||
|
||
/** | ||
* Based on | ||
* https://codegolf.stackexchange.com/questions/48326/remove-single-line-and-multiline-comments-from-string | ||
* | ||
* @author adminuser | ||
* | ||
*/ | ||
public class RemoveComments { | ||
|
||
public static final int DEFAULT = 1; | ||
public static final int ESCAPE = 2; | ||
public static final int STRING = 3; | ||
public static final int ONE_LINE = 4; | ||
public static final int MULTI_LINE = 5; | ||
|
||
public static String perform(String s) { | ||
String out = ""; | ||
int mod = DEFAULT; | ||
for (int i = 0; i < s.length(); i++) { | ||
String substring = s.substring(i, Math.min(i + 2, s.length())); | ||
char c = s.charAt(i); | ||
switch (mod) { | ||
case DEFAULT: // default | ||
mod = substring.equals("/*") ? MULTI_LINE | ||
: substring.equals("//") ? ONE_LINE : c == '"' || c =='\'' ? STRING : DEFAULT; | ||
break; | ||
case STRING: // string | ||
mod = c == '"' || c == '\'' ? DEFAULT : c == '\\' ? ESCAPE : STRING; | ||
break; | ||
case ESCAPE: // string | ||
mod = STRING; | ||
break; | ||
case ONE_LINE: // one line comment | ||
mod = c == '\n' ? DEFAULT : ONE_LINE; | ||
continue; | ||
case MULTI_LINE: // multi line comment | ||
mod = substring.equals("*/") ? DEFAULT : MULTI_LINE; | ||
i += mod == DEFAULT ? 1 : 0; | ||
continue; | ||
} | ||
out += mod < 4 ? c : ""; | ||
} | ||
|
||
return out; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/java/delight/nashornsandbox/TestRemoveComments.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package delight.nashornsandbox; | ||
|
||
import org.junit.Test; | ||
|
||
import delight.nashornsandbox.internal.RemoveComments; | ||
import junit.framework.Assert; | ||
|
||
public class TestRemoveComments { | ||
|
||
@Test | ||
public void test() { | ||
|
||
Assert.assertEquals("var url = 'http://hello.com'", RemoveComments.perform("var url = 'http://hello.com'")); | ||
|
||
Assert.assertEquals("var url = \"http://hello.com\"", RemoveComments.perform("var url = \"http://hello.com\"")); | ||
|
||
Assert.assertEquals("var url = 'http://hello.com';", RemoveComments.perform("var url = 'http://hello.com';// mycomment")); | ||
|
||
Assert.assertEquals("var url = 'http://hello.com'", RemoveComments.perform("/* whatisthis */var url = 'http://hello.com'")); | ||
|
||
} | ||
|
||
} |