@@ -87,7 +87,7 @@ public int writePrefix(String program)
8787 // an OutOfMemoryError or NullPointerException will happen.
8888 // again, not gonna bother tracking this down, but here's a hack.
8989 // http://dev.processing.org/bugs/show_bug.cgi?id=16
90- scrubComments (program );
90+ program = scrubComments (program );
9191 // If there are errors, an exception is thrown and this fxn exits.
9292
9393 if (Preferences .getBoolean ("preproc.substitute_unicode" )) {
@@ -242,26 +242,27 @@ public int firstStatement(String in) {
242242 */
243243 public String strip (String in ) {
244244 // XXX: doesn't properly handle special single-quoted characters
245+ List <Pattern > patterns = new ArrayList <Pattern >();
245246 // single-quoted character
246- String p = "('.')" ;
247-
248- p += "|('\\ \\ \" ')" ;
249-
250- // double-quoted string
251- p += "|(\" (?:[^\" \\ \\ ]|\\ \\ .)*\" )" ;
252-
247+ patterns .add (Pattern .compile ("('.')" , Pattern .MULTILINE ));
253248 // single and multi-line comment
254- //p += "|" + "(//\\s*?$)|(/\\*\\s*?\\*/)" ;
255- p += "| (//.*?$)|(/ \\ *[^*]*(?: \\ *(?!/)[^*]*)* \\ */)" ;
256-
249+ patterns . add ( Pattern . compile ( "(' \\ \\ \" ')" , Pattern . MULTILINE )) ;
250+ patterns . add ( Pattern . compile ( " (//.*?$)" , Pattern . MULTILINE )) ;
251+ patterns . add ( Pattern . compile ( "(/ \\ *[^*]*(?: \\ *(?!/)[^*]*)* \\ */)" , Pattern . MULTILINE ));
257252 // pre-processor directive
258- p += "|" + "(^\\ s*#.*?$)" ;
253+ patterns .add (Pattern .compile ("(^\\ s*#.*?$)" , Pattern .MULTILINE ));
254+ // double-quoted string
255+ patterns .add (Pattern .compile ("(\" (?:[^\" \\ \\ ]|\\ \\ .)*\" )" , Pattern .MULTILINE ));
259256
260- Pattern pattern = Pattern .compile (p , Pattern .MULTILINE );
261- Matcher matcher = pattern .matcher (in );
262- return matcher .replaceAll (" " );
257+ String code = in ;
258+ for (Pattern p : patterns ) {
259+ Matcher matcher = p .matcher (code );
260+ code = matcher .replaceAll (" " );
261+ }
262+
263+ return code ;
263264 }
264-
265+
265266 /**
266267 * Removes the contents of all top-level curly brace pairs {}.
267268 * @param in the String to collapse
@@ -333,49 +334,16 @@ public ArrayList<String> prototypes(String in) {
333334 * Utility function used here and in the preprocessor.
334335 */
335336 static public String scrubComments (String what ) {
336- char p [] = what .toCharArray ();
337-
338- int index = 0 ;
339- while (index < p .length ) {
340- // for any double slash comments, ignore until the end of the line
341- if ((p [index ] == '/' ) &&
342- (index < p .length - 1 ) &&
343- (p [index +1 ] == '/' )) {
344- p [index ++] = ' ' ;
345- p [index ++] = ' ' ;
346- while ((index < p .length ) &&
347- (p [index ] != '\n' )) {
348- p [index ++] = ' ' ;
349- }
350-
351- // check to see if this is the start of a new multiline comment.
352- // if it is, then make sure it's actually terminated somewhere.
353- } else if ((p [index ] == '/' ) &&
354- (index < p .length - 1 ) &&
355- (p [index +1 ] == '*' )) {
356- p [index ++] = ' ' ;
357- p [index ++] = ' ' ;
358- boolean endOfRainbow = false ;
359- while (index < p .length - 1 ) {
360- if ((p [index ] == '*' ) && (p [index +1 ] == '/' )) {
361- p [index ++] = ' ' ;
362- p [index ++] = ' ' ;
363- endOfRainbow = true ;
364- break ;
365-
366- } else {
367- // continue blanking this area
368- p [index ++] = ' ' ;
369- }
370- }
371- if (!endOfRainbow ) {
372- throw new RuntimeException (_ ("Missing the */ from the end of a " +
373- "/* comment */" ));
374- }
375- } else { // any old character, move along
376- index ++;
377- }
337+ List <Pattern > patterns = new ArrayList <Pattern >();
338+ patterns .add (Pattern .compile ("('\\ \\ \" ')" , Pattern .MULTILINE ));
339+ patterns .add (Pattern .compile ("(//.*?$)" , Pattern .MULTILINE ));
340+ patterns .add (Pattern .compile ("(/\\ *[^*]*(?:\\ *(?!/)[^*]*)*\\ */)" , Pattern .MULTILINE ));
341+
342+ String result = what ;
343+ for (Pattern p : patterns ) {
344+ result = p .matcher (result ).replaceAll ("" );
378345 }
379- return new String (p );
346+
347+ return result ;
380348 }
381349}
0 commit comments