aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/text/Ascii.java
blob: 9b9f8c8df465f10bd1b89351922d20f540c60c93 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

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

    public final static char ESCAPE_CHAR = '\\';

    public static String encode(String str, Charset charset, int... requiresEscape) {
        return newEncoder(charset, requiresEscape).encode(str);
    }

    public static String decode(String str, Charset charset) {
        return newDecoder(charset).decode(str);
    }

    public static Encoder newEncoder(Charset charset, int... requiresEscape) {
        switch (requiresEscape.length) {
        case 0:
            return new Encoder(charset, new EmptyPredicate());
        case 1:
            return new Encoder(charset, new SingletonPredicate(requiresEscape[0]));
        default:
            return new Encoder(charset, new ArrayPredicate(requiresEscape));
        }
    }

    public static Decoder newDecoder(Charset charset) {
        return new Decoder(charset);
    }

    public static class Encoder {

        private final Charset charset;
        private final EncodePredicate predicate;

        private Encoder(Charset charset, EncodePredicate predicate) {
            this.charset = charset;
            this.predicate = predicate;
        }

        public String encode(String str) {
            StringBuilder out = new StringBuilder();
            for (int c : new CodePointSequence(str)) {
                if (c < 0x20 || c >= 0x7F || c == ESCAPE_CHAR || predicate.requiresEscape(c)) {
                    escape(c, out);
                } else {
                    out.appendCodePoint(c);
                }
            }
            return out.toString();
        }

        private void escape(int c, StringBuilder out) {
            switch (c) {
            case ESCAPE_CHAR:
                out.append(ESCAPE_CHAR).append(ESCAPE_CHAR);
                break;
            case '\f':
                out.append(ESCAPE_CHAR).append("f");
                break;
            case '\n':
                out.append(ESCAPE_CHAR).append("n");
                break;
            case '\r':
                out.append(ESCAPE_CHAR).append("r");
                break;
            case '\t':
                out.append(ESCAPE_CHAR).append("t");
                break;
            default:
                ByteBuffer buf = charset.encode(CharBuffer.wrap(Character.toChars(c)));
                while (buf.hasRemaining()) {
                    out.append(ESCAPE_CHAR).append(String.format("x%02X", buf.get()));
                }
                break;
            }
        }
    }

    public static class Decoder {

        private final Charset charset;

        private Decoder(Charset charset) {
            this.charset = charset;
        }

        public String decode(String str) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            for (Iterator<Integer> it = new CodePointIterator(str); it.hasNext(); ) {
                int c = it.next();
                if (c == ESCAPE_CHAR) {
                    unescape(it, out);
                } else {
                    ByteBuffer buf = charset.encode(CharBuffer.wrap(Character.toChars(c)));
                    while (buf.hasRemaining()) {
                        out.write(buf.get());
                    }
                }
            }
            return new String(out.toByteArray(), charset);
        }

        private void unescape(Iterator<Integer> it, ByteArrayOutputStream out) {
            int c = it.next();
            switch (c) {
            case 'f':
                out.write('\f');
                break;
            case 'n':
                out.write('\n');
                break;
            case 'r':
                out.write('\r');
                break;
            case 't':
                out.write('\t');
                break;
            case 'x':
                int x1 = it.next();
                int x2 = it.next();
                out.write((Character.digit(x1, 16) << 4) +
                          (Character.digit(x2, 16)));
                break;
            default:
                out.write(c);
                break;
            }
        }
    }

    private static interface EncodePredicate {

        boolean requiresEscape(int codePoint);
    }

    private static class EmptyPredicate implements EncodePredicate {

        @Override
        public boolean requiresEscape(int codePoint) {
            return false;
        }
    }

    private static class SingletonPredicate implements EncodePredicate {

        final int requiresEscape;

        private SingletonPredicate(int requiresEscape) {
            this.requiresEscape = requiresEscape;
        }

        @Override
        public boolean requiresEscape(int codePoint) {
            return codePoint == requiresEscape;
        }
    }

    private static class ArrayPredicate implements EncodePredicate {

        final Set<Integer> requiresEscape = new TreeSet<>();

        private ArrayPredicate(int[] requiresEscape) {
            for (int codePoint : requiresEscape) {
                this.requiresEscape.add(codePoint);
            }
        }

        @Override
        public boolean requiresEscape(int codePoint) {
            return requiresEscape.contains(codePoint);
        }
    }

    private static class CodePointSequence implements Iterable<Integer> {

        final String str;

        CodePointSequence(String str) {
            this.str = str;
        }

        @Override
        public Iterator<Integer> iterator() {
            return new CodePointIterator(str);
        }
    }

    private static class CodePointIterator implements Iterator<Integer> {

        final String str;
        int idx = 0;

        CodePointIterator(String str) {
            this.str = str;
        }

        @Override
        public boolean hasNext() {
            return idx < str.length();
        }

        @Override
        public Integer next() {
            int c = str.codePointAt(idx);
            idx += Character.charCount(c);
            return c;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}