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