1 package persistence.antlr; 2 3 8 9 11 class CppCharFormatter implements CharFormatter { 12 13 24 public String escapeChar(int c, boolean forCharLiteral) { 25 switch (c) { 27 case '\n' : return "\\n"; 28 case '\t' : return "\\t"; 29 case '\r' : return "\\r"; 30 case '\\' : return "\\\\"; 31 case '\'' : return forCharLiteral ? "\\'" : "'"; 32 case '"' : return forCharLiteral ? "\"" : "\\\""; 33 default : 34 if ( c < ' ' || c > 126 ) 35 { 36 if (c > 255) 37 { 38 String s = Integer.toString(c,16); 39 while( s.length() < 4 ) 41 s = '0' + s; 42 return "\\u" + s; 43 } 44 else { 45 return "\\" + Integer.toString(c,8); 46 } 47 } 48 else { 49 return String.valueOf((char)c); 50 } 51 } 52 } 53 54 61 public String escapeString(String s) 62 { 63 String retval = new String (); 64 for (int i = 0; i < s.length(); i++) 65 retval += escapeChar(s.charAt(i), false); 66 67 return retval; 68 } 69 70 75 public String literalChar(int c) { 76 String ret = "0x"+Integer.toString(c,16); 77 if( c >= 0 && c <= 126 ) 78 ret += " /* '"+escapeChar(c,true)+"' */ "; 79 return ret; 80 } 81 82 90 public String literalString(String s) 91 { 92 return "\"" + escapeString(s) + "\""; 93 } 94 95 } 96 | Popular Tags |