Skip to content

Commit

Permalink
Fix #248 : "Escape unicode characters not work!"
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanue1 committed Jun 30, 2019
1 parent cf9262f commit 031ccc6
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<indentationCount; i++) {
append(TAB);
}
}
@Override public void extraLine(int count) {
if (realignmentLineNumber) {
while (count-- > 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<indentationCount; i++) {
stringBuffer.append(TAB);
}
}
@Override public void extraLine(int count) {
if (realignmentLineNumber) {
while (count-- > 0) {
if (maxLineNumber > 0) {
stringBuffer.append(lineNumberBeginPrefix);
stringBuffer.append(unknownLineNumberPrefix);
stringBuffer.append(lineNumberEndPrefix);
}

stringBuffer.append(NEWLINE);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,38 @@ 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();

for (int i=0; i<length; i++) {
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);
}
}

Expand All @@ -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<indentationCount; i++) append(TAB); }
@Override public void endLine() { append(NEWLINE); }
@Override public void extraLine(int count) { if (realignmentLineNumber) while (count-- > 0) append(NEWLINE); }
@Override public void startLine(int lineNumber) { for (int i=0; i<indentationCount; i++) stringBuffer.append(TAB); }
@Override public void endLine() { stringBuffer.append(NEWLINE); }
@Override public void extraLine(int count) { if (realignmentLineNumber) while (count-- > 0) stringBuffer.append(NEWLINE); }

@Override public void startMarker(int type) {}
@Override public void endMarker(int type) {}
Expand Down

0 comments on commit 031ccc6

Please sign in to comment.