Home | History | Annotate | Download | only in util
      1 package annotations.util;
      2 
      3 /*>>>
      4 import org.checkerframework.checker.nullness.qual.*;
      5 */
      6 
      7 /**
      8  * {@link Strings} provides useful static methods related to strings.
      9  */
     10 public abstract class Strings {
     11     private Strings() {}
     12 
     13     /**
     14      * Returns the given string, escaped and quoted according to Java
     15      * conventions.  Currently, only newlines, backslashes, tabs, and
     16      * single and double quotes are escaped.  Perhaps nonprinting
     17      * characters should also be escaped somehow.
     18      */
     19     public static String escape(String in) {
     20         StringBuilder out = new StringBuilder("\"");
     21         for (int pos = 0; pos < in.length(); pos++) {
     22             switch (in.charAt(pos)) {
     23             case '\n':
     24                 out.append("\\n");
     25                 break;
     26             case '\t':
     27                 out.append("\\t");
     28                 break;
     29             case '\\':
     30                 out.append("\\\\");
     31                 break;
     32             case '\'':
     33                 out.append("\\\'");
     34                 break;
     35             case '\"':
     36                 out.append("\\\"");
     37                 break;
     38             default:
     39                 out.append(in.charAt(pos));
     40             }
     41         }
     42         out.append('\"');
     43         return out.toString();
     44     }
     45 }
     46