michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2007-2013, International Business Machines Corporation and michael@0: * others. All Rights Reserved. michael@0: ******************************************************************************* michael@0: * michael@0: * File DTPTNGEN.CPP michael@0: * michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/datefmt.h" michael@0: #include "unicode/decimfmt.h" michael@0: #include "unicode/dtfmtsym.h" michael@0: #include "unicode/dtptngen.h" michael@0: #include "unicode/msgfmt.h" michael@0: #include "unicode/smpdtfmt.h" michael@0: #include "unicode/udat.h" michael@0: #include "unicode/udatpg.h" michael@0: #include "unicode/uniset.h" michael@0: #include "unicode/uloc.h" michael@0: #include "unicode/ures.h" michael@0: #include "unicode/ustring.h" michael@0: #include "unicode/rep.h" michael@0: #include "cpputils.h" michael@0: #include "ucln_in.h" michael@0: #include "mutex.h" michael@0: #include "cmemory.h" michael@0: #include "cstring.h" michael@0: #include "locbased.h" michael@0: #include "gregoimp.h" michael@0: #include "hash.h" michael@0: #include "uresimp.h" michael@0: #include "dtptngen_impl.h" michael@0: michael@0: #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) michael@0: michael@0: #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY michael@0: /** michael@0: * If we are on EBCDIC, use an iterator which will michael@0: * traverse the bundles in ASCII order. michael@0: */ michael@0: #define U_USE_ASCII_BUNDLE_ITERATOR michael@0: #define U_SORT_ASCII_BUNDLE_ITERATOR michael@0: #endif michael@0: michael@0: #if defined(U_USE_ASCII_BUNDLE_ITERATOR) michael@0: michael@0: #include "unicode/ustring.h" michael@0: #include "uarrsort.h" michael@0: michael@0: struct UResAEntry { michael@0: UChar *key; michael@0: UResourceBundle *item; michael@0: }; michael@0: michael@0: struct UResourceBundleAIterator { michael@0: UResourceBundle *bund; michael@0: UResAEntry *entries; michael@0: int32_t num; michael@0: int32_t cursor; michael@0: }; michael@0: michael@0: /* Must be C linkage to pass function pointer to the sort function */ michael@0: michael@0: U_CDECL_BEGIN michael@0: michael@0: static int32_t U_CALLCONV michael@0: ures_a_codepointSort(const void *context, const void *left, const void *right) { michael@0: //CompareContext *cmp=(CompareContext *)context; michael@0: return u_strcmp(((const UResAEntry *)left)->key, michael@0: ((const UResAEntry *)right)->key); michael@0: } michael@0: michael@0: U_CDECL_END michael@0: michael@0: static void ures_a_open(UResourceBundleAIterator *aiter, UResourceBundle *bund, UErrorCode *status) { michael@0: if(U_FAILURE(*status)) { michael@0: return; michael@0: } michael@0: aiter->bund = bund; michael@0: aiter->num = ures_getSize(aiter->bund); michael@0: aiter->cursor = 0; michael@0: #if !defined(U_SORT_ASCII_BUNDLE_ITERATOR) michael@0: aiter->entries = NULL; michael@0: #else michael@0: aiter->entries = (UResAEntry*)uprv_malloc(sizeof(UResAEntry)*aiter->num); michael@0: for(int i=0;inum;i++) { michael@0: aiter->entries[i].item = ures_getByIndex(aiter->bund, i, NULL, status); michael@0: const char *akey = ures_getKey(aiter->entries[i].item); michael@0: int32_t len = uprv_strlen(akey)+1; michael@0: aiter->entries[i].key = (UChar*)uprv_malloc(len*sizeof(UChar)); michael@0: u_charsToUChars(akey, aiter->entries[i].key, len); michael@0: } michael@0: uprv_sortArray(aiter->entries, aiter->num, sizeof(UResAEntry), ures_a_codepointSort, NULL, TRUE, status); michael@0: #endif michael@0: } michael@0: michael@0: static void ures_a_close(UResourceBundleAIterator *aiter) { michael@0: #if defined(U_SORT_ASCII_BUNDLE_ITERATOR) michael@0: for(int i=0;inum;i++) { michael@0: uprv_free(aiter->entries[i].key); michael@0: ures_close(aiter->entries[i].item); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: static const UChar *ures_a_getNextString(UResourceBundleAIterator *aiter, int32_t *len, const char **key, UErrorCode *err) { michael@0: #if !defined(U_SORT_ASCII_BUNDLE_ITERATOR) michael@0: return ures_getNextString(aiter->bund, len, key, err); michael@0: #else michael@0: if(U_FAILURE(*err)) return NULL; michael@0: UResourceBundle *item = aiter->entries[aiter->cursor].item; michael@0: const UChar* ret = ures_getString(item, len, err); michael@0: *key = ures_getKey(item); michael@0: aiter->cursor++; michael@0: return ret; michael@0: #endif michael@0: } michael@0: michael@0: michael@0: #endif michael@0: michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: michael@0: // ***************************************************************************** michael@0: // class DateTimePatternGenerator michael@0: // ***************************************************************************** michael@0: static const UChar Canonical_Items[] = { michael@0: // GyQMwWEdDFHmsSv michael@0: CAP_G, LOW_Y, CAP_Q, CAP_M, LOW_W, CAP_W, CAP_E, LOW_D, CAP_D, CAP_F, michael@0: CAP_H, LOW_M, LOW_S, CAP_S, LOW_V, 0 michael@0: }; michael@0: michael@0: static const dtTypeElem dtTypes[] = { michael@0: // patternChar, field, type, minLen, weight michael@0: {CAP_G, UDATPG_ERA_FIELD, DT_SHORT, 1, 3,}, michael@0: {CAP_G, UDATPG_ERA_FIELD, DT_LONG, 4, 0}, michael@0: {LOW_Y, UDATPG_YEAR_FIELD, DT_NUMERIC, 1, 20}, michael@0: {CAP_Y, UDATPG_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 20}, michael@0: {LOW_U, UDATPG_YEAR_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 20}, michael@0: {CAP_U, UDATPG_YEAR_FIELD, DT_SHORT, 1, 3}, michael@0: {CAP_U, UDATPG_YEAR_FIELD, DT_LONG, 4, 0}, michael@0: {CAP_U, UDATPG_YEAR_FIELD, DT_NARROW, 5, 0}, michael@0: {CAP_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC, 1, 2}, michael@0: {CAP_Q, UDATPG_QUARTER_FIELD, DT_SHORT, 3, 0}, michael@0: {CAP_Q, UDATPG_QUARTER_FIELD, DT_LONG, 4, 0}, michael@0: {LOW_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, michael@0: {LOW_Q, UDATPG_QUARTER_FIELD, DT_SHORT + DT_DELTA, 3, 0}, michael@0: {LOW_Q, UDATPG_QUARTER_FIELD, DT_LONG + DT_DELTA, 4, 0}, michael@0: {CAP_M, UDATPG_MONTH_FIELD, DT_NUMERIC, 1, 2}, michael@0: {CAP_M, UDATPG_MONTH_FIELD, DT_SHORT, 3, 0}, michael@0: {CAP_M, UDATPG_MONTH_FIELD, DT_LONG, 4, 0}, michael@0: {CAP_M, UDATPG_MONTH_FIELD, DT_NARROW, 5, 0}, michael@0: {CAP_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, michael@0: {CAP_L, UDATPG_MONTH_FIELD, DT_SHORT - DT_DELTA, 3, 0}, michael@0: {CAP_L, UDATPG_MONTH_FIELD, DT_LONG - DT_DELTA, 4, 0}, michael@0: {CAP_L, UDATPG_MONTH_FIELD, DT_NARROW - DT_DELTA, 5, 0}, michael@0: {LOW_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 1}, michael@0: {LOW_W, UDATPG_WEEK_OF_YEAR_FIELD, DT_NUMERIC, 1, 2}, michael@0: {CAP_W, UDATPG_WEEK_OF_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 0}, michael@0: {CAP_E, UDATPG_WEEKDAY_FIELD, DT_SHORT, 1, 3}, michael@0: {CAP_E, UDATPG_WEEKDAY_FIELD, DT_LONG, 4, 0}, michael@0: {CAP_E, UDATPG_WEEKDAY_FIELD, DT_NARROW, 5, 0}, michael@0: {LOW_C, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 2}, michael@0: {LOW_C, UDATPG_WEEKDAY_FIELD, DT_SHORT - 2*DT_DELTA, 3, 0}, michael@0: {LOW_C, UDATPG_WEEKDAY_FIELD, DT_LONG - 2*DT_DELTA, 4, 0}, michael@0: {LOW_C, UDATPG_WEEKDAY_FIELD, DT_NARROW - 2*DT_DELTA, 5, 0}, michael@0: {LOW_E, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // LOW_E is currently not used in CLDR data, should not be canonical michael@0: {LOW_E, UDATPG_WEEKDAY_FIELD, DT_SHORT - DT_DELTA, 3, 0}, michael@0: {LOW_E, UDATPG_WEEKDAY_FIELD, DT_LONG - DT_DELTA, 4, 0}, michael@0: {LOW_E, UDATPG_WEEKDAY_FIELD, DT_NARROW - DT_DELTA, 5, 0}, michael@0: {LOW_D, UDATPG_DAY_FIELD, DT_NUMERIC, 1, 2}, michael@0: {CAP_D, UDATPG_DAY_OF_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 3}, michael@0: {CAP_F, UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 0}, michael@0: {LOW_G, UDATPG_DAY_FIELD, DT_NUMERIC + 3*DT_DELTA, 1, 20}, // really internal use, so we don't care michael@0: {LOW_A, UDATPG_DAYPERIOD_FIELD, DT_SHORT, 1, 0}, michael@0: {CAP_H, UDATPG_HOUR_FIELD, DT_NUMERIC + 10*DT_DELTA, 1, 2}, // 24 hour michael@0: {LOW_K, UDATPG_HOUR_FIELD, DT_NUMERIC + 11*DT_DELTA, 1, 2}, // 24 hour michael@0: {LOW_H, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12 hour michael@0: {CAP_K, UDATPG_HOUR_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // 12 hour michael@0: {LOW_M, UDATPG_MINUTE_FIELD, DT_NUMERIC, 1, 2}, michael@0: {LOW_S, UDATPG_SECOND_FIELD, DT_NUMERIC, 1, 2}, michael@0: {CAP_S, UDATPG_FRACTIONAL_SECOND_FIELD, DT_NUMERIC + DT_DELTA, 1, 1000}, michael@0: {CAP_A, UDATPG_SECOND_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 1000}, michael@0: {LOW_V, UDATPG_ZONE_FIELD, DT_SHORT - 2*DT_DELTA, 1, 0}, michael@0: {LOW_V, UDATPG_ZONE_FIELD, DT_LONG - 2*DT_DELTA, 4, 0}, michael@0: {LOW_Z, UDATPG_ZONE_FIELD, DT_SHORT, 1, 3}, michael@0: {LOW_Z, UDATPG_ZONE_FIELD, DT_LONG, 4, 0}, michael@0: {CAP_Z, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 3}, michael@0: {CAP_Z, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0}, michael@0: {CAP_Z, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 5, 0}, michael@0: {CAP_O, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 0}, michael@0: {CAP_O, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0}, michael@0: {CAP_V, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 0}, michael@0: {CAP_V, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 2, 0}, michael@0: {CAP_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0}, michael@0: {CAP_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0}, michael@0: {CAP_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0}, michael@0: {LOW_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0}, michael@0: {LOW_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0}, michael@0: {LOW_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0}, michael@0: {0, UDATPG_FIELD_COUNT, 0, 0, 0} , // last row of dtTypes[] michael@0: }; michael@0: michael@0: static const char* const CLDR_FIELD_APPEND[] = { michael@0: "Era", "Year", "Quarter", "Month", "Week", "*", "Day-Of-Week", "Day", "*", "*", "*", michael@0: "Hour", "Minute", "Second", "*", "Timezone" michael@0: }; michael@0: michael@0: static const char* const CLDR_FIELD_NAME[] = { michael@0: "era", "year", "quarter", "month", "week", "*", "weekday", "*", "*", "day", "dayperiod", michael@0: "hour", "minute", "second", "*", "zone" michael@0: }; michael@0: michael@0: static const char* const Resource_Fields[] = { michael@0: "day", "dayperiod", "era", "hour", "minute", "month", "second", "week", michael@0: "weekday", "year", "zone", "quarter" }; michael@0: michael@0: // For appendItems michael@0: static const UChar UDATPG_ItemFormat[]= {0x7B, 0x30, 0x7D, 0x20, 0x251C, 0x7B, 0x32, 0x7D, 0x3A, michael@0: 0x20, 0x7B, 0x31, 0x7D, 0x2524, 0}; // {0} \u251C{2}: {1}\u2524 michael@0: michael@0: //static const UChar repeatedPatterns[6]={CAP_G, CAP_E, LOW_Z, LOW_V, CAP_Q, 0}; // "GEzvQ" michael@0: michael@0: static const char DT_DateTimePatternsTag[]="DateTimePatterns"; michael@0: static const char DT_DateTimeCalendarTag[]="calendar"; michael@0: static const char DT_DateTimeGregorianTag[]="gregorian"; michael@0: static const char DT_DateTimeAppendItemsTag[]="appendItems"; michael@0: static const char DT_DateTimeFieldsTag[]="fields"; michael@0: static const char DT_DateTimeAvailableFormatsTag[]="availableFormats"; michael@0: //static const UnicodeString repeatedPattern=UnicodeString(repeatedPatterns); michael@0: michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateTimePatternGenerator) michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTSkeletonEnumeration) michael@0: UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTRedundantEnumeration) michael@0: michael@0: DateTimePatternGenerator* U_EXPORT2 michael@0: DateTimePatternGenerator::createInstance(UErrorCode& status) { michael@0: return createInstance(Locale::getDefault(), status); michael@0: } michael@0: michael@0: DateTimePatternGenerator* U_EXPORT2 michael@0: DateTimePatternGenerator::createInstance(const Locale& locale, UErrorCode& status) { michael@0: DateTimePatternGenerator *result = new DateTimePatternGenerator(locale, status); michael@0: if (result == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: if (U_FAILURE(status)) { michael@0: delete result; michael@0: result = NULL; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: DateTimePatternGenerator* U_EXPORT2 michael@0: DateTimePatternGenerator::createEmptyInstance(UErrorCode& status) { michael@0: DateTimePatternGenerator *result = new DateTimePatternGenerator(status); michael@0: if (result == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: if (U_FAILURE(status)) { michael@0: delete result; michael@0: result = NULL; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: DateTimePatternGenerator::DateTimePatternGenerator(UErrorCode &status) : michael@0: skipMatcher(NULL), michael@0: fAvailableFormatKeyHash(NULL) michael@0: { michael@0: fp = new FormatParser(); michael@0: dtMatcher = new DateTimeMatcher(); michael@0: distanceInfo = new DistanceInfo(); michael@0: patternMap = new PatternMap(); michael@0: if (fp == NULL || dtMatcher == NULL || distanceInfo == NULL || patternMap == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: } michael@0: michael@0: DateTimePatternGenerator::DateTimePatternGenerator(const Locale& locale, UErrorCode &status) : michael@0: skipMatcher(NULL), michael@0: fAvailableFormatKeyHash(NULL) michael@0: { michael@0: fp = new FormatParser(); michael@0: dtMatcher = new DateTimeMatcher(); michael@0: distanceInfo = new DistanceInfo(); michael@0: patternMap = new PatternMap(); michael@0: if (fp == NULL || dtMatcher == NULL || distanceInfo == NULL || patternMap == NULL) { michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: } michael@0: else { michael@0: initData(locale, status); michael@0: } michael@0: } michael@0: michael@0: DateTimePatternGenerator::DateTimePatternGenerator(const DateTimePatternGenerator& other) : michael@0: UObject(), michael@0: skipMatcher(NULL), michael@0: fAvailableFormatKeyHash(NULL) michael@0: { michael@0: fp = new FormatParser(); michael@0: dtMatcher = new DateTimeMatcher(); michael@0: distanceInfo = new DistanceInfo(); michael@0: patternMap = new PatternMap(); michael@0: *this=other; michael@0: } michael@0: michael@0: DateTimePatternGenerator& michael@0: DateTimePatternGenerator::operator=(const DateTimePatternGenerator& other) { michael@0: pLocale = other.pLocale; michael@0: fDefaultHourFormatChar = other.fDefaultHourFormatChar; michael@0: *fp = *(other.fp); michael@0: dtMatcher->copyFrom(other.dtMatcher->skeleton); michael@0: *distanceInfo = *(other.distanceInfo); michael@0: dateTimeFormat = other.dateTimeFormat; michael@0: decimal = other.decimal; michael@0: // NUL-terminate for the C API. michael@0: dateTimeFormat.getTerminatedBuffer(); michael@0: decimal.getTerminatedBuffer(); michael@0: delete skipMatcher; michael@0: if ( other.skipMatcher == NULL ) { michael@0: skipMatcher = NULL; michael@0: } michael@0: else { michael@0: skipMatcher = new DateTimeMatcher(*other.skipMatcher); michael@0: } michael@0: for (int32_t i=0; i< UDATPG_FIELD_COUNT; ++i ) { michael@0: appendItemFormats[i] = other.appendItemFormats[i]; michael@0: appendItemNames[i] = other.appendItemNames[i]; michael@0: // NUL-terminate for the C API. michael@0: appendItemFormats[i].getTerminatedBuffer(); michael@0: appendItemNames[i].getTerminatedBuffer(); michael@0: } michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: patternMap->copyFrom(*other.patternMap, status); michael@0: copyHashtable(other.fAvailableFormatKeyHash, status); michael@0: return *this; michael@0: } michael@0: michael@0: michael@0: UBool michael@0: DateTimePatternGenerator::operator==(const DateTimePatternGenerator& other) const { michael@0: if (this == &other) { michael@0: return TRUE; michael@0: } michael@0: if ((pLocale==other.pLocale) && (patternMap->equals(*other.patternMap)) && michael@0: (dateTimeFormat==other.dateTimeFormat) && (decimal==other.decimal)) { michael@0: for ( int32_t i=0 ; iset(pattern, fp); michael@0: return dtMatcher->getSkeletonPtr()->getSkeleton(); michael@0: } michael@0: michael@0: UnicodeString michael@0: DateTimePatternGenerator::getBaseSkeleton(const UnicodeString& pattern, UErrorCode& /*status*/) { michael@0: dtMatcher->set(pattern, fp); michael@0: return dtMatcher->getSkeletonPtr()->getBaseSkeleton(); michael@0: } michael@0: michael@0: void michael@0: DateTimePatternGenerator::addICUPatterns(const Locale& locale, UErrorCode& status) { michael@0: UnicodeString dfPattern; michael@0: UnicodeString conflictingString; michael@0: DateFormat* df; michael@0: michael@0: if (U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: michael@0: // Load with ICU patterns michael@0: for (int32_t i=DateFormat::kFull; i<=DateFormat::kShort; i++) { michael@0: DateFormat::EStyle style = (DateFormat::EStyle)i; michael@0: df = DateFormat::createDateInstance(style, locale); michael@0: SimpleDateFormat* sdf; michael@0: if (df != NULL && (sdf = dynamic_cast(df)) != NULL) { michael@0: addPattern(sdf->toPattern(dfPattern), FALSE, conflictingString, status); michael@0: } michael@0: // TODO Maybe we should return an error when the date format isn't simple. michael@0: delete df; michael@0: if (U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: michael@0: df = DateFormat::createTimeInstance(style, locale); michael@0: if (df != NULL && (sdf = dynamic_cast(df)) != NULL) { michael@0: addPattern(sdf->toPattern(dfPattern), FALSE, conflictingString, status); michael@0: // HACK for hh:ss michael@0: if ( i==DateFormat::kMedium ) { michael@0: hackPattern = dfPattern; michael@0: } michael@0: } michael@0: // TODO Maybe we should return an error when the date format isn't simple. michael@0: delete df; michael@0: if (U_FAILURE(status)) { michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: DateTimePatternGenerator::hackTimes(const UnicodeString& hackPattern, UErrorCode& status) { michael@0: UnicodeString conflictingString; michael@0: michael@0: fp->set(hackPattern); michael@0: UnicodeString mmss; michael@0: UBool gotMm=FALSE; michael@0: for (int32_t i=0; iitemNumber; ++i) { michael@0: UnicodeString field = fp->items[i]; michael@0: if ( fp->isQuoteLiteral(field) ) { michael@0: if ( gotMm ) { michael@0: UnicodeString quoteLiteral; michael@0: fp->getQuoteLiteral(quoteLiteral, &i); michael@0: mmss += quoteLiteral; michael@0: } michael@0: } michael@0: else { michael@0: if (fp->isPatternSeparator(field) && gotMm) { michael@0: mmss+=field; michael@0: } michael@0: else { michael@0: UChar ch=field.charAt(0); michael@0: if (ch==LOW_M) { michael@0: gotMm=TRUE; michael@0: mmss+=field; michael@0: } michael@0: else { michael@0: if (ch==LOW_S) { michael@0: if (!gotMm) { michael@0: break; michael@0: } michael@0: mmss+= field; michael@0: addPattern(mmss, FALSE, conflictingString, status); michael@0: break; michael@0: } michael@0: else { michael@0: if (gotMm || ch==LOW_Z || ch==CAP_Z || ch==LOW_V || ch==CAP_V) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY) michael@0: michael@0: static const UChar hourFormatChars[] = { CAP_H, LOW_H, CAP_K, LOW_K, 0 }; // HhKk, the hour format characters michael@0: michael@0: void michael@0: DateTimePatternGenerator::addCLDRData(const Locale& locale, UErrorCode& err) { michael@0: UResourceBundle *rb, *calTypeBundle, *calBundle; michael@0: UResourceBundle *patBundle, *fieldBundle, *fBundle; michael@0: UnicodeString rbPattern, value, field; michael@0: UnicodeString conflictingPattern; michael@0: const char *key=NULL; michael@0: int32_t i; michael@0: michael@0: UnicodeString defaultItemFormat(TRUE, UDATPG_ItemFormat, LENGTHOF(UDATPG_ItemFormat)-1); // Read-only alias. michael@0: michael@0: err = U_ZERO_ERROR; michael@0: michael@0: fDefaultHourFormatChar = 0; michael@0: for (i=0; i0) { michael@0: hackTimes(hackPattern, err); michael@0: } michael@0: } michael@0: michael@0: void michael@0: DateTimePatternGenerator::initHashtable(UErrorCode& err) { michael@0: if (fAvailableFormatKeyHash!=NULL) { michael@0: return; michael@0: } michael@0: if ((fAvailableFormatKeyHash = new Hashtable(FALSE, err))==NULL) { michael@0: err=U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: michael@0: void michael@0: DateTimePatternGenerator::setAppendItemFormat(UDateTimePatternField field, const UnicodeString& value) { michael@0: appendItemFormats[field] = value; michael@0: // NUL-terminate for the C API. michael@0: appendItemFormats[field].getTerminatedBuffer(); michael@0: } michael@0: michael@0: const UnicodeString& michael@0: DateTimePatternGenerator::getAppendItemFormat(UDateTimePatternField field) const { michael@0: return appendItemFormats[field]; michael@0: } michael@0: michael@0: void michael@0: DateTimePatternGenerator::setAppendItemName(UDateTimePatternField field, const UnicodeString& value) { michael@0: appendItemNames[field] = value; michael@0: // NUL-terminate for the C API. michael@0: appendItemNames[field].getTerminatedBuffer(); michael@0: } michael@0: michael@0: const UnicodeString& michael@0: DateTimePatternGenerator:: getAppendItemName(UDateTimePatternField field) const { michael@0: return appendItemNames[field]; michael@0: } michael@0: michael@0: void michael@0: DateTimePatternGenerator::getAppendName(UDateTimePatternField field, UnicodeString& value) { michael@0: value = SINGLE_QUOTE; michael@0: value += appendItemNames[field]; michael@0: value += SINGLE_QUOTE; michael@0: } michael@0: michael@0: UnicodeString michael@0: DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UErrorCode& status) { michael@0: return getBestPattern(patternForm, UDATPG_MATCH_NO_OPTIONS, status); michael@0: } michael@0: michael@0: UnicodeString michael@0: DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UDateTimePatternMatchOptions options, UErrorCode& status) { michael@0: const UnicodeString *bestPattern=NULL; michael@0: UnicodeString dtFormat; michael@0: UnicodeString resultPattern; michael@0: int32_t flags = kDTPGNoFlags; michael@0: michael@0: int32_t dateMask=(1<set(patternFormCopy, fp); michael@0: const PtnSkeleton* specifiedSkeleton=NULL; michael@0: bestPattern=getBestRaw(*dtMatcher, -1, distanceInfo, &specifiedSkeleton); michael@0: if ( distanceInfo->missingFieldMask==0 && distanceInfo->extraFieldMask==0 ) { michael@0: resultPattern = adjustFieldTypes(*bestPattern, specifiedSkeleton, flags, options); michael@0: michael@0: return resultPattern; michael@0: } michael@0: int32_t neededFields = dtMatcher->getFieldMask(); michael@0: UnicodeString datePattern=getBestAppending(neededFields & dateMask, flags, options); michael@0: UnicodeString timePattern=getBestAppending(neededFields & timeMask, flags, options); michael@0: if (datePattern.length()==0) { michael@0: if (timePattern.length()==0) { michael@0: resultPattern.remove(); michael@0: } michael@0: else { michael@0: return timePattern; michael@0: } michael@0: } michael@0: if (timePattern.length()==0) { michael@0: return datePattern; michael@0: } michael@0: resultPattern.remove(); michael@0: status = U_ZERO_ERROR; michael@0: dtFormat=getDateTimeFormat(); michael@0: Formattable dateTimeObject[] = { timePattern, datePattern }; michael@0: resultPattern = MessageFormat::format(dtFormat, dateTimeObject, 2, resultPattern, status ); michael@0: return resultPattern; michael@0: } michael@0: michael@0: UnicodeString michael@0: DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern, michael@0: const UnicodeString& skeleton, michael@0: UErrorCode& status) { michael@0: return replaceFieldTypes(pattern, skeleton, UDATPG_MATCH_NO_OPTIONS, status); michael@0: } michael@0: michael@0: UnicodeString michael@0: DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern, michael@0: const UnicodeString& skeleton, michael@0: UDateTimePatternMatchOptions options, michael@0: UErrorCode& /*status*/) { michael@0: dtMatcher->set(skeleton, fp); michael@0: UnicodeString result = adjustFieldTypes(pattern, NULL, kDTPGNoFlags, options); michael@0: return result; michael@0: } michael@0: michael@0: void michael@0: DateTimePatternGenerator::setDecimal(const UnicodeString& newDecimal) { michael@0: this->decimal = newDecimal; michael@0: // NUL-terminate for the C API. michael@0: this->decimal.getTerminatedBuffer(); michael@0: } michael@0: michael@0: const UnicodeString& michael@0: DateTimePatternGenerator::getDecimal() const { michael@0: return decimal; michael@0: } michael@0: michael@0: void michael@0: DateTimePatternGenerator::addCanonicalItems() { michael@0: UnicodeString conflictingPattern; michael@0: UErrorCode status = U_ZERO_ERROR; michael@0: michael@0: for (int32_t i=0; igetType():NULL, status); michael@0: UResourceBundle *dateTimePatterns = calData.getByKey(DT_DateTimePatternsTag, status); michael@0: if (U_FAILURE(status)) return; michael@0: michael@0: if (ures_getSize(dateTimePatterns) <= DateFormat::kDateTime) michael@0: { michael@0: status = U_INVALID_FORMAT_ERROR; michael@0: return; michael@0: } michael@0: resStr = ures_getStringByIndex(dateTimePatterns, (int32_t)DateFormat::kDateTime, &resStrLen, &status); michael@0: setDateTimeFormat(UnicodeString(TRUE, resStr, resStrLen)); michael@0: michael@0: delete fCalendar; michael@0: } michael@0: michael@0: void michael@0: DateTimePatternGenerator::setDecimalSymbols(const Locale& locale, UErrorCode& status) { michael@0: DecimalFormatSymbols dfs = DecimalFormatSymbols(locale, status); michael@0: if(U_SUCCESS(status)) { michael@0: decimal = dfs.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol); michael@0: // NUL-terminate for the C API. michael@0: decimal.getTerminatedBuffer(); michael@0: } michael@0: } michael@0: michael@0: UDateTimePatternConflict michael@0: DateTimePatternGenerator::addPattern( michael@0: const UnicodeString& pattern, michael@0: UBool override, michael@0: UnicodeString &conflictingPattern, michael@0: UErrorCode& status) michael@0: { michael@0: return addPatternWithSkeleton(pattern, NULL, override, conflictingPattern, status); michael@0: } michael@0: michael@0: // For DateTimePatternGenerator::addPatternWithSkeleton - michael@0: // If skeletonToUse is specified, then an availableFormats entry is being added. In this case: michael@0: // 1. We pass that skeleton to matcher.set instead of having it derive a skeleton from the pattern. michael@0: // 2. If the new entry's skeleton or basePattern does match an existing entry but that entry also had a skeleton specified michael@0: // (i.e. it was also from availableFormats), then the new entry does not override it regardless of the value of the override michael@0: // parameter. This prevents later availableFormats entries from a parent locale overriding earlier ones from the actual michael@0: // specified locale. However, availableFormats entries *should* override entries with matching skeleton whose skeleton was michael@0: // derived (i.e. entries derived from the standard date/time patters for the specified locale). michael@0: // 3. When adding the pattern (patternMap->add), we set a new boolean to indicate that the added entry had a michael@0: // specified skeleton (which sets a new field in the PtnElem in the PatternMap). michael@0: UDateTimePatternConflict michael@0: DateTimePatternGenerator::addPatternWithSkeleton( michael@0: const UnicodeString& pattern, michael@0: const UnicodeString* skeletonToUse, michael@0: UBool override, michael@0: UnicodeString& conflictingPattern, michael@0: UErrorCode& status) michael@0: { michael@0: michael@0: UnicodeString basePattern; michael@0: PtnSkeleton skeleton; michael@0: UDateTimePatternConflict conflictingStatus = UDATPG_NO_CONFLICT; michael@0: michael@0: DateTimeMatcher matcher; michael@0: if ( skeletonToUse == NULL ) { michael@0: matcher.set(pattern, fp, skeleton); michael@0: matcher.getBasePattern(basePattern); michael@0: } else { michael@0: matcher.set(*skeletonToUse, fp, skeleton); // no longer trims skeleton fields to max len 3, per #7930 michael@0: matcher.getBasePattern(basePattern); // or perhaps instead: basePattern = *skeletonToUse; michael@0: } michael@0: // We only care about base conflicts - and replacing the pattern associated with a base - if: michael@0: // 1. the conflicting previous base pattern did *not* have an explicit skeleton; in that case the previous michael@0: // base + pattern combination was derived from either (a) a canonical item, (b) a standard format, or michael@0: // (c) a pattern specified programmatically with a previous call to addPattern (which would only happen michael@0: // if we are getting here from a subsequent call to addPattern). michael@0: // 2. a skeleton is specified for the current pattern, but override=false; in that case we are checking michael@0: // availableFormats items from root, which should not override any previous entry with the same base. michael@0: UBool entryHadSpecifiedSkeleton; michael@0: const UnicodeString *duplicatePattern = patternMap->getPatternFromBasePattern(basePattern, entryHadSpecifiedSkeleton); michael@0: if (duplicatePattern != NULL && (!entryHadSpecifiedSkeleton || (skeletonToUse != NULL && !override))) { michael@0: conflictingStatus = UDATPG_BASE_CONFLICT; michael@0: conflictingPattern = *duplicatePattern; michael@0: if (!override) { michael@0: return conflictingStatus; michael@0: } michael@0: } michael@0: // The only time we get here with override=true and skeletonToUse!=null is when adding availableFormats michael@0: // items from CLDR data. In that case, we don't want an item from a parent locale to replace an item with michael@0: // same skeleton from the specified locale, so skip the current item if skeletonWasSpecified is true for michael@0: // the previously-specified conflicting item. michael@0: const PtnSkeleton* entrySpecifiedSkeleton = NULL; michael@0: duplicatePattern = patternMap->getPatternFromSkeleton(skeleton, &entrySpecifiedSkeleton); michael@0: if (duplicatePattern != NULL ) { michael@0: conflictingStatus = UDATPG_CONFLICT; michael@0: conflictingPattern = *duplicatePattern; michael@0: if (!override || (skeletonToUse != NULL && entrySpecifiedSkeleton != NULL)) { michael@0: return conflictingStatus; michael@0: } michael@0: } michael@0: patternMap->add(basePattern, skeleton, pattern, skeletonToUse != NULL, status); michael@0: if(U_FAILURE(status)) { michael@0: return conflictingStatus; michael@0: } michael@0: michael@0: return UDATPG_NO_CONFLICT; michael@0: } michael@0: michael@0: michael@0: UDateTimePatternField michael@0: DateTimePatternGenerator::getAppendFormatNumber(const char* field) const { michael@0: for (int32_t i=0; igetPatternFromSkeleton(*trial.getSkeletonPtr(), &specifiedSkeleton); michael@0: missingFields->setTo(tempInfo); michael@0: if (distance==0) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // If the best raw match had a specified skeleton and that skeleton was requested by the caller, michael@0: // then return it too. This generally happens when the caller needs to pass that skeleton michael@0: // through to adjustFieldTypes so the latter can do a better job. michael@0: if (bestPattern && specifiedSkeletonPtr) { michael@0: *specifiedSkeletonPtr = specifiedSkeleton; michael@0: } michael@0: return bestPattern; michael@0: } michael@0: michael@0: UnicodeString michael@0: DateTimePatternGenerator::adjustFieldTypes(const UnicodeString& pattern, michael@0: const PtnSkeleton* specifiedSkeleton, michael@0: int32_t flags, michael@0: UDateTimePatternMatchOptions options) { michael@0: UnicodeString newPattern; michael@0: fp->set(pattern); michael@0: for (int32_t i=0; i < fp->itemNumber; i++) { michael@0: UnicodeString field = fp->items[i]; michael@0: if ( fp->isQuoteLiteral(field) ) { michael@0: michael@0: UnicodeString quoteLiteral; michael@0: fp->getQuoteLiteral(quoteLiteral, &i); michael@0: newPattern += quoteLiteral; michael@0: } michael@0: else { michael@0: if (fp->isPatternSeparator(field)) { michael@0: newPattern+=field; michael@0: continue; michael@0: } michael@0: int32_t canonicalIndex = fp->getCanonicalIndex(field); michael@0: if (canonicalIndex < 0) { michael@0: newPattern+=field; michael@0: continue; // don't adjust michael@0: } michael@0: const dtTypeElem *row = &dtTypes[canonicalIndex]; michael@0: int32_t typeValue = row->field; michael@0: if ((flags & kDTPGFixFractionalSeconds) != 0 && typeValue == UDATPG_SECOND_FIELD) { michael@0: UnicodeString newField=dtMatcher->skeleton.original[UDATPG_FRACTIONAL_SECOND_FIELD]; michael@0: field = field + decimal + newField; michael@0: } else if (dtMatcher->skeleton.type[typeValue]!=0) { michael@0: // Here: michael@0: // - "reqField" is the field from the originally requested skeleton, with length michael@0: // "reqFieldLen". michael@0: // - "field" is the field from the found pattern. michael@0: // michael@0: // The adjusted field should consist of characters from the originally requested michael@0: // skeleton, except in the case of UDATPG_HOUR_FIELD or UDATPG_MONTH_FIELD or michael@0: // UDATPG_WEEKDAY_FIELD or UDATPG_YEAR_FIELD, in which case it should consist michael@0: // of characters from the found pattern. michael@0: // michael@0: // The length of the adjusted field (adjFieldLen) should match that in the originally michael@0: // requested skeleton, except that in the following cases the length of the adjusted field michael@0: // should match that in the found pattern (i.e. the length of this pattern field should michael@0: // not be adjusted): michael@0: // 1. typeValue is UDATPG_HOUR_FIELD/MINUTE/SECOND and the corresponding bit in options is michael@0: // not set (ticket #7180). Note, we may want to implement a similar change for other michael@0: // numeric fields (MM, dd, etc.) so the default behavior is to get locale preference for michael@0: // field length, but options bits can be used to override this. michael@0: // 2. There is a specified skeleton for the found pattern and one of the following is true: michael@0: // a) The length of the field in the skeleton (skelFieldLen) is equal to reqFieldLen. michael@0: // b) The pattern field is numeric and the skeleton field is not, or vice versa. michael@0: michael@0: UnicodeString reqField = dtMatcher->skeleton.original[typeValue]; michael@0: int32_t reqFieldLen = reqField.length(); michael@0: if (reqField.charAt(0) == CAP_E && reqFieldLen < 3) michael@0: reqFieldLen = 3; // 1-3 for E are equivalent to 3 for c,e michael@0: int32_t adjFieldLen = reqFieldLen; michael@0: if ( (typeValue==UDATPG_HOUR_FIELD && (options & UDATPG_MATCH_HOUR_FIELD_LENGTH)==0) || michael@0: (typeValue==UDATPG_MINUTE_FIELD && (options & UDATPG_MATCH_MINUTE_FIELD_LENGTH)==0) || michael@0: (typeValue==UDATPG_SECOND_FIELD && (options & UDATPG_MATCH_SECOND_FIELD_LENGTH)==0) ) { michael@0: adjFieldLen = field.length(); michael@0: } else if (specifiedSkeleton) { michael@0: UnicodeString skelField = specifiedSkeleton->original[typeValue]; michael@0: int32_t skelFieldLen = skelField.length(); michael@0: UBool patFieldIsNumeric = (row->type > 0); michael@0: UBool skelFieldIsNumeric = (specifiedSkeleton->type[typeValue] > 0); michael@0: if (skelFieldLen == reqFieldLen || (patFieldIsNumeric && !skelFieldIsNumeric) || (skelFieldIsNumeric && !patFieldIsNumeric)) { michael@0: // don't adjust the field length in the found pattern michael@0: adjFieldLen = field.length(); michael@0: } michael@0: } michael@0: UChar c = (typeValue!= UDATPG_HOUR_FIELD && typeValue!= UDATPG_MONTH_FIELD && michael@0: typeValue!= UDATPG_WEEKDAY_FIELD && (typeValue!= UDATPG_YEAR_FIELD || reqField.charAt(0)==CAP_Y))? michael@0: reqField.charAt(0): field.charAt(0); michael@0: if (typeValue == UDATPG_HOUR_FIELD && (flags & kDTPGSkeletonUsesCapJ) != 0) { michael@0: c = fDefaultHourFormatChar; michael@0: } michael@0: field.remove(); michael@0: for (int32_t i=adjFieldLen; i>0; --i) { michael@0: field+=c; michael@0: } michael@0: } michael@0: newPattern+=field; michael@0: } michael@0: } michael@0: return newPattern; michael@0: } michael@0: michael@0: UnicodeString michael@0: DateTimePatternGenerator::getBestAppending(int32_t missingFields, int32_t flags, UDateTimePatternMatchOptions options) { michael@0: UnicodeString resultPattern, tempPattern; michael@0: UErrorCode err=U_ZERO_ERROR; michael@0: int32_t lastMissingFieldMask=0; michael@0: if (missingFields!=0) { michael@0: resultPattern=UnicodeString(); michael@0: const PtnSkeleton* specifiedSkeleton=NULL; michael@0: tempPattern = *getBestRaw(*dtMatcher, missingFields, distanceInfo, &specifiedSkeleton); michael@0: resultPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, flags, options); michael@0: if ( distanceInfo->missingFieldMask==0 ) { michael@0: return resultPattern; michael@0: } michael@0: while (distanceInfo->missingFieldMask!=0) { // precondition: EVERY single field must work! michael@0: if ( lastMissingFieldMask == distanceInfo->missingFieldMask ) { michael@0: break; // cannot find the proper missing field michael@0: } michael@0: if (((distanceInfo->missingFieldMask & UDATPG_SECOND_AND_FRACTIONAL_MASK)==UDATPG_FRACTIONAL_MASK) && michael@0: ((missingFields & UDATPG_SECOND_AND_FRACTIONAL_MASK) == UDATPG_SECOND_AND_FRACTIONAL_MASK)) { michael@0: resultPattern = adjustFieldTypes(resultPattern, specifiedSkeleton, flags | kDTPGFixFractionalSeconds, options); michael@0: distanceInfo->missingFieldMask &= ~UDATPG_FRACTIONAL_MASK; michael@0: continue; michael@0: } michael@0: int32_t startingMask = distanceInfo->missingFieldMask; michael@0: tempPattern = *getBestRaw(*dtMatcher, distanceInfo->missingFieldMask, distanceInfo, &specifiedSkeleton); michael@0: tempPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, flags, options); michael@0: int32_t foundMask=startingMask& ~distanceInfo->missingFieldMask; michael@0: int32_t topField=getTopBitNumber(foundMask); michael@0: UnicodeString appendName; michael@0: getAppendName((UDateTimePatternField)topField, appendName); michael@0: const Formattable formatPattern[] = { michael@0: resultPattern, michael@0: tempPattern, michael@0: appendName michael@0: }; michael@0: UnicodeString emptyStr; michael@0: resultPattern = MessageFormat::format(appendItemFormats[topField], formatPattern, 3, emptyStr, err); michael@0: lastMissingFieldMask = distanceInfo->missingFieldMask; michael@0: } michael@0: } michael@0: return resultPattern; michael@0: } michael@0: michael@0: int32_t michael@0: DateTimePatternGenerator::getTopBitNumber(int32_t foundMask) { michael@0: if ( foundMask==0 ) { michael@0: return 0; michael@0: } michael@0: int32_t i=0; michael@0: while (foundMask!=0) { michael@0: foundMask >>=1; michael@0: ++i; michael@0: } michael@0: if (i-1 >UDATPG_ZONE_FIELD) { michael@0: return UDATPG_ZONE_FIELD; michael@0: } michael@0: else michael@0: return i-1; michael@0: } michael@0: michael@0: void michael@0: DateTimePatternGenerator::setAvailableFormat(const UnicodeString &key, UErrorCode& err) michael@0: { michael@0: fAvailableFormatKeyHash->puti(key, 1, err); michael@0: } michael@0: michael@0: UBool michael@0: DateTimePatternGenerator::isAvailableFormatSet(const UnicodeString &key) const { michael@0: return (UBool)(fAvailableFormatKeyHash->geti(key) == 1); michael@0: } michael@0: michael@0: void michael@0: DateTimePatternGenerator::copyHashtable(Hashtable *other, UErrorCode &status) { michael@0: michael@0: if (other == NULL) { michael@0: return; michael@0: } michael@0: if (fAvailableFormatKeyHash != NULL) { michael@0: delete fAvailableFormatKeyHash; michael@0: fAvailableFormatKeyHash = NULL; michael@0: } michael@0: initHashtable(status); michael@0: if(U_FAILURE(status)){ michael@0: return; michael@0: } michael@0: int32_t pos = -1; michael@0: const UHashElement* elem = NULL; michael@0: // walk through the hash table and create a deep clone michael@0: while((elem = other->nextElement(pos))!= NULL){ michael@0: const UHashTok otherKeyTok = elem->key; michael@0: UnicodeString* otherKey = (UnicodeString*)otherKeyTok.pointer; michael@0: fAvailableFormatKeyHash->puti(*otherKey, 1, status); michael@0: if(U_FAILURE(status)){ michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: StringEnumeration* michael@0: DateTimePatternGenerator::getSkeletons(UErrorCode& status) const { michael@0: StringEnumeration* skeletonEnumerator = new DTSkeletonEnumeration(*patternMap, DT_SKELETON, status); michael@0: return skeletonEnumerator; michael@0: } michael@0: michael@0: const UnicodeString& michael@0: DateTimePatternGenerator::getPatternForSkeleton(const UnicodeString& skeleton) const { michael@0: PtnElem *curElem; michael@0: michael@0: if (skeleton.length() ==0) { michael@0: return emptyString; michael@0: } michael@0: curElem = patternMap->getHeader(skeleton.charAt(0)); michael@0: while ( curElem != NULL ) { michael@0: if ( curElem->skeleton->getSkeleton()==skeleton ) { michael@0: return curElem->pattern; michael@0: } michael@0: curElem=curElem->next; michael@0: } michael@0: return emptyString; michael@0: } michael@0: michael@0: StringEnumeration* michael@0: DateTimePatternGenerator::getBaseSkeletons(UErrorCode& status) const { michael@0: StringEnumeration* baseSkeletonEnumerator = new DTSkeletonEnumeration(*patternMap, DT_BASESKELETON, status); michael@0: return baseSkeletonEnumerator; michael@0: } michael@0: michael@0: StringEnumeration* michael@0: DateTimePatternGenerator::getRedundants(UErrorCode& status) { michael@0: StringEnumeration* output = new DTRedundantEnumeration(); michael@0: const UnicodeString *pattern; michael@0: PatternMapIterator it; michael@0: for (it.set(*patternMap); it.hasNext(); ) { michael@0: DateTimeMatcher current = it.next(); michael@0: pattern = patternMap->getPatternFromSkeleton(*(it.getSkeleton())); michael@0: if ( isCanonicalItem(*pattern) ) { michael@0: continue; michael@0: } michael@0: if ( skipMatcher == NULL ) { michael@0: skipMatcher = new DateTimeMatcher(current); michael@0: } michael@0: else { michael@0: *skipMatcher = current; michael@0: } michael@0: UnicodeString trial = getBestPattern(current.getPattern(), status); michael@0: if (trial == *pattern) { michael@0: ((DTRedundantEnumeration *)output)->add(*pattern, status); michael@0: } michael@0: if (current.equals(skipMatcher)) { michael@0: continue; michael@0: } michael@0: } michael@0: return output; michael@0: } michael@0: michael@0: UBool michael@0: DateTimePatternGenerator::isCanonicalItem(const UnicodeString& item) const { michael@0: if ( item.length() != 1 ) { michael@0: return FALSE; michael@0: } michael@0: for (int32_t i=0; iisDupAllowed = other.isDupAllowed; michael@0: for (int32_t bootIndex=0; bootIndexbasePattern, otherElem->pattern))==NULL) { michael@0: // out of memory michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: if ( this->boot[bootIndex]== NULL ) { michael@0: this->boot[bootIndex] = curElem; michael@0: } michael@0: if ((curElem->skeleton=new PtnSkeleton(*(otherElem->skeleton))) == NULL ) { michael@0: // out of memory michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: michael@0: if (prevElem!=NULL) { michael@0: prevElem->next=curElem; michael@0: } michael@0: curElem->next=NULL; michael@0: prevElem = curElem; michael@0: otherElem = otherElem->next; michael@0: } michael@0: michael@0: } michael@0: } michael@0: michael@0: PtnElem* michael@0: PatternMap::getHeader(UChar baseChar) { michael@0: PtnElem* curElem; michael@0: michael@0: if ( (baseChar >= CAP_A) && (baseChar <= CAP_Z) ) { michael@0: curElem = boot[baseChar-CAP_A]; michael@0: } michael@0: else { michael@0: if ( (baseChar >=LOW_A) && (baseChar <= LOW_Z) ) { michael@0: curElem = boot[26+baseChar-LOW_A]; michael@0: } michael@0: else { michael@0: return NULL; michael@0: } michael@0: } michael@0: return curElem; michael@0: } michael@0: michael@0: PatternMap::~PatternMap() { michael@0: for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) { michael@0: if (boot[i]!=NULL ) { michael@0: delete boot[i]; michael@0: boot[i]=NULL; michael@0: } michael@0: } michael@0: } // PatternMap destructor michael@0: michael@0: void michael@0: PatternMap::add(const UnicodeString& basePattern, michael@0: const PtnSkeleton& skeleton, michael@0: const UnicodeString& value,// mapped pattern value michael@0: UBool skeletonWasSpecified, michael@0: UErrorCode &status) { michael@0: UChar baseChar = basePattern.charAt(0); michael@0: PtnElem *curElem, *baseElem; michael@0: status = U_ZERO_ERROR; michael@0: michael@0: // the baseChar must be A-Z or a-z michael@0: if ((baseChar >= CAP_A) && (baseChar <= CAP_Z)) { michael@0: baseElem = boot[baseChar-CAP_A]; michael@0: } michael@0: else { michael@0: if ((baseChar >=LOW_A) && (baseChar <= LOW_Z)) { michael@0: baseElem = boot[26+baseChar-LOW_A]; michael@0: } michael@0: else { michael@0: status = U_ILLEGAL_CHARACTER; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (baseElem == NULL) { michael@0: if ((curElem = new PtnElem(basePattern, value)) == NULL ) { michael@0: // out of memory michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: if (baseChar >= LOW_A) { michael@0: boot[26 + (baseChar-LOW_A)] = curElem; michael@0: } michael@0: else { michael@0: boot[baseChar-CAP_A] = curElem; michael@0: } michael@0: curElem->skeleton = new PtnSkeleton(skeleton); michael@0: curElem->skeletonWasSpecified = skeletonWasSpecified; michael@0: } michael@0: if ( baseElem != NULL ) { michael@0: curElem = getDuplicateElem(basePattern, skeleton, baseElem); michael@0: michael@0: if (curElem == NULL) { michael@0: // add new element to the list. michael@0: curElem = baseElem; michael@0: while( curElem -> next != NULL ) michael@0: { michael@0: curElem = curElem->next; michael@0: } michael@0: if ((curElem->next = new PtnElem(basePattern, value)) == NULL ) { michael@0: // out of memory michael@0: status = U_MEMORY_ALLOCATION_ERROR; michael@0: return; michael@0: } michael@0: curElem=curElem->next; michael@0: curElem->skeleton = new PtnSkeleton(skeleton); michael@0: curElem->skeletonWasSpecified = skeletonWasSpecified; michael@0: } michael@0: else { michael@0: // Pattern exists in the list already. michael@0: if ( !isDupAllowed ) { michael@0: return; michael@0: } michael@0: // Overwrite the value. michael@0: curElem->pattern = value; michael@0: // It was a bug that we were not doing the following previously, michael@0: // though that bug hid other problems by making things partly work. michael@0: curElem->skeletonWasSpecified = skeletonWasSpecified; michael@0: } michael@0: } michael@0: } // PatternMap::add michael@0: michael@0: // Find the pattern from the given basePattern string. michael@0: const UnicodeString * michael@0: PatternMap::getPatternFromBasePattern(UnicodeString& basePattern, UBool& skeletonWasSpecified) { // key to search for michael@0: PtnElem *curElem; michael@0: michael@0: if ((curElem=getHeader(basePattern.charAt(0)))==NULL) { michael@0: return NULL; // no match michael@0: } michael@0: michael@0: do { michael@0: if ( basePattern.compare(curElem->basePattern)==0 ) { michael@0: skeletonWasSpecified = curElem->skeletonWasSpecified; michael@0: return &(curElem->pattern); michael@0: } michael@0: curElem=curElem->next; michael@0: }while (curElem != NULL); michael@0: michael@0: return NULL; michael@0: } // PatternMap::getFromBasePattern michael@0: michael@0: michael@0: // Find the pattern from the given skeleton. michael@0: // At least when this is called from getBestRaw & addPattern (in which case specifiedSkeletonPtr is non-NULL), michael@0: // the comparison should be based on skeleton.original (which is unique and tied to the distance measurement in bestRaw) michael@0: // and not skeleton.baseOriginal (which is not unique); otherwise we may pick a different skeleton than the one with the michael@0: // optimum distance value in getBestRaw. When this is called from public getRedundants (specifiedSkeletonPtr is NULL), michael@0: // for now it will continue to compare based on baseOriginal so as not to change the behavior unnecessarily. michael@0: const UnicodeString * michael@0: PatternMap::getPatternFromSkeleton(PtnSkeleton& skeleton, const PtnSkeleton** specifiedSkeletonPtr) { // key to search for michael@0: PtnElem *curElem; michael@0: michael@0: if (specifiedSkeletonPtr) { michael@0: *specifiedSkeletonPtr = NULL; michael@0: } michael@0: michael@0: // find boot entry michael@0: UChar baseChar='\0'; michael@0: for (int32_t i=0; iskeleton->original[i].compare(skeleton.original[i]) != 0 ) michael@0: { michael@0: break; michael@0: } michael@0: } michael@0: } else { // called from DateTimePatternGenerator::getRedundants, use baseOriginal michael@0: for (i=0; iskeleton->baseOriginal[i].compare(skeleton.baseOriginal[i]) != 0 ) michael@0: { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: if (i == UDATPG_FIELD_COUNT) { michael@0: if (specifiedSkeletonPtr && curElem->skeletonWasSpecified) { michael@0: *specifiedSkeletonPtr = curElem->skeleton; michael@0: } michael@0: return &(curElem->pattern); michael@0: } michael@0: curElem=curElem->next; michael@0: }while (curElem != NULL); michael@0: michael@0: return NULL; michael@0: } michael@0: michael@0: UBool michael@0: PatternMap::equals(const PatternMap& other) { michael@0: if ( this==&other ) { michael@0: return TRUE; michael@0: } michael@0: for (int32_t bootIndex=0; bootIndexbasePattern != otherElem->basePattern) || michael@0: (myElem->pattern != otherElem->pattern) ) { michael@0: return FALSE; michael@0: } michael@0: if ((myElem->skeleton!=otherElem->skeleton)&& michael@0: !myElem->skeleton->equals(*(otherElem->skeleton))) { michael@0: return FALSE; michael@0: } michael@0: myElem = myElem->next; michael@0: otherElem=otherElem->next; michael@0: } michael@0: } michael@0: return TRUE; michael@0: } michael@0: michael@0: // find any key existing in the mapping table already. michael@0: // return TRUE if there is an existing key, otherwise return FALSE. michael@0: PtnElem* michael@0: PatternMap::getDuplicateElem( michael@0: const UnicodeString &basePattern, michael@0: const PtnSkeleton &skeleton, michael@0: PtnElem *baseElem) { michael@0: PtnElem *curElem; michael@0: michael@0: if ( baseElem == (PtnElem *)NULL ) { michael@0: return (PtnElem*)NULL; michael@0: } michael@0: else { michael@0: curElem = baseElem; michael@0: } michael@0: do { michael@0: if ( basePattern.compare(curElem->basePattern)==0 ) { michael@0: UBool isEqual=TRUE; michael@0: for (int32_t i=0; iskeleton->type[i] != skeleton.type[i] ) { michael@0: isEqual=FALSE; michael@0: break; michael@0: } michael@0: } michael@0: if (isEqual) { michael@0: return curElem; michael@0: } michael@0: } michael@0: curElem = curElem->next; michael@0: } while( curElem != (PtnElem *)NULL ); michael@0: michael@0: // end of the list michael@0: return (PtnElem*)NULL; michael@0: michael@0: } // PatternMap::getDuplicateElem michael@0: michael@0: DateTimeMatcher::DateTimeMatcher(void) { michael@0: } michael@0: michael@0: DateTimeMatcher::~DateTimeMatcher() {} michael@0: michael@0: DateTimeMatcher::DateTimeMatcher(const DateTimeMatcher& other) { michael@0: copyFrom(other.skeleton); michael@0: } michael@0: michael@0: michael@0: void michael@0: DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp) { michael@0: PtnSkeleton localSkeleton; michael@0: return set(pattern, fp, localSkeleton); michael@0: } michael@0: michael@0: void michael@0: DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp, PtnSkeleton& skeletonResult) { michael@0: int32_t i; michael@0: for (i=0; iset(pattern); michael@0: for (i=0; i < fp->itemNumber; i++) { michael@0: UnicodeString field = fp->items[i]; michael@0: if ( field.charAt(0) == LOW_A ) { michael@0: continue; // skip 'a' michael@0: } michael@0: michael@0: if ( fp->isQuoteLiteral(field) ) { michael@0: UnicodeString quoteLiteral; michael@0: fp->getQuoteLiteral(quoteLiteral, &i); michael@0: continue; michael@0: } michael@0: int32_t canonicalIndex = fp->getCanonicalIndex(field); michael@0: if (canonicalIndex < 0 ) { michael@0: continue; michael@0: } michael@0: const dtTypeElem *row = &dtTypes[canonicalIndex]; michael@0: int32_t typeValue = row->field; michael@0: skeletonResult.original[typeValue]=field; michael@0: UChar repeatChar = row->patternChar; michael@0: int32_t repeatCount = row->minLen; // #7930 removes cap at 3 michael@0: while (repeatCount-- > 0) { michael@0: skeletonResult.baseOriginal[typeValue] += repeatChar; michael@0: } michael@0: int16_t subTypeValue = row->type; michael@0: if ( row->type > 0) { michael@0: subTypeValue += field.length(); michael@0: } michael@0: skeletonResult.type[typeValue] = subTypeValue; michael@0: } michael@0: copyFrom(skeletonResult); michael@0: } michael@0: michael@0: void michael@0: DateTimeMatcher::getBasePattern(UnicodeString &result ) { michael@0: result.remove(); // Reset the result first. michael@0: for (int32_t i=0; iskeleton.type[i]=newSkeleton.type[i]; michael@0: this->skeleton.original[i]=newSkeleton.original[i]; michael@0: this->skeleton.baseOriginal[i]=newSkeleton.baseOriginal[i]; michael@0: } michael@0: } michael@0: michael@0: void michael@0: DateTimeMatcher::copyFrom() { michael@0: // same as clear michael@0: for (int32_t i=0; iskeleton.type[i]=0; michael@0: this->skeleton.original[i].remove(); michael@0: this->skeleton.baseOriginal[i].remove(); michael@0: } michael@0: } michael@0: michael@0: UBool michael@0: DateTimeMatcher::equals(const DateTimeMatcher* other) const { michael@0: if (other==NULL) { michael@0: return FALSE; michael@0: } michael@0: for (int32_t i=0; iskeleton.original[i]!=other->skeleton.original[i] ) { michael@0: return FALSE; michael@0: } michael@0: } michael@0: return TRUE; michael@0: } michael@0: michael@0: int32_t michael@0: DateTimeMatcher::getFieldMask() { michael@0: int32_t result=0; michael@0: michael@0: for (int32_t i=0; i= pattern.length()) { michael@0: return DONE; michael@0: } michael@0: // check the current char is between A-Z or a-z michael@0: do { michael@0: UChar c=pattern.charAt(curLoc); michael@0: if ( (c>=CAP_A && c<=CAP_Z) || (c>=LOW_A && c<=LOW_Z) ) { michael@0: curLoc++; michael@0: } michael@0: else { michael@0: startPos = curLoc; michael@0: *len=1; michael@0: return ADD_TOKEN; michael@0: } michael@0: michael@0: if ( pattern.charAt(curLoc)!= pattern.charAt(startPos) ) { michael@0: break; // not the same token michael@0: } michael@0: } while(curLoc <= pattern.length()); michael@0: *len = curLoc-startPos; michael@0: return ADD_TOKEN; michael@0: } michael@0: michael@0: void michael@0: FormatParser::set(const UnicodeString& pattern) { michael@0: int32_t startPos=0; michael@0: TokenStatus result=START; michael@0: int32_t len=0; michael@0: itemNumber =0; michael@0: michael@0: do { michael@0: result = setTokens( pattern, startPos, &len ); michael@0: if ( result == ADD_TOKEN ) michael@0: { michael@0: items[itemNumber++] = UnicodeString(pattern, startPos, len ); michael@0: startPos += len; michael@0: } michael@0: else { michael@0: break; michael@0: } michael@0: } while (result==ADD_TOKEN && itemNumber < MAX_DT_TOKEN); michael@0: } michael@0: michael@0: int32_t michael@0: FormatParser::getCanonicalIndex(const UnicodeString& s, UBool strict) { michael@0: int32_t len = s.length(); michael@0: if (len == 0) { michael@0: return -1; michael@0: } michael@0: UChar ch = s.charAt(0); michael@0: michael@0: // Verify that all are the same character. michael@0: for (int32_t l = 1; l < len; l++) { michael@0: if (ch != s.charAt(l)) { michael@0: return -1; michael@0: } michael@0: } michael@0: int32_t i = 0; michael@0: int32_t bestRow = -1; michael@0: while (dtTypes[i].patternChar != '\0') { michael@0: if ( dtTypes[i].patternChar != ch ) { michael@0: ++i; michael@0: continue; michael@0: } michael@0: bestRow = i; michael@0: if (dtTypes[i].patternChar != dtTypes[i+1].patternChar) { michael@0: return i; michael@0: } michael@0: if (dtTypes[i+1].minLen <= len) { michael@0: ++i; michael@0: continue; michael@0: } michael@0: return i; michael@0: } michael@0: return strict ? -1 : bestRow; michael@0: } michael@0: michael@0: UBool michael@0: FormatParser::isQuoteLiteral(const UnicodeString& s) const { michael@0: return (UBool)(s.charAt(0)==SINGLE_QUOTE); michael@0: } michael@0: michael@0: // This function aussumes the current itemIndex points to the quote literal. michael@0: // Please call isQuoteLiteral prior to this function. michael@0: void michael@0: FormatParser::getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex) { michael@0: int32_t i=*itemIndex; michael@0: michael@0: quote.remove(); michael@0: if (items[i].charAt(0)==SINGLE_QUOTE) { michael@0: quote += items[i]; michael@0: ++i; michael@0: } michael@0: while ( i < itemNumber ) { michael@0: if ( items[i].charAt(0)==SINGLE_QUOTE ) { michael@0: if ( (i+1patternMap=&newPatternMap; michael@0: } michael@0: michael@0: PtnSkeleton* michael@0: PatternMapIterator::getSkeleton() { michael@0: if ( nodePtr == NULL ) { michael@0: return NULL; michael@0: } michael@0: else { michael@0: return nodePtr->skeleton; michael@0: } michael@0: } michael@0: michael@0: UBool michael@0: PatternMapIterator::hasNext() { michael@0: int32_t headIndex=bootIndex; michael@0: PtnElem *curPtr=nodePtr; michael@0: michael@0: if (patternMap==NULL) { michael@0: return FALSE; michael@0: } michael@0: while ( headIndex < MAX_PATTERN_ENTRIES ) { michael@0: if ( curPtr != NULL ) { michael@0: if ( curPtr->next != NULL ) { michael@0: return TRUE; michael@0: } michael@0: else { michael@0: headIndex++; michael@0: curPtr=NULL; michael@0: continue; michael@0: } michael@0: } michael@0: else { michael@0: if ( patternMap->boot[headIndex] != NULL ) { michael@0: return TRUE; michael@0: } michael@0: else { michael@0: headIndex++; michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: } michael@0: return FALSE; michael@0: } michael@0: michael@0: DateTimeMatcher& michael@0: PatternMapIterator::next() { michael@0: while ( bootIndex < MAX_PATTERN_ENTRIES ) { michael@0: if ( nodePtr != NULL ) { michael@0: if ( nodePtr->next != NULL ) { michael@0: nodePtr = nodePtr->next; michael@0: break; michael@0: } michael@0: else { michael@0: bootIndex++; michael@0: nodePtr=NULL; michael@0: continue; michael@0: } michael@0: } michael@0: else { michael@0: if ( patternMap->boot[bootIndex] != NULL ) { michael@0: nodePtr = patternMap->boot[bootIndex]; michael@0: break; michael@0: } michael@0: else { michael@0: bootIndex++; michael@0: continue; michael@0: } michael@0: } michael@0: } michael@0: if (nodePtr!=NULL) { michael@0: matcher->copyFrom(*nodePtr->skeleton); michael@0: } michael@0: else { michael@0: matcher->copyFrom(); michael@0: } michael@0: return *matcher; michael@0: } michael@0: michael@0: PtnSkeleton::PtnSkeleton() { michael@0: } michael@0: michael@0: michael@0: PtnSkeleton::PtnSkeleton(const PtnSkeleton& other) { michael@0: for (int32_t i=0; itype[i]=other.type[i]; michael@0: this->original[i]=other.original[i]; michael@0: this->baseOriginal[i]=other.baseOriginal[i]; michael@0: } michael@0: } michael@0: michael@0: UBool michael@0: PtnSkeleton::equals(const PtnSkeleton& other) { michael@0: for (int32_t i=0; ibasePattern; michael@0: break; michael@0: case DT_PATTERN: michael@0: s=curElem->pattern; michael@0: break; michael@0: case DT_SKELETON: michael@0: curSkeleton=curElem->skeleton; michael@0: s=curSkeleton->getSkeleton(); michael@0: break; michael@0: } michael@0: if ( !isCanonicalItem(s) ) { michael@0: fSkeletons->addElement(new UnicodeString(s), status); michael@0: if (U_FAILURE(status)) { michael@0: delete fSkeletons; michael@0: fSkeletons = NULL; michael@0: return; michael@0: } michael@0: } michael@0: curElem = curElem->next; michael@0: } michael@0: } michael@0: if ((bootIndex==MAX_PATTERN_ENTRIES) && (curElem!=NULL) ) { michael@0: status = U_BUFFER_OVERFLOW_ERROR; michael@0: } michael@0: } michael@0: michael@0: const UnicodeString* michael@0: DTSkeletonEnumeration::snext(UErrorCode& status) { michael@0: if (U_SUCCESS(status) && pos < fSkeletons->size()) { michael@0: return (const UnicodeString*)fSkeletons->elementAt(pos++); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: void michael@0: DTSkeletonEnumeration::reset(UErrorCode& /*status*/) { michael@0: pos=0; michael@0: } michael@0: michael@0: int32_t michael@0: DTSkeletonEnumeration::count(UErrorCode& /*status*/) const { michael@0: return (fSkeletons==NULL) ? 0 : fSkeletons->size(); michael@0: } michael@0: michael@0: UBool michael@0: DTSkeletonEnumeration::isCanonicalItem(const UnicodeString& item) { michael@0: if ( item.length() != 1 ) { michael@0: return FALSE; michael@0: } michael@0: for (int32_t i=0; isize(); ++i) { michael@0: if ((s=(UnicodeString *)fSkeletons->elementAt(i))!=NULL) { michael@0: delete s; michael@0: } michael@0: } michael@0: delete fSkeletons; michael@0: } michael@0: michael@0: DTRedundantEnumeration::DTRedundantEnumeration() { michael@0: pos=0; michael@0: fPatterns = NULL; michael@0: } michael@0: michael@0: void michael@0: DTRedundantEnumeration::add(const UnicodeString& pattern, UErrorCode& status) { michael@0: if (U_FAILURE(status)) return; michael@0: if (fPatterns == NULL) { michael@0: fPatterns = new UVector(status); michael@0: if (U_FAILURE(status)) { michael@0: delete fPatterns; michael@0: fPatterns = NULL; michael@0: return; michael@0: } michael@0: } michael@0: fPatterns->addElement(new UnicodeString(pattern), status); michael@0: if (U_FAILURE(status)) { michael@0: delete fPatterns; michael@0: fPatterns = NULL; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: const UnicodeString* michael@0: DTRedundantEnumeration::snext(UErrorCode& status) { michael@0: if (U_SUCCESS(status) && pos < fPatterns->size()) { michael@0: return (const UnicodeString*)fPatterns->elementAt(pos++); michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: void michael@0: DTRedundantEnumeration::reset(UErrorCode& /*status*/) { michael@0: pos=0; michael@0: } michael@0: michael@0: int32_t michael@0: DTRedundantEnumeration::count(UErrorCode& /*status*/) const { michael@0: return (fPatterns==NULL) ? 0 : fPatterns->size(); michael@0: } michael@0: michael@0: UBool michael@0: DTRedundantEnumeration::isCanonicalItem(const UnicodeString& item) { michael@0: if ( item.length() != 1 ) { michael@0: return FALSE; michael@0: } michael@0: for (int32_t i=0; isize(); ++i) { michael@0: if ((s=(UnicodeString *)fPatterns->elementAt(i))!=NULL) { michael@0: delete s; michael@0: } michael@0: } michael@0: delete fPatterns; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: //eof