Home | History | Annotate | Download | only in i18n
      1 /*
      2 **********************************************************************
      3 *   Copyright (C) 2001-2011, International Business Machines
      4 *   Corporation and others.  All Rights Reserved.
      5 **********************************************************************
      6 *   Date        Name        Description
      7 *   06/07/01    aliu        Creation.
      8 **********************************************************************
      9 */
     10 
     11 #include "unicode/utypes.h"
     12 
     13 #if !UCONFIG_NO_TRANSLITERATION
     14 
     15 #include "unicode/unifilt.h"
     16 #include "unicode/uchar.h"
     17 #include "unicode/uniset.h"
     18 #include "cmemory.h"
     19 #include "name2uni.h"
     20 #include "patternprops.h"
     21 #include "uprops.h"
     22 #include "uinvchar.h"
     23 #include "util.h"
     24 
     25 U_NAMESPACE_BEGIN
     26 
     27 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(NameUnicodeTransliterator)
     28 
     29 static const UChar OPEN[] = {92,78,126,123,126,0}; // "\N~{~"
     30 static const UChar OPEN_DELIM  = 92;  // '\\' first char of OPEN
     31 static const UChar CLOSE_DELIM = 125; // '}'
     32 static const UChar SPACE       = 32;  // ' '
     33 
     34 U_CDECL_BEGIN
     35 
     36 // USetAdder implementation
     37 // Does not use uset.h to reduce code dependencies
     38 static void U_CALLCONV
     39 _set_add(USet *set, UChar32 c) {
     40     uset_add(set, c);
     41 }
     42 
     43 // These functions aren't used.
     44 /*static void U_CALLCONV
     45 _set_addRange(USet *set, UChar32 start, UChar32 end) {
     46     ((UnicodeSet *)set)->add(start, end);
     47 }
     48 
     49 static void U_CALLCONV
     50 _set_addString(USet *set, const UChar *str, int32_t length) {
     51     ((UnicodeSet *)set)->add(UnicodeString((UBool)(length<0), str, length));
     52 }*/
     53 
     54 U_CDECL_END
     55 
     56 /**
     57  * Constructs a transliterator with the default delimiters '{' and
     58  * '}'.
     59  */
     60 NameUnicodeTransliterator::NameUnicodeTransliterator(UnicodeFilter* adoptedFilter) :
     61     Transliterator(UNICODE_STRING("Name-Any", 8), adoptedFilter) {
     62 
     63     UnicodeSet *legalPtr = &legal;
     64     // Get the legal character set
     65     USetAdder sa = {
     66         (USet *)legalPtr, // USet* == UnicodeSet*
     67         _set_add,
     68         NULL, // Don't need _set_addRange
     69         NULL, // Don't need _set_addString
     70         NULL, // Don't need remove()
     71         NULL
     72     };
     73     uprv_getCharNameCharacters(&sa);
     74 }
     75 
     76 /**
     77  * Destructor.
     78  */
     79 NameUnicodeTransliterator::~NameUnicodeTransliterator() {}
     80 
     81 /**
     82  * Copy constructor.
     83  */
     84 NameUnicodeTransliterator::NameUnicodeTransliterator(const NameUnicodeTransliterator& o) :
     85     Transliterator(o), legal(o.legal) {}
     86 
     87 /**
     88  * Assignment operator.
     89  */
     90 /*NameUnicodeTransliterator& NameUnicodeTransliterator::operator=(
     91                              const NameUnicodeTransliterator& o) {
     92     Transliterator::operator=(o);
     93     // not necessary: the legal sets should all be the same -- legal=o.legal;
     94     return *this;
     95 }*/
     96 
     97 /**
     98  * Transliterator API.
     99  */
    100 Transliterator* NameUnicodeTransliterator::clone(void) const {
    101     return new NameUnicodeTransliterator(*this);
    102 }
    103 
    104 /**
    105  * Implements {@link Transliterator#handleTransliterate}.
    106  */
    107 void NameUnicodeTransliterator::handleTransliterate(Replaceable& text, UTransPosition& offsets,
    108                                                     UBool isIncremental) const {
    109     // The failure mode, here and below, is to behave like Any-Null,
    110     // if either there is no name data (max len == 0) or there is no
    111     // memory (malloc() => NULL).
    112 
    113     int32_t maxLen = uprv_getMaxCharNameLength();
    114     if (maxLen == 0) {
    115         offsets.start = offsets.limit;
    116         return;
    117     }
    118 
    119     // Accomodate the longest possible name
    120     ++maxLen; // allow for temporary trailing space
    121     char* cbuf = (char*) uprv_malloc(maxLen);
    122     if (cbuf == NULL) {
    123         offsets.start = offsets.limit;
    124         return;
    125     }
    126 
    127     UnicodeString openPat(TRUE, OPEN, -1);
    128     UnicodeString str, name;
    129 
    130     int32_t cursor = offsets.start;
    131     int32_t limit = offsets.limit;
    132 
    133     // Modes:
    134     // 0 - looking for open delimiter
    135     // 1 - after open delimiter
    136     int32_t mode = 0;
    137     int32_t openPos = -1; // open delim candidate pos
    138 
    139     UChar32 c;
    140     while (cursor < limit) {
    141         c = text.char32At(cursor);
    142 
    143         switch (mode) {
    144         case 0: // looking for open delimiter
    145             if (c == OPEN_DELIM) { // quick check first
    146                 openPos = cursor;
    147                 int32_t i =
    148                     ICU_Utility::parsePattern(openPat, text, cursor, limit);
    149                 if (i >= 0 && i < limit) {
    150                     mode = 1;
    151                     name.truncate(0);
    152                     cursor = i;
    153                     continue; // *** reprocess char32At(cursor)
    154                 }
    155             }
    156             break;
    157 
    158         case 1: // after open delimiter
    159             // Look for legal chars.  If \s+ is found, convert it
    160             // to a single space.  If closeDelimiter is found, exit
    161             // the loop.  If any other character is found, exit the
    162             // loop.  If the limit is reached, exit the loop.
    163 
    164             // Convert \s+ => SPACE.  This assumes there are no
    165             // runs of >1 space characters in names.
    166             if (PatternProps::isWhiteSpace(c)) {
    167                 // Ignore leading whitespace
    168                 if (name.length() > 0 &&
    169                     name.charAt(name.length()-1) != SPACE) {
    170                     name.append(SPACE);
    171                     // If we are too long then abort.  maxLen includes
    172                     // temporary trailing space, so use '>'.
    173                     if (name.length() > maxLen) {
    174                         mode = 0;
    175                     }
    176                 }
    177                 break;
    178             }
    179 
    180             if (c == CLOSE_DELIM) {
    181                 int32_t len = name.length();
    182 
    183                 // Delete trailing space, if any
    184                 if (len > 0 &&
    185                     name.charAt(len-1) == SPACE) {
    186                     --len;
    187                 }
    188 
    189                 if (uprv_isInvariantUString(name.getBuffer(), len)) {
    190                     name.extract(0, len, cbuf, maxLen, US_INV);
    191 
    192                     UErrorCode status = U_ZERO_ERROR;
    193                     c = u_charFromName(U_EXTENDED_CHAR_NAME, cbuf, &status);
    194                     if (U_SUCCESS(status)) {
    195                         // Lookup succeeded
    196 
    197                         // assert(UTF_CHAR_LENGTH(CLOSE_DELIM) == 1);
    198                         cursor++; // advance over CLOSE_DELIM
    199 
    200                         str.truncate(0);
    201                         str.append(c);
    202                         text.handleReplaceBetween(openPos, cursor, str);
    203 
    204                         // Adjust indices for the change in the length of
    205                         // the string.  Do not assume that str.length() ==
    206                         // 1, in case of surrogates.
    207                         int32_t delta = cursor - openPos - str.length();
    208                         cursor -= delta;
    209                         limit -= delta;
    210                         // assert(cursor == openPos + str.length());
    211                     }
    212                 }
    213                 // If the lookup failed, we leave things as-is and
    214                 // still switch to mode 0 and continue.
    215                 mode = 0;
    216                 openPos = -1; // close off candidate
    217                 continue; // *** reprocess char32At(cursor)
    218             }
    219 
    220             // Check if c is a legal char.  We assume here that
    221             // legal.contains(OPEN_DELIM) is FALSE, so when we abort a
    222             // name, we don't have to go back to openPos+1.
    223             if (legal.contains(c)) {
    224                 name.append(c);
    225                 // If we go past the longest possible name then abort.
    226                 // maxLen includes temporary trailing space, so use '>='.
    227                 if (name.length() >= maxLen) {
    228                     mode = 0;
    229                 }
    230             }
    231 
    232             // Invalid character
    233             else {
    234                 --cursor; // Backup and reprocess this character
    235                 mode = 0;
    236             }
    237 
    238             break;
    239         }
    240 
    241         cursor += UTF_CHAR_LENGTH(c);
    242     }
    243 
    244     offsets.contextLimit += limit - offsets.limit;
    245     offsets.limit = limit;
    246     // In incremental mode, only advance the cursor up to the last
    247     // open delimiter candidate.
    248     offsets.start = (isIncremental && openPos >= 0) ? openPos : cursor;
    249 
    250     uprv_free(cbuf);
    251 }
    252 
    253 U_NAMESPACE_END
    254 
    255 #endif /* #if !UCONFIG_NO_TRANSLITERATION */
    256