< prev index next >

src/jdk.jextract/share/classes/com/sun/tools/jextract/Utils.java

Print this page

        

@@ -250,6 +250,52 @@
     public static Optional<Path> findLibraryPath(Path[] paths, String libName) {
         return Arrays.stream(paths).
                 map(p -> p.resolve(System.mapLibraryName(libName))).
                 filter(Files::isRegularFile).map(Path::toAbsolutePath).findFirst();
     }
+
+    /*
+     * FIXME: when we add jdk.compiler dependency from jdk.jextract module, revisit
+     * the following. The following methods 'quote', 'quote' and 'isPrintableAscii'
+     * are from javac source. See also com.sun.tools.javac.util.Convert.java.
+     */
+
+    /**
+     * Escapes each character in a string that has an escape sequence or
+     * is non-printable ASCII.  Leaves non-ASCII characters alone.
+     */
+    public static String quote(String s) {
+        StringBuilder buf = new StringBuilder();
+        for (int i = 0; i < s.length(); i++) {
+            buf.append(quote(s.charAt(i)));
+        }
+        return buf.toString();
+    }
+
+    /**
+     * Escapes a character if it has an escape sequence or is
+     * non-printable ASCII.  Leaves non-ASCII characters alone.
+     */
+    public static String quote(char ch) {
+        switch (ch) {
+        case '\b':  return "\\b";
+        case '\f':  return "\\f";
+        case '\n':  return "\\n";
+        case '\r':  return "\\r";
+        case '\t':  return "\\t";
+        case '\'':  return "\\'";
+        case '\"':  return "\\\"";
+        case '\\':  return "\\\\";
+        default:
+            return (isPrintableAscii(ch))
+                ? String.valueOf(ch)
+                : String.format("\\u%04x", (int) ch);
+        }
+    }
+
+    /**
+     * Is a character printable ASCII?
+     */
+    private static boolean isPrintableAscii(char ch) {
+        return ch >= ' ' && ch <= '~';
+    }
 }
< prev index next >