Merging CL9604 to main (optimized string literals in generated code)

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9777]
This commit is contained in:
sharwell 2012-01-02 12:08:18 -08:00
parent 9114749c51
commit 3d620734af
2 changed files with 14 additions and 2 deletions

View File

@ -302,11 +302,23 @@ public class Target {
/** Assume 16-bit char */
public String encodeIntAsCharEscape(int v) {
if (v < Character.MIN_VALUE || v > Character.MAX_VALUE) {
throw new IllegalArgumentException(String.format("Cannot encode the specified value: %d", v));
}
if (v >= 0 && v < targetCharValueEscape.length && targetCharValueEscape[v] != null) {
return targetCharValueEscape[v];
}
if (v >= 0x20 && v < 127 && (!Character.isDigit(v) || v == '8' || v == '9')) {
return String.valueOf((char)v);
}
if ( v>=0 && v<=127 ) {
String oct = Integer.toOctalString(v);
if ( oct.length()<3 ) oct = '0'+oct;
return "\\"+ oct;
}
String hex = Integer.toHexString(v|0x10000).substring(1,5);
return "\\u"+hex;
}

View File

@ -43,7 +43,7 @@ public class SerializedATN extends OutputModelObject {
List<Integer> data = ATNSerializer.getSerialized(factory.getGrammar(), atn);
serialized = new ArrayList<String>(data.size());
for (int c : data) {
String encoded = factory.getGenerator().target.encodeIntAsCharEscape(c);
String encoded = factory.getGenerator().target.encodeIntAsCharEscape(c == -1 ? Character.MAX_VALUE : c);
serialized.add(encoded);
}
// System.out.println(ATNSerializer.getDecoded(factory.getGrammar(), atn));