1
0
mirror of https://github.com/fadden/6502bench.git synced 2026-04-24 13:17:24 +00:00

Fix trailing backslash issue

If an end-of-line comment ended with '\', the code that "prettifies"
the JSON output would get confused, and would start inserting \r\n
after commas inside comment strings.  This didn't corrupt the project
files, but it did make them look funny, and required manual cleanup.

Added a sample.  This won't catch regressions of this particular
problem because it only happens when you save the file, but if
nothing else it'll act as documentation.
This commit is contained in:
Andy McFadden
2021-11-04 12:59:15 -07:00
parent 3ea07f3eb7
commit fdf2a0777c
6 changed files with 157 additions and 36 deletions
+12 -4
View File
@@ -127,15 +127,23 @@ namespace CommonUtil {
Debug.Assert(findStrQuote == repStr.Contains('\"'));
bool inQuote = false;
bool lastBackSlash = false;
for (int i = 0; i < inStr.Length; i++) {
char ch = inStr[i];
if (inQuote) {
// Check to see if the double-quote is quoted. It's safe to back up
// one because we don't start in-quote.
if (ch == '\"' && inStr[i-1] != '\\') {
if (lastBackSlash) {
lastBackSlash = false;
// JSON recognizes: \" \\ \/ \b \f \n \r \t \uXXXX
// Ignore them all. Especially important for \", so we don't
// mishandle quoted quotes, and for \\, so we don't fail to handle
// quotes on a string that ends with a backslash.
} else if (ch == '\\') {
lastBackSlash = true;
} else if (ch == '"') {
// Un-quoted quote. End of string.
inQuote = false;
} else {
// in quoted text, keep going
// Still in quoted string, keep going.
}
sb.Append(ch);
} else {