Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ********************************************************************** |
michael@0 | 3 | * Copyright (c) 2001-2011, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ********************************************************************** |
michael@0 | 6 | * Date Name Description |
michael@0 | 7 | * 11/19/2001 aliu Creation. |
michael@0 | 8 | ********************************************************************** |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "unicode/utypes.h" |
michael@0 | 12 | |
michael@0 | 13 | #if !UCONFIG_NO_TRANSLITERATION |
michael@0 | 14 | |
michael@0 | 15 | #include "unicode/utf16.h" |
michael@0 | 16 | #include "esctrn.h" |
michael@0 | 17 | #include "util.h" |
michael@0 | 18 | |
michael@0 | 19 | U_NAMESPACE_BEGIN |
michael@0 | 20 | |
michael@0 | 21 | static const UChar UNIPRE[] = {85,43,0}; // "U+" |
michael@0 | 22 | static const UChar BS_u[] = {92,117,0}; // "\\u" |
michael@0 | 23 | static const UChar BS_U[] = {92,85,0}; // "\\U" |
michael@0 | 24 | static const UChar XMLPRE[] = {38,35,120,0}; // "&#x" |
michael@0 | 25 | static const UChar XML10PRE[] = {38,35,0}; // "&#" |
michael@0 | 26 | static const UChar PERLPRE[] = {92,120,123,0}; // "\\x{" |
michael@0 | 27 | static const UChar SEMI[] = {59,0}; // ";" |
michael@0 | 28 | static const UChar RBRACE[] = {125,0}; // "}" |
michael@0 | 29 | |
michael@0 | 30 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EscapeTransliterator) |
michael@0 | 31 | |
michael@0 | 32 | /** |
michael@0 | 33 | * Factory methods |
michael@0 | 34 | */ |
michael@0 | 35 | static Transliterator* _createEscUnicode(const UnicodeString& ID, Transliterator::Token /*context*/) { |
michael@0 | 36 | // Unicode: "U+10FFFF" hex, min=4, max=6 |
michael@0 | 37 | return new EscapeTransliterator(ID, UnicodeString(TRUE, UNIPRE, 2), UnicodeString(), 16, 4, TRUE, NULL); |
michael@0 | 38 | } |
michael@0 | 39 | static Transliterator* _createEscJava(const UnicodeString& ID, Transliterator::Token /*context*/) { |
michael@0 | 40 | // Java: "\\uFFFF" hex, min=4, max=4 |
michael@0 | 41 | return new EscapeTransliterator(ID, UnicodeString(TRUE, BS_u, 2), UnicodeString(), 16, 4, FALSE, NULL); |
michael@0 | 42 | } |
michael@0 | 43 | static Transliterator* _createEscC(const UnicodeString& ID, Transliterator::Token /*context*/) { |
michael@0 | 44 | // C: "\\uFFFF" hex, min=4, max=4; \\U0010FFFF hex, min=8, max=8 |
michael@0 | 45 | return new EscapeTransliterator(ID, UnicodeString(TRUE, BS_u, 2), UnicodeString(), 16, 4, TRUE, |
michael@0 | 46 | new EscapeTransliterator(UnicodeString(), UnicodeString(TRUE, BS_U, 2), UnicodeString(), 16, 8, TRUE, NULL)); |
michael@0 | 47 | } |
michael@0 | 48 | static Transliterator* _createEscXML(const UnicodeString& ID, Transliterator::Token /*context*/) { |
michael@0 | 49 | // XML: "" hex, min=1, max=6 |
michael@0 | 50 | return new EscapeTransliterator(ID, UnicodeString(TRUE, XMLPRE, 3), UnicodeString(SEMI[0]), 16, 1, TRUE, NULL); |
michael@0 | 51 | } |
michael@0 | 52 | static Transliterator* _createEscXML10(const UnicodeString& ID, Transliterator::Token /*context*/) { |
michael@0 | 53 | // XML10: "&1114111;" dec, min=1, max=7 (not really "Any-Hex") |
michael@0 | 54 | return new EscapeTransliterator(ID, UnicodeString(TRUE, XML10PRE, 2), UnicodeString(SEMI[0]), 10, 1, TRUE, NULL); |
michael@0 | 55 | } |
michael@0 | 56 | static Transliterator* _createEscPerl(const UnicodeString& ID, Transliterator::Token /*context*/) { |
michael@0 | 57 | // Perl: "\\x{263A}" hex, min=1, max=6 |
michael@0 | 58 | return new EscapeTransliterator(ID, UnicodeString(TRUE, PERLPRE, 3), UnicodeString(RBRACE[0]), 16, 1, TRUE, NULL); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | /** |
michael@0 | 62 | * Registers standard variants with the system. Called by |
michael@0 | 63 | * Transliterator during initialization. |
michael@0 | 64 | */ |
michael@0 | 65 | void EscapeTransliterator::registerIDs() { |
michael@0 | 66 | Token t = integerToken(0); |
michael@0 | 67 | |
michael@0 | 68 | Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-Hex/Unicode"), _createEscUnicode, t); |
michael@0 | 69 | |
michael@0 | 70 | Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-Hex/Java"), _createEscJava, t); |
michael@0 | 71 | |
michael@0 | 72 | Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-Hex/C"), _createEscC, t); |
michael@0 | 73 | |
michael@0 | 74 | Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-Hex/XML"), _createEscXML, t); |
michael@0 | 75 | |
michael@0 | 76 | Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-Hex/XML10"), _createEscXML10, t); |
michael@0 | 77 | |
michael@0 | 78 | Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-Hex/Perl"), _createEscPerl, t); |
michael@0 | 79 | |
michael@0 | 80 | Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-Hex"), _createEscJava, t); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | /** |
michael@0 | 84 | * Constructs an escape transliterator with the given ID and |
michael@0 | 85 | * parameters. See the class member documentation for details. |
michael@0 | 86 | */ |
michael@0 | 87 | EscapeTransliterator::EscapeTransliterator(const UnicodeString& newID, |
michael@0 | 88 | const UnicodeString& _prefix, const UnicodeString& _suffix, |
michael@0 | 89 | int32_t _radix, int32_t _minDigits, |
michael@0 | 90 | UBool _grokSupplementals, |
michael@0 | 91 | EscapeTransliterator* adoptedSupplementalHandler) : |
michael@0 | 92 | Transliterator(newID, NULL) |
michael@0 | 93 | { |
michael@0 | 94 | this->prefix = _prefix; |
michael@0 | 95 | this->suffix = _suffix; |
michael@0 | 96 | this->radix = _radix; |
michael@0 | 97 | this->minDigits = _minDigits; |
michael@0 | 98 | this->grokSupplementals = _grokSupplementals; |
michael@0 | 99 | this->supplementalHandler = adoptedSupplementalHandler; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | /** |
michael@0 | 103 | * Copy constructor. |
michael@0 | 104 | */ |
michael@0 | 105 | EscapeTransliterator::EscapeTransliterator(const EscapeTransliterator& o) : |
michael@0 | 106 | Transliterator(o), |
michael@0 | 107 | prefix(o.prefix), |
michael@0 | 108 | suffix(o.suffix), |
michael@0 | 109 | radix(o.radix), |
michael@0 | 110 | minDigits(o.minDigits), |
michael@0 | 111 | grokSupplementals(o.grokSupplementals) { |
michael@0 | 112 | supplementalHandler = (o.supplementalHandler != 0) ? |
michael@0 | 113 | new EscapeTransliterator(*o.supplementalHandler) : NULL; |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | EscapeTransliterator::~EscapeTransliterator() { |
michael@0 | 117 | delete supplementalHandler; |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | /** |
michael@0 | 121 | * Transliterator API. |
michael@0 | 122 | */ |
michael@0 | 123 | Transliterator* EscapeTransliterator::clone() const { |
michael@0 | 124 | return new EscapeTransliterator(*this); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * Implements {@link Transliterator#handleTransliterate}. |
michael@0 | 129 | */ |
michael@0 | 130 | void EscapeTransliterator::handleTransliterate(Replaceable& text, |
michael@0 | 131 | UTransPosition& pos, |
michael@0 | 132 | UBool /*isIncremental*/) const |
michael@0 | 133 | { |
michael@0 | 134 | /* TODO: Verify that isIncremental can be ignored */ |
michael@0 | 135 | int32_t start = pos.start; |
michael@0 | 136 | int32_t limit = pos.limit; |
michael@0 | 137 | |
michael@0 | 138 | UnicodeString buf(prefix); |
michael@0 | 139 | int32_t prefixLen = prefix.length(); |
michael@0 | 140 | UBool redoPrefix = FALSE; |
michael@0 | 141 | |
michael@0 | 142 | while (start < limit) { |
michael@0 | 143 | int32_t c = grokSupplementals ? text.char32At(start) : text.charAt(start); |
michael@0 | 144 | int32_t charLen = grokSupplementals ? U16_LENGTH(c) : 1; |
michael@0 | 145 | |
michael@0 | 146 | if ((c & 0xFFFF0000) != 0 && supplementalHandler != NULL) { |
michael@0 | 147 | buf.truncate(0); |
michael@0 | 148 | buf.append(supplementalHandler->prefix); |
michael@0 | 149 | ICU_Utility::appendNumber(buf, c, supplementalHandler->radix, |
michael@0 | 150 | supplementalHandler->minDigits); |
michael@0 | 151 | buf.append(supplementalHandler->suffix); |
michael@0 | 152 | redoPrefix = TRUE; |
michael@0 | 153 | } else { |
michael@0 | 154 | if (redoPrefix) { |
michael@0 | 155 | buf.truncate(0); |
michael@0 | 156 | buf.append(prefix); |
michael@0 | 157 | redoPrefix = FALSE; |
michael@0 | 158 | } else { |
michael@0 | 159 | buf.truncate(prefixLen); |
michael@0 | 160 | } |
michael@0 | 161 | ICU_Utility::appendNumber(buf, c, radix, minDigits); |
michael@0 | 162 | buf.append(suffix); |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | text.handleReplaceBetween(start, start + charLen, buf); |
michael@0 | 166 | start += buf.length(); |
michael@0 | 167 | limit += buf.length() - charLen; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | pos.contextLimit += limit - pos.limit; |
michael@0 | 171 | pos.limit = limit; |
michael@0 | 172 | pos.start = start; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | U_NAMESPACE_END |
michael@0 | 176 | |
michael@0 | 177 | #endif /* #if !UCONFIG_NO_TRANSLITERATION */ |
michael@0 | 178 | |
michael@0 | 179 | //eof |