1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2001-2011, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Date Name Description
9 * 11/19/2001 aliu Creation.
10 **********************************************************************
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_TRANSLITERATION
17 #include "unicode/uchar.h"
18 #include "unicode/utf16.h"
27 * Special character marking the end of the spec[] array.
29 static const UChar END = 0xFFFF;
31 // Unicode: "U+10FFFF" hex, min=4, max=6
32 static const UChar SPEC_Unicode[] = {
33 2, 0, 16, 4, 6, 85/*U*/, 43/*+*/,
37 // Java: "\\uFFFF" hex, min=4, max=4
38 static const UChar SPEC_Java[] = {
39 2, 0, 16, 4, 4, 92/*\*/, 117/*u*/,
43 // C: "\\uFFFF" hex, min=4, max=4; \\U0010FFFF hex, min=8, max=8
44 static const UChar SPEC_C[] = {
45 2, 0, 16, 4, 4, 92/*\*/, 117/*u*/,
46 2, 0, 16, 8, 8, 92/*\*/, 85/*U*/,
50 // XML: "" hex, min=1, max=6
51 static const UChar SPEC_XML[] = {
52 3, 1, 16, 1, 6, 38/*&*/, 35/*#*/, 120/*x*/, 59/*;*/,
56 // XML10: "" dec, min=1, max=7 (not really "Hex-Any")
57 static const UChar SPEC_XML10[] = {
58 2, 1, 10, 1, 7, 38/*&*/, 35/*#*/, 59/*;*/,
62 // Perl: "\\x{263A}" hex, min=1, max=6
63 static const UChar SPEC_Perl[] = {
64 3, 1, 16, 1, 6, 92/*\*/, 120/*x*/, 123/*{*/, 125/*}*/,
68 // All: Java, C, Perl, XML, XML10, Unicode
69 static const UChar SPEC_Any[] = {
70 2, 0, 16, 4, 6, 85/*U*/, 43/*+*/, // Unicode
71 2, 0, 16, 4, 4, 92/*\*/, 117/*u*/, // Java
72 2, 0, 16, 8, 8, 92/*\*/, 85/*U*/, // C (surrogates)
73 3, 1, 16, 1, 6, 38/*&*/, 35/*#*/, 120/*x*/, 59/*;*/, // XML
74 2, 1, 10, 1, 7, 38/*&*/, 35/*#*/, 59/*;*/, // XML10
75 3, 1, 16, 1, 6, 92/*\*/, 120/*x*/, 123/*{*/, 125/*}*/, // Perl
79 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UnescapeTransliterator)
81 static UChar* copySpec(const UChar* spec) {
83 while (spec[len] != END) {
87 UChar *result = (UChar *)uprv_malloc(len*sizeof(UChar));
88 // Check for memory allocation error.
90 uprv_memcpy(result, spec, (size_t)len*sizeof(result[0]));
96 * Factory methods. Ignore the context.
98 static Transliterator* _createUnicode(const UnicodeString& ID, Transliterator::Token /*context*/) {
99 return new UnescapeTransliterator(ID, SPEC_Unicode);
101 static Transliterator* _createJava(const UnicodeString& ID, Transliterator::Token /*context*/) {
102 return new UnescapeTransliterator(ID, SPEC_Java);
104 static Transliterator* _createC(const UnicodeString& ID, Transliterator::Token /*context*/) {
105 return new UnescapeTransliterator(ID, SPEC_C);
107 static Transliterator* _createXML(const UnicodeString& ID, Transliterator::Token /*context*/) {
108 return new UnescapeTransliterator(ID, SPEC_XML);
110 static Transliterator* _createXML10(const UnicodeString& ID, Transliterator::Token /*context*/) {
111 return new UnescapeTransliterator(ID, SPEC_XML10);
113 static Transliterator* _createPerl(const UnicodeString& ID, Transliterator::Token /*context*/) {
114 return new UnescapeTransliterator(ID, SPEC_Perl);
116 static Transliterator* _createAny(const UnicodeString& ID, Transliterator::Token /*context*/) {
117 return new UnescapeTransliterator(ID, SPEC_Any);
121 * Registers standard variants with the system. Called by
122 * Transliterator during initialization.
124 void UnescapeTransliterator::registerIDs() {
125 Token t = integerToken(0);
127 Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Hex-Any/Unicode"), _createUnicode, t);
129 Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Hex-Any/Java"), _createJava, t);
131 Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Hex-Any/C"), _createC, t);
133 Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Hex-Any/XML"), _createXML, t);
135 Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Hex-Any/XML10"), _createXML10, t);
137 Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Hex-Any/Perl"), _createPerl, t);
139 Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Hex-Any"), _createAny, t);
143 * Constructor. Takes the encoded spec array.
145 UnescapeTransliterator::UnescapeTransliterator(const UnicodeString& newID,
146 const UChar *newSpec) :
147 Transliterator(newID, NULL)
149 this->spec = copySpec(newSpec);
155 UnescapeTransliterator::UnescapeTransliterator(const UnescapeTransliterator& o) :
157 this->spec = copySpec(o.spec);
160 UnescapeTransliterator::~UnescapeTransliterator() {
165 * Transliterator API.
167 Transliterator* UnescapeTransliterator::clone() const {
168 return new UnescapeTransliterator(*this);
172 * Implements {@link Transliterator#handleTransliterate}.
174 void UnescapeTransliterator::handleTransliterate(Replaceable& text, UTransPosition& pos,
175 UBool isIncremental) const {
176 int32_t start = pos.start;
177 int32_t limit = pos.limit;
180 while (start < limit) {
181 // Loop over the forms in spec[]. Exit this loop when we
182 // match one of the specs. Exit the outer loop if a
183 // partial match is detected and isIncremental is true.
184 for (j=0, ipat=0; spec[ipat] != END; ++j) {
187 int32_t prefixLen = spec[ipat++];
188 int32_t suffixLen = spec[ipat++];
189 int8_t radix = (int8_t) spec[ipat++];
190 int32_t minDigits = spec[ipat++];
191 int32_t maxDigits = spec[ipat++];
193 // s is a copy of start that is advanced over the
194 // characters as we parse them.
198 for (i=0; i<prefixLen; ++i) {
201 // We've already matched a character. This is
202 // a partial match, so we return if in
203 // incremental mode. In non-incremental mode,
204 // go to the next spec.
212 UChar c = text.charAt(s++);
213 if (c != spec[ipat + i]) {
221 int32_t digitCount = 0;
224 // Check for partial match in incremental mode.
225 if (s > start && isIncremental) {
230 UChar32 ch = text.char32At(s);
231 int32_t digit = u_digit(ch, radix);
236 u = (u * radix) + digit;
237 if (++digitCount == maxDigits) {
242 match = (digitCount >= minDigits);
245 for (i=0; i<suffixLen; ++i) {
247 // Check for partial match in incremental mode.
248 if (s > start && isIncremental) {
254 UChar c = text.charAt(s++);
255 if (c != spec[ipat + prefixLen + i]) {
262 // At this point, we have a match
263 UnicodeString str(u);
264 text.handleReplaceBetween(start, s, str);
265 limit -= s - start - str.length();
266 // The following break statement leaves the
267 // loop that is traversing the forms in
268 // spec[]. We then parse the next input
275 ipat += prefixLen + suffixLen;
279 start += U16_LENGTH(text.char32At(start));
284 pos.contextLimit += limit - pos.limit;
291 #endif /* #if !UCONFIG_NO_TRANSLITERATION */