-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnicodeEscapeReplacer.java
198 lines (168 loc) · 7.22 KB
/
UnicodeEscapeReplacer.java
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
/*
* MIT License
*
* Copyright (c) 2020 NUM Technology Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package uk.modl.utils;
import lombok.val;
import lombok.var;
class UnicodeEscapeReplacer {
private static final String BACKSLASH_U = "\\u";
private static final String TILDE_U = "~u";
private static final char TILDE = '~';
private static final char BACKSLASH = '\\';
private static final int HEX = 16;
/**
* Convert explicit unicode escape sequences to unicode characters.
* (recursive implementation)
*
* @param str a String possible containing escape sequences.
* @return the string with escape sequences converted to unicode characters.
*/
static String convertUnicodeSequences(final String str) {
int start = 0;
var result = str;
while (result != null) {
// We could have a backslash-u escape sequence or a ~u escape sequence
val backslashUIndex = result.indexOf(BACKSLASH_U, start);
val tildeUIndex = result.indexOf(TILDE_U, start);
// Filter out cases with no escape sequences.
int unicodeStrIdx;
if (tildeUIndex < 0 && backslashUIndex < 0) {
break;
} else if (tildeUIndex < 0) {
unicodeStrIdx = backslashUIndex;// No ~? Must be backslash
} else if (backslashUIndex < 0) {
unicodeStrIdx = tildeUIndex;// No backslash? Must be ~
} else {
// Pick the first escaped character and proceed with that one.
unicodeStrIdx = Math.min(backslashUIndex, tildeUIndex);
}
val tryParse = tryParse(result, unicodeStrIdx + 2);
// Next time round the loop we start searching after the current escape sequence.
start = unicodeStrIdx + 1;
// If the escape sequence is itself escaped then don't replace it
if (!escapeSequenceIsEscaped(result, unicodeStrIdx)) {
// Get the codepoint value and replace the escape sequence
if (tryParse.codePoint > 0) {
val chars = Character.toChars(tryParse.codePoint);
result = replace(result, chars, unicodeStrIdx, tryParse.length + 2);
}
}
}
return result;
}
private static boolean escapeSequenceIsEscaped(final String result, final int unicodeStrIdx) {
return unicodeStrIdx > 0 && (result.charAt(unicodeStrIdx - 1) == TILDE || result.charAt(unicodeStrIdx - 1) == BACKSLASH);
}
/**
* Replace a unicode value in a String
*
* @param s the String with the unicode value to be replaced.
* @param value the replacement character
* @param unicodeStrIdx the index of the unicode escape sequence
* @param length the number of characters to be replaced.
* @return a String with the unicode escape sequence replaced by the replacement character
*/
private static String replace(final String s, final char[] value, final int unicodeStrIdx, final int length) {
val left = s.substring(0, unicodeStrIdx);
val end = Math.min(s.length(), unicodeStrIdx + length);
val right = s.substring(end);
return left + String.valueOf(value) + right;
}
/**
* Check whether the value is a valid unicode codepoint
*
* @param value the int to check
* @return true if the value is a valid unicode codepoint
*/
private static boolean isValidRange(final int value) {
return (value >= 0x100000 && value <= 0x10ffff) || (value >= 0x10000 && value <= 0xfffff) || (value >= 0 && value <= 0xd7ff) || (value >= 0xe000 && value <= 0xffff);
}
/**
* Can we get `n` hex digits from the string at the `idx` location?
*
* @param s the String to check
* @param idx the index to start searching in the string
* @param n the number of digits needed
* @return true if enough digits are available
*/
private static boolean hasEnoughDigits(final String s, final int idx, final int n) {
var i = 0;
var chars = s.toCharArray();
while (i < n && (idx + i) < s.length()) {
var c = chars[idx + i];
if (!Character.isDigit(c) && !("abcdefABCDEF".indexOf(c) > -1)) {
return false;
}
i++;
}
return i == n;
}
/**
* Attempt to parse a unicode character starting at `idx` in `str`
*
* @param str the String to parse
* @param idx the starting index
* @return a TryParseResult with codepoint set to 0 on failure.
*/
private static TryParseResult tryParse(final String str, final int idx) {
// Check for a 6-digit unicode value
if (hasEnoughDigits(str, idx, 6)) {
val value = getPossibleUnicodeValue(str, idx, 6);
if (isValidRange(value)) {
return new TryParseResult(value, 6);
}
}
// Check for a 5-digit unicode value
if (hasEnoughDigits(str, idx, 5)) {
val value = getPossibleUnicodeValue(str, idx, 5);
if (isValidRange(value)) {
return new TryParseResult(value, 5);
}
}
// Check for a 4-digit unicode value
if (hasEnoughDigits(str, idx, 4)) {
val value = getPossibleUnicodeValue(str, idx, 4);
if (isValidRange(value)) {
return new TryParseResult(value, 4);
}
}
// Failed
return new TryParseResult(0, 4);
}
private static int getPossibleUnicodeValue(final String str, final int idx, final int i) {
return Integer.parseInt(str.substring(idx, idx + i), HEX);
}
/**
* Class to hold the result of the tryParse method
*/
private static class TryParseResult {
final int codePoint;
final int length;
/**
* Constructor
*
* @param codePoint the codepoint that was found, or zero
* @param length the number of characters used by the codepoint.
*/
private TryParseResult(final int codePoint, final int length) {
this.codePoint = codePoint;
this.length = length;
}
}
}