From a1e68fb8c70781a8a2529d2a3cdee319720a93ce Mon Sep 17 00:00:00 2001 From: Magesh Umasankar Date: Thu, 7 Mar 2002 18:30:17 +0000 Subject: [PATCH] Redo comment filtering logic. The previous one was buggy. I will add a testcase this evening. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271788 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/filters/StripJavaComments.java | 74 +++++++++---------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/src/main/org/apache/tools/ant/filters/StripJavaComments.java b/src/main/org/apache/tools/ant/filters/StripJavaComments.java index fb01559b1..1d5df37f1 100644 --- a/src/main/org/apache/tools/ant/filters/StripJavaComments.java +++ b/src/main/org/apache/tools/ant/filters/StripJavaComments.java @@ -62,11 +62,17 @@ import java.io.Reader; * (if you have more complex Java parsing needs, use a real lexer). * Since this class heavily relies on the single char read function, * you are reccomended to make it work on top of a buffered reader. + * + * @author Magesh Umasankar */ public final class StripJavaComments extends BaseFilterReader implements ChainableReader { + private int readAheadCh = -1; + + private boolean inString = false; + /** * This constructor is a dummy constructor and is * not meant to be used by any class other than Ant's @@ -90,52 +96,46 @@ public final class StripJavaComments * Filter out Java Style comments */ public final int read() throws IOException { - int ch = in.read(); - if (ch == '/') { + int ch = -1; + if (readAheadCh != -1) { + ch = readAheadCh; + readAheadCh = -1; + } else { ch = in.read(); - if (ch == '/') { - while (ch != '\n' && ch != -1) { - ch = in.read(); - } - } else if (ch == '*') { - while (ch != -1) { - ch = in.read(); - if (ch == '*') { + if (ch == '"') { + inString = !inString; + } else { + if (!inString) { + if (ch == '/') { ch = in.read(); - while (ch == '*' && ch != -1) { - ch = in.read(); - } - if (ch == '/') { - ch = read(); - break; + while (ch != '\n' && ch != -1) { + ch = in.read(); + } + } else if (ch == '*') { + while (ch != -1) { + ch = in.read(); + if (ch == '*') { + ch = in.read(); + while (ch == '*' && ch != -1) { + ch = in.read(); + } + + if (ch == '/') { + ch = read(); + break; + } + } + } + } else { + readAheadCh = ch; + ch = '/'; } } } } } - if (ch == '"') { - while (ch != -1) { - ch = in.read(); - if (ch == '\\') { - ch = in.read(); - } else if (ch == '"') { - ch = read(); - break; - } - } - } - - if (ch == '\'') { - ch = in.read(); - if (ch == '\\') { - ch = in.read(); - } - ch = in.read(); - ch = read(); - } - return ch; }