@papyrus-sdk/engine-native 0.2.28 → 0.2.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/android/build.gradle
CHANGED
|
@@ -76,12 +76,117 @@ final class PapyrusNativeTextModel {
|
|
|
76
76
|
if ((charset == StandardCharsets.UTF_16LE || charset == StandardCharsets.UTF_16BE) && (bytes.length - offset) % 2 != 0) {
|
|
77
77
|
throw new IllegalArgumentException("Incomplete UTF-16 code unit");
|
|
78
78
|
}
|
|
79
|
+
try {
|
|
80
|
+
return decodeStrict(bytes, offset, charset);
|
|
81
|
+
} catch (CharacterCodingException utf8Error) {
|
|
82
|
+
if (charset != StandardCharsets.UTF_8 || offset != 0) throw utf8Error;
|
|
83
|
+
String legacy = decodeLegacy(bytes);
|
|
84
|
+
if (legacy == null) throw utf8Error;
|
|
85
|
+
return legacy;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private static final java.util.Set<String> CJK_ENCODINGS = new java.util.HashSet<>(java.util.Arrays.asList(
|
|
90
|
+
"GB18030", "Big5", "Shift_JIS", "EUC-JP", "EUC-KR"));
|
|
91
|
+
private static final java.util.Set<String> LATIN_ENCODINGS = new java.util.HashSet<>(java.util.Arrays.asList(
|
|
92
|
+
"ISO-8859-1", "windows-1252"));
|
|
93
|
+
private static final int MIN_DETECTION_CONFIDENCE = 10;
|
|
94
|
+
private static final int DETECTION_SAMPLE_BYTES = 64 * 1024;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Text that is not UTF-8 and has no BOM (old Chinese, Japanese, Korean or
|
|
98
|
+
* Windows TXT files). The platform's ICU detector picks the encoding; Latin
|
|
99
|
+
* encodings are refused when the bytes look like double-byte CJK, and GB18030
|
|
100
|
+
* is the fallback when no detector is available. Returns null when nothing fits.
|
|
101
|
+
*/
|
|
102
|
+
static String decodeLegacy(byte[] bytes) {
|
|
103
|
+
boolean cjkPairs = looksLikeDoubleByteCjk(bytes);
|
|
104
|
+
for (String name : detectEncodings(bytes)) {
|
|
105
|
+
try {
|
|
106
|
+
if (CJK_ENCODINGS.contains(name)) return decodeStrict(bytes, 0, Charset.forName(name));
|
|
107
|
+
if (LATIN_ENCODINGS.contains(name) && !cjkPairs) {
|
|
108
|
+
try {
|
|
109
|
+
return decodeStrict(bytes, 0, Charset.forName("windows-1252"));
|
|
110
|
+
} catch (CharacterCodingException undefinedInWindows1252) {
|
|
111
|
+
return decodeStrict(bytes, 0, StandardCharsets.ISO_8859_1);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
} catch (CharacterCodingException | IllegalArgumentException ignored) {
|
|
115
|
+
// Try the next candidate.
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (!cjkPairs) return null;
|
|
119
|
+
try {
|
|
120
|
+
return decodeStrict(bytes, 0, Charset.forName("GB18030"));
|
|
121
|
+
} catch (CharacterCodingException | IllegalArgumentException gbError) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Encoding names from ICU's CharsetDetector, most confident first. Loaded by
|
|
128
|
+
* reflection: android.icu exists from API 24, and plain JVM tests use ICU4J
|
|
129
|
+
* (same API). Empty when neither is available.
|
|
130
|
+
*/
|
|
131
|
+
static List<String> detectEncodings(byte[] bytes) {
|
|
132
|
+
List<String> names = new ArrayList<>();
|
|
133
|
+
byte[] sample = bytes.length > DETECTION_SAMPLE_BYTES ? java.util.Arrays.copyOf(bytes, DETECTION_SAMPLE_BYTES) : bytes;
|
|
134
|
+
for (String className : new String[] {"android.icu.text.CharsetDetector", "com.ibm.icu.text.CharsetDetector"}) {
|
|
135
|
+
try {
|
|
136
|
+
Class<?> detectorClass = Class.forName(className);
|
|
137
|
+
Object detector = detectorClass.getConstructor().newInstance();
|
|
138
|
+
detectorClass.getMethod("setText", byte[].class).invoke(detector, (Object) sample);
|
|
139
|
+
Object[] matches = (Object[]) detectorClass.getMethod("detectAll").invoke(detector);
|
|
140
|
+
if (matches == null) return names;
|
|
141
|
+
List<String> found = new ArrayList<>();
|
|
142
|
+
for (Object match : matches) {
|
|
143
|
+
int confidence = (Integer) match.getClass().getMethod("getConfidence").invoke(match);
|
|
144
|
+
if (confidence < MIN_DETECTION_CONFIDENCE) continue;
|
|
145
|
+
found.add((String) match.getClass().getMethod("getName").invoke(match));
|
|
146
|
+
}
|
|
147
|
+
return found;
|
|
148
|
+
} catch (ReflectiveOperationException | RuntimeException | LinkageError unavailable) {
|
|
149
|
+
// Missing, or a "Stub!" android.jar class in JVM tests: try the next implementation.
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return names;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private static String decodeStrict(byte[] bytes, int offset, Charset charset) throws CharacterCodingException {
|
|
79
156
|
return charset.newDecoder()
|
|
80
157
|
.onMalformedInput(CodingErrorAction.REPORT)
|
|
81
158
|
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
|
82
159
|
.decode(ByteBuffer.wrap(bytes, offset, bytes.length - offset)).toString();
|
|
83
160
|
}
|
|
84
161
|
|
|
162
|
+
/**
|
|
163
|
+
* True when the non-ASCII bytes come almost only in high/high pairs, as in
|
|
164
|
+
* GBK/GB18030 Chinese text. Latin-1 text (single accented bytes between
|
|
165
|
+
* ASCII letters) does not qualify, so it keeps failing instead of turning
|
|
166
|
+
* into Chinese mojibake. Mirrors looksLikeDoubleByteCjk in nativeTextModel.ts.
|
|
167
|
+
*/
|
|
168
|
+
static boolean looksLikeDoubleByteCjk(byte[] bytes) {
|
|
169
|
+
int highPairs = 0;
|
|
170
|
+
int other = 0;
|
|
171
|
+
int index = 0;
|
|
172
|
+
while (index < bytes.length) {
|
|
173
|
+
int lead = u(bytes[index]);
|
|
174
|
+
if (lead < 0x80) {
|
|
175
|
+
index += 1;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
int trail = index + 1 < bytes.length ? u(bytes[index + 1]) : -1;
|
|
179
|
+
if (lead >= 0x81 && lead <= 0xfe && trail >= 0x81 && trail <= 0xfe) {
|
|
180
|
+
highPairs += 1;
|
|
181
|
+
index += 2;
|
|
182
|
+
} else {
|
|
183
|
+
other += 1;
|
|
184
|
+
index += trail >= 0x40 && trail <= 0x7e ? 2 : 1;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return highPairs > 0 && highPairs * 5 >= (highPairs + other) * 4;
|
|
188
|
+
}
|
|
189
|
+
|
|
85
190
|
static List<SearchMatch> search(String text, String query) {
|
|
86
191
|
String normalizedQuery = normalize(query).value;
|
|
87
192
|
if (normalizedQuery.isEmpty()) return java.util.Collections.emptyList();
|