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