mirror of
https://github.com/moparisthebest/fernflower
synced 2024-11-23 09:42:18 -05:00
escape non-printable Unicode characters in literals
This commit is contained in:
parent
4df7866a5c
commit
9ae885c9ec
@ -27,8 +27,7 @@ import de.fernflower.struct.gen.VarType;
|
|||||||
import de.fernflower.util.InterpreterUtil;
|
import de.fernflower.util.InterpreterUtil;
|
||||||
|
|
||||||
public class ConstExprent extends Exprent {
|
public class ConstExprent extends Exprent {
|
||||||
|
private static final HashMap<Integer, String> escapes = new HashMap<Integer, String>();
|
||||||
private static final HashMap<Integer, String> escapes = new HashMap<Integer, String>();
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
escapes.put(new Integer(0x8), "\\b"); /* \u0008: backspace BS */
|
escapes.put(new Integer(0x8), "\\b"); /* \u0008: backspace BS */
|
||||||
@ -114,10 +113,11 @@ public class ConstExprent extends Exprent {
|
|||||||
Integer val = (Integer)value;
|
Integer val = (Integer)value;
|
||||||
String ret = escapes.get(val);
|
String ret = escapes.get(val);
|
||||||
if(ret == null) {
|
if(ret == null) {
|
||||||
if(!ascii || val.intValue() >= 32 && val.intValue() < 127) {
|
char c = (char)val.intValue();
|
||||||
ret = String.valueOf((char)val.intValue());
|
if(c >= 32 && c < 127 || !ascii && InterpreterUtil.isPrintableUnicode(c)) {
|
||||||
|
ret = String.valueOf(c);
|
||||||
} else {
|
} else {
|
||||||
ret = InterpreterUtil.charToUnicodeLiteral(val);
|
ret = InterpreterUtil.charToUnicodeLiteral(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "\'"+ret+"\'";
|
return "\'"+ret+"\'";
|
||||||
@ -263,7 +263,7 @@ public class ConstExprent extends Exprent {
|
|||||||
buffer.append("\\\'");
|
buffer.append("\\\'");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if(!ascii || (c >= 32 && c < 127)) {
|
if(c >= 32 && c < 127 || !ascii && InterpreterUtil.isPrintableUnicode(c)) {
|
||||||
buffer.append(c);
|
buffer.append(c);
|
||||||
} else {
|
} else {
|
||||||
buffer.append(InterpreterUtil.charToUnicodeLiteral(c));
|
buffer.append(InterpreterUtil.charToUnicodeLiteral(c));
|
||||||
|
@ -138,10 +138,15 @@ public class InterpreterUtil {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean isPrintableUnicode(char c) {
|
||||||
|
int t = Character.getType(c);
|
||||||
|
return t != Character.UNASSIGNED && t != Character.LINE_SEPARATOR && t != Character.PARAGRAPH_SEPARATOR &&
|
||||||
|
t != Character.CONTROL && t != Character.FORMAT && t != Character.PRIVATE_USE && t != Character.SURROGATE;
|
||||||
|
}
|
||||||
|
|
||||||
public static String charToUnicodeLiteral(int value) {
|
public static String charToUnicodeLiteral(int value) {
|
||||||
String sTemp = Integer.toHexString(value);
|
String sTemp = Integer.toHexString(value);
|
||||||
sTemp = ("0000"+sTemp).substring(sTemp.length());
|
sTemp = ("0000"+sTemp).substring(sTemp.length());
|
||||||
|
|
||||||
return "\\u"+sTemp;
|
return "\\u"+sTemp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user