示例#1
0
 String stringLiteral(String value) {
   StringBuilder result = new StringBuilder();
   result.append('"');
   for (int i = 0; i < value.length(); i++) {
     char c = value.charAt(i);
     switch (c) {
       case '"':
         result.append("\\\"");
         break;
       case '\\':
         result.append("\\\\");
         break;
       case '\b':
         result.append("\\b");
         break;
       case '\t':
         result.append("\\t");
         break;
       case '\n':
         result.append("\\n");
         if (i + 1 < value.length()) {
           result.append("\"\n").append("  ").append("  ").append("+ \"");
         }
         break;
       case '\f':
         result.append("\\f");
         break;
       case '\r':
         result.append("\\r");
         break;
       default:
         if (Character.isISOControl(c)) {
           new Formatter(result).format("\\u%04x", (int) c);
         } else {
           result.append(c);
         }
     }
   }
   result.append('"');
   return result.toString();
 }