@@ -87,7 +87,7 @@ public int writePrefix(String program)
87
87
// an OutOfMemoryError or NullPointerException will happen.
88
88
// again, not gonna bother tracking this down, but here's a hack.
89
89
// http://dev.processing.org/bugs/show_bug.cgi?id=16
90
- scrubComments (program );
90
+ program = scrubComments (program );
91
91
// If there are errors, an exception is thrown and this fxn exits.
92
92
93
93
if (Preferences .getBoolean ("preproc.substitute_unicode" )) {
@@ -242,26 +242,27 @@ public int firstStatement(String in) {
242
242
*/
243
243
public String strip (String in ) {
244
244
// XXX: doesn't properly handle special single-quoted characters
245
+ List <Pattern > patterns = new ArrayList <Pattern >();
245
246
// single-quoted character
246
- String p = "('.')" ;
247
-
248
- p += "|('\\ \\ \" ')" ;
249
-
250
- // double-quoted string
251
- p += "|(\" (?:[^\" \\ \\ ]|\\ \\ .)*\" )" ;
252
-
247
+ patterns .add (Pattern .compile ("('.')" , Pattern .MULTILINE ));
253
248
// 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 ));
257
252
// 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 ));
259
256
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 ;
263
264
}
264
-
265
+
265
266
/**
266
267
* Removes the contents of all top-level curly brace pairs {}.
267
268
* @param in the String to collapse
@@ -333,49 +334,16 @@ public ArrayList<String> prototypes(String in) {
333
334
* Utility function used here and in the preprocessor.
334
335
*/
335
336
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 ("" );
378
345
}
379
- return new String (p );
346
+
347
+ return result ;
380
348
}
381
349
}
0 commit comments