1

I'm writing Java that gets strings like "C:\Temp\file.txt" and generates C source code containing those strings, the code will be compiled later. So I need to render the strings in a form that can be parsed back in by the C compiler; for my example that would be "C:\\Temp\\file.txt". Of course the string might have other special characters too.

Basically I need in Java the same feature as discussed in this SO answer: standard c library for escaping a string

NB Python provides something like this with 'repr' and I figured there might be a Java library method. But so far I have not found it. Thanks in advance.

Community
  • 1
  • 1
chrisinmtown
  • 3,571
  • 3
  • 34
  • 43

2 Answers2

3

I suggest you look at Apache Commons StringEscapeUtils, which you might use thusly -

StringEscapeUtils.escapeJava(str); // this will likely work for most cases.
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
3

Here's my code for solving the problem. Very much welcome improvements.

package my.package;

public class ParseableStringHelper {

/**
 * http://stackoverflow.com/questions/1029897/comparing-a-char-to-a-code-
 * point
 * 
 * Does not implement Iterator
 */
static class CodePointIterator {

    private final String sequence;
    private int index = 0;

    public CodePointIterator(final String sequence) {
        this.sequence = sequence;
    }

    public boolean hasNext() {
        return index < sequence.length();
    }

    public int next() {
        int codePoint = sequence.codePointAt(index);
        index += Character.charCount(codePoint);
        return codePoint;
    }
}

/**
 * Converts a String to a version that can be parsed by a C or Java compiler
 * when enclosed in double quotes, which requires escaping all special
 * characters by prefixing them with a backslash. Does NOT do anything
 * special for single-quote characters (which are perfectly valid inside a
 * quoted string); always escapes double-quote characters.
 * 
 * http://docs.oracle.com/javase/tutorial/java/data/characters.html
 * 
 * @param internal
 *            String that may have special characters
 * @return String with proper escape sequences for special characters like
 *         backslash and newline.
 */
public static String getParseableVersion(final String internal) {
    StringBuilder sb = new StringBuilder();
    CodePointIterator pointIterator = new CodePointIterator(internal);
    while (pointIterator.hasNext()) {
        int point = pointIterator.next();
        switch (point) {
        case '\b':
            sb.append("\\b");
            break;
        case '\f':
            sb.append("\\f");
            break;
        case '\n':
            sb.append("\\n");
            break;
        case '\r':
            sb.append("\\r");
            break;
        case '\t':
            sb.append("\\t");
            break;
        case '\\':
            // double these up
            sb.append("\\\\");
            break;
        case '"':
            sb.append("\"");
            break;
        default:
            sb.append(Character.toChars(point));
            break;
        }
    }
    return sb.toString();
}

/**
 * Test the helper
 * 
 * @param args
 */
public static void main(String[] args) {
    String[] tests = { "C:\\Temp\\file.txt",
            "This has\tsome special characters\n" };
    for (String t : tests)
        System.out.println("Orig is " + t + ", parseable is \""
                + getParseableVersion(t) + "\"");
  }
}
chrisinmtown
  • 3,571
  • 3
  • 34
  • 43