From eea5e29202e857629feb884e52075bca1586aa7f Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Fri, 25 Mar 2016 23:56:02 +0100 Subject: [PATCH] ECS: Scrub string literals too --- .../mode/java/pdex/ErrorCheckerService.java | 2 +- .../mode/java/pdex/SourceUtils.java | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/java/src/processing/mode/java/pdex/ErrorCheckerService.java b/java/src/processing/mode/java/pdex/ErrorCheckerService.java index fcb00d9ae..3998af3af 100644 --- a/java/src/processing/mode/java/pdex/ErrorCheckerService.java +++ b/java/src/processing/mode/java/pdex/ErrorCheckerService.java @@ -386,7 +386,7 @@ public class ErrorCheckerService { SourceUtils.substituteUnicode(rawCode); try { - SourceUtils.scrubComments(rawCode); + SourceUtils.scrubCommentsAndStrings(rawCode); } catch (RuntimeException e) { // TODO: Unterminated block comment: add to errors // Continue normally, comments were scrubbed diff --git a/java/src/processing/mode/java/pdex/SourceUtils.java b/java/src/processing/mode/java/pdex/SourceUtils.java index 780b0f708..76a2c0aa6 100644 --- a/java/src/processing/mode/java/pdex/SourceUtils.java +++ b/java/src/processing/mode/java/pdex/SourceUtils.java @@ -229,9 +229,9 @@ public class SourceUtils { } - static public String scrubComments(String p) { + static public String scrubCommentsAndStrings(String p) { StringBuilder sb = new StringBuilder(p); - scrubComments(sb); + scrubCommentsAndStrings(sb); return sb.toString(); } @@ -239,7 +239,7 @@ public class SourceUtils { * Replace all commented portions of a given String as spaces. * Utility function used here and in the preprocessor. */ - static public void scrubComments(StringBuilder p) { + static public void scrubCommentsAndStrings(StringBuilder p) { // Track quotes to avoid problems with code like: String t = "*/*"; // http://code.google.com/p/processing/issues/detail?id=1435 boolean insideQuote = false; @@ -285,12 +285,23 @@ public class SourceUtils { throw new RuntimeException("Missing the */ from the end of a " + "/* comment */"); } - } else if (p.charAt(index) == '"' && index > 0 && p.charAt(index-1) != '\\') { - insideQuote = !insideQuote; - index++; - - } else { // any old character, move along - index++; + } else { + boolean isChar = index > 0 && p.charAt(index-1) == '\\'; + if ((insideQuote && p.charAt(index) == '\n') || + (p.charAt(index) == '"' && !isChar)) { + insideQuote = !insideQuote; + index++; + } else if (insideQuote && index < p.length() - 1 && + p.charAt(index) == '\\' && p.charAt(index+1) == '"') { + p.setCharAt(index, ' '); + p.setCharAt(index + 1, ' '); + index++; + } else if (insideQuote) { + p.setCharAt(index, ' '); + index++; + } else { // any old character, move along + index++; + } } } }