Skip to content

Commit

Permalink
fix #1393: spoon handle octal code value
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb committed Jun 12, 2017
1 parent 29ade2f commit 370e2e4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ public <T> void visitCtLiteral(CtLiteral<T> literal) {
// if the string in the source is not the same as the string in the literal, the string may contains special characters
mayContainsSpecialCharacter = stringLength != 1;
}
printer.writeStringLiteral(new String(new char[] { (Character) literal.getValue() }), mayContainsSpecialCharacter);
printer.writeCharLiteral((Character)literal.getValue(), mayContainsSpecialCharacter);

printer.write("'");
} else if (literal.getValue() instanceof String) {
Expand Down
94 changes: 49 additions & 45 deletions src/main/java/spoon/reflect/visitor/printer/PrinterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -337,54 +337,58 @@ public PrinterHelper writeOperator(BinaryOperatorKind o) {
return this;
}

public void writeStringLiteral(String value, boolean mayContainsSpecialCharacter) {
public void writeCharLiteral(Character c, boolean mayContainsSpecialCharacter) {
if (!mayContainsSpecialCharacter) {
write(value);
return;
}
// handle some special char.....
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN) {
if (c < 0x10) {
write("\\u000" + Integer.toHexString(c));
} else if (c < 0x100) {
write("\\u00" + Integer.toHexString(c));
} else if (c < 0x1000) {
write("\\u0" + Integer.toHexString(c));
} else {
write("\\u" + Integer.toHexString(c));
}
continue;
write(c);
} else if (Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN) {
if (c < 0x10) {
write("\\u000" + Integer.toHexString(c));
} else if (c < 0x100) {
write("\\u00" + Integer.toHexString(c));
} else if (c < 0x1000) {
write("\\u0" + Integer.toHexString(c));
} else {
write("\\u" + Integer.toHexString(c));
}
} else {
switch (c) {
case '\b':
write("\\b"); //$NON-NLS-1$
break;
case '\t':
write("\\t"); //$NON-NLS-1$
break;
case '\n':
write("\\n"); //$NON-NLS-1$
break;
case '\f':
write("\\f"); //$NON-NLS-1$
break;
case '\r':
write("\\r"); //$NON-NLS-1$
break;
case '\"':
write("\\\""); //$NON-NLS-1$
break;
case '\'':
write("\\'"); //$NON-NLS-1$
break;
case '\\': // take care not to display the escape as a potential
// real char
write("\\\\"); //$NON-NLS-1$
break;
default:
write(value.charAt(i));
case '\b':
write("\\b"); //$NON-NLS-1$
break;
case '\t':
write("\\t"); //$NON-NLS-1$
break;
case '\n':
write("\\n"); //$NON-NLS-1$
break;
case '\f':
write("\\f"); //$NON-NLS-1$
break;
case '\r':
write("\\r"); //$NON-NLS-1$
break;
case '\"':
write("\\\""); //$NON-NLS-1$
break;
case '\'':
write("\\'"); //$NON-NLS-1$
break;
case '\\': // take care not to display the escape as a potential
// real char
write("\\\\"); //$NON-NLS-1$
break;
default:
write(Character.isISOControl(c) ? String.format("\\u%04x", (int) c) : Character.toString(c));
}
}
}

public void writeStringLiteral(String value, boolean mayContainsSpecialCharacter) {
if (!mayContainsSpecialCharacter) {
write(value);
} else {
for (int i = 0; i < value.length(); i++) {
writeCharLiteral(value.charAt(i), mayContainsSpecialCharacter);
}
}
}
Expand Down

0 comments on commit 370e2e4

Please sign in to comment.