From 031ccc67c716f95debb2b2bf7eff121b84cb93a8 Mon Sep 17 00:00:00 2001 From: emmanue1 Date: Sun, 30 Jun 2019 09:47:02 +0200 Subject: [PATCH] Fix #248 : "Escape unicode characters not work!" --- .../LineNumberStringBuilderPrinter.java | 190 +++++++++--------- .../util/decompiler/StringBuilderPrinter.java | 45 ++--- 2 files changed, 116 insertions(+), 119 deletions(-) diff --git a/services/src/main/java/org/jd/gui/util/decompiler/LineNumberStringBuilderPrinter.java b/services/src/main/java/org/jd/gui/util/decompiler/LineNumberStringBuilderPrinter.java index 0b195e12..018384ae 100644 --- a/services/src/main/java/org/jd/gui/util/decompiler/LineNumberStringBuilderPrinter.java +++ b/services/src/main/java/org/jd/gui/util/decompiler/LineNumberStringBuilderPrinter.java @@ -8,99 +8,99 @@ package org.jd.gui.util.decompiler; public class LineNumberStringBuilderPrinter extends StringBuilderPrinter { - protected boolean showLineNumbers = false; - - protected int maxLineNumber = 0; - protected int digitCount = 0; - - protected String lineNumberBeginPrefix; - protected String lineNumberEndPrefix; - protected String unknownLineNumberPrefix; - - public void setShowLineNumbers(boolean showLineNumbers) { this.showLineNumbers = showLineNumbers; } - - protected int printDigit(int dcv, int lineNumber, int divisor, int left) { - if (digitCount >= dcv) { - if (lineNumber < divisor) { - append(' '); - } else { - int e = (lineNumber-left) / divisor; - append((char)('0' + e)); - left += e*divisor; - } - } - - return left; - } - - // --- Printer --- // - @Override - public void start(int maxLineNumber, int majorVersion, int minorVersion) { - super.start(maxLineNumber, majorVersion, minorVersion); - - if (showLineNumbers) { - this.maxLineNumber = maxLineNumber; - - if (maxLineNumber > 0) { - digitCount = 1; - unknownLineNumberPrefix = " "; - int maximum = 9; - - while (maximum < maxLineNumber) { - digitCount++; - unknownLineNumberPrefix += ' '; - maximum = maximum*10 + 9; - } - - lineNumberBeginPrefix = "/* "; - lineNumberEndPrefix = " */ "; - } else { - unknownLineNumberPrefix = ""; - lineNumberBeginPrefix = ""; - lineNumberEndPrefix = ""; - } - } else { - this.maxLineNumber = 0; - unknownLineNumberPrefix = ""; - lineNumberBeginPrefix = ""; - lineNumberEndPrefix = ""; - } - } - - @Override public void startLine(int lineNumber) { - if (maxLineNumber > 0) { - append(lineNumberBeginPrefix); - - if (lineNumber == UNKNOWN_LINE_NUMBER) { - append(unknownLineNumberPrefix); - } else { - int left = 0; - - left = printDigit(5, lineNumber, 10000, left); - left = printDigit(4, lineNumber, 1000, left); - left = printDigit(3, lineNumber, 100, left); - left = printDigit(2, lineNumber, 10, left); - append((char)('0' + (lineNumber-left))); - } - - append(lineNumberEndPrefix); - } - - for (int i=0; i 0) { - if (maxLineNumber > 0) { - append(lineNumberBeginPrefix); - append(unknownLineNumberPrefix); - append(lineNumberEndPrefix); - } - - append(NEWLINE); - } - } - } + protected boolean showLineNumbers = false; + + protected int maxLineNumber = 0; + protected int digitCount = 0; + + protected String lineNumberBeginPrefix; + protected String lineNumberEndPrefix; + protected String unknownLineNumberPrefix; + + public void setShowLineNumbers(boolean showLineNumbers) { this.showLineNumbers = showLineNumbers; } + + protected int printDigit(int dcv, int lineNumber, int divisor, int left) { + if (digitCount >= dcv) { + if (lineNumber < divisor) { + stringBuffer.append(' '); + } else { + int e = (lineNumber-left) / divisor; + stringBuffer.append((char)('0' + e)); + left += e*divisor; + } + } + + return left; + } + + // --- Printer --- // + @Override + public void start(int maxLineNumber, int majorVersion, int minorVersion) { + super.start(maxLineNumber, majorVersion, minorVersion); + + if (showLineNumbers) { + this.maxLineNumber = maxLineNumber; + + if (maxLineNumber > 0) { + digitCount = 1; + unknownLineNumberPrefix = " "; + int maximum = 9; + + while (maximum < maxLineNumber) { + digitCount++; + unknownLineNumberPrefix += ' '; + maximum = maximum*10 + 9; + } + + lineNumberBeginPrefix = "/* "; + lineNumberEndPrefix = " */ "; + } else { + unknownLineNumberPrefix = ""; + lineNumberBeginPrefix = ""; + lineNumberEndPrefix = ""; + } + } else { + this.maxLineNumber = 0; + unknownLineNumberPrefix = ""; + lineNumberBeginPrefix = ""; + lineNumberEndPrefix = ""; + } + } + + @Override public void startLine(int lineNumber) { + if (maxLineNumber > 0) { + stringBuffer.append(lineNumberBeginPrefix); + + if (lineNumber == UNKNOWN_LINE_NUMBER) { + stringBuffer.append(unknownLineNumberPrefix); + } else { + int left = 0; + + left = printDigit(5, lineNumber, 10000, left); + left = printDigit(4, lineNumber, 1000, left); + left = printDigit(3, lineNumber, 100, left); + left = printDigit(2, lineNumber, 10, left); + stringBuffer.append((char)('0' + (lineNumber-left))); + } + + stringBuffer.append(lineNumberEndPrefix); + } + + for (int i=0; i 0) { + if (maxLineNumber > 0) { + stringBuffer.append(lineNumberBeginPrefix); + stringBuffer.append(unknownLineNumberPrefix); + stringBuffer.append(lineNumberEndPrefix); + } + + stringBuffer.append(NEWLINE); + } + } + } } diff --git a/services/src/main/java/org/jd/gui/util/decompiler/StringBuilderPrinter.java b/services/src/main/java/org/jd/gui/util/decompiler/StringBuilderPrinter.java index 4d98095e..345a615f 100644 --- a/services/src/main/java/org/jd/gui/util/decompiler/StringBuilderPrinter.java +++ b/services/src/main/java/org/jd/gui/util/decompiler/StringBuilderPrinter.java @@ -29,10 +29,7 @@ public class StringBuilderPrinter implements Printer { public int getMinorVersion() { return minorVersion; } public StringBuilder getStringBuffer() { return stringBuffer; } - public void append(char c) { stringBuffer.append(c); } - public void append(String s) { stringBuffer.append(s); } - - protected void printEscape(String s) { + protected void escape(String s) { if (unicodeEscape && (s != null)) { int length = s.length(); @@ -40,30 +37,30 @@ protected void printEscape(String s) { char c = s.charAt(i); if (c == '\t') { - append(c); + stringBuffer.append(c); } else if (c < 32) { // Write octal format - append("\\0"); - append((char) ('0' + (c >> 3))); - append((char) ('0' + (c & 0x7))); + stringBuffer.append("\\0"); + stringBuffer.append((char) ('0' + (c >> 3))); + stringBuffer.append((char) ('0' + (c & 0x7))); } else if (c > 127) { // Write octal format - append("\\u"); + stringBuffer.append("\\u"); int z = (c >> 12); - append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); + stringBuffer.append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); z = ((c >> 8) & 0xF); - append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); + stringBuffer.append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); z = ((c >> 4) & 0xF); - append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); + stringBuffer.append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); z = (c & 0xF); - append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); + stringBuffer.append((char) ((z <= 9) ? ('0' + z) : (('A' - 10) + z))); } else { - append(c); + stringBuffer.append(c); } } } else { - append(s); + stringBuffer.append(s); } } @@ -78,20 +75,20 @@ public void start(int maxLineNumber, int majorVersion, int minorVersion) { @Override public void end() {} - @Override public void printText(String text) { printEscape(text); } - @Override public void printNumericConstant(String constant) { append(constant); } - @Override public void printStringConstant(String constant, String ownerInternalName) { append(constant); } - @Override public void printKeyword(String keyword) { append(keyword); } + @Override public void printText(String text) { escape(text); } + @Override public void printNumericConstant(String constant) { escape(constant); } + @Override public void printStringConstant(String constant, String ownerInternalName) { escape(constant); } + @Override public void printKeyword(String keyword) { stringBuffer.append(keyword); } - @Override public void printDeclaration(int type, String internalTypeName, String name, String descriptor) { append(name); } - @Override public void printReference(int type, String internalTypeName, String name, String descriptor, String ownerInternalName) { append(name); } + @Override public void printDeclaration(int type, String internalTypeName, String name, String descriptor) { escape(name); } + @Override public void printReference(int type, String internalTypeName, String name, String descriptor, String ownerInternalName) { escape(name); } @Override public void indent() { indentationCount++; } @Override public void unindent() { if (indentationCount > 0) indentationCount--; } - @Override public void startLine(int lineNumber) { for (int i=0; i 0) append(NEWLINE); } + @Override public void startLine(int lineNumber) { for (int i=0; i 0) stringBuffer.append(NEWLINE); } @Override public void startMarker(int type) {} @Override public void endMarker(int type) {}