
delight.nashornsandbox.internal.RemoveComments Maven / Gradle / Ivy
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;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy