@@ -242,25 +242,24 @@ 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 >();
246
245
// single-quoted character
247
- patterns .add (Pattern .compile ("('.')" , Pattern .MULTILINE ));
248
- // single and multi-line comment
249
- patterns .add (Pattern .compile ("('\\ \\ \" ')" , Pattern .MULTILINE ));
250
- patterns .add (Pattern .compile ("(//.*?$)" , Pattern .MULTILINE ));
251
- patterns .add (Pattern .compile ("(/\\ *[^*]*(?:\\ *(?!/)[^*]*)*\\ */)" , Pattern .MULTILINE ));
252
- // pre-processor directive
253
- patterns .add (Pattern .compile ("(^\\ s*#.*?$)" , Pattern .MULTILINE ));
246
+ String p = "('.')" ;
247
+
248
+ p += "|('\\ \\ \" ')" ;
249
+
254
250
// double-quoted string
255
- patterns . add ( Pattern . compile ( " (\" (?:[^\" \\ \\ ]|\\ \\ .)*\" )", Pattern . MULTILINE )) ;
251
+ p += "| (\" (?:[^\" \\ \\ ]|\\ \\ .)*\" )" ;
256
252
257
- String code = in ;
258
- for (Pattern p : patterns ) {
259
- Matcher matcher = p .matcher (code );
260
- code = matcher .replaceAll (" " );
261
- }
253
+ // single and multi-line comment
254
+ //p += "|" + "(//\\s*?$)|(/\\*\\s*?\\*/)";
255
+ p += "|(//.*?$)|(/\\ *[^*]*(?:\\ *(?!/)[^*]*)*\\ */)" ;
256
+
257
+ // pre-processor directive
258
+ p += "|" + "(^\\ s*#.*?$)" ;
262
259
263
- return code ;
260
+ Pattern pattern = Pattern .compile (p , Pattern .MULTILINE );
261
+ Matcher matcher = pattern .matcher (in );
262
+ return matcher .replaceAll (" " );
264
263
}
265
264
266
265
/**
@@ -333,17 +332,54 @@ public ArrayList<String> prototypes(String in) {
333
332
* Replace all commented portions of a given String as spaces.
334
333
* Utility function used here and in the preprocessor.
335
334
*/
336
- static public String scrubComments (String what ) {
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 ("" );
345
- }
335
+ public String scrubComments (String what ) {
336
+ char p [] = what .toCharArray ();
337
+
338
+ int index = 0 ;
339
+ boolean insideString = false ;
340
+ while (index < p .length ) {
341
+ if (p [index ] == '\"' ) {
342
+ insideString = !insideString ;
343
+ }
344
+ // for any double slash comments, ignore until the end of the line
345
+ if (!insideString && (p [index ] == '/' ) &&
346
+ (index < p .length - 1 ) &&
347
+ (p [index +1 ] == '/' )) {
348
+ p [index ++] = ' ' ;
349
+ p [index ++] = ' ' ;
350
+ while ((index < p .length ) &&
351
+ (p [index ] != '\n' )) {
352
+ p [index ++] = ' ' ;
353
+ }
346
354
347
- return result ;
355
+ // check to see if this is the start of a new multiline comment.
356
+ // if it is, then make sure it's actually terminated somewhere.
357
+ } else if (!insideString && (p [index ] == '/' ) &&
358
+ (index < p .length - 1 ) &&
359
+ (p [index +1 ] == '*' )) {
360
+ p [index ++] = ' ' ;
361
+ p [index ++] = ' ' ;
362
+ boolean endOfRainbow = false ;
363
+ while (index < p .length - 1 ) {
364
+ if ((p [index ] == '*' ) && (p [index +1 ] == '/' )) {
365
+ p [index ++] = ' ' ;
366
+ p [index ++] = ' ' ;
367
+ endOfRainbow = true ;
368
+ break ;
369
+
370
+ } else {
371
+ // continue blanking this area
372
+ p [index ++] = ' ' ;
373
+ }
374
+ }
375
+ if (!endOfRainbow ) {
376
+ throw new RuntimeException (_ ("Missing the */ from the end of a " +
377
+ "/* comment */" ));
378
+ }
379
+ } else { // any old character, move along
380
+ index ++;
381
+ }
382
+ }
383
+ return new String (p );
348
384
}
349
385
}
0 commit comments