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