aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/javacc/UnicodeUtilities.java
blob: 61bd69882c86cff428513791b336678719fa3366 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.javacc;

/**
 * @author Simon Thoresen Hult
 */
public class UnicodeUtilities {

    /**
     * Adds a leading and trailing double quotation mark to the given string. This will escape whatever content is
     * within the string literal.
     *
     * @param str   The string to quote.
     * @param quote The quote character.
     * @return The quoted string.
     */
    public static String quote(String str, char quote) {
        StringBuilder ret = new StringBuilder();
        ret.append(quote);
        for (int i = 0; i < str.length(); ++i) {
            char c = str.charAt(i);
            if (c == quote) {
                ret.append("\\").append(c);
            } else {
                ret.append(escape(c));
            }
        }
        ret.append(quote);
        return ret.toString();
    }

    /**
     * Removes leading and trailing quotation mark from the given string. This method will properly unescape whatever
     * content is withing the string literal as well.
     *
     * @param str The string to unquote.
     * @return The unquoted string.
     */
    public static String unquote(String str) {
        if (str.length() == 0) {
            return str;
        }
        char quote = str.charAt(0);
        if (quote != '"' && quote != '\'') {
            return str;
        }
        if (str.charAt(str.length() - 1) != quote) {
            return str;
        }
        StringBuilder ret = new StringBuilder();
        for (int i = 1; i < str.length() - 1; ++i) {
            char c = str.charAt(i);
            if (c == '\\') {
                if (++i == str.length() - 1) {
                    break; // done
                }
                c = str.charAt(i);
                if (c == 'f') {
                    ret.append("\f");
                } else if (c == 'n') {
                    ret.append("\n");
                } else if (c == 'r') {
                    ret.append("\r");
                } else if (c == 't') {
                    ret.append("\t");
                } else if (c == 'u') {
                    if (++i > str.length() - 4) {
                        break; // done
                    }
                    try {
                        ret.append((char)Integer.parseInt(str.substring(i, i + 4), 16));
                    } catch (NumberFormatException e) {
                        throw new IllegalArgumentException(e);
                    }
                    i += 3;
                } else {
                    ret.append(c);
                }
            } else if (c == quote) {
                throw new IllegalArgumentException();
            } else {
                ret.append(c);
            }
        }
        return ret.toString();
    }

    private static String escape(char c) {
        switch (c) {
        case '\b':
            return "\\b";
        case '\t':
            return "\\t";
        case '\n':
            return "\\n";
        case '\f':
            return "\\f";
        case '\r':
            return "\\r";
        case '\\':
            return "\\\\";
        }
        if (c < 0x20 || c > 0x7e) {
            String unicode = Integer.toString(c, 16);
            return "\\u" + "0000".substring(0, 4 - unicode.length()) + unicode + "";
        }
        return "" + c;
    }

    public static String generateToken(Predicate predicate) {
        TokenBuilder builder = new TokenBuilder();
        for (int c = 0; c <= 0xffff; ++c) {
            if (!predicate.accepts((char)c)) {
                continue;
            }
            builder.add(c);
        }
        return builder.build();
    }

    public static interface Predicate {

        public boolean accepts(char c);
    }

    private static class TokenBuilder {

        final StringBuilder token = new StringBuilder();
        int prevC = -1;
        int fromC = 0;
        int charCnt = 0;

        void add(int c) {
            if (prevC + 1 == c) {
                // in range
            } else {
                flushRange();
                fromC = c;
            }
            prevC = c;
        }

        void flushRange() {
            if (fromC > prevC) {
                return; // handle initial condition
            }
            append(fromC);
            if (fromC < prevC) {
                token.append('-');
                append(prevC);
                ++charCnt;
            }
            token.append(',');
            if (++charCnt > 16) {
                token.append('\n');
                charCnt = 0;
            }
        }

        void append(int c) {
            token.append("\"");
            if (c == '\n') {
                token.append("\\n");
            } else if (c == '\r') {
                token.append("\\r");
            } else if (c == '"') {
                token.append("\\\"");
            } else if (c == '\\') {
                token.append("\\\\");
            } else {
                token.append("\\u").append(String.format("%04x", c & 0xffff));
            }
            token.append("\"");
        }

        String build() {
            flushRange();
            return token.toString();
        }
    }
}