Home | History | Annotate | Download | only in i18n
      1 /*
      2 *******************************************************************************
      3 * Copyright (C) 2007-2010, International Business Machines Corporation and
      4 * others. All Rights Reserved.
      5 *******************************************************************************
      6 *
      7 * File DTPTNGEN.CPP
      8 *
      9 *******************************************************************************
     10 */
     11 
     12 #include "unicode/utypes.h"
     13 #if !UCONFIG_NO_FORMATTING
     14 
     15 #include "unicode/datefmt.h"
     16 #include "unicode/decimfmt.h"
     17 #include "unicode/dtfmtsym.h"
     18 #include "unicode/dtptngen.h"
     19 #include "unicode/msgfmt.h"
     20 #include "unicode/smpdtfmt.h"
     21 #include "unicode/udat.h"
     22 #include "unicode/udatpg.h"
     23 #include "unicode/uniset.h"
     24 #include "unicode/uloc.h"
     25 #include "unicode/ures.h"
     26 #include "unicode/ustring.h"
     27 #include "unicode/rep.h"
     28 #include "cpputils.h"
     29 #include "ucln_in.h"
     30 #include "mutex.h"
     31 #include "cmemory.h"
     32 #include "cstring.h"
     33 #include "locbased.h"
     34 #include "gregoimp.h"
     35 #include "hash.h"
     36 #include "uresimp.h"
     37 #include "dtptngen_impl.h"
     38 
     39 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
     40 
     41 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
     42 /**
     43  * If we are on EBCDIC, use an iterator which will
     44  * traverse the bundles in ASCII order.
     45  */
     46 #define U_USE_ASCII_BUNDLE_ITERATOR
     47 #define U_SORT_ASCII_BUNDLE_ITERATOR
     48 #endif
     49 
     50 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
     51 
     52 #include "unicode/ustring.h"
     53 #include "uarrsort.h"
     54 
     55 struct UResAEntry {
     56     UChar *key;
     57     UResourceBundle *item;
     58 };
     59 
     60 struct UResourceBundleAIterator {
     61     UResourceBundle  *bund;
     62     UResAEntry *entries;
     63     int32_t num;
     64     int32_t cursor;
     65 };
     66 
     67 /* Must be C linkage to pass function pointer to the sort function */
     68 
     69 #if !defined (OS390) && !defined (OS400)
     70 extern "C"
     71 #endif
     72 static int32_t U_CALLCONV
     73 ures_a_codepointSort(const void *context, const void *left, const void *right) {
     74     //CompareContext *cmp=(CompareContext *)context;
     75     return u_strcmp(((const UResAEntry *)left)->key,
     76                     ((const UResAEntry *)right)->key);
     77 }
     78 
     79 
     80 static void ures_a_open(UResourceBundleAIterator *aiter, UResourceBundle *bund, UErrorCode *status) {
     81     if(U_FAILURE(*status)) {
     82         return;
     83     }
     84     aiter->bund = bund;
     85     aiter->num = ures_getSize(aiter->bund);
     86     aiter->cursor = 0;
     87 #if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
     88     aiter->entries = NULL;
     89 #else
     90     aiter->entries = (UResAEntry*)uprv_malloc(sizeof(UResAEntry)*aiter->num);
     91     for(int i=0;i<aiter->num;i++) {
     92         aiter->entries[i].item = ures_getByIndex(aiter->bund, i, NULL, status);
     93         const char *akey = ures_getKey(aiter->entries[i].item);
     94         int32_t len = uprv_strlen(akey)+1;
     95         aiter->entries[i].key = (UChar*)uprv_malloc(len*sizeof(UChar));
     96         u_charsToUChars(akey, aiter->entries[i].key, len);
     97     }
     98     uprv_sortArray(aiter->entries, aiter->num, sizeof(UResAEntry), ures_a_codepointSort, NULL, TRUE, status);
     99 #endif
    100 }
    101 
    102 static void ures_a_close(UResourceBundleAIterator *aiter) {
    103 #if defined(U_SORT_ASCII_BUNDLE_ITERATOR)
    104     for(int i=0;i<aiter->num;i++) {
    105         uprv_free(aiter->entries[i].key);
    106         ures_close(aiter->entries[i].item);
    107     }
    108 #endif
    109 }
    110 
    111 static const UChar *ures_a_getNextString(UResourceBundleAIterator *aiter, int32_t *len, const char **key, UErrorCode *err) {
    112 #if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
    113     return ures_getNextString(aiter->bund, len, key, err);
    114 #else
    115     if(U_FAILURE(*err)) return NULL;
    116     UResourceBundle *item = aiter->entries[aiter->cursor].item;
    117     const UChar* ret = ures_getString(item, len, err);
    118     *key = ures_getKey(item);
    119     aiter->cursor++;
    120     return ret;
    121 #endif
    122 }
    123 
    124 
    125 #endif
    126 
    127 
    128 U_NAMESPACE_BEGIN
    129 
    130 
    131 // *****************************************************************************
    132 // class DateTimePatternGenerator
    133 // *****************************************************************************
    134 static const UChar Canonical_Items[] = {
    135     // GyQMwWedDFHmsSv
    136     CAP_G, LOW_Y, CAP_Q, CAP_M, LOW_W, CAP_W, LOW_E, LOW_D, CAP_D, CAP_F,
    137     CAP_H, LOW_M, LOW_S, CAP_S, LOW_V, 0
    138 };
    139 
    140 static const dtTypeElem dtTypes[] = {
    141     // patternChar, field, type, minLen, weight
    142     {CAP_G, UDATPG_ERA_FIELD, DT_SHORT, 1, 3,},
    143     {CAP_G, UDATPG_ERA_FIELD, DT_LONG, 4, 0},
    144     {LOW_Y, UDATPG_YEAR_FIELD, DT_NUMERIC, 1, 20},
    145     {CAP_Y, UDATPG_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 20},
    146     {LOW_U, UDATPG_YEAR_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 20},
    147     {CAP_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC, 1, 2},
    148     {CAP_Q, UDATPG_QUARTER_FIELD, DT_SHORT, 3, 0},
    149     {CAP_Q, UDATPG_QUARTER_FIELD, DT_LONG, 4, 0},
    150     {LOW_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
    151     {LOW_Q, UDATPG_QUARTER_FIELD, DT_SHORT + DT_DELTA, 3, 0},
    152     {LOW_Q, UDATPG_QUARTER_FIELD, DT_LONG + DT_DELTA, 4, 0},
    153     {CAP_M, UDATPG_MONTH_FIELD, DT_NUMERIC, 1, 2},
    154     {CAP_M, UDATPG_MONTH_FIELD, DT_SHORT, 3, 0},
    155     {CAP_M, UDATPG_MONTH_FIELD, DT_LONG, 4, 0},
    156     {CAP_M, UDATPG_MONTH_FIELD, DT_NARROW, 5, 0},
    157     {CAP_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
    158     {CAP_L, UDATPG_MONTH_FIELD, DT_SHORT - DT_DELTA, 3, 0},
    159     {CAP_L, UDATPG_MONTH_FIELD, DT_LONG - DT_DELTA, 4, 0},
    160     {CAP_L, UDATPG_MONTH_FIELD, DT_NARROW - DT_DELTA, 5, 0},
    161     {LOW_W, UDATPG_WEEK_OF_YEAR_FIELD, DT_NUMERIC, 1, 2},
    162     {CAP_W, UDATPG_WEEK_OF_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 0},
    163     {LOW_E, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
    164     {LOW_E, UDATPG_WEEKDAY_FIELD, DT_SHORT - DT_DELTA, 3, 0},
    165     {LOW_E, UDATPG_WEEKDAY_FIELD, DT_LONG - DT_DELTA, 4, 0},
    166     {LOW_E, UDATPG_WEEKDAY_FIELD, DT_NARROW - DT_DELTA, 5, 0},
    167     {CAP_E, UDATPG_WEEKDAY_FIELD, DT_SHORT, 1, 3},
    168     {CAP_E, UDATPG_WEEKDAY_FIELD, DT_LONG, 4, 0},
    169     {CAP_E, UDATPG_WEEKDAY_FIELD, DT_NARROW, 5, 0},
    170     {LOW_C, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 2},
    171     {LOW_C, UDATPG_WEEKDAY_FIELD, DT_SHORT - 2*DT_DELTA, 3, 0},
    172     {LOW_C, UDATPG_WEEKDAY_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
    173     {LOW_C, UDATPG_WEEKDAY_FIELD, DT_NARROW - 2*DT_DELTA, 5, 0},
    174     {LOW_D, UDATPG_DAY_FIELD, DT_NUMERIC, 1, 2},
    175     {CAP_D, UDATPG_DAY_OF_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 3},
    176     {CAP_F, UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 0},
    177     {LOW_G, UDATPG_DAY_FIELD, DT_NUMERIC + 3*DT_DELTA, 1, 20}, // really internal use, so we don't care
    178     {LOW_A, UDATPG_DAYPERIOD_FIELD, DT_SHORT, 1, 0},
    179     {CAP_H, UDATPG_HOUR_FIELD, DT_NUMERIC + 10*DT_DELTA, 1, 2}, // 24 hour
    180     {LOW_K, UDATPG_HOUR_FIELD, DT_NUMERIC + 11*DT_DELTA, 1, 2},
    181     {LOW_H, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12 hour
    182     {LOW_K, UDATPG_HOUR_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
    183     {LOW_M, UDATPG_MINUTE_FIELD, DT_NUMERIC, 1, 2},
    184     {LOW_S, UDATPG_SECOND_FIELD, DT_NUMERIC, 1, 2},
    185     {CAP_S, UDATPG_FRACTIONAL_SECOND_FIELD, DT_NUMERIC + DT_DELTA, 1, 1000},
    186     {CAP_A, UDATPG_SECOND_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 1000},
    187     {LOW_V, UDATPG_ZONE_FIELD, DT_SHORT - 2*DT_DELTA, 1, 0},
    188     {LOW_V, UDATPG_ZONE_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
    189     {LOW_Z, UDATPG_ZONE_FIELD, DT_SHORT, 1, 3},
    190     {LOW_Z, UDATPG_ZONE_FIELD, DT_LONG, 4, 0},
    191     {CAP_Z, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 3},
    192     {CAP_Z, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
    193     {CAP_V, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 3},
    194     {CAP_V, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
    195     {0, UDATPG_FIELD_COUNT, 0, 0, 0} , // last row of dtTypes[]
    196  };
    197 
    198 static const char* const CLDR_FIELD_APPEND[] = {
    199     "Era", "Year", "Quarter", "Month", "Week", "*", "Day-Of-Week", "Day", "*", "*", "*",
    200     "Hour", "Minute", "Second", "*", "Timezone"
    201 };
    202 
    203 static const char* const CLDR_FIELD_NAME[] = {
    204     "era", "year", "quarter", "month", "week", "*", "weekday", "day", "*", "*", "dayperiod",
    205     "hour", "minute", "second", "*", "zone"
    206 };
    207 
    208 static const char* const Resource_Fields[] = {
    209     "day", "dayperiod", "era", "hour", "minute", "month", "second", "week",
    210     "weekday", "year", "zone", "quarter" };
    211 
    212 // For appendItems
    213 static const UChar UDATPG_ItemFormat[]= {0x7B, 0x30, 0x7D, 0x20, 0x251C, 0x7B, 0x32, 0x7D, 0x3A,
    214     0x20, 0x7B, 0x31, 0x7D, 0x2524, 0};  // {0} \u251C{2}: {1}\u2524
    215 
    216 static const UChar repeatedPatterns[6]={CAP_G, CAP_E, LOW_Z, LOW_V, CAP_Q, 0}; // "GEzvQ"
    217 
    218 static const char DT_DateTimePatternsTag[]="DateTimePatterns";
    219 static const char DT_DateTimeCalendarTag[]="calendar";
    220 static const char DT_DateTimeGregorianTag[]="gregorian";
    221 static const char DT_DateTimeAppendItemsTag[]="appendItems";
    222 static const char DT_DateTimeFieldsTag[]="fields";
    223 static const char DT_DateTimeAvailableFormatsTag[]="availableFormats";
    224 //static const UnicodeString repeatedPattern=UnicodeString(repeatedPatterns);
    225 
    226 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateTimePatternGenerator)
    227 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTSkeletonEnumeration)
    228 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTRedundantEnumeration)
    229 
    230 DateTimePatternGenerator*  U_EXPORT2
    231 DateTimePatternGenerator::createInstance(UErrorCode& status) {
    232     return createInstance(Locale::getDefault(), status);
    233 }
    234 
    235 DateTimePatternGenerator* U_EXPORT2
    236 DateTimePatternGenerator::createInstance(const Locale& locale, UErrorCode& status) {
    237     DateTimePatternGenerator *result = new DateTimePatternGenerator(locale, status);
    238     if (result == NULL) {
    239         status = U_MEMORY_ALLOCATION_ERROR;
    240     }
    241     if (U_FAILURE(status)) {
    242         delete result;
    243         result = NULL;
    244     }
    245     return result;
    246 }
    247 
    248 DateTimePatternGenerator*  U_EXPORT2
    249 DateTimePatternGenerator::createEmptyInstance(UErrorCode& status) {
    250     DateTimePatternGenerator *result = new DateTimePatternGenerator(status);
    251     if (result == NULL) {
    252         status = U_MEMORY_ALLOCATION_ERROR;
    253     }
    254     if (U_FAILURE(status)) {
    255         delete result;
    256         result = NULL;
    257     }
    258     return result;
    259 }
    260 
    261 DateTimePatternGenerator::DateTimePatternGenerator(UErrorCode &status) :
    262     skipMatcher(NULL),
    263     fAvailableFormatKeyHash(NULL)
    264 {
    265     fp = new FormatParser();
    266     dtMatcher = new DateTimeMatcher();
    267     distanceInfo = new DistanceInfo();
    268     patternMap = new PatternMap();
    269     if (fp == NULL || dtMatcher == NULL || distanceInfo == NULL || patternMap == NULL) {
    270         status = U_MEMORY_ALLOCATION_ERROR;
    271     }
    272 }
    273 
    274 DateTimePatternGenerator::DateTimePatternGenerator(const Locale& locale, UErrorCode &status) :
    275     skipMatcher(NULL),
    276     fAvailableFormatKeyHash(NULL)
    277 {
    278     fp = new FormatParser();
    279     dtMatcher = new DateTimeMatcher();
    280     distanceInfo = new DistanceInfo();
    281     patternMap = new PatternMap();
    282     if (fp == NULL || dtMatcher == NULL || distanceInfo == NULL || patternMap == NULL) {
    283         status = U_MEMORY_ALLOCATION_ERROR;
    284     }
    285     else {
    286         initData(locale, status);
    287     }
    288 }
    289 
    290 DateTimePatternGenerator::DateTimePatternGenerator(const DateTimePatternGenerator& other) :
    291     UObject(),
    292     skipMatcher(NULL),
    293     fAvailableFormatKeyHash(NULL)
    294 {
    295     fp = new FormatParser();
    296     dtMatcher = new DateTimeMatcher();
    297     distanceInfo = new DistanceInfo();
    298     patternMap = new PatternMap();
    299     *this=other;
    300 }
    301 
    302 DateTimePatternGenerator&
    303 DateTimePatternGenerator::operator=(const DateTimePatternGenerator& other) {
    304     pLocale = other.pLocale;
    305     fDefaultHourFormatChar = other.fDefaultHourFormatChar;
    306     *fp = *(other.fp);
    307     dtMatcher->copyFrom(other.dtMatcher->skeleton);
    308     *distanceInfo = *(other.distanceInfo);
    309     dateTimeFormat = other.dateTimeFormat;
    310     decimal = other.decimal;
    311     // NUL-terminate for the C API.
    312     dateTimeFormat.getTerminatedBuffer();
    313     decimal.getTerminatedBuffer();
    314     delete skipMatcher;
    315     if ( other.skipMatcher == NULL ) {
    316         skipMatcher = NULL;
    317     }
    318     else {
    319         skipMatcher = new DateTimeMatcher(*other.skipMatcher);
    320     }
    321     for (int32_t i=0; i< UDATPG_FIELD_COUNT; ++i ) {
    322         appendItemFormats[i] = other.appendItemFormats[i];
    323         appendItemNames[i] = other.appendItemNames[i];
    324         // NUL-terminate for the C API.
    325         appendItemFormats[i].getTerminatedBuffer();
    326         appendItemNames[i].getTerminatedBuffer();
    327     }
    328     UErrorCode status = U_ZERO_ERROR;
    329     patternMap->copyFrom(*other.patternMap, status);
    330     copyHashtable(other.fAvailableFormatKeyHash, status);
    331     return *this;
    332 }
    333 
    334 
    335 UBool
    336 DateTimePatternGenerator::operator==(const DateTimePatternGenerator& other) const {
    337     if (this == &other) {
    338         return TRUE;
    339     }
    340     if ((pLocale==other.pLocale) && (patternMap->equals(*other.patternMap)) &&
    341         (dateTimeFormat==other.dateTimeFormat) && (decimal==other.decimal)) {
    342         for ( int32_t i=0 ; i<UDATPG_FIELD_COUNT; ++i ) {
    343            if ((appendItemFormats[i] != other.appendItemFormats[i]) ||
    344                (appendItemNames[i] != other.appendItemNames[i]) ) {
    345                return FALSE;
    346            }
    347         }
    348         return TRUE;
    349     }
    350     else {
    351         return FALSE;
    352     }
    353 }
    354 
    355 UBool
    356 DateTimePatternGenerator::operator!=(const DateTimePatternGenerator& other) const {
    357     return  !operator==(other);
    358 }
    359 
    360 DateTimePatternGenerator::~DateTimePatternGenerator() {
    361     if (fAvailableFormatKeyHash!=NULL) {
    362         delete fAvailableFormatKeyHash;
    363     }
    364 
    365     if (fp != NULL) delete fp;
    366     if (dtMatcher != NULL) delete dtMatcher;
    367     if (distanceInfo != NULL) delete distanceInfo;
    368     if (patternMap != NULL) delete patternMap;
    369     if (skipMatcher != NULL) delete skipMatcher;
    370 }
    371 
    372 void
    373 DateTimePatternGenerator::initData(const Locale& locale, UErrorCode &status) {
    374     //const char *baseLangName = locale.getBaseName(); // unused
    375 
    376     skipMatcher = NULL;
    377     fAvailableFormatKeyHash=NULL;
    378     addCanonicalItems();
    379     addICUPatterns(locale, status);
    380     if (U_FAILURE(status)) {
    381         return;
    382     }
    383     addCLDRData(locale, status);
    384     setDateTimeFromCalendar(locale, status);
    385     setDecimalSymbols(locale, status);
    386 } // DateTimePatternGenerator::initData
    387 
    388 UnicodeString
    389 DateTimePatternGenerator::getSkeleton(const UnicodeString& pattern, UErrorCode&
    390 /*status*/) {
    391     dtMatcher->set(pattern, fp);
    392     return dtMatcher->getSkeletonPtr()->getSkeleton();
    393 }
    394 
    395 UnicodeString
    396 DateTimePatternGenerator::getBaseSkeleton(const UnicodeString& pattern, UErrorCode& /*status*/) {
    397     dtMatcher->set(pattern, fp);
    398     return dtMatcher->getSkeletonPtr()->getBaseSkeleton();
    399 }
    400 
    401 void
    402 DateTimePatternGenerator::addICUPatterns(const Locale& locale, UErrorCode& status) {
    403     UnicodeString dfPattern;
    404     UnicodeString conflictingString;
    405     UDateTimePatternConflict conflictingStatus;
    406     DateFormat* df;
    407 
    408     if (U_FAILURE(status)) {
    409         return;
    410     }
    411 
    412     // Load with ICU patterns
    413     for (int32_t i=DateFormat::kFull; i<=DateFormat::kShort; i++) {
    414         DateFormat::EStyle style = (DateFormat::EStyle)i;
    415         df = DateFormat::createDateInstance(style, locale);
    416         if (df != NULL && df->getDynamicClassID() == SimpleDateFormat::getStaticClassID()) {
    417             SimpleDateFormat* sdf = (SimpleDateFormat*)df;
    418             conflictingStatus = addPattern(sdf->toPattern(dfPattern), FALSE, conflictingString, status);
    419         }
    420         // TODO Maybe we should return an error when the date format isn't simple.
    421         delete df;
    422         if (U_FAILURE(status)) {
    423             return;
    424         }
    425 
    426         df = DateFormat::createTimeInstance(style, locale);
    427         if (df != NULL && df->getDynamicClassID() == SimpleDateFormat::getStaticClassID()) {
    428             SimpleDateFormat* sdf = (SimpleDateFormat*)df;
    429             conflictingStatus = addPattern(sdf->toPattern(dfPattern), FALSE, conflictingString, status);
    430             // HACK for hh:ss
    431             if ( i==DateFormat::kMedium ) {
    432                 hackPattern = dfPattern;
    433             }
    434         }
    435         // TODO Maybe we should return an error when the date format isn't simple.
    436         delete df;
    437         if (U_FAILURE(status)) {
    438             return;
    439         }
    440     }
    441 }
    442 
    443 void
    444 DateTimePatternGenerator::hackTimes(const UnicodeString& hackPattern, UErrorCode& status)  {
    445     UDateTimePatternConflict conflictingStatus;
    446     UnicodeString conflictingString;
    447 
    448     fp->set(hackPattern);
    449     UnicodeString mmss;
    450     UBool gotMm=FALSE;
    451     for (int32_t i=0; i<fp->itemNumber; ++i) {
    452         UnicodeString field = fp->items[i];
    453         if ( fp->isQuoteLiteral(field) ) {
    454             if ( gotMm ) {
    455                UnicodeString quoteLiteral;
    456                fp->getQuoteLiteral(quoteLiteral, &i);
    457                mmss += quoteLiteral;
    458             }
    459         }
    460         else {
    461             if (fp->isPatternSeparator(field) && gotMm) {
    462                 mmss+=field;
    463             }
    464             else {
    465                 UChar ch=field.charAt(0);
    466                 if (ch==LOW_M) {
    467                     gotMm=TRUE;
    468                     mmss+=field;
    469                 }
    470                 else {
    471                     if (ch==LOW_S) {
    472                         if (!gotMm) {
    473                             break;
    474                         }
    475                         mmss+= field;
    476                         conflictingStatus = addPattern(mmss, FALSE, conflictingString, status);
    477                         break;
    478                     }
    479                     else {
    480                         if (gotMm || ch==LOW_Z || ch==CAP_Z || ch==LOW_V || ch==CAP_V) {
    481                             break;
    482                         }
    483                     }
    484                 }
    485             }
    486         }
    487     }
    488 }
    489 
    490 #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
    491 
    492 static const UChar hourFormatChars[] = { CAP_H, LOW_H, CAP_K, LOW_K, 0 }; // HhKk, the hour format characters
    493 
    494 void
    495 DateTimePatternGenerator::addCLDRData(const Locale& locale, UErrorCode& err) {
    496     UResourceBundle *rb, *calTypeBundle, *calBundle;
    497     UResourceBundle *patBundle, *fieldBundle, *fBundle;
    498     UnicodeString rbPattern, value, field;
    499     UnicodeString conflictingPattern;
    500     UDateTimePatternConflict conflictingStatus;
    501     const char *key=NULL;
    502     int32_t i;
    503 
    504     UnicodeString defaultItemFormat(TRUE, UDATPG_ItemFormat, LENGTHOF(UDATPG_ItemFormat)-1);  // Read-only alias.
    505 
    506     err = U_ZERO_ERROR;
    507 
    508     fDefaultHourFormatChar = 0;
    509     for (i=0; i<UDATPG_FIELD_COUNT; ++i ) {
    510         appendItemNames[i]=CAP_F;
    511         if (i<10) {
    512             appendItemNames[i]+=(UChar)(i+0x30);
    513         }
    514         else {
    515             appendItemNames[i]+=(UChar)0x31;
    516             appendItemNames[i]+=(UChar)(i-10 + 0x30);
    517         }
    518         // NUL-terminate for the C API.
    519         appendItemNames[i].getTerminatedBuffer();
    520     }
    521 
    522     rb = ures_open(NULL, locale.getName(), &err);
    523     if (rb == NULL || U_FAILURE(err)) {
    524         return;
    525     }
    526     const char *curLocaleName=ures_getLocaleByType(rb, ULOC_ACTUAL_LOCALE, &err);
    527     const char * calendarTypeToUse = DT_DateTimeGregorianTag; // initial default
    528     char         calendarType[ULOC_KEYWORDS_CAPACITY]; // to be filled in with the type to use, if all goes well
    529     if ( U_SUCCESS(err) ) {
    530         char    localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY];
    531         // obtain a locale that always has the calendar key value that should be used
    532         (void)ures_getFunctionalEquivalent(localeWithCalendarKey, ULOC_LOCALE_IDENTIFIER_CAPACITY, NULL,
    533                                             "calendar", "calendar", locale.getName(), NULL, FALSE, &err);
    534         localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY-1] = 0; // ensure null termination
    535         // now get the calendar key value from that locale
    536         int32_t calendarTypeLen = uloc_getKeywordValue(localeWithCalendarKey, "calendar", calendarType, ULOC_KEYWORDS_CAPACITY, &err);
    537         if (U_SUCCESS(err) && calendarTypeLen < ULOC_KEYWORDS_CAPACITY) {
    538             calendarTypeToUse = calendarType;
    539         }
    540         err = U_ZERO_ERROR;
    541     }
    542     calBundle = ures_getByKeyWithFallback(rb, DT_DateTimeCalendarTag, NULL, &err);
    543     calTypeBundle = ures_getByKeyWithFallback(calBundle, calendarTypeToUse, NULL, &err);
    544 
    545     key=NULL;
    546     int32_t dtCount=0;
    547     patBundle = ures_getByKeyWithFallback(calTypeBundle, DT_DateTimePatternsTag, NULL, &err);
    548     while (U_SUCCESS(err)) {
    549         rbPattern = ures_getNextUnicodeString(patBundle, &key, &err);
    550         dtCount++;
    551         if (rbPattern.length()==0 ) {
    552             break;  // no more pattern
    553         }
    554         else {
    555             if (dtCount==9) {
    556                 setDateTimeFormat(rbPattern);
    557             } else if (dtCount==4) { // short time format
    558                 // set fDefaultHourFormatChar to the hour format character from this pattern
    559                 int32_t tfIdx, tfLen = rbPattern.length();
    560                 UBool ignoreChars = FALSE;
    561                 for (tfIdx = 0; tfIdx < tfLen; tfIdx++) {
    562                     UChar tfChar = rbPattern.charAt(tfIdx);
    563                     if ( tfChar == SINGLE_QUOTE ) {
    564                         ignoreChars = !ignoreChars; // toggle (handle quoted literals & '' for single quote)
    565                     } else if ( !ignoreChars && u_strchr(hourFormatChars, tfChar) != NULL ) {
    566                         fDefaultHourFormatChar = tfChar;
    567                         break;
    568                     }
    569                 }
    570             }
    571         }
    572     }
    573     ures_close(patBundle);
    574 
    575     err = U_ZERO_ERROR;
    576     patBundle = ures_getByKeyWithFallback(calTypeBundle, DT_DateTimeAppendItemsTag, NULL, &err);
    577     key=NULL;
    578     UnicodeString itemKey;
    579     while (U_SUCCESS(err)) {
    580         rbPattern = ures_getNextUnicodeString(patBundle, &key, &err);
    581         if (rbPattern.length()==0 ) {
    582             break;  // no more pattern
    583         }
    584         else {
    585             setAppendItemFormat(getAppendFormatNumber(key), rbPattern);
    586         }
    587     }
    588     ures_close(patBundle);
    589 
    590     key=NULL;
    591     err = U_ZERO_ERROR;
    592     fBundle = ures_getByKeyWithFallback(calTypeBundle, DT_DateTimeFieldsTag, NULL, &err);
    593     for (i=0; i<MAX_RESOURCE_FIELD; ++i) {
    594         err = U_ZERO_ERROR;
    595         patBundle = ures_getByKeyWithFallback(fBundle, Resource_Fields[i], NULL, &err);
    596         fieldBundle = ures_getByKeyWithFallback(patBundle, "dn", NULL, &err);
    597         rbPattern = ures_getNextUnicodeString(fieldBundle, &key, &err);
    598         ures_close(fieldBundle);
    599         ures_close(patBundle);
    600         if (rbPattern.length()==0 ) {
    601             continue;
    602         }
    603         else {
    604             setAppendItemName(getAppendNameNumber(Resource_Fields[i]), rbPattern);
    605         }
    606     }
    607     ures_close(fBundle);
    608 
    609     // add available formats
    610     err = U_ZERO_ERROR;
    611     initHashtable(err);
    612     patBundle = ures_getByKeyWithFallback(calTypeBundle, DT_DateTimeAvailableFormatsTag, NULL, &err);
    613     if (U_SUCCESS(err)) {
    614         int32_t numberKeys = ures_getSize(patBundle);
    615         int32_t len;
    616         const UChar *retPattern;
    617         key=NULL;
    618 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
    619         UResourceBundleAIterator aiter;
    620         ures_a_open(&aiter, patBundle, &err);
    621 #endif
    622         for(i=0; i<numberKeys; ++i) {
    623 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
    624             retPattern=ures_a_getNextString(&aiter, &len, &key, &err);
    625 #else
    626             retPattern=ures_getNextString(patBundle, &len, &key, &err);
    627 #endif
    628             UnicodeString format=UnicodeString(retPattern);
    629             UnicodeString retKey=UnicodeString(key, -1, US_INV);
    630             setAvailableFormat(retKey, err);
    631             // Add pattern with its associated skeleton. Override any duplicate derived from std patterns,
    632             // but not a previous availableFormats entry:
    633             conflictingStatus = addPatternWithSkeleton(format, &retKey, TRUE, conflictingPattern, err);
    634         }
    635 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
    636         ures_a_close(&aiter);
    637 #endif
    638     }
    639     ures_close(patBundle);
    640     ures_close(calTypeBundle);
    641     ures_close(calBundle);
    642     ures_close(rb);
    643 
    644     err = U_ZERO_ERROR;
    645     char parentLocale[50];
    646     int32_t localeNameLen=0;
    647     uprv_strcpy(parentLocale, curLocaleName);
    648     while((localeNameLen=uloc_getParent(parentLocale, parentLocale, 50, &err))>=0 ) {
    649         rb = ures_open(NULL, parentLocale, &err);
    650         curLocaleName=ures_getLocaleByType(rb, ULOC_ACTUAL_LOCALE, &err);
    651         uprv_strcpy(parentLocale, curLocaleName);
    652         calBundle = ures_getByKey(rb, DT_DateTimeCalendarTag, NULL, &err);
    653         calTypeBundle = ures_getByKey(calBundle, calendarTypeToUse, NULL, &err);
    654         patBundle = ures_getByKeyWithFallback(calTypeBundle, DT_DateTimeAvailableFormatsTag, NULL, &err);
    655         if (U_SUCCESS(err)) {
    656             int32_t numberKeys = ures_getSize(patBundle);
    657             int32_t len;
    658             const UChar *retPattern;
    659             key=NULL;
    660 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
    661             UResourceBundleAIterator aiter;
    662             ures_a_open(&aiter, patBundle, &err);
    663 #endif
    664             for(i=0; i<numberKeys; ++i) {
    665 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
    666                 retPattern=ures_a_getNextString(&aiter, &len, &key, &err);
    667 #else
    668                 retPattern=ures_getNextString(patBundle, &len, &key, &err);
    669 #endif
    670                 UnicodeString format=UnicodeString(retPattern);
    671                 UnicodeString retKey=UnicodeString(key, -1, US_INV);
    672                 if ( !isAvailableFormatSet(retKey) ) {
    673                     setAvailableFormat(retKey, err);
    674                     // Add pattern with its associated skeleton. Override any duplicate derived from std patterns,
    675                     // but not a previous availableFormats entry:
    676                     conflictingStatus = addPatternWithSkeleton(format, &retKey, TRUE, conflictingPattern, err);
    677                 }
    678             }
    679 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
    680             ures_a_close(&aiter);
    681 #endif
    682         }
    683         err = U_ZERO_ERROR; // reset; if this locale lacks the necessary data, need to keep checking up to root.
    684         ures_close(patBundle);
    685         ures_close(calTypeBundle);
    686         ures_close(calBundle);
    687         ures_close(rb);
    688         if (localeNameLen==0) {
    689             break;
    690         }
    691     }
    692 
    693     if (hackPattern.length()>0) {
    694         hackTimes(hackPattern, err);
    695     }
    696 }
    697 
    698 void
    699 DateTimePatternGenerator::initHashtable(UErrorCode& err) {
    700     if (fAvailableFormatKeyHash!=NULL) {
    701         return;
    702     }
    703     if ((fAvailableFormatKeyHash = new Hashtable(FALSE, err))==NULL) {
    704         err=U_MEMORY_ALLOCATION_ERROR;
    705         return;
    706     }
    707 }
    708 
    709 
    710 void
    711 DateTimePatternGenerator::setAppendItemFormat(UDateTimePatternField field, const UnicodeString& value) {
    712     appendItemFormats[field] = value;
    713     // NUL-terminate for the C API.
    714     appendItemFormats[field].getTerminatedBuffer();
    715 }
    716 
    717 const UnicodeString&
    718 DateTimePatternGenerator::getAppendItemFormat(UDateTimePatternField field) const {
    719     return appendItemFormats[field];
    720 }
    721 
    722 void
    723 DateTimePatternGenerator::setAppendItemName(UDateTimePatternField field, const UnicodeString& value) {
    724     appendItemNames[field] = value;
    725     // NUL-terminate for the C API.
    726     appendItemNames[field].getTerminatedBuffer();
    727 }
    728 
    729 const UnicodeString&
    730 DateTimePatternGenerator:: getAppendItemName(UDateTimePatternField field) const {
    731     return appendItemNames[field];
    732 }
    733 
    734 void
    735 DateTimePatternGenerator::getAppendName(UDateTimePatternField field, UnicodeString& value) {
    736     value = SINGLE_QUOTE;
    737     value += appendItemNames[field];
    738     value += SINGLE_QUOTE;
    739 }
    740 
    741 UnicodeString
    742 DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UErrorCode& status) {
    743     return getBestPattern(patternForm, UDATPG_MATCH_NO_OPTIONS, status);
    744 }
    745 
    746 UnicodeString
    747 DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UDateTimePatternMatchOptions options, UErrorCode& status) {
    748     const UnicodeString *bestPattern=NULL;
    749     UnicodeString dtFormat;
    750     UnicodeString resultPattern;
    751 
    752     int32_t dateMask=(1<<UDATPG_DAYPERIOD_FIELD) - 1;
    753     int32_t timeMask=(1<<UDATPG_FIELD_COUNT) - 1 - dateMask;
    754 
    755     UnicodeString patternFormCopy = UnicodeString(patternForm);
    756     patternFormCopy.findAndReplace(UnicodeString(LOW_J), UnicodeString(fDefaultHourFormatChar));
    757 
    758     resultPattern.remove();
    759     dtMatcher->set(patternFormCopy, fp);
    760     const PtnSkeleton* specifiedSkeleton=NULL;
    761     bestPattern=getBestRaw(*dtMatcher, -1, distanceInfo, &specifiedSkeleton);
    762     if ( distanceInfo->missingFieldMask==0 && distanceInfo->extraFieldMask==0 ) {
    763         resultPattern = adjustFieldTypes(*bestPattern, specifiedSkeleton, FALSE, options);
    764 
    765         return resultPattern;
    766     }
    767     int32_t neededFields = dtMatcher->getFieldMask();
    768     UnicodeString datePattern=getBestAppending(neededFields & dateMask, options);
    769     UnicodeString timePattern=getBestAppending(neededFields & timeMask, options);
    770     if (datePattern.length()==0) {
    771         if (timePattern.length()==0) {
    772             resultPattern.remove();
    773         }
    774         else {
    775             return timePattern;
    776         }
    777     }
    778     if (timePattern.length()==0) {
    779         return datePattern;
    780     }
    781     resultPattern.remove();
    782     status = U_ZERO_ERROR;
    783     dtFormat=getDateTimeFormat();
    784     Formattable dateTimeObject[] = { timePattern, datePattern };
    785     resultPattern = MessageFormat::format(dtFormat, dateTimeObject, 2, resultPattern, status );
    786     return resultPattern;
    787 }
    788 
    789 UnicodeString
    790 DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern,
    791                                             const UnicodeString& skeleton,
    792                                             UErrorCode& status) {
    793     return replaceFieldTypes(pattern, skeleton, UDATPG_MATCH_NO_OPTIONS, status);
    794 }
    795 
    796 UnicodeString
    797 DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern,
    798                                             const UnicodeString& skeleton,
    799                                             UDateTimePatternMatchOptions options,
    800                                             UErrorCode& /*status*/) {
    801     dtMatcher->set(skeleton, fp);
    802     UnicodeString result = adjustFieldTypes(pattern, NULL, FALSE, options);
    803     return result;
    804 }
    805 
    806 void
    807 DateTimePatternGenerator::setDecimal(const UnicodeString& newDecimal) {
    808     this->decimal = newDecimal;
    809     // NUL-terminate for the C API.
    810     this->decimal.getTerminatedBuffer();
    811 }
    812 
    813 const UnicodeString&
    814 DateTimePatternGenerator::getDecimal() const {
    815     return decimal;
    816 }
    817 
    818 void
    819 DateTimePatternGenerator::addCanonicalItems() {
    820     UnicodeString  conflictingPattern;
    821     UDateTimePatternConflict conflictingStatus;
    822     UErrorCode status = U_ZERO_ERROR;
    823 
    824     for (int32_t i=0; i<UDATPG_FIELD_COUNT; i++) {
    825         conflictingStatus = addPattern(UnicodeString(Canonical_Items[i]), FALSE, conflictingPattern, status);
    826     }
    827 }
    828 
    829 void
    830 DateTimePatternGenerator::setDateTimeFormat(const UnicodeString& dtFormat) {
    831     dateTimeFormat = dtFormat;
    832     // NUL-terminate for the C API.
    833     dateTimeFormat.getTerminatedBuffer();
    834 }
    835 
    836 const UnicodeString&
    837 DateTimePatternGenerator::getDateTimeFormat() const {
    838     return dateTimeFormat;
    839 }
    840 
    841 void
    842 DateTimePatternGenerator::setDateTimeFromCalendar(const Locale& locale, UErrorCode& status) {
    843     const UChar *resStr;
    844     int32_t resStrLen = 0;
    845 
    846     Calendar* fCalendar = Calendar::createInstance(locale, status);
    847     CalendarData calData(locale, fCalendar?fCalendar->getType():NULL, status);
    848     UResourceBundle *dateTimePatterns = calData.getByKey(DT_DateTimePatternsTag, status);
    849     if (U_FAILURE(status)) return;
    850 
    851     if (ures_getSize(dateTimePatterns) <= DateFormat::kDateTime)
    852     {
    853         status = U_INVALID_FORMAT_ERROR;
    854         return;
    855     }
    856     resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)DateFormat::kDateTime, &resStrLen, &status);
    857     setDateTimeFormat(UnicodeString(TRUE, resStr, resStrLen));
    858 
    859     delete fCalendar;
    860 }
    861 
    862 void
    863 DateTimePatternGenerator::setDecimalSymbols(const Locale& locale, UErrorCode& status) {
    864     DecimalFormatSymbols dfs = DecimalFormatSymbols(locale, status);
    865     if(U_SUCCESS(status)) {
    866         decimal = dfs.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
    867         // NUL-terminate for the C API.
    868         decimal.getTerminatedBuffer();
    869     }
    870 }
    871 
    872 UDateTimePatternConflict
    873 DateTimePatternGenerator::addPattern(
    874     const UnicodeString& pattern,
    875     UBool override,
    876     UnicodeString &conflictingPattern,
    877     UErrorCode& status)
    878 {
    879     return addPatternWithSkeleton(pattern, NULL, override, conflictingPattern, status);
    880 }
    881 
    882 // For DateTimePatternGenerator::addPatternWithSkeleton -
    883 // If skeletonToUse is specified, then an availableFormats entry is being added. In this case:
    884 // 1. We pass that skeleton to matcher.set instead of having it derive a skeleton from the pattern.
    885 // 2. If the new entry's skeleton or basePattern does match an existing entry but that entry also had a skeleton specified
    886 // (i.e. it was also from availableFormats), then the new entry does not override it regardless of the value of the override
    887 // parameter. This prevents later availableFormats entries from a parent locale overriding earlier ones from the actual
    888 // specified locale. However, availableFormats entries *should* override entries with matching skeleton whose skeleton was
    889 // derived (i.e. entries derived from the standard date/time patters for the specified locale).
    890 // 3. When adding the pattern (patternMap->add), we set a new boolean to indicate that the added entry had a
    891 // specified skeleton (which sets a new field in the PtnElem in the PatternMap).
    892 UDateTimePatternConflict
    893 DateTimePatternGenerator::addPatternWithSkeleton(
    894     const UnicodeString& pattern,
    895     const UnicodeString* skeletonToUse,
    896     UBool override,
    897     UnicodeString& conflictingPattern,
    898     UErrorCode& status)
    899 {
    900 
    901     UnicodeString basePattern;
    902     PtnSkeleton   skeleton;
    903     UDateTimePatternConflict conflictingStatus = UDATPG_NO_CONFLICT;
    904 
    905     DateTimeMatcher matcher;
    906     if ( skeletonToUse == NULL ) {
    907         matcher.set(pattern, fp, skeleton);
    908         matcher.getBasePattern(basePattern);
    909     } else {
    910         matcher.set(*skeletonToUse, fp, skeleton); // this still trims skeleton fields to max len 3, may need to change it.
    911         matcher.getBasePattern(basePattern); // or perhaps instead: basePattern = *skeletonToUse;
    912     }
    913     UBool entryHadSpecifiedSkeleton;
    914     const UnicodeString *duplicatePattern = patternMap->getPatternFromBasePattern(basePattern, entryHadSpecifiedSkeleton);
    915     if (duplicatePattern != NULL ) {
    916         conflictingStatus = UDATPG_BASE_CONFLICT;
    917         conflictingPattern = *duplicatePattern;
    918         if (!override || (skeletonToUse != NULL && entryHadSpecifiedSkeleton)) {
    919             return conflictingStatus;
    920         }
    921     }
    922     const PtnSkeleton* entrySpecifiedSkeleton = NULL;
    923     duplicatePattern = patternMap->getPatternFromSkeleton(skeleton, &entrySpecifiedSkeleton);
    924     if (duplicatePattern != NULL ) {
    925         conflictingStatus = UDATPG_CONFLICT;
    926         conflictingPattern = *duplicatePattern;
    927         if (!override || (skeletonToUse != NULL && entrySpecifiedSkeleton != NULL)) {
    928             return conflictingStatus;
    929         }
    930     }
    931     patternMap->add(basePattern, skeleton, pattern, skeletonToUse != NULL, status);
    932     if(U_FAILURE(status)) {
    933         return conflictingStatus;
    934     }
    935 
    936     return UDATPG_NO_CONFLICT;
    937 }
    938 
    939 
    940 UDateTimePatternField
    941 DateTimePatternGenerator::getAppendFormatNumber(const char* field) const {
    942     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
    943         if (uprv_strcmp(CLDR_FIELD_APPEND[i], field)==0) {
    944             return (UDateTimePatternField)i;
    945         }
    946     }
    947     return UDATPG_FIELD_COUNT;
    948 }
    949 
    950 UDateTimePatternField
    951 DateTimePatternGenerator::getAppendNameNumber(const char* field) const {
    952     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
    953         if (uprv_strcmp(CLDR_FIELD_NAME[i],field)==0) {
    954             return (UDateTimePatternField)i;
    955         }
    956     }
    957     return UDATPG_FIELD_COUNT;
    958 }
    959 
    960 const UnicodeString*
    961 DateTimePatternGenerator::getBestRaw(DateTimeMatcher& source,
    962                                      int32_t includeMask,
    963                                      DistanceInfo* missingFields,
    964                                      const PtnSkeleton** specifiedSkeletonPtr) {
    965     int32_t bestDistance = 0x7fffffff;
    966     DistanceInfo tempInfo;
    967     const UnicodeString *bestPattern=NULL;
    968     const PtnSkeleton* specifiedSkeleton=NULL;
    969 
    970     PatternMapIterator it;
    971     for (it.set(*patternMap); it.hasNext(); ) {
    972         DateTimeMatcher trial = it.next();
    973         if (trial.equals(skipMatcher)) {
    974             continue;
    975         }
    976         int32_t distance=source.getDistance(trial, includeMask, tempInfo);
    977         if (distance<bestDistance) {
    978             bestDistance=distance;
    979             bestPattern=patternMap->getPatternFromSkeleton(*trial.getSkeletonPtr(), &specifiedSkeleton);
    980             missingFields->setTo(tempInfo);
    981             if (distance==0) {
    982                 break;
    983             }
    984         }
    985     }
    986 
    987     // If the best raw match had a specified skeleton and that skeleton was requested by the caller,
    988     // then return it too. This generally happens when the caller needs to pass that skeleton
    989     // through to adjustFieldTypes so the latter can do a better job.
    990     if (bestPattern && specifiedSkeletonPtr) {
    991         *specifiedSkeletonPtr = specifiedSkeleton;
    992     }
    993     return bestPattern;
    994 }
    995 
    996 UnicodeString
    997 DateTimePatternGenerator::adjustFieldTypes(const UnicodeString& pattern,
    998                                            const PtnSkeleton* specifiedSkeleton,
    999                                            UBool fixFractionalSeconds,
   1000                                            UDateTimePatternMatchOptions options) {
   1001     UnicodeString newPattern;
   1002     fp->set(pattern);
   1003     for (int32_t i=0; i < fp->itemNumber; i++) {
   1004         UnicodeString field = fp->items[i];
   1005         if ( fp->isQuoteLiteral(field) ) {
   1006 
   1007             UnicodeString quoteLiteral;
   1008             fp->getQuoteLiteral(quoteLiteral, &i);
   1009             newPattern += quoteLiteral;
   1010         }
   1011         else {
   1012             if (fp->isPatternSeparator(field)) {
   1013                 newPattern+=field;
   1014                 continue;
   1015             }
   1016             int32_t canonicalIndex = fp->getCanonicalIndex(field);
   1017             if (canonicalIndex < 0) {
   1018                 newPattern+=field;
   1019                 continue;  // don't adjust
   1020             }
   1021             const dtTypeElem *row = &dtTypes[canonicalIndex];
   1022             int32_t typeValue = row->field;
   1023             if (fixFractionalSeconds && typeValue == UDATPG_SECOND_FIELD) {
   1024                 UnicodeString newField=dtMatcher->skeleton.original[UDATPG_FRACTIONAL_SECOND_FIELD];
   1025                 field = field + decimal + newField;
   1026             }
   1027             else {
   1028                 if (dtMatcher->skeleton.type[typeValue]!=0) {
   1029                     // Here:
   1030                     // - "reqField" is the field from the originally requested skeleton, with length
   1031                     // "reqFieldLen".
   1032                     // - "field" is the field from the found pattern.
   1033                     //
   1034                     // The adjusted field should consist of characters from the originally requested
   1035                     // skeleton, except in the case of UDATPG_HOUR_FIELD or UDATPG_MONTH_FIELD, in
   1036                     // which case it should consist of characters from the found pattern.
   1037                     //
   1038                     // The length of the adjusted field (adjFieldLen) should match that in the originally
   1039                     // requested skeleton, except that in the following cases the length of the adjusted field
   1040                     // should match that in the found pattern (i.e. the length of this pattern field should
   1041                     // not be adjusted):
   1042                     // 1. typeValue is UDATPG_HOUR_FIELD/MINUTE/SECOND and the corresponding bit in options is
   1043                     //    not set (ticket #7180). Note, we may want to implement a similar change for other
   1044                     //    numeric fields (MM, dd, etc.) so the default behavior is to get locale preference for
   1045                     //    field length, but options bits can be used to override this.
   1046                     // 2. There is a specified skeleton for the found pattern and one of the following is true:
   1047                     //    a) The length of the field in the skeleton (skelFieldLen) is equal to reqFieldLen.
   1048                     //    b) The pattern field is numeric and the skeleton field is not, or vice versa.
   1049 
   1050                     UnicodeString reqField = dtMatcher->skeleton.original[typeValue];
   1051                     int32_t reqFieldLen = reqField.length();
   1052                     int32_t adjFieldLen = reqFieldLen;
   1053                     if ( (typeValue==UDATPG_HOUR_FIELD && (options & UDATPG_MATCH_HOUR_FIELD_LENGTH)==0) ||
   1054                          (typeValue==UDATPG_MINUTE_FIELD && (options & UDATPG_MATCH_MINUTE_FIELD_LENGTH)==0) ||
   1055                          (typeValue==UDATPG_SECOND_FIELD && (options & UDATPG_MATCH_SECOND_FIELD_LENGTH)==0) ) {
   1056                          adjFieldLen = field.length();
   1057                     } else if (specifiedSkeleton) {
   1058                         UnicodeString skelField = specifiedSkeleton->original[typeValue];
   1059                         int32_t skelFieldLen = skelField.length();
   1060                         UBool patFieldIsNumeric = (row->type > 0);
   1061                         UBool skelFieldIsNumeric = (specifiedSkeleton->type[typeValue] > 0);
   1062                         if (skelFieldLen == reqFieldLen || (patFieldIsNumeric && !skelFieldIsNumeric) || (skelFieldIsNumeric && !patFieldIsNumeric)) {
   1063                             // don't adjust the field length in the found pattern
   1064                             adjFieldLen = field.length();
   1065                         }
   1066                     }
   1067                     UChar c = (typeValue!= UDATPG_HOUR_FIELD && typeValue!= UDATPG_MONTH_FIELD)? reqField.charAt(0): field.charAt(0);
   1068                     field.remove();
   1069                     for (int32_t i=adjFieldLen; i>0; --i) {
   1070                         field+=c;
   1071                     }
   1072                 }
   1073                 newPattern+=field;
   1074             }
   1075         }
   1076     }
   1077     return newPattern;
   1078 }
   1079 
   1080 UnicodeString
   1081 DateTimePatternGenerator::getBestAppending(int32_t missingFields, UDateTimePatternMatchOptions options) {
   1082     UnicodeString  resultPattern, tempPattern, formattedPattern;
   1083     UErrorCode err=U_ZERO_ERROR;
   1084     int32_t lastMissingFieldMask=0;
   1085     if (missingFields!=0) {
   1086         resultPattern=UnicodeString();
   1087         const PtnSkeleton* specifiedSkeleton=NULL;
   1088         tempPattern = *getBestRaw(*dtMatcher, missingFields, distanceInfo, &specifiedSkeleton);
   1089         resultPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, FALSE, options);
   1090         if ( distanceInfo->missingFieldMask==0 ) {
   1091             return resultPattern;
   1092         }
   1093         while (distanceInfo->missingFieldMask!=0) { // precondition: EVERY single field must work!
   1094             if ( lastMissingFieldMask == distanceInfo->missingFieldMask ) {
   1095                 break;  // cannot find the proper missing field
   1096             }
   1097             if (((distanceInfo->missingFieldMask & UDATPG_SECOND_AND_FRACTIONAL_MASK)==UDATPG_FRACTIONAL_MASK) &&
   1098                 ((missingFields & UDATPG_SECOND_AND_FRACTIONAL_MASK) == UDATPG_SECOND_AND_FRACTIONAL_MASK)) {
   1099                 resultPattern = adjustFieldTypes(resultPattern, specifiedSkeleton, FALSE, options);
   1100                 //resultPattern = tempPattern;
   1101                 distanceInfo->missingFieldMask &= ~UDATPG_FRACTIONAL_MASK;
   1102                 continue;
   1103             }
   1104             int32_t startingMask = distanceInfo->missingFieldMask;
   1105             tempPattern = *getBestRaw(*dtMatcher, distanceInfo->missingFieldMask, distanceInfo, &specifiedSkeleton);
   1106             tempPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, FALSE, options);
   1107             int32_t foundMask=startingMask& ~distanceInfo->missingFieldMask;
   1108             int32_t topField=getTopBitNumber(foundMask);
   1109             UnicodeString appendName;
   1110             getAppendName((UDateTimePatternField)topField, appendName);
   1111             const Formattable formatPattern[] = {
   1112                 resultPattern,
   1113                 tempPattern,
   1114                 appendName
   1115             };
   1116             UnicodeString emptyStr;
   1117             formattedPattern = MessageFormat::format(appendItemFormats[topField], formatPattern, 3, emptyStr, err);
   1118             lastMissingFieldMask = distanceInfo->missingFieldMask;
   1119         }
   1120     }
   1121     return formattedPattern;
   1122 }
   1123 
   1124 int32_t
   1125 DateTimePatternGenerator::getTopBitNumber(int32_t foundMask) {
   1126     if ( foundMask==0 ) {
   1127         return 0;
   1128     }
   1129     int32_t i=0;
   1130     while (foundMask!=0) {
   1131         foundMask >>=1;
   1132         ++i;
   1133     }
   1134     if (i-1 >UDATPG_ZONE_FIELD) {
   1135         return UDATPG_ZONE_FIELD;
   1136     }
   1137     else
   1138         return i-1;
   1139 }
   1140 
   1141 void
   1142 DateTimePatternGenerator::setAvailableFormat(const UnicodeString &key, UErrorCode& err)
   1143 {
   1144     fAvailableFormatKeyHash->puti(key, 1, err);
   1145 }
   1146 
   1147 UBool
   1148 DateTimePatternGenerator::isAvailableFormatSet(const UnicodeString &key) const {
   1149     return (UBool)(fAvailableFormatKeyHash->geti(key) == 1);
   1150 }
   1151 
   1152 void
   1153 DateTimePatternGenerator::copyHashtable(Hashtable *other, UErrorCode &status) {
   1154 
   1155     if (other == NULL) {
   1156         return;
   1157     }
   1158     if (fAvailableFormatKeyHash != NULL) {
   1159         delete fAvailableFormatKeyHash;
   1160         fAvailableFormatKeyHash = NULL;
   1161     }
   1162     initHashtable(status);
   1163     if(U_FAILURE(status)){
   1164         return;
   1165     }
   1166     int32_t pos = -1;
   1167     const UHashElement* elem = NULL;
   1168     // walk through the hash table and create a deep clone
   1169     while((elem = other->nextElement(pos))!= NULL){
   1170         const UHashTok otherKeyTok = elem->key;
   1171         UnicodeString* otherKey = (UnicodeString*)otherKeyTok.pointer;
   1172         fAvailableFormatKeyHash->puti(*otherKey, 1, status);
   1173         if(U_FAILURE(status)){
   1174             return;
   1175         }
   1176     }
   1177 }
   1178 
   1179 StringEnumeration*
   1180 DateTimePatternGenerator::getSkeletons(UErrorCode& status) const {
   1181     StringEnumeration* skeletonEnumerator = new DTSkeletonEnumeration(*patternMap, DT_SKELETON, status);
   1182     return skeletonEnumerator;
   1183 }
   1184 
   1185 const UnicodeString&
   1186 DateTimePatternGenerator::getPatternForSkeleton(const UnicodeString& skeleton) const {
   1187     PtnElem *curElem;
   1188 
   1189     if (skeleton.length() ==0) {
   1190         return emptyString;
   1191     }
   1192     curElem = patternMap->getHeader(skeleton.charAt(0));
   1193     while ( curElem != NULL ) {
   1194         if ( curElem->skeleton->getSkeleton()==skeleton ) {
   1195             return curElem->pattern;
   1196         }
   1197         curElem=curElem->next;
   1198     }
   1199     return emptyString;
   1200 }
   1201 
   1202 StringEnumeration*
   1203 DateTimePatternGenerator::getBaseSkeletons(UErrorCode& status) const {
   1204     StringEnumeration* baseSkeletonEnumerator = new DTSkeletonEnumeration(*patternMap, DT_BASESKELETON, status);
   1205     return baseSkeletonEnumerator;
   1206 }
   1207 
   1208 StringEnumeration*
   1209 DateTimePatternGenerator::getRedundants(UErrorCode& status) {
   1210     StringEnumeration* output = new DTRedundantEnumeration();
   1211     const UnicodeString *pattern;
   1212     PatternMapIterator it;
   1213     for (it.set(*patternMap); it.hasNext(); ) {
   1214         DateTimeMatcher current = it.next();
   1215         pattern = patternMap->getPatternFromSkeleton(*(it.getSkeleton()));
   1216         if ( isCanonicalItem(*pattern) ) {
   1217             continue;
   1218         }
   1219         if ( skipMatcher == NULL ) {
   1220             skipMatcher = new DateTimeMatcher(current);
   1221         }
   1222         else {
   1223             *skipMatcher = current;
   1224         }
   1225         UnicodeString trial = getBestPattern(current.getPattern(), status);
   1226         if (trial == *pattern) {
   1227             ((DTRedundantEnumeration *)output)->add(*pattern, status);
   1228         }
   1229         if (current.equals(skipMatcher)) {
   1230             continue;
   1231         }
   1232     }
   1233     return output;
   1234 }
   1235 
   1236 UBool
   1237 DateTimePatternGenerator::isCanonicalItem(const UnicodeString& item) const {
   1238     if ( item.length() != 1 ) {
   1239         return FALSE;
   1240     }
   1241     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1242         if (item.charAt(0)==Canonical_Items[i]) {
   1243             return TRUE;
   1244         }
   1245     }
   1246     return FALSE;
   1247 }
   1248 
   1249 
   1250 DateTimePatternGenerator*
   1251 DateTimePatternGenerator::clone() const {
   1252     return new DateTimePatternGenerator(*this);
   1253 }
   1254 
   1255 PatternMap::PatternMap() {
   1256    for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
   1257       boot[i]=NULL;
   1258    }
   1259    isDupAllowed = TRUE;
   1260 }
   1261 
   1262 void
   1263 PatternMap::copyFrom(const PatternMap& other, UErrorCode& status) {
   1264     this->isDupAllowed = other.isDupAllowed;
   1265     for (int32_t bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
   1266         PtnElem *curElem, *otherElem, *prevElem=NULL;
   1267         otherElem = other.boot[bootIndex];
   1268         while (otherElem!=NULL) {
   1269             if ((curElem = new PtnElem(otherElem->basePattern, otherElem->pattern))==NULL) {
   1270                 // out of memory
   1271                 status = U_MEMORY_ALLOCATION_ERROR;
   1272                 return;
   1273             }
   1274             if ( this->boot[bootIndex]== NULL ) {
   1275                 this->boot[bootIndex] = curElem;
   1276             }
   1277             if ((curElem->skeleton=new PtnSkeleton(*(otherElem->skeleton))) == NULL ) {
   1278                 // out of memory
   1279                 status = U_MEMORY_ALLOCATION_ERROR;
   1280                 return;
   1281             }
   1282 
   1283             if (prevElem!=NULL) {
   1284                 prevElem->next=curElem;
   1285             }
   1286             curElem->next=NULL;
   1287             prevElem = curElem;
   1288             otherElem = otherElem->next;
   1289         }
   1290 
   1291     }
   1292 }
   1293 
   1294 PtnElem*
   1295 PatternMap::getHeader(UChar baseChar) {
   1296     PtnElem* curElem;
   1297 
   1298     if ( (baseChar >= CAP_A) && (baseChar <= CAP_Z) ) {
   1299          curElem = boot[baseChar-CAP_A];
   1300     }
   1301     else {
   1302         if ( (baseChar >=LOW_A) && (baseChar <= LOW_Z) ) {
   1303             curElem = boot[26+baseChar-LOW_A];
   1304         }
   1305         else {
   1306             return NULL;
   1307         }
   1308     }
   1309     return curElem;
   1310 }
   1311 
   1312 PatternMap::~PatternMap() {
   1313    for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
   1314        if (boot[i]!=NULL ) {
   1315            delete boot[i];
   1316            boot[i]=NULL;
   1317        }
   1318    }
   1319 }  // PatternMap destructor
   1320 
   1321 void
   1322 PatternMap::add(const UnicodeString& basePattern,
   1323                 const PtnSkeleton& skeleton,
   1324                 const UnicodeString& value,// mapped pattern value
   1325                 UBool skeletonWasSpecified,
   1326                 UErrorCode &status) {
   1327     UChar baseChar = basePattern.charAt(0);
   1328     PtnElem *curElem, *baseElem;
   1329     status = U_ZERO_ERROR;
   1330 
   1331     // the baseChar must be A-Z or a-z
   1332     if ((baseChar >= CAP_A) && (baseChar <= CAP_Z)) {
   1333         baseElem = boot[baseChar-CAP_A];
   1334     }
   1335     else {
   1336         if ((baseChar >=LOW_A) && (baseChar <= LOW_Z)) {
   1337             baseElem = boot[26+baseChar-LOW_A];
   1338          }
   1339          else {
   1340              status = U_ILLEGAL_CHARACTER;
   1341              return;
   1342          }
   1343     }
   1344 
   1345     if (baseElem == NULL) {
   1346         if ((curElem = new PtnElem(basePattern, value)) == NULL ) {
   1347             // out of memory
   1348             status = U_MEMORY_ALLOCATION_ERROR;
   1349             return;
   1350         }
   1351         if (baseChar >= LOW_A) {
   1352             boot[26 + (baseChar-LOW_A)] = curElem;
   1353         }
   1354         else {
   1355             boot[baseChar-CAP_A] = curElem;
   1356         }
   1357         curElem->skeleton = new PtnSkeleton(skeleton);
   1358         curElem->skeletonWasSpecified = skeletonWasSpecified;
   1359     }
   1360     if ( baseElem != NULL ) {
   1361         curElem = getDuplicateElem(basePattern, skeleton, baseElem);
   1362 
   1363         if (curElem == NULL) {
   1364             // add new element to the list.
   1365             curElem = baseElem;
   1366             while( curElem -> next != NULL )
   1367             {
   1368                 curElem = curElem->next;
   1369             }
   1370             if ((curElem->next = new PtnElem(basePattern, value)) == NULL ) {
   1371                 // out of memory
   1372                 status = U_MEMORY_ALLOCATION_ERROR;
   1373                 return;
   1374             }
   1375             curElem=curElem->next;
   1376             curElem->skeleton = new PtnSkeleton(skeleton);
   1377             curElem->skeletonWasSpecified = skeletonWasSpecified;
   1378         }
   1379         else {
   1380             // Pattern exists in the list already.
   1381             if ( !isDupAllowed ) {
   1382                 return;
   1383             }
   1384             // Overwrite the value.
   1385             curElem->pattern = value;
   1386         }
   1387     }
   1388 }  // PatternMap::add
   1389 
   1390 // Find the pattern from the given basePattern string.
   1391 const UnicodeString *
   1392 PatternMap::getPatternFromBasePattern(UnicodeString& basePattern, UBool& skeletonWasSpecified) { // key to search for
   1393    PtnElem *curElem;
   1394 
   1395    if ((curElem=getHeader(basePattern.charAt(0)))==NULL) {
   1396        return NULL;  // no match
   1397    }
   1398 
   1399    do  {
   1400        if ( basePattern.compare(curElem->basePattern)==0 ) {
   1401           skeletonWasSpecified = curElem->skeletonWasSpecified;
   1402           return &(curElem->pattern);
   1403        }
   1404        curElem=curElem->next;
   1405    }while (curElem != NULL);
   1406 
   1407    return NULL;
   1408 }  // PatternMap::getFromBasePattern
   1409 
   1410 
   1411 // Find the pattern from the given skeleton.
   1412 // At least when this is called from getBestRaw & addPattern (in which case specifiedSkeletonPtr is non-NULL),
   1413 // the comparison should be based on skeleton.original (which is unique and tied to the distance measurement in bestRaw)
   1414 // and not skeleton.baseOriginal (which is not unique); otherwise we may pick a different skeleton than the one with the
   1415 // optimum distance value in getBestRaw. When this is called from public getRedundants (specifiedSkeletonPtr is NULL),
   1416 // for now it will continue to compare based on baseOriginal so as not to change the behavior unnecessarily.
   1417 const UnicodeString *
   1418 PatternMap::getPatternFromSkeleton(PtnSkeleton& skeleton, const PtnSkeleton** specifiedSkeletonPtr) { // key to search for
   1419    PtnElem *curElem;
   1420 
   1421    if (specifiedSkeletonPtr) {
   1422        *specifiedSkeletonPtr = NULL;
   1423    }
   1424 
   1425    // find boot entry
   1426    UChar baseChar='\0';
   1427    for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1428        if (skeleton.baseOriginal[i].length() !=0 ) {
   1429            baseChar = skeleton.baseOriginal[i].charAt(0);
   1430            break;
   1431        }
   1432    }
   1433 
   1434    if ((curElem=getHeader(baseChar))==NULL) {
   1435        return NULL;  // no match
   1436    }
   1437 
   1438    do  {
   1439        int32_t i=0;
   1440        if (specifiedSkeletonPtr != NULL) { // called from DateTimePatternGenerator::getBestRaw or addPattern, use original
   1441            for (i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1442                if (curElem->skeleton->original[i].compare(skeleton.original[i]) != 0 )
   1443                {
   1444                    break;
   1445                }
   1446            }
   1447        } else { // called from DateTimePatternGenerator::getRedundants, use baseOriginal
   1448            for (i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1449                if (curElem->skeleton->baseOriginal[i].compare(skeleton.baseOriginal[i]) != 0 )
   1450                {
   1451                    break;
   1452                }
   1453            }
   1454        }
   1455        if (i == UDATPG_FIELD_COUNT) {
   1456            if (specifiedSkeletonPtr && curElem->skeletonWasSpecified) {
   1457                *specifiedSkeletonPtr = curElem->skeleton;
   1458            }
   1459            return &(curElem->pattern);
   1460        }
   1461        curElem=curElem->next;
   1462    }while (curElem != NULL);
   1463 
   1464    return NULL;
   1465 }
   1466 
   1467 UBool
   1468 PatternMap::equals(const PatternMap& other) {
   1469     if ( this==&other ) {
   1470         return TRUE;
   1471     }
   1472     for (int32_t bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
   1473         if ( boot[bootIndex]==other.boot[bootIndex] ) {
   1474             continue;
   1475         }
   1476         if ( (boot[bootIndex]==NULL)||(other.boot[bootIndex]==NULL) ) {
   1477             return FALSE;
   1478         }
   1479         PtnElem *otherElem = other.boot[bootIndex];
   1480         PtnElem *myElem = boot[bootIndex];
   1481         while ((otherElem!=NULL) || (myElem!=NULL)) {
   1482             if ( myElem == otherElem ) {
   1483                 break;
   1484             }
   1485             if ((otherElem==NULL) || (myElem==NULL)) {
   1486                 return FALSE;
   1487             }
   1488             if ( (myElem->basePattern != otherElem->basePattern) ||
   1489                  (myElem->pattern != otherElem->pattern) ) {
   1490                 return FALSE;
   1491             }
   1492             if ((myElem->skeleton!=otherElem->skeleton)&&
   1493                 !myElem->skeleton->equals(*(otherElem->skeleton))) {
   1494                 return FALSE;
   1495             }
   1496             myElem = myElem->next;
   1497             otherElem=otherElem->next;
   1498         }
   1499     }
   1500     return TRUE;
   1501 }
   1502 
   1503 // find any key existing in the mapping table already.
   1504 // return TRUE if there is an existing key, otherwise return FALSE.
   1505 PtnElem*
   1506 PatternMap::getDuplicateElem(
   1507             const UnicodeString &basePattern,
   1508             const PtnSkeleton &skeleton,
   1509             PtnElem *baseElem)  {
   1510    PtnElem *curElem;
   1511 
   1512    if ( baseElem == (PtnElem *)NULL )  {
   1513          return (PtnElem*)NULL;
   1514    }
   1515    else {
   1516          curElem = baseElem;
   1517    }
   1518    do {
   1519      if ( basePattern.compare(curElem->basePattern)==0 ) {
   1520         UBool isEqual=TRUE;
   1521         for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1522             if (curElem->skeleton->type[i] != skeleton.type[i] ) {
   1523                 isEqual=FALSE;
   1524                 break;
   1525             }
   1526         }
   1527         if (isEqual) {
   1528             return curElem;
   1529         }
   1530      }
   1531      curElem = curElem->next;
   1532    } while( curElem != (PtnElem *)NULL );
   1533 
   1534    // end of the list
   1535    return (PtnElem*)NULL;
   1536 
   1537 }  // PatternMap::getDuplicateElem
   1538 
   1539 DateTimeMatcher::DateTimeMatcher(void) {
   1540 }
   1541 
   1542 DateTimeMatcher::DateTimeMatcher(const DateTimeMatcher& other) {
   1543     copyFrom(other.skeleton);
   1544 }
   1545 
   1546 
   1547 void
   1548 DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp) {
   1549     PtnSkeleton localSkeleton;
   1550     return set(pattern, fp, localSkeleton);
   1551 }
   1552 
   1553 void
   1554 DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp, PtnSkeleton& skeletonResult) {
   1555     int32_t i;
   1556     for (i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1557         skeletonResult.type[i]=NONE;
   1558     }
   1559     fp->set(pattern);
   1560     for (i=0; i < fp->itemNumber; i++) {
   1561         UnicodeString field = fp->items[i];
   1562         if ( field.charAt(0) == LOW_A ) {
   1563             continue;  // skip 'a'
   1564         }
   1565 
   1566         if ( fp->isQuoteLiteral(field) ) {
   1567             UnicodeString quoteLiteral;
   1568             fp->getQuoteLiteral(quoteLiteral, &i);
   1569             continue;
   1570         }
   1571         int32_t canonicalIndex = fp->getCanonicalIndex(field);
   1572         if (canonicalIndex < 0 ) {
   1573             continue;
   1574         }
   1575         const dtTypeElem *row = &dtTypes[canonicalIndex];
   1576         int32_t typeValue = row->field;
   1577         skeletonResult.original[typeValue]=field;
   1578         UChar repeatChar = row->patternChar;
   1579         int32_t repeatCount = row->minLen > 3 ? 3: row->minLen;
   1580         while (repeatCount-- > 0) {
   1581             skeletonResult.baseOriginal[typeValue] += repeatChar;
   1582         }
   1583         int16_t subTypeValue = row->type;
   1584         if ( row->type > 0) {
   1585             subTypeValue += field.length();
   1586         }
   1587         skeletonResult.type[typeValue] = subTypeValue;
   1588     }
   1589     copyFrom(skeletonResult);
   1590 }
   1591 
   1592 void
   1593 DateTimeMatcher::getBasePattern(UnicodeString &result ) {
   1594     result.remove(); // Reset the result first.
   1595     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
   1596         if (skeleton.baseOriginal[i].length()!=0) {
   1597             result += skeleton.baseOriginal[i];
   1598         }
   1599     }
   1600 }
   1601 
   1602 UnicodeString
   1603 DateTimeMatcher::getPattern() {
   1604     UnicodeString result;
   1605 
   1606     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
   1607         if (skeleton.original[i].length()!=0) {
   1608             result += skeleton.original[i];
   1609         }
   1610     }
   1611     return result;
   1612 }
   1613 
   1614 int32_t
   1615 DateTimeMatcher::getDistance(const DateTimeMatcher& other, int32_t includeMask, DistanceInfo& distanceInfo) {
   1616     int32_t result=0;
   1617     distanceInfo.clear();
   1618     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
   1619         int32_t myType = (includeMask&(1<<i))==0 ? 0 : skeleton.type[i];
   1620         int32_t otherType = other.skeleton.type[i];
   1621         if (myType==otherType) {
   1622             continue;
   1623         }
   1624         if (myType==0) {// and other is not
   1625             result += EXTRA_FIELD;
   1626             distanceInfo.addExtra(i);
   1627         }
   1628         else {
   1629             if (otherType==0) {
   1630                 result += MISSING_FIELD;
   1631                 distanceInfo.addMissing(i);
   1632             }
   1633             else {
   1634                 result += abs(myType - otherType);
   1635             }
   1636         }
   1637 
   1638     }
   1639     return result;
   1640 }
   1641 
   1642 void
   1643 DateTimeMatcher::copyFrom(const PtnSkeleton& newSkeleton) {
   1644     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1645         this->skeleton.type[i]=newSkeleton.type[i];
   1646         this->skeleton.original[i]=newSkeleton.original[i];
   1647         this->skeleton.baseOriginal[i]=newSkeleton.baseOriginal[i];
   1648     }
   1649 }
   1650 
   1651 void
   1652 DateTimeMatcher::copyFrom() {
   1653     // same as clear
   1654     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1655         this->skeleton.type[i]=0;
   1656         this->skeleton.original[i].remove();
   1657         this->skeleton.baseOriginal[i].remove();
   1658     }
   1659 }
   1660 
   1661 UBool
   1662 DateTimeMatcher::equals(const DateTimeMatcher* other) const {
   1663     if (other==NULL) {
   1664         return FALSE;
   1665     }
   1666     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1667         if (this->skeleton.original[i]!=other->skeleton.original[i] ) {
   1668             return FALSE;
   1669         }
   1670     }
   1671     return TRUE;
   1672 }
   1673 
   1674 int32_t
   1675 DateTimeMatcher::getFieldMask() {
   1676     int32_t result=0;
   1677 
   1678     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1679         if (skeleton.type[i]!=0) {
   1680             result |= (1<<i);
   1681         }
   1682     }
   1683     return result;
   1684 }
   1685 
   1686 PtnSkeleton*
   1687 DateTimeMatcher::getSkeletonPtr() {
   1688     return &skeleton;
   1689 }
   1690 
   1691 FormatParser::FormatParser () {
   1692     status = START;
   1693     itemNumber=0;
   1694 }
   1695 
   1696 
   1697 FormatParser::~FormatParser () {
   1698 }
   1699 
   1700 
   1701 // Find the next token with the starting position and length
   1702 // Note: the startPos may
   1703 FormatParser::TokenStatus
   1704 FormatParser::setTokens(const UnicodeString& pattern, int32_t startPos, int32_t *len) {
   1705     int32_t  curLoc = startPos;
   1706     if ( curLoc >= pattern.length()) {
   1707         return DONE;
   1708     }
   1709     // check the current char is between A-Z or a-z
   1710     do {
   1711         UChar c=pattern.charAt(curLoc);
   1712         if ( (c>=CAP_A && c<=CAP_Z) || (c>=LOW_A && c<=LOW_Z) ) {
   1713            curLoc++;
   1714         }
   1715         else {
   1716                startPos = curLoc;
   1717                *len=1;
   1718                return ADD_TOKEN;
   1719         }
   1720 
   1721         if ( pattern.charAt(curLoc)!= pattern.charAt(startPos) ) {
   1722             break;  // not the same token
   1723         }
   1724     } while(curLoc <= pattern.length());
   1725     *len = curLoc-startPos;
   1726     return ADD_TOKEN;
   1727 }
   1728 
   1729 void
   1730 FormatParser::set(const UnicodeString& pattern) {
   1731     int32_t startPos=0;
   1732     TokenStatus result=START;
   1733     int32_t len=0;
   1734     itemNumber =0;
   1735 
   1736     do {
   1737         result = setTokens( pattern, startPos, &len );
   1738         if ( result == ADD_TOKEN )
   1739         {
   1740             items[itemNumber++] = UnicodeString(pattern, startPos, len );
   1741             startPos += len;
   1742         }
   1743         else {
   1744             break;
   1745         }
   1746     } while (result==ADD_TOKEN && itemNumber < MAX_DT_TOKEN);
   1747 }
   1748 
   1749 int32_t
   1750 FormatParser::getCanonicalIndex(const UnicodeString& s, UBool strict) {
   1751     int32_t len = s.length();
   1752     if (len == 0) {
   1753         return -1;
   1754     }
   1755     UChar ch = s.charAt(0);
   1756 
   1757     // Verify that all are the same character.
   1758     for (int32_t l = 1; l < len; l++) {
   1759         if (ch != s.charAt(l)) {
   1760             return -1;
   1761         }
   1762     }
   1763     int32_t i = 0;
   1764     int32_t bestRow = -1;
   1765     while (dtTypes[i].patternChar != '\0') {
   1766         if ( dtTypes[i].patternChar != ch ) {
   1767             ++i;
   1768             continue;
   1769         }
   1770         bestRow = i;
   1771         if (dtTypes[i].patternChar != dtTypes[i+1].patternChar) {
   1772             return i;
   1773         }
   1774         if (dtTypes[i+1].minLen <= len) {
   1775             ++i;
   1776             continue;
   1777         }
   1778         return i;
   1779     }
   1780     return strict ? -1 : bestRow;
   1781 }
   1782 
   1783 UBool
   1784 FormatParser::isQuoteLiteral(const UnicodeString& s) const {
   1785     return (UBool)(s.charAt(0)==SINGLE_QUOTE);
   1786 }
   1787 
   1788 // This function aussumes the current itemIndex points to the quote literal.
   1789 // Please call isQuoteLiteral prior to this function.
   1790 void
   1791 FormatParser::getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex) {
   1792     int32_t i=*itemIndex;
   1793 
   1794     quote.remove();
   1795     if (items[i].charAt(0)==SINGLE_QUOTE) {
   1796         quote += items[i];
   1797         ++i;
   1798     }
   1799     while ( i < itemNumber ) {
   1800         if ( items[i].charAt(0)==SINGLE_QUOTE ) {
   1801             if ( (i+1<itemNumber) && (items[i+1].charAt(0)==SINGLE_QUOTE)) {
   1802                 // two single quotes e.g. 'o''clock'
   1803                 quote += items[i++];
   1804                 quote += items[i++];
   1805                 continue;
   1806             }
   1807             else {
   1808                 quote += items[i];
   1809                 break;
   1810             }
   1811         }
   1812         else {
   1813             quote += items[i];
   1814         }
   1815         ++i;
   1816     }
   1817     *itemIndex=i;
   1818 }
   1819 
   1820 UBool
   1821 FormatParser::isPatternSeparator(UnicodeString& field) {
   1822     for (int32_t i=0; i<field.length(); ++i ) {
   1823         UChar c= field.charAt(i);
   1824         if ( (c==SINGLE_QUOTE) || (c==BACKSLASH) || (c==SPACE) || (c==COLON) ||
   1825              (c==QUOTATION_MARK) || (c==COMMA) || (c==HYPHEN) ||(items[i].charAt(0)==DOT) ) {
   1826             continue;
   1827         }
   1828         else {
   1829             return FALSE;
   1830         }
   1831     }
   1832     return TRUE;
   1833 }
   1834 
   1835 void
   1836 DistanceInfo::setTo(DistanceInfo &other) {
   1837     missingFieldMask = other.missingFieldMask;
   1838     extraFieldMask= other.extraFieldMask;
   1839 }
   1840 
   1841 PatternMapIterator::PatternMapIterator() {
   1842     bootIndex = 0;
   1843     nodePtr = NULL;
   1844     patternMap=NULL;
   1845     matcher= new DateTimeMatcher();
   1846 }
   1847 
   1848 
   1849 PatternMapIterator::~PatternMapIterator() {
   1850     delete matcher;
   1851 }
   1852 
   1853 void
   1854 PatternMapIterator::set(PatternMap& newPatternMap) {
   1855     this->patternMap=&newPatternMap;
   1856 }
   1857 
   1858 PtnSkeleton*
   1859 PatternMapIterator::getSkeleton() {
   1860     if ( nodePtr == NULL ) {
   1861         return NULL;
   1862     }
   1863     else {
   1864         return nodePtr->skeleton;
   1865     }
   1866 }
   1867 
   1868 UBool
   1869 PatternMapIterator::hasNext() {
   1870     int32_t headIndex=bootIndex;
   1871     PtnElem *curPtr=nodePtr;
   1872 
   1873     if (patternMap==NULL) {
   1874         return FALSE;
   1875     }
   1876     while ( headIndex < MAX_PATTERN_ENTRIES ) {
   1877         if ( curPtr != NULL ) {
   1878             if ( curPtr->next != NULL ) {
   1879                 return TRUE;
   1880             }
   1881             else {
   1882                 headIndex++;
   1883                 curPtr=NULL;
   1884                 continue;
   1885             }
   1886         }
   1887         else {
   1888             if ( patternMap->boot[headIndex] != NULL ) {
   1889                 return TRUE;
   1890             }
   1891             else {
   1892                 headIndex++;
   1893                 continue;
   1894             }
   1895         }
   1896 
   1897     }
   1898     return FALSE;
   1899 }
   1900 
   1901 DateTimeMatcher&
   1902 PatternMapIterator::next() {
   1903     while ( bootIndex < MAX_PATTERN_ENTRIES ) {
   1904         if ( nodePtr != NULL ) {
   1905             if ( nodePtr->next != NULL ) {
   1906                 nodePtr = nodePtr->next;
   1907                 break;
   1908             }
   1909             else {
   1910                 bootIndex++;
   1911                 nodePtr=NULL;
   1912                 continue;
   1913             }
   1914         }
   1915         else {
   1916             if ( patternMap->boot[bootIndex] != NULL ) {
   1917                 nodePtr = patternMap->boot[bootIndex];
   1918                 break;
   1919             }
   1920             else {
   1921                 bootIndex++;
   1922                 continue;
   1923             }
   1924         }
   1925     }
   1926     if (nodePtr!=NULL) {
   1927         matcher->copyFrom(*nodePtr->skeleton);
   1928     }
   1929     else {
   1930         matcher->copyFrom();
   1931     }
   1932     return *matcher;
   1933 }
   1934 
   1935 PtnSkeleton::PtnSkeleton() {
   1936 }
   1937 
   1938 
   1939 PtnSkeleton::PtnSkeleton(const PtnSkeleton& other) {
   1940     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1941         this->type[i]=other.type[i];
   1942         this->original[i]=other.original[i];
   1943         this->baseOriginal[i]=other.baseOriginal[i];
   1944     }
   1945 }
   1946 
   1947 UBool
   1948 PtnSkeleton::equals(const PtnSkeleton& other)  {
   1949     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   1950         if ( (type[i]!= other.type[i]) ||
   1951              (original[i]!=other.original[i]) ||
   1952              (baseOriginal[i]!=other.baseOriginal[i]) ) {
   1953             return FALSE;
   1954         }
   1955     }
   1956     return TRUE;
   1957 }
   1958 
   1959 UnicodeString
   1960 PtnSkeleton::getSkeleton() {
   1961     UnicodeString result;
   1962 
   1963     for(int32_t i=0; i< UDATPG_FIELD_COUNT; ++i) {
   1964         if (original[i].length()!=0) {
   1965             result += original[i];
   1966         }
   1967     }
   1968     return result;
   1969 }
   1970 
   1971 UnicodeString
   1972 PtnSkeleton::getBaseSkeleton() {
   1973     UnicodeString result;
   1974 
   1975     for(int32_t i=0; i< UDATPG_FIELD_COUNT; ++i) {
   1976         if (baseOriginal[i].length()!=0) {
   1977             result += baseOriginal[i];
   1978         }
   1979     }
   1980     return result;
   1981 }
   1982 
   1983 PtnSkeleton::~PtnSkeleton() {
   1984 }
   1985 
   1986 PtnElem::PtnElem(const UnicodeString &basePat, const UnicodeString &pat) :
   1987 basePattern(basePat),
   1988 skeleton(NULL),
   1989 pattern(pat),
   1990 next(NULL)
   1991 {
   1992 }
   1993 
   1994 PtnElem::~PtnElem() {
   1995 
   1996     if (next!=NULL) {
   1997         delete next;
   1998     }
   1999     delete skeleton;
   2000 }
   2001 
   2002 DTSkeletonEnumeration::DTSkeletonEnumeration(PatternMap &patternMap, dtStrEnum type, UErrorCode& status) {
   2003     PtnElem  *curElem;
   2004     PtnSkeleton *curSkeleton;
   2005     UnicodeString s;
   2006     int32_t bootIndex;
   2007 
   2008     pos=0;
   2009     fSkeletons = new UVector(status);
   2010     if (U_FAILURE(status)) {
   2011         delete fSkeletons;
   2012         return;
   2013     }
   2014     for (bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
   2015         curElem = patternMap.boot[bootIndex];
   2016         while (curElem!=NULL) {
   2017             switch(type) {
   2018                 case DT_BASESKELETON:
   2019                     s=curElem->basePattern;
   2020                     break;
   2021                 case DT_PATTERN:
   2022                     s=curElem->pattern;
   2023                     break;
   2024                 case DT_SKELETON:
   2025                     curSkeleton=curElem->skeleton;
   2026                     s=curSkeleton->getSkeleton();
   2027                     break;
   2028             }
   2029             if ( !isCanonicalItem(s) ) {
   2030                 fSkeletons->addElement(new UnicodeString(s), status);
   2031                 if (U_FAILURE(status)) {
   2032                     delete fSkeletons;
   2033                     fSkeletons = NULL;
   2034                     return;
   2035                 }
   2036             }
   2037             curElem = curElem->next;
   2038         }
   2039     }
   2040     if ((bootIndex==MAX_PATTERN_ENTRIES) && (curElem!=NULL) ) {
   2041         status = U_BUFFER_OVERFLOW_ERROR;
   2042     }
   2043 }
   2044 
   2045 const UnicodeString*
   2046 DTSkeletonEnumeration::snext(UErrorCode& status) {
   2047     if (U_SUCCESS(status) && pos < fSkeletons->size()) {
   2048         return (const UnicodeString*)fSkeletons->elementAt(pos++);
   2049     }
   2050     return NULL;
   2051 }
   2052 
   2053 void
   2054 DTSkeletonEnumeration::reset(UErrorCode& /*status*/) {
   2055     pos=0;
   2056 }
   2057 
   2058 int32_t
   2059 DTSkeletonEnumeration::count(UErrorCode& /*status*/) const {
   2060    return (fSkeletons==NULL) ? 0 : fSkeletons->size();
   2061 }
   2062 
   2063 UBool
   2064 DTSkeletonEnumeration::isCanonicalItem(const UnicodeString& item) {
   2065     if ( item.length() != 1 ) {
   2066         return FALSE;
   2067     }
   2068     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   2069         if (item.charAt(0)==Canonical_Items[i]) {
   2070             return TRUE;
   2071         }
   2072     }
   2073     return FALSE;
   2074 }
   2075 
   2076 DTSkeletonEnumeration::~DTSkeletonEnumeration() {
   2077     UnicodeString *s;
   2078     for (int32_t i=0; i<fSkeletons->size(); ++i) {
   2079         if ((s=(UnicodeString *)fSkeletons->elementAt(i))!=NULL) {
   2080             delete s;
   2081         }
   2082     }
   2083     delete fSkeletons;
   2084 }
   2085 
   2086 DTRedundantEnumeration::DTRedundantEnumeration() {
   2087     pos=0;
   2088     fPatterns = NULL;
   2089 }
   2090 
   2091 void
   2092 DTRedundantEnumeration::add(const UnicodeString& pattern, UErrorCode& status) {
   2093     if (U_FAILURE(status)) return;
   2094     if (fPatterns == NULL)  {
   2095         fPatterns = new UVector(status);
   2096         if (U_FAILURE(status)) {
   2097             delete fPatterns;
   2098             fPatterns = NULL;
   2099             return;
   2100        }
   2101     }
   2102     fPatterns->addElement(new UnicodeString(pattern), status);
   2103     if (U_FAILURE(status)) {
   2104         delete fPatterns;
   2105         fPatterns = NULL;
   2106         return;
   2107     }
   2108 }
   2109 
   2110 const UnicodeString*
   2111 DTRedundantEnumeration::snext(UErrorCode& status) {
   2112     if (U_SUCCESS(status) && pos < fPatterns->size()) {
   2113         return (const UnicodeString*)fPatterns->elementAt(pos++);
   2114     }
   2115     return NULL;
   2116 }
   2117 
   2118 void
   2119 DTRedundantEnumeration::reset(UErrorCode& /*status*/) {
   2120     pos=0;
   2121 }
   2122 
   2123 int32_t
   2124 DTRedundantEnumeration::count(UErrorCode& /*status*/) const {
   2125        return (fPatterns==NULL) ? 0 : fPatterns->size();
   2126 }
   2127 
   2128 UBool
   2129 DTRedundantEnumeration::isCanonicalItem(const UnicodeString& item) {
   2130     if ( item.length() != 1 ) {
   2131         return FALSE;
   2132     }
   2133     for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
   2134         if (item.charAt(0)==Canonical_Items[i]) {
   2135             return TRUE;
   2136         }
   2137     }
   2138     return FALSE;
   2139 }
   2140 
   2141 DTRedundantEnumeration::~DTRedundantEnumeration() {
   2142     UnicodeString *s;
   2143     for (int32_t i=0; i<fPatterns->size(); ++i) {
   2144         if ((s=(UnicodeString *)fPatterns->elementAt(i))!=NULL) {
   2145             delete s;
   2146         }
   2147     }
   2148     delete fPatterns;
   2149 }
   2150 
   2151 U_NAMESPACE_END
   2152 
   2153 
   2154 #endif /* #if !UCONFIG_NO_FORMATTING */
   2155 
   2156 //eof
   2157