1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2007-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
11 *******************************************************************************
14 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_FORMATTING
17 #include "unicode/datefmt.h"
18 #include "unicode/decimfmt.h"
19 #include "unicode/dtfmtsym.h"
20 #include "unicode/dtptngen.h"
21 #include "unicode/simpleformatter.h"
22 #include "unicode/smpdtfmt.h"
23 #include "unicode/udat.h"
24 #include "unicode/udatpg.h"
25 #include "unicode/uniset.h"
26 #include "unicode/uloc.h"
27 #include "unicode/ures.h"
28 #include "unicode/ustring.h"
29 #include "unicode/rep.h"
39 #include "dtptngen_impl.h"
44 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
46 * If we are on EBCDIC, use an iterator which will
47 * traverse the bundles in ASCII order.
49 #define U_USE_ASCII_BUNDLE_ITERATOR
50 #define U_SORT_ASCII_BUNDLE_ITERATOR
53 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
55 #include "unicode/ustring.h"
60 UResourceBundle *item;
63 struct UResourceBundleAIterator {
64 UResourceBundle *bund;
70 /* Must be C linkage to pass function pointer to the sort function */
74 static int32_t U_CALLCONV
75 ures_a_codepointSort(const void *context, const void *left, const void *right) {
76 //CompareContext *cmp=(CompareContext *)context;
77 return u_strcmp(((const UResAEntry *)left)->key,
78 ((const UResAEntry *)right)->key);
83 static void ures_a_open(UResourceBundleAIterator *aiter, UResourceBundle *bund, UErrorCode *status) {
84 if(U_FAILURE(*status)) {
88 aiter->num = ures_getSize(aiter->bund);
90 #if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
91 aiter->entries = NULL;
93 aiter->entries = (UResAEntry*)uprv_malloc(sizeof(UResAEntry)*aiter->num);
94 for(int i=0;i<aiter->num;i++) {
95 aiter->entries[i].item = ures_getByIndex(aiter->bund, i, NULL, status);
96 const char *akey = ures_getKey(aiter->entries[i].item);
97 int32_t len = uprv_strlen(akey)+1;
98 aiter->entries[i].key = (UChar*)uprv_malloc(len*sizeof(UChar));
99 u_charsToUChars(akey, aiter->entries[i].key, len);
101 uprv_sortArray(aiter->entries, aiter->num, sizeof(UResAEntry), ures_a_codepointSort, NULL, TRUE, status);
105 static void ures_a_close(UResourceBundleAIterator *aiter) {
106 #if defined(U_SORT_ASCII_BUNDLE_ITERATOR)
107 for(int i=0;i<aiter->num;i++) {
108 uprv_free(aiter->entries[i].key);
109 ures_close(aiter->entries[i].item);
114 static const UChar *ures_a_getNextString(UResourceBundleAIterator *aiter, int32_t *len, const char **key, UErrorCode *err) {
115 #if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
116 return ures_getNextString(aiter->bund, len, key, err);
118 if(U_FAILURE(*err)) return NULL;
119 UResourceBundle *item = aiter->entries[aiter->cursor].item;
120 const UChar* ret = ures_getString(item, len, err);
121 *key = ures_getKey(item);
133 // *****************************************************************************
134 // class DateTimePatternGenerator
135 // *****************************************************************************
136 static const UChar Canonical_Items[] = {
138 CAP_G, LOW_Y, CAP_Q, CAP_M, LOW_W, CAP_W, CAP_E, LOW_D, CAP_D, CAP_F,
139 CAP_H, LOW_M, LOW_S, CAP_S, LOW_V, 0
142 static const dtTypeElem dtTypes[] = {
143 // patternChar, field, type, minLen, weight
144 {CAP_G, UDATPG_ERA_FIELD, DT_SHORT, 1, 3,},
145 {CAP_G, UDATPG_ERA_FIELD, DT_LONG, 4, 0},
146 {LOW_Y, UDATPG_YEAR_FIELD, DT_NUMERIC, 1, 20},
147 {CAP_Y, UDATPG_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 20},
148 {LOW_U, UDATPG_YEAR_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 20},
149 {LOW_R, UDATPG_YEAR_FIELD, DT_NUMERIC + 3*DT_DELTA, 1, 20},
150 {CAP_U, UDATPG_YEAR_FIELD, DT_SHORT, 1, 3},
151 {CAP_U, UDATPG_YEAR_FIELD, DT_LONG, 4, 0},
152 {CAP_U, UDATPG_YEAR_FIELD, DT_NARROW, 5, 0},
153 {CAP_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC, 1, 2},
154 {CAP_Q, UDATPG_QUARTER_FIELD, DT_SHORT, 3, 0},
155 {CAP_Q, UDATPG_QUARTER_FIELD, DT_LONG, 4, 0},
156 {LOW_Q, UDATPG_QUARTER_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
157 {LOW_Q, UDATPG_QUARTER_FIELD, DT_SHORT + DT_DELTA, 3, 0},
158 {LOW_Q, UDATPG_QUARTER_FIELD, DT_LONG + DT_DELTA, 4, 0},
159 {CAP_M, UDATPG_MONTH_FIELD, DT_NUMERIC, 1, 2},
160 {CAP_M, UDATPG_MONTH_FIELD, DT_SHORT, 3, 0},
161 {CAP_M, UDATPG_MONTH_FIELD, DT_LONG, 4, 0},
162 {CAP_M, UDATPG_MONTH_FIELD, DT_NARROW, 5, 0},
163 {CAP_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 2},
164 {CAP_L, UDATPG_MONTH_FIELD, DT_SHORT - DT_DELTA, 3, 0},
165 {CAP_L, UDATPG_MONTH_FIELD, DT_LONG - DT_DELTA, 4, 0},
166 {CAP_L, UDATPG_MONTH_FIELD, DT_NARROW - DT_DELTA, 5, 0},
167 {LOW_L, UDATPG_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 1},
168 {LOW_W, UDATPG_WEEK_OF_YEAR_FIELD, DT_NUMERIC, 1, 2},
169 {CAP_W, UDATPG_WEEK_OF_MONTH_FIELD, DT_NUMERIC + DT_DELTA, 1, 0},
170 {CAP_E, UDATPG_WEEKDAY_FIELD, DT_SHORT, 1, 3},
171 {CAP_E, UDATPG_WEEKDAY_FIELD, DT_LONG, 4, 0},
172 {CAP_E, UDATPG_WEEKDAY_FIELD, DT_NARROW, 5, 0},
173 {LOW_C, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 2},
174 {LOW_C, UDATPG_WEEKDAY_FIELD, DT_SHORT - 2*DT_DELTA, 3, 0},
175 {LOW_C, UDATPG_WEEKDAY_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
176 {LOW_C, UDATPG_WEEKDAY_FIELD, DT_NARROW - 2*DT_DELTA, 5, 0},
177 {LOW_E, UDATPG_WEEKDAY_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // LOW_E is currently not used in CLDR data, should not be canonical
178 {LOW_E, UDATPG_WEEKDAY_FIELD, DT_SHORT - DT_DELTA, 3, 0},
179 {LOW_E, UDATPG_WEEKDAY_FIELD, DT_LONG - DT_DELTA, 4, 0},
180 {LOW_E, UDATPG_WEEKDAY_FIELD, DT_NARROW - DT_DELTA, 5, 0},
181 {LOW_D, UDATPG_DAY_FIELD, DT_NUMERIC, 1, 2},
182 {CAP_D, UDATPG_DAY_OF_YEAR_FIELD, DT_NUMERIC + DT_DELTA, 1, 3},
183 {CAP_F, UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 0},
184 {LOW_G, UDATPG_DAY_FIELD, DT_NUMERIC + 3*DT_DELTA, 1, 20}, // really internal use, so we don't care
185 {LOW_A, UDATPG_DAYPERIOD_FIELD, DT_SHORT, 1, 0},
186 {CAP_H, UDATPG_HOUR_FIELD, DT_NUMERIC + 10*DT_DELTA, 1, 2}, // 24 hour
187 {LOW_K, UDATPG_HOUR_FIELD, DT_NUMERIC + 11*DT_DELTA, 1, 2}, // 24 hour
188 {LOW_H, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12 hour
189 {CAP_K, UDATPG_HOUR_FIELD, DT_NUMERIC + DT_DELTA, 1, 2}, // 12 hour
190 {LOW_M, UDATPG_MINUTE_FIELD, DT_NUMERIC, 1, 2},
191 {LOW_S, UDATPG_SECOND_FIELD, DT_NUMERIC, 1, 2},
192 {CAP_S, UDATPG_FRACTIONAL_SECOND_FIELD, DT_NUMERIC + DT_DELTA, 1, 1000},
193 {CAP_A, UDATPG_SECOND_FIELD, DT_NUMERIC + 2*DT_DELTA, 1, 1000},
194 {LOW_V, UDATPG_ZONE_FIELD, DT_SHORT - 2*DT_DELTA, 1, 0},
195 {LOW_V, UDATPG_ZONE_FIELD, DT_LONG - 2*DT_DELTA, 4, 0},
196 {LOW_Z, UDATPG_ZONE_FIELD, DT_SHORT, 1, 3},
197 {LOW_Z, UDATPG_ZONE_FIELD, DT_LONG, 4, 0},
198 {CAP_Z, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 3},
199 {CAP_Z, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
200 {CAP_Z, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 5, 0},
201 {CAP_O, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 0},
202 {CAP_O, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
203 {CAP_V, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 1, 0},
204 {CAP_V, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 2, 0},
205 {CAP_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0},
206 {CAP_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0},
207 {CAP_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
208 {LOW_X, UDATPG_ZONE_FIELD, DT_NARROW - DT_DELTA, 1, 0},
209 {LOW_X, UDATPG_ZONE_FIELD, DT_SHORT - DT_DELTA, 2, 0},
210 {LOW_X, UDATPG_ZONE_FIELD, DT_LONG - DT_DELTA, 4, 0},
211 {LOW_J, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12/24 hour
212 {CAP_J, UDATPG_HOUR_FIELD, DT_NUMERIC, 1, 2}, // 12/24 hour no AM/PM
213 {0, UDATPG_FIELD_COUNT, 0, 0, 0} , // last row of dtTypes[]
216 static const char* const CLDR_FIELD_APPEND[] = {
217 "Era", "Year", "Quarter", "Month", "Week", "*", "Day-Of-Week", "Day", "*", "*", "*",
218 "Hour", "Minute", "Second", "*", "Timezone"
221 static const char* const CLDR_FIELD_NAME[] = {
222 "era", "year", "quarter", "month", "week", "*", "weekday", "*", "*", "day", "dayperiod",
223 "hour", "minute", "second", "*", "zone"
227 static const UChar UDATPG_ItemFormat[]= {0x7B, 0x30, 0x7D, 0x20, 0x251C, 0x7B, 0x32, 0x7D, 0x3A,
228 0x20, 0x7B, 0x31, 0x7D, 0x2524, 0}; // {0} \u251C{2}: {1}\u2524
230 //static const UChar repeatedPatterns[6]={CAP_G, CAP_E, LOW_Z, LOW_V, CAP_Q, 0}; // "GEzvQ"
232 static const char DT_DateTimePatternsTag[]="DateTimePatterns";
233 static const char DT_DateTimeCalendarTag[]="calendar";
234 static const char DT_DateTimeGregorianTag[]="gregorian";
235 static const char DT_DateTimeAppendItemsTag[]="appendItems";
236 static const char DT_DateTimeFieldsTag[]="fields";
237 static const char DT_DateTimeAvailableFormatsTag[]="availableFormats";
238 //static const UnicodeString repeatedPattern=UnicodeString(repeatedPatterns);
240 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateTimePatternGenerator)
241 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTSkeletonEnumeration)
242 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTRedundantEnumeration)
244 DateTimePatternGenerator* U_EXPORT2
245 DateTimePatternGenerator::createInstance(UErrorCode& status) {
246 return createInstance(Locale::getDefault(), status);
249 DateTimePatternGenerator* U_EXPORT2
250 DateTimePatternGenerator::createInstance(const Locale& locale, UErrorCode& status) {
251 if (U_FAILURE(status)) {
254 LocalPointer<DateTimePatternGenerator> result(
255 new DateTimePatternGenerator(locale, status), status);
256 return U_SUCCESS(status) ? result.orphan() : NULL;
259 DateTimePatternGenerator* U_EXPORT2
260 DateTimePatternGenerator::createEmptyInstance(UErrorCode& status) {
261 DateTimePatternGenerator *result = new DateTimePatternGenerator(status);
262 if (result == NULL) {
263 status = U_MEMORY_ALLOCATION_ERROR;
265 if (U_FAILURE(status)) {
272 DateTimePatternGenerator::DateTimePatternGenerator(UErrorCode &status) :
274 fAvailableFormatKeyHash(NULL)
276 fp = new FormatParser();
277 dtMatcher = new DateTimeMatcher();
278 distanceInfo = new DistanceInfo();
279 patternMap = new PatternMap();
280 if (fp == NULL || dtMatcher == NULL || distanceInfo == NULL || patternMap == NULL) {
281 status = U_MEMORY_ALLOCATION_ERROR;
285 DateTimePatternGenerator::DateTimePatternGenerator(const Locale& locale, UErrorCode &status) :
287 fAvailableFormatKeyHash(NULL)
289 fp = new FormatParser();
290 dtMatcher = new DateTimeMatcher();
291 distanceInfo = new DistanceInfo();
292 patternMap = new PatternMap();
293 if (fp == NULL || dtMatcher == NULL || distanceInfo == NULL || patternMap == NULL) {
294 status = U_MEMORY_ALLOCATION_ERROR;
297 initData(locale, status);
301 DateTimePatternGenerator::DateTimePatternGenerator(const DateTimePatternGenerator& other) :
304 fAvailableFormatKeyHash(NULL)
306 fp = new FormatParser();
307 dtMatcher = new DateTimeMatcher();
308 distanceInfo = new DistanceInfo();
309 patternMap = new PatternMap();
313 DateTimePatternGenerator&
314 DateTimePatternGenerator::operator=(const DateTimePatternGenerator& other) {
316 if (&other == this) {
319 pLocale = other.pLocale;
320 fDefaultHourFormatChar = other.fDefaultHourFormatChar;
322 dtMatcher->copyFrom(other.dtMatcher->skeleton);
323 *distanceInfo = *(other.distanceInfo);
324 dateTimeFormat = other.dateTimeFormat;
325 decimal = other.decimal;
326 // NUL-terminate for the C API.
327 dateTimeFormat.getTerminatedBuffer();
328 decimal.getTerminatedBuffer();
330 if ( other.skipMatcher == NULL ) {
334 skipMatcher = new DateTimeMatcher(*other.skipMatcher);
336 for (int32_t i=0; i< UDATPG_FIELD_COUNT; ++i ) {
337 appendItemFormats[i] = other.appendItemFormats[i];
338 appendItemNames[i] = other.appendItemNames[i];
339 // NUL-terminate for the C API.
340 appendItemFormats[i].getTerminatedBuffer();
341 appendItemNames[i].getTerminatedBuffer();
343 UErrorCode status = U_ZERO_ERROR;
344 patternMap->copyFrom(*other.patternMap, status);
345 copyHashtable(other.fAvailableFormatKeyHash, status);
351 DateTimePatternGenerator::operator==(const DateTimePatternGenerator& other) const {
352 if (this == &other) {
355 if ((pLocale==other.pLocale) && (patternMap->equals(*other.patternMap)) &&
356 (dateTimeFormat==other.dateTimeFormat) && (decimal==other.decimal)) {
357 for ( int32_t i=0 ; i<UDATPG_FIELD_COUNT; ++i ) {
358 if ((appendItemFormats[i] != other.appendItemFormats[i]) ||
359 (appendItemNames[i] != other.appendItemNames[i]) ) {
371 DateTimePatternGenerator::operator!=(const DateTimePatternGenerator& other) const {
372 return !operator==(other);
375 DateTimePatternGenerator::~DateTimePatternGenerator() {
376 if (fAvailableFormatKeyHash!=NULL) {
377 delete fAvailableFormatKeyHash;
380 if (fp != NULL) delete fp;
381 if (dtMatcher != NULL) delete dtMatcher;
382 if (distanceInfo != NULL) delete distanceInfo;
383 if (patternMap != NULL) delete patternMap;
384 if (skipMatcher != NULL) delete skipMatcher;
389 UInitOnce initOnce = U_INITONCE_INITIALIZER;
390 UHashtable *localeToAllowedHourFormatsMap = NULL;
392 // Value deleter for hashmap.
393 U_CFUNC void U_CALLCONV deleteAllowedHourFormats(void *ptr) {
397 // Close hashmap at cleanup.
398 U_CFUNC UBool U_CALLCONV allowedHourFormatsCleanup() {
399 uhash_close(localeToAllowedHourFormatsMap);
403 enum AllowedHourFormat{
404 ALLOWED_HOUR_FORMAT_UNKNOWN = -1,
405 ALLOWED_HOUR_FORMAT_h,
406 ALLOWED_HOUR_FORMAT_H,
407 ALLOWED_HOUR_FORMAT_hb,
408 ALLOWED_HOUR_FORMAT_Hb,
409 ALLOWED_HOUR_FORMAT_hB,
410 ALLOWED_HOUR_FORMAT_HB
416 DateTimePatternGenerator::initData(const Locale& locale, UErrorCode &status) {
417 //const char *baseLangName = locale.getBaseName(); // unused
420 fAvailableFormatKeyHash=NULL;
421 addCanonicalItems(status);
422 addICUPatterns(locale, status);
423 addCLDRData(locale, status);
424 setDateTimeFromCalendar(locale, status);
425 setDecimalSymbols(locale, status);
426 umtx_initOnce(initOnce, loadAllowedHourFormatsData, status);
427 getAllowedHourFormats(locale, status);
428 } // DateTimePatternGenerator::initData
432 struct AllowedHourFormatsSink : public ResourceSink {
433 // Initialize sub-sinks.
434 AllowedHourFormatsSink() {}
435 virtual ~AllowedHourFormatsSink();
437 virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
438 UErrorCode &errorCode) {
439 ResourceTable timeData = value.getTable(errorCode);
440 if (U_FAILURE(errorCode)) { return; }
441 for (int32_t i = 0; timeData.getKeyAndValue(i, key, value); ++i) {
442 const char *regionOrLocale = key;
443 ResourceTable formatList = value.getTable(errorCode);
444 if (U_FAILURE(errorCode)) { return; }
445 for (int32_t j = 0; formatList.getKeyAndValue(j, key, value); ++j) {
446 if (uprv_strcmp(key, "allowed") == 0) { // Ignore "preferred" list.
447 LocalMemory<int32_t> list;
449 if (value.getType() == URES_STRING) {
450 if (list.allocateInsteadAndReset(2) == NULL) {
451 errorCode = U_MEMORY_ALLOCATION_ERROR;
454 list[0] = getHourFormatFromUnicodeString(value.getUnicodeString(errorCode));
458 ResourceArray allowedFormats = value.getArray(errorCode);
459 length = allowedFormats.getSize();
460 if (list.allocateInsteadAndReset(length + 1) == NULL) {
461 errorCode = U_MEMORY_ALLOCATION_ERROR;
464 for (int32_t k = 0; k < length; ++k) {
465 allowedFormats.getValue(k, value);
466 list[k] = getHourFormatFromUnicodeString(value.getUnicodeString(errorCode));
469 list[length] = ALLOWED_HOUR_FORMAT_UNKNOWN;
470 uhash_put(localeToAllowedHourFormatsMap,
471 const_cast<char *>(regionOrLocale), list.orphan(), &errorCode);
472 if (U_FAILURE(errorCode)) { return; }
478 AllowedHourFormat getHourFormatFromUnicodeString(UnicodeString s) {
479 if (s.length() == 1) {
480 if (s[0] == LOW_H) { return ALLOWED_HOUR_FORMAT_h; }
481 if (s[0] == CAP_H) { return ALLOWED_HOUR_FORMAT_H; }
482 } else if (s.length() == 2) {
483 if (s[0] == LOW_H && s[1] == LOW_B) { return ALLOWED_HOUR_FORMAT_hb; }
484 if (s[0] == CAP_H && s[1] == LOW_B) { return ALLOWED_HOUR_FORMAT_Hb; }
485 if (s[0] == LOW_H && s[1] == CAP_B) { return ALLOWED_HOUR_FORMAT_hB; }
486 if (s[0] == CAP_H && s[1] == CAP_B) { return ALLOWED_HOUR_FORMAT_HB; }
489 return ALLOWED_HOUR_FORMAT_UNKNOWN;
495 AllowedHourFormatsSink::~AllowedHourFormatsSink() {}
497 U_CFUNC void U_CALLCONV DateTimePatternGenerator::loadAllowedHourFormatsData(UErrorCode &status) {
498 if (U_FAILURE(status)) { return; }
499 localeToAllowedHourFormatsMap = uhash_open(
500 uhash_hashChars, uhash_compareChars, NULL, &status);
501 uhash_setValueDeleter(localeToAllowedHourFormatsMap, deleteAllowedHourFormats);
502 LocalUResourceBundlePointer rb(ures_openDirect(NULL, "supplementalData", &status));
504 AllowedHourFormatsSink sink;
505 // TODO: Currently in the enumeration each table allocates a new array.
506 // Try to reduce the number of memory allocations. Consider storing a
507 // UVector32 with the concatenation of all of the sub-arrays, put the start index
508 // into the hashmap, store 6 single-value sub-arrays right at the beginning of the
509 // vector (at index enum*2) for easy data sharing, copy sub-arrays into runtime
510 // object. Remember to clean up the vector, too.
511 ures_getAllItemsWithFallback(rb.getAlias(), "timeData", sink, status);
513 ucln_i18n_registerCleanup(UCLN_I18N_ALLOWED_HOUR_FORMATS, allowedHourFormatsCleanup);
516 void DateTimePatternGenerator::getAllowedHourFormats(const Locale &locale, UErrorCode &status) {
517 if (U_FAILURE(status)) { return; }
518 const char *localeID = locale.getName();
519 char maxLocaleID[ULOC_FULLNAME_CAPACITY];
520 int32_t length = uloc_addLikelySubtags(localeID, maxLocaleID, ULOC_FULLNAME_CAPACITY, &status);
521 if (U_FAILURE(status)) {
523 } else if (length == ULOC_FULLNAME_CAPACITY) { // no room for NUL
524 status = U_BUFFER_OVERFLOW_ERROR;
527 Locale maxLocale = Locale(maxLocaleID);
529 const char *country = maxLocale.getCountry();
530 if (*country == '\0') { country = "001"; }
531 const char *language = maxLocale.getLanguage();
533 CharString langCountry;
534 langCountry.append(language, uprv_strlen(language), status);
535 langCountry.append('_', status);
536 langCountry.append(country, uprv_strlen(country), status);
538 int32_t *allowedFormats;
539 allowedFormats = (int32_t *)uhash_get(localeToAllowedHourFormatsMap, langCountry.data());
540 if (allowedFormats == NULL) {
541 allowedFormats = (int32_t *)uhash_get(localeToAllowedHourFormatsMap, const_cast<char *>(country));
544 if (allowedFormats != NULL) { // Lookup is successful
545 for (int32_t i = 0; i < UPRV_LENGTHOF(fAllowedHourFormats); ++i) {
546 fAllowedHourFormats[i] = allowedFormats[i];
547 if (allowedFormats[i] == ALLOWED_HOUR_FORMAT_UNKNOWN) {
551 } else { // Lookup failed, twice
552 fAllowedHourFormats[0] = ALLOWED_HOUR_FORMAT_H;
553 fAllowedHourFormats[1] = ALLOWED_HOUR_FORMAT_UNKNOWN;
558 DateTimePatternGenerator::getSkeleton(const UnicodeString& pattern, UErrorCode&
561 DateTimeMatcher matcher;
562 PtnSkeleton localSkeleton;
563 matcher.set(pattern, &fp, localSkeleton);
564 return localSkeleton.getSkeleton();
568 DateTimePatternGenerator::staticGetSkeleton(
569 const UnicodeString& pattern, UErrorCode& /*status*/) {
571 DateTimeMatcher matcher;
572 PtnSkeleton localSkeleton;
573 matcher.set(pattern, &fp, localSkeleton);
574 return localSkeleton.getSkeleton();
578 DateTimePatternGenerator::getBaseSkeleton(const UnicodeString& pattern, UErrorCode& /*status*/) {
580 DateTimeMatcher matcher;
581 PtnSkeleton localSkeleton;
582 matcher.set(pattern, &fp, localSkeleton);
583 return localSkeleton.getBaseSkeleton();
587 DateTimePatternGenerator::staticGetBaseSkeleton(
588 const UnicodeString& pattern, UErrorCode& /*status*/) {
590 DateTimeMatcher matcher;
591 PtnSkeleton localSkeleton;
592 matcher.set(pattern, &fp, localSkeleton);
593 return localSkeleton.getBaseSkeleton();
597 DateTimePatternGenerator::addICUPatterns(const Locale& locale, UErrorCode& status) {
598 if (U_FAILURE(status)) { return; }
599 UnicodeString dfPattern;
600 UnicodeString conflictingString;
603 // Load with ICU patterns
604 for (int32_t i=DateFormat::kFull; i<=DateFormat::kShort; i++) {
605 DateFormat::EStyle style = (DateFormat::EStyle)i;
606 df = DateFormat::createDateInstance(style, locale);
607 SimpleDateFormat* sdf;
608 if (df != NULL && (sdf = dynamic_cast<SimpleDateFormat*>(df)) != NULL) {
609 sdf->toPattern(dfPattern);
610 addPattern(dfPattern, FALSE, conflictingString, status);
612 // TODO Maybe we should return an error when the date format isn't simple.
614 if (U_FAILURE(status)) { return; }
616 df = DateFormat::createTimeInstance(style, locale);
617 if (df != NULL && (sdf = dynamic_cast<SimpleDateFormat*>(df)) != NULL) {
618 sdf->toPattern(dfPattern);
619 addPattern(dfPattern, FALSE, conflictingString, status);
621 // TODO: C++ and Java are inconsistent (see #12568).
622 // C++ uses MEDIUM, but Java uses SHORT.
623 if ( i==DateFormat::kShort && !dfPattern.isEmpty() ) {
624 consumeShortTimePattern(dfPattern, status);
627 // TODO Maybe we should return an error when the date format isn't simple.
629 if (U_FAILURE(status)) { return; }
634 DateTimePatternGenerator::hackTimes(const UnicodeString& hackPattern, UErrorCode& status) {
635 UnicodeString conflictingString;
637 fp->set(hackPattern);
640 for (int32_t i=0; i<fp->itemNumber; ++i) {
641 UnicodeString field = fp->items[i];
642 if ( fp->isQuoteLiteral(field) ) {
644 UnicodeString quoteLiteral;
645 fp->getQuoteLiteral(quoteLiteral, &i);
646 mmss += quoteLiteral;
650 if (fp->isPatternSeparator(field) && gotMm) {
654 UChar ch=field.charAt(0);
665 addPattern(mmss, FALSE, conflictingString, status);
669 if (gotMm || ch==LOW_Z || ch==CAP_Z || ch==LOW_V || ch==CAP_V) {
679 #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
681 static const UChar hourFormatChars[] = { CAP_H, LOW_H, CAP_K, LOW_K, 0 }; // HhKk, the hour format characters
684 DateTimePatternGenerator::getCalendarTypeToUse(const Locale& locale, CharString& destination, UErrorCode& err) {
685 destination.clear().append(DT_DateTimeGregorianTag, -1, err); // initial default
686 if ( U_SUCCESS(err) ) {
687 char localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY];
688 // obtain a locale that always has the calendar key value that should be used
689 ures_getFunctionalEquivalent(
690 localeWithCalendarKey,
691 ULOC_LOCALE_IDENTIFIER_CAPACITY,
699 localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY-1] = 0; // ensure null termination
700 // now get the calendar key value from that locale
701 char calendarType[ULOC_KEYWORDS_CAPACITY];
702 int32_t calendarTypeLen = uloc_getKeywordValue(
703 localeWithCalendarKey,
706 ULOC_KEYWORDS_CAPACITY,
708 if (U_SUCCESS(err) && calendarTypeLen < ULOC_KEYWORDS_CAPACITY) {
709 destination.clear().append(calendarType, -1, err);
710 if (U_FAILURE(err)) { return; }
717 DateTimePatternGenerator::consumeShortTimePattern(const UnicodeString& shortTimePattern,
718 UErrorCode& status) {
720 // set fDefaultHourFormatChar to the hour format character from this pattern
721 int32_t tfIdx, tfLen = shortTimePattern.length();
722 UBool ignoreChars = FALSE;
723 for (tfIdx = 0; tfIdx < tfLen; tfIdx++) {
724 UChar tfChar = shortTimePattern.charAt(tfIdx);
725 if ( tfChar == SINGLE_QUOTE ) {
726 ignoreChars = !ignoreChars; // toggle (handle quoted literals & '' for single quote)
727 } else if ( !ignoreChars && u_strchr(hourFormatChars, tfChar) != NULL ) {
728 fDefaultHourFormatChar = tfChar;
734 hackTimes(shortTimePattern, status);
737 struct DateTimePatternGenerator::AppendItemFormatsSink : public ResourceSink {
739 // Destination for data, modified via setters.
740 DateTimePatternGenerator& dtpg;
742 AppendItemFormatsSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
743 virtual ~AppendItemFormatsSink();
745 virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
746 UErrorCode &errorCode) {
747 ResourceTable itemsTable = value.getTable(errorCode);
748 if (U_FAILURE(errorCode)) { return; }
749 for (int32_t i = 0; itemsTable.getKeyAndValue(i, key, value); ++i) {
750 UDateTimePatternField field = dtpg.getAppendFormatNumber(key);
751 if (field == UDATPG_FIELD_COUNT) { continue; }
752 const UnicodeString& valueStr = value.getUnicodeString(errorCode);
753 if (dtpg.getAppendItemFormat(field).isEmpty() && !valueStr.isEmpty()) {
754 dtpg.setAppendItemFormat(field, valueStr);
759 void fillInMissing() {
760 UnicodeString defaultItemFormat(TRUE, UDATPG_ItemFormat, UPRV_LENGTHOF(UDATPG_ItemFormat)-1); // Read-only alias.
761 for (int32_t i = 0; i < UDATPG_FIELD_COUNT; i++) {
762 UDateTimePatternField field = (UDateTimePatternField)i;
763 if (dtpg.getAppendItemFormat(field).isEmpty()) {
764 dtpg.setAppendItemFormat(field, defaultItemFormat);
770 struct DateTimePatternGenerator::AppendItemNamesSink : public ResourceSink {
772 // Destination for data, modified via setters.
773 DateTimePatternGenerator& dtpg;
775 AppendItemNamesSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
776 virtual ~AppendItemNamesSink();
778 virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
779 UErrorCode &errorCode) {
780 ResourceTable itemsTable = value.getTable(errorCode);
781 if (U_FAILURE(errorCode)) { return; }
782 for (int32_t i = 0; itemsTable.getKeyAndValue(i, key, value); ++i) {
783 UDateTimePatternField field = dtpg.getAppendNameNumber(key);
784 if (field == UDATPG_FIELD_COUNT) { continue; }
785 ResourceTable detailsTable = value.getTable(errorCode);
786 if (U_FAILURE(errorCode)) { return; }
787 for (int32_t j = 0; detailsTable.getKeyAndValue(j, key, value); ++j) {
788 if (uprv_strcmp(key, "dn") != 0) { continue; }
789 const UnicodeString& valueStr = value.getUnicodeString(errorCode);
790 if (dtpg.getAppendItemName(field).isEmpty() && !valueStr.isEmpty()) {
791 dtpg.setAppendItemName(field, valueStr);
798 void fillInMissing() {
799 for (int32_t i = 0; i < UDATPG_FIELD_COUNT; i++) {
800 UDateTimePatternField field = (UDateTimePatternField)i;
801 UnicodeString& valueStr = dtpg.getMutableAppendItemName(field);
802 if (valueStr.isEmpty()) {
807 valueStr += (UChar)(i+0x30);
810 valueStr += (UChar)0x31;
811 valueStr += (UChar)(i-10 + 0x30);
813 // NUL-terminate for the C API.
814 valueStr.getTerminatedBuffer();
820 struct DateTimePatternGenerator::AvailableFormatsSink : public ResourceSink {
822 // Destination for data, modified via setters.
823 DateTimePatternGenerator& dtpg;
825 // Temporary variable, required for calling addPatternWithSkeleton.
826 UnicodeString conflictingPattern;
828 AvailableFormatsSink(DateTimePatternGenerator& _dtpg) : dtpg(_dtpg) {}
829 virtual ~AvailableFormatsSink();
831 virtual void put(const char *key, ResourceValue &value, UBool isRoot,
832 UErrorCode &errorCode) {
833 ResourceTable itemsTable = value.getTable(errorCode);
834 if (U_FAILURE(errorCode)) { return; }
835 for (int32_t i = 0; itemsTable.getKeyAndValue(i, key, value); ++i) {
836 const UnicodeString formatKey(key, -1, US_INV);
837 if (!dtpg.isAvailableFormatSet(formatKey) ) {
838 dtpg.setAvailableFormat(formatKey, errorCode);
839 // Add pattern with its associated skeleton. Override any duplicate
840 // derived from std patterns, but not a previous availableFormats entry:
841 const UnicodeString& formatValue = value.getUnicodeString(errorCode);
842 conflictingPattern.remove();
843 dtpg.addPatternWithSkeleton(formatValue, &formatKey, !isRoot, conflictingPattern, errorCode);
849 // Virtual destructors must be defined out of line.
850 DateTimePatternGenerator::AppendItemFormatsSink::~AppendItemFormatsSink() {}
851 DateTimePatternGenerator::AppendItemNamesSink::~AppendItemNamesSink() {}
852 DateTimePatternGenerator::AvailableFormatsSink::~AvailableFormatsSink() {}
855 DateTimePatternGenerator::addCLDRData(const Locale& locale, UErrorCode& errorCode) {
856 if (U_FAILURE(errorCode)) { return; }
857 UnicodeString rbPattern, value, field;
860 LocalUResourceBundlePointer rb(ures_open(NULL, locale.getName(), &errorCode));
861 if (U_FAILURE(errorCode)) { return; }
863 CharString calendarTypeToUse; // to be filled in with the type to use, if all goes well
864 getCalendarTypeToUse(locale, calendarTypeToUse, errorCode);
865 if (U_FAILURE(errorCode)) { return; }
867 // Local err to ignore resource not found exceptions
868 UErrorCode err = U_ZERO_ERROR;
870 // Load append item formats.
871 AppendItemFormatsSink appendItemFormatsSink(*this);
873 .append(DT_DateTimeCalendarTag, errorCode)
874 .append('/', errorCode)
875 .append(calendarTypeToUse, errorCode)
876 .append('/', errorCode)
877 .append(DT_DateTimeAppendItemsTag, errorCode); // i.e., calendar/xxx/appendItems
878 if (U_FAILURE(errorCode)) { return; }
879 ures_getAllItemsWithFallback(rb.getAlias(), path.data(), appendItemFormatsSink, err);
880 appendItemFormatsSink.fillInMissing();
882 // Load CLDR item names.
884 AppendItemNamesSink appendItemNamesSink(*this);
885 ures_getAllItemsWithFallback(rb.getAlias(), DT_DateTimeFieldsTag, appendItemNamesSink, err);
886 appendItemNamesSink.fillInMissing();
888 // Load the available formats from CLDR.
890 initHashtable(errorCode);
891 if (U_FAILURE(errorCode)) { return; }
892 AvailableFormatsSink availableFormatsSink(*this);
894 .append(DT_DateTimeCalendarTag, errorCode)
895 .append('/', errorCode)
896 .append(calendarTypeToUse, errorCode)
897 .append('/', errorCode)
898 .append(DT_DateTimeAvailableFormatsTag, errorCode); // i.e., calendar/xxx/availableFormats
899 if (U_FAILURE(errorCode)) { return; }
900 ures_getAllItemsWithFallback(rb.getAlias(), path.data(), availableFormatsSink, err);
904 DateTimePatternGenerator::initHashtable(UErrorCode& err) {
905 if (fAvailableFormatKeyHash!=NULL) {
908 if ((fAvailableFormatKeyHash = new Hashtable(FALSE, err))==NULL) {
909 err=U_MEMORY_ALLOCATION_ERROR;
915 DateTimePatternGenerator::setAppendItemFormat(UDateTimePatternField field, const UnicodeString& value) {
916 appendItemFormats[field] = value;
917 // NUL-terminate for the C API.
918 appendItemFormats[field].getTerminatedBuffer();
922 DateTimePatternGenerator::getAppendItemFormat(UDateTimePatternField field) const {
923 return appendItemFormats[field];
927 DateTimePatternGenerator::setAppendItemName(UDateTimePatternField field, const UnicodeString& value) {
928 appendItemNames[field] = value;
929 // NUL-terminate for the C API.
930 appendItemNames[field].getTerminatedBuffer();
934 DateTimePatternGenerator::getAppendItemName(UDateTimePatternField field) const {
935 return appendItemNames[field];
939 DateTimePatternGenerator::getMutableAppendItemName(UDateTimePatternField field) {
940 return appendItemNames[field];
944 DateTimePatternGenerator::getAppendName(UDateTimePatternField field, UnicodeString& value) {
945 value = SINGLE_QUOTE;
946 value += appendItemNames[field];
947 value += SINGLE_QUOTE;
951 DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UErrorCode& status) {
952 return getBestPattern(patternForm, UDATPG_MATCH_NO_OPTIONS, status);
956 DateTimePatternGenerator::getBestPattern(const UnicodeString& patternForm, UDateTimePatternMatchOptions options, UErrorCode& status) {
957 const UnicodeString *bestPattern=NULL;
958 UnicodeString dtFormat;
959 UnicodeString resultPattern;
960 int32_t flags = kDTPGNoFlags;
962 int32_t dateMask=(1<<UDATPG_DAYPERIOD_FIELD) - 1;
963 int32_t timeMask=(1<<UDATPG_FIELD_COUNT) - 1 - dateMask;
965 // Replace hour metacharacters 'j', 'C' and 'J', set flags as necessary
966 UnicodeString patternFormCopy = UnicodeString(patternForm);
967 int32_t patPos, patLen = patternFormCopy.length();
968 UBool inQuoted = FALSE;
969 for (patPos = 0; patPos < patLen; patPos++) {
970 UChar patChr = patternFormCopy.charAt(patPos);
971 if (patChr == SINGLE_QUOTE) {
972 inQuoted = !inQuoted;
973 } else if (!inQuoted) {
974 if (patChr == LOW_J) {
975 patternFormCopy.setCharAt(patPos, fDefaultHourFormatChar);
976 } else if (patChr == CAP_C) {
977 AllowedHourFormat preferred;
978 if (fAllowedHourFormats[0] != ALLOWED_HOUR_FORMAT_UNKNOWN) {
979 preferred = (AllowedHourFormat)fAllowedHourFormats[0];
981 status = U_INVALID_FORMAT_ERROR;
982 return UnicodeString();
985 if (preferred == ALLOWED_HOUR_FORMAT_H || preferred == ALLOWED_HOUR_FORMAT_HB || preferred == ALLOWED_HOUR_FORMAT_Hb) {
986 patternFormCopy.setCharAt(patPos, CAP_H);
988 patternFormCopy.setCharAt(patPos, LOW_H);
991 if (preferred == ALLOWED_HOUR_FORMAT_HB || preferred == ALLOWED_HOUR_FORMAT_hB) {
992 flags |= kDTPGSkeletonUsesCapB;
993 } else if (preferred == ALLOWED_HOUR_FORMAT_Hb || preferred == ALLOWED_HOUR_FORMAT_hb) {
994 flags |= kDTPGSkeletonUsesLowB;
996 } else if (patChr == CAP_J) {
997 // Get pattern for skeleton with H, then replace H or k
998 // with fDefaultHourFormatChar (if different)
999 patternFormCopy.setCharAt(patPos, CAP_H);
1000 flags |= kDTPGSkeletonUsesCapJ;
1005 resultPattern.remove();
1006 dtMatcher->set(patternFormCopy, fp);
1007 const PtnSkeleton* specifiedSkeleton=NULL;
1008 bestPattern=getBestRaw(*dtMatcher, -1, distanceInfo, &specifiedSkeleton);
1009 if ( distanceInfo->missingFieldMask==0 && distanceInfo->extraFieldMask==0 ) {
1010 resultPattern = adjustFieldTypes(*bestPattern, specifiedSkeleton, flags, options);
1012 return resultPattern;
1014 int32_t neededFields = dtMatcher->getFieldMask();
1015 UnicodeString datePattern=getBestAppending(neededFields & dateMask, flags, options);
1016 UnicodeString timePattern=getBestAppending(neededFields & timeMask, flags, options);
1017 if (datePattern.length()==0) {
1018 if (timePattern.length()==0) {
1019 resultPattern.remove();
1025 if (timePattern.length()==0) {
1028 resultPattern.remove();
1029 status = U_ZERO_ERROR;
1030 dtFormat=getDateTimeFormat();
1031 SimpleFormatter(dtFormat, 2, 2, status).format(timePattern, datePattern, resultPattern, status);
1032 return resultPattern;
1036 DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern,
1037 const UnicodeString& skeleton,
1038 UErrorCode& status) {
1039 return replaceFieldTypes(pattern, skeleton, UDATPG_MATCH_NO_OPTIONS, status);
1043 DateTimePatternGenerator::replaceFieldTypes(const UnicodeString& pattern,
1044 const UnicodeString& skeleton,
1045 UDateTimePatternMatchOptions options,
1046 UErrorCode& /*status*/) {
1047 dtMatcher->set(skeleton, fp);
1048 UnicodeString result = adjustFieldTypes(pattern, NULL, kDTPGNoFlags, options);
1053 DateTimePatternGenerator::setDecimal(const UnicodeString& newDecimal) {
1054 this->decimal = newDecimal;
1055 // NUL-terminate for the C API.
1056 this->decimal.getTerminatedBuffer();
1059 const UnicodeString&
1060 DateTimePatternGenerator::getDecimal() const {
1065 DateTimePatternGenerator::addCanonicalItems(UErrorCode& status) {
1066 if (U_FAILURE(status)) { return; }
1067 UnicodeString conflictingPattern;
1069 for (int32_t i=0; i<UDATPG_FIELD_COUNT; i++) {
1070 if (Canonical_Items[i] > 0) {
1071 addPattern(UnicodeString(Canonical_Items[i]), FALSE, conflictingPattern, status);
1073 if (U_FAILURE(status)) { return; }
1078 DateTimePatternGenerator::setDateTimeFormat(const UnicodeString& dtFormat) {
1079 dateTimeFormat = dtFormat;
1080 // NUL-terminate for the C API.
1081 dateTimeFormat.getTerminatedBuffer();
1084 const UnicodeString&
1085 DateTimePatternGenerator::getDateTimeFormat() const {
1086 return dateTimeFormat;
1090 DateTimePatternGenerator::setDateTimeFromCalendar(const Locale& locale, UErrorCode& status) {
1091 const UChar *resStr;
1092 int32_t resStrLen = 0;
1094 Calendar* fCalendar = Calendar::createInstance(locale, status);
1095 if (U_FAILURE(status)) { return; }
1097 LocalUResourceBundlePointer calData(ures_open(NULL, locale.getBaseName(), &status));
1098 ures_getByKey(calData.getAlias(), DT_DateTimeCalendarTag, calData.getAlias(), &status);
1100 LocalUResourceBundlePointer dateTimePatterns;
1101 if (fCalendar != NULL && fCalendar->getType() != NULL && *fCalendar->getType() != '\0'
1102 && uprv_strcmp(fCalendar->getType(), DT_DateTimeGregorianTag) != 0) {
1103 dateTimePatterns.adoptInstead(ures_getByKeyWithFallback(calData.getAlias(), fCalendar->getType(),
1105 ures_getByKeyWithFallback(dateTimePatterns.getAlias(), DT_DateTimePatternsTag,
1106 dateTimePatterns.getAlias(), &status);
1109 if (dateTimePatterns.isNull() || status == U_MISSING_RESOURCE_ERROR) {
1110 status = U_ZERO_ERROR;
1111 dateTimePatterns.adoptInstead(ures_getByKeyWithFallback(calData.getAlias(), DT_DateTimeGregorianTag,
1112 dateTimePatterns.orphan(), &status));
1113 ures_getByKeyWithFallback(dateTimePatterns.getAlias(), DT_DateTimePatternsTag,
1114 dateTimePatterns.getAlias(), &status);
1116 if (U_FAILURE(status)) { return; }
1118 if (ures_getSize(dateTimePatterns.getAlias()) <= DateFormat::kDateTime)
1120 status = U_INVALID_FORMAT_ERROR;
1123 resStr = ures_getStringByIndex(dateTimePatterns.getAlias(), (int32_t)DateFormat::kDateTime, &resStrLen, &status);
1124 setDateTimeFormat(UnicodeString(TRUE, resStr, resStrLen));
1130 DateTimePatternGenerator::setDecimalSymbols(const Locale& locale, UErrorCode& status) {
1131 DecimalFormatSymbols dfs = DecimalFormatSymbols(locale, status);
1132 if(U_SUCCESS(status)) {
1133 decimal = dfs.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
1134 // NUL-terminate for the C API.
1135 decimal.getTerminatedBuffer();
1139 UDateTimePatternConflict
1140 DateTimePatternGenerator::addPattern(
1141 const UnicodeString& pattern,
1143 UnicodeString &conflictingPattern,
1146 return addPatternWithSkeleton(pattern, NULL, override, conflictingPattern, status);
1149 // For DateTimePatternGenerator::addPatternWithSkeleton -
1150 // If skeletonToUse is specified, then an availableFormats entry is being added. In this case:
1151 // 1. We pass that skeleton to matcher.set instead of having it derive a skeleton from the pattern.
1152 // 2. If the new entry's skeleton or basePattern does match an existing entry but that entry also had a skeleton specified
1153 // (i.e. it was also from availableFormats), then the new entry does not override it regardless of the value of the override
1154 // parameter. This prevents later availableFormats entries from a parent locale overriding earlier ones from the actual
1155 // specified locale. However, availableFormats entries *should* override entries with matching skeleton whose skeleton was
1156 // derived (i.e. entries derived from the standard date/time patters for the specified locale).
1157 // 3. When adding the pattern (patternMap->add), we set a new boolean to indicate that the added entry had a
1158 // specified skeleton (which sets a new field in the PtnElem in the PatternMap).
1159 UDateTimePatternConflict
1160 DateTimePatternGenerator::addPatternWithSkeleton(
1161 const UnicodeString& pattern,
1162 const UnicodeString* skeletonToUse,
1164 UnicodeString& conflictingPattern,
1168 UnicodeString basePattern;
1169 PtnSkeleton skeleton;
1170 UDateTimePatternConflict conflictingStatus = UDATPG_NO_CONFLICT;
1172 DateTimeMatcher matcher;
1173 if ( skeletonToUse == NULL ) {
1174 matcher.set(pattern, fp, skeleton);
1175 matcher.getBasePattern(basePattern);
1177 matcher.set(*skeletonToUse, fp, skeleton); // no longer trims skeleton fields to max len 3, per #7930
1178 matcher.getBasePattern(basePattern); // or perhaps instead: basePattern = *skeletonToUse;
1180 // We only care about base conflicts - and replacing the pattern associated with a base - if:
1181 // 1. the conflicting previous base pattern did *not* have an explicit skeleton; in that case the previous
1182 // base + pattern combination was derived from either (a) a canonical item, (b) a standard format, or
1183 // (c) a pattern specified programmatically with a previous call to addPattern (which would only happen
1184 // if we are getting here from a subsequent call to addPattern).
1185 // 2. a skeleton is specified for the current pattern, but override=false; in that case we are checking
1186 // availableFormats items from root, which should not override any previous entry with the same base.
1187 UBool entryHadSpecifiedSkeleton;
1188 const UnicodeString *duplicatePattern = patternMap->getPatternFromBasePattern(basePattern, entryHadSpecifiedSkeleton);
1189 if (duplicatePattern != NULL && (!entryHadSpecifiedSkeleton || (skeletonToUse != NULL && !override))) {
1190 conflictingStatus = UDATPG_BASE_CONFLICT;
1191 conflictingPattern = *duplicatePattern;
1193 return conflictingStatus;
1196 // The only time we get here with override=true and skeletonToUse!=null is when adding availableFormats
1197 // items from CLDR data. In that case, we don't want an item from a parent locale to replace an item with
1198 // same skeleton from the specified locale, so skip the current item if skeletonWasSpecified is true for
1199 // the previously-specified conflicting item.
1200 const PtnSkeleton* entrySpecifiedSkeleton = NULL;
1201 duplicatePattern = patternMap->getPatternFromSkeleton(skeleton, &entrySpecifiedSkeleton);
1202 if (duplicatePattern != NULL ) {
1203 conflictingStatus = UDATPG_CONFLICT;
1204 conflictingPattern = *duplicatePattern;
1205 if (!override || (skeletonToUse != NULL && entrySpecifiedSkeleton != NULL)) {
1206 return conflictingStatus;
1209 patternMap->add(basePattern, skeleton, pattern, skeletonToUse != NULL, status);
1210 if(U_FAILURE(status)) {
1211 return conflictingStatus;
1214 return UDATPG_NO_CONFLICT;
1218 UDateTimePatternField
1219 DateTimePatternGenerator::getAppendFormatNumber(const char* field) const {
1220 for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
1221 if (uprv_strcmp(CLDR_FIELD_APPEND[i], field)==0) {
1222 return (UDateTimePatternField)i;
1225 return UDATPG_FIELD_COUNT;
1228 UDateTimePatternField
1229 DateTimePatternGenerator::getAppendNameNumber(const char* field) const {
1230 for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
1231 if (uprv_strcmp(CLDR_FIELD_NAME[i],field)==0) {
1232 return (UDateTimePatternField)i;
1235 return UDATPG_FIELD_COUNT;
1238 const UnicodeString*
1239 DateTimePatternGenerator::getBestRaw(DateTimeMatcher& source,
1240 int32_t includeMask,
1241 DistanceInfo* missingFields,
1242 const PtnSkeleton** specifiedSkeletonPtr) {
1243 int32_t bestDistance = 0x7fffffff;
1244 DistanceInfo tempInfo;
1245 const UnicodeString *bestPattern=NULL;
1246 const PtnSkeleton* specifiedSkeleton=NULL;
1248 PatternMapIterator it;
1249 for (it.set(*patternMap); it.hasNext(); ) {
1250 DateTimeMatcher trial = it.next();
1251 if (trial.equals(skipMatcher)) {
1254 int32_t distance=source.getDistance(trial, includeMask, tempInfo);
1255 if (distance<bestDistance) {
1256 bestDistance=distance;
1257 bestPattern=patternMap->getPatternFromSkeleton(*trial.getSkeletonPtr(), &specifiedSkeleton);
1258 missingFields->setTo(tempInfo);
1265 // If the best raw match had a specified skeleton and that skeleton was requested by the caller,
1266 // then return it too. This generally happens when the caller needs to pass that skeleton
1267 // through to adjustFieldTypes so the latter can do a better job.
1268 if (bestPattern && specifiedSkeletonPtr) {
1269 *specifiedSkeletonPtr = specifiedSkeleton;
1275 DateTimePatternGenerator::adjustFieldTypes(const UnicodeString& pattern,
1276 const PtnSkeleton* specifiedSkeleton,
1278 UDateTimePatternMatchOptions options) {
1279 UnicodeString newPattern;
1281 for (int32_t i=0; i < fp->itemNumber; i++) {
1282 UnicodeString field = fp->items[i];
1283 if ( fp->isQuoteLiteral(field) ) {
1285 UnicodeString quoteLiteral;
1286 fp->getQuoteLiteral(quoteLiteral, &i);
1287 newPattern += quoteLiteral;
1290 if (fp->isPatternSeparator(field)) {
1294 int32_t canonicalIndex = fp->getCanonicalIndex(field);
1295 if (canonicalIndex < 0) {
1297 continue; // don't adjust
1299 const dtTypeElem *row = &dtTypes[canonicalIndex];
1300 int32_t typeValue = row->field;
1302 // Handle special day periods.
1303 if (typeValue == UDATPG_DAYPERIOD_FIELD && flags != 0) {
1304 UChar c = NONE; // '0'
1305 if (flags & kDTPGSkeletonUsesCapB) { c = CAP_B; }
1306 if (flags & kDTPGSkeletonUsesLowB) { c = LOW_B; }
1309 for (int32_t i = 0; i < field.length(); ++i)
1310 field.setCharAt(i, c);
1314 if ((flags & kDTPGFixFractionalSeconds) != 0 && typeValue == UDATPG_SECOND_FIELD) {
1316 dtMatcher->skeleton.original.appendFieldTo(UDATPG_FRACTIONAL_SECOND_FIELD, field);
1317 } else if (dtMatcher->skeleton.type[typeValue]!=0) {
1319 // - "reqField" is the field from the originally requested skeleton, with length
1321 // - "field" is the field from the found pattern.
1323 // The adjusted field should consist of characters from the originally requested
1324 // skeleton, except in the case of UDATPG_HOUR_FIELD or UDATPG_MONTH_FIELD or
1325 // UDATPG_WEEKDAY_FIELD or UDATPG_YEAR_FIELD, in which case it should consist
1326 // of characters from the found pattern.
1328 // The length of the adjusted field (adjFieldLen) should match that in the originally
1329 // requested skeleton, except that in the following cases the length of the adjusted field
1330 // should match that in the found pattern (i.e. the length of this pattern field should
1331 // not be adjusted):
1332 // 1. typeValue is UDATPG_HOUR_FIELD/MINUTE/SECOND and the corresponding bit in options is
1333 // not set (ticket #7180). Note, we may want to implement a similar change for other
1334 // numeric fields (MM, dd, etc.) so the default behavior is to get locale preference for
1335 // field length, but options bits can be used to override this.
1336 // 2. There is a specified skeleton for the found pattern and one of the following is true:
1337 // a) The length of the field in the skeleton (skelFieldLen) is equal to reqFieldLen.
1338 // b) The pattern field is numeric and the skeleton field is not, or vice versa.
1340 UChar reqFieldChar = dtMatcher->skeleton.original.getFieldChar(typeValue);
1341 int32_t reqFieldLen = dtMatcher->skeleton.original.getFieldLength(typeValue);
1342 if (reqFieldChar == CAP_E && reqFieldLen < 3)
1343 reqFieldLen = 3; // 1-3 for E are equivalent to 3 for c,e
1344 int32_t adjFieldLen = reqFieldLen;
1345 if ( (typeValue==UDATPG_HOUR_FIELD && (options & UDATPG_MATCH_HOUR_FIELD_LENGTH)==0) ||
1346 (typeValue==UDATPG_MINUTE_FIELD && (options & UDATPG_MATCH_MINUTE_FIELD_LENGTH)==0) ||
1347 (typeValue==UDATPG_SECOND_FIELD && (options & UDATPG_MATCH_SECOND_FIELD_LENGTH)==0) ) {
1348 adjFieldLen = field.length();
1349 } else if (specifiedSkeleton) {
1350 int32_t skelFieldLen = specifiedSkeleton->original.getFieldLength(typeValue);
1351 UBool patFieldIsNumeric = (row->type > 0);
1352 UBool skelFieldIsNumeric = (specifiedSkeleton->type[typeValue] > 0);
1353 if (skelFieldLen == reqFieldLen || (patFieldIsNumeric && !skelFieldIsNumeric) || (skelFieldIsNumeric && !patFieldIsNumeric)) {
1354 // don't adjust the field length in the found pattern
1355 adjFieldLen = field.length();
1358 UChar c = (typeValue!= UDATPG_HOUR_FIELD
1359 && typeValue!= UDATPG_MONTH_FIELD
1360 && typeValue!= UDATPG_WEEKDAY_FIELD
1361 && (typeValue!= UDATPG_YEAR_FIELD || reqFieldChar==CAP_Y))
1364 if (typeValue == UDATPG_HOUR_FIELD && (flags & kDTPGSkeletonUsesCapJ) != 0) {
1365 c = fDefaultHourFormatChar;
1368 for (int32_t i=adjFieldLen; i>0; --i) {
1379 DateTimePatternGenerator::getBestAppending(int32_t missingFields, int32_t flags, UDateTimePatternMatchOptions options) {
1380 UnicodeString resultPattern, tempPattern;
1381 UErrorCode err=U_ZERO_ERROR;
1382 int32_t lastMissingFieldMask=0;
1383 if (missingFields!=0) {
1384 resultPattern=UnicodeString();
1385 const PtnSkeleton* specifiedSkeleton=NULL;
1386 tempPattern = *getBestRaw(*dtMatcher, missingFields, distanceInfo, &specifiedSkeleton);
1387 resultPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, flags, options);
1388 if ( distanceInfo->missingFieldMask==0 ) {
1389 return resultPattern;
1391 while (distanceInfo->missingFieldMask!=0) { // precondition: EVERY single field must work!
1392 if ( lastMissingFieldMask == distanceInfo->missingFieldMask ) {
1393 break; // cannot find the proper missing field
1395 if (((distanceInfo->missingFieldMask & UDATPG_SECOND_AND_FRACTIONAL_MASK)==UDATPG_FRACTIONAL_MASK) &&
1396 ((missingFields & UDATPG_SECOND_AND_FRACTIONAL_MASK) == UDATPG_SECOND_AND_FRACTIONAL_MASK)) {
1397 resultPattern = adjustFieldTypes(resultPattern, specifiedSkeleton, flags | kDTPGFixFractionalSeconds, options);
1398 distanceInfo->missingFieldMask &= ~UDATPG_FRACTIONAL_MASK;
1401 int32_t startingMask = distanceInfo->missingFieldMask;
1402 tempPattern = *getBestRaw(*dtMatcher, distanceInfo->missingFieldMask, distanceInfo, &specifiedSkeleton);
1403 tempPattern = adjustFieldTypes(tempPattern, specifiedSkeleton, flags, options);
1404 int32_t foundMask=startingMask& ~distanceInfo->missingFieldMask;
1405 int32_t topField=getTopBitNumber(foundMask);
1406 UnicodeString appendName;
1407 getAppendName((UDateTimePatternField)topField, appendName);
1408 const UnicodeString *values[3] = {
1413 SimpleFormatter(appendItemFormats[topField], 2, 3, err).
1414 formatAndReplace(values, 3, resultPattern, NULL, 0, err);
1415 lastMissingFieldMask = distanceInfo->missingFieldMask;
1418 return resultPattern;
1422 DateTimePatternGenerator::getTopBitNumber(int32_t foundMask) {
1423 if ( foundMask==0 ) {
1427 while (foundMask!=0) {
1431 if (i-1 >UDATPG_ZONE_FIELD) {
1432 return UDATPG_ZONE_FIELD;
1439 DateTimePatternGenerator::setAvailableFormat(const UnicodeString &key, UErrorCode& err)
1441 fAvailableFormatKeyHash->puti(key, 1, err);
1445 DateTimePatternGenerator::isAvailableFormatSet(const UnicodeString &key) const {
1446 return (UBool)(fAvailableFormatKeyHash->geti(key) == 1);
1450 DateTimePatternGenerator::copyHashtable(Hashtable *other, UErrorCode &status) {
1452 if (other == NULL) {
1455 if (fAvailableFormatKeyHash != NULL) {
1456 delete fAvailableFormatKeyHash;
1457 fAvailableFormatKeyHash = NULL;
1459 initHashtable(status);
1460 if(U_FAILURE(status)){
1463 int32_t pos = UHASH_FIRST;
1464 const UHashElement* elem = NULL;
1465 // walk through the hash table and create a deep clone
1466 while((elem = other->nextElement(pos))!= NULL){
1467 const UHashTok otherKeyTok = elem->key;
1468 UnicodeString* otherKey = (UnicodeString*)otherKeyTok.pointer;
1469 fAvailableFormatKeyHash->puti(*otherKey, 1, status);
1470 if(U_FAILURE(status)){
1477 DateTimePatternGenerator::getSkeletons(UErrorCode& status) const {
1478 StringEnumeration* skeletonEnumerator = new DTSkeletonEnumeration(*patternMap, DT_SKELETON, status);
1479 return skeletonEnumerator;
1482 const UnicodeString&
1483 DateTimePatternGenerator::getPatternForSkeleton(const UnicodeString& skeleton) const {
1486 if (skeleton.length() ==0) {
1489 curElem = patternMap->getHeader(skeleton.charAt(0));
1490 while ( curElem != NULL ) {
1491 if ( curElem->skeleton->getSkeleton()==skeleton ) {
1492 return curElem->pattern;
1494 curElem=curElem->next;
1500 DateTimePatternGenerator::getBaseSkeletons(UErrorCode& status) const {
1501 StringEnumeration* baseSkeletonEnumerator = new DTSkeletonEnumeration(*patternMap, DT_BASESKELETON, status);
1502 return baseSkeletonEnumerator;
1506 DateTimePatternGenerator::getRedundants(UErrorCode& status) {
1507 StringEnumeration* output = new DTRedundantEnumeration();
1508 const UnicodeString *pattern;
1509 PatternMapIterator it;
1510 for (it.set(*patternMap); it.hasNext(); ) {
1511 DateTimeMatcher current = it.next();
1512 pattern = patternMap->getPatternFromSkeleton(*(it.getSkeleton()));
1513 if ( isCanonicalItem(*pattern) ) {
1516 if ( skipMatcher == NULL ) {
1517 skipMatcher = new DateTimeMatcher(current);
1520 *skipMatcher = current;
1522 UnicodeString trial = getBestPattern(current.getPattern(), status);
1523 if (trial == *pattern) {
1524 ((DTRedundantEnumeration *)output)->add(*pattern, status);
1526 if (current.equals(skipMatcher)) {
1534 DateTimePatternGenerator::isCanonicalItem(const UnicodeString& item) const {
1535 if ( item.length() != 1 ) {
1538 for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
1539 if (item.charAt(0)==Canonical_Items[i]) {
1547 DateTimePatternGenerator*
1548 DateTimePatternGenerator::clone() const {
1549 return new DateTimePatternGenerator(*this);
1552 PatternMap::PatternMap() {
1553 for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
1556 isDupAllowed = TRUE;
1560 PatternMap::copyFrom(const PatternMap& other, UErrorCode& status) {
1561 this->isDupAllowed = other.isDupAllowed;
1562 for (int32_t bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
1563 PtnElem *curElem, *otherElem, *prevElem=NULL;
1564 otherElem = other.boot[bootIndex];
1565 while (otherElem!=NULL) {
1566 if ((curElem = new PtnElem(otherElem->basePattern, otherElem->pattern))==NULL) {
1568 status = U_MEMORY_ALLOCATION_ERROR;
1571 if ( this->boot[bootIndex]== NULL ) {
1572 this->boot[bootIndex] = curElem;
1574 if ((curElem->skeleton=new PtnSkeleton(*(otherElem->skeleton))) == NULL ) {
1576 status = U_MEMORY_ALLOCATION_ERROR;
1579 curElem->skeletonWasSpecified = otherElem->skeletonWasSpecified;
1580 if (prevElem!=NULL) {
1581 prevElem->next=curElem;
1585 otherElem = otherElem->next;
1592 PatternMap::getHeader(UChar baseChar) {
1595 if ( (baseChar >= CAP_A) && (baseChar <= CAP_Z) ) {
1596 curElem = boot[baseChar-CAP_A];
1599 if ( (baseChar >=LOW_A) && (baseChar <= LOW_Z) ) {
1600 curElem = boot[26+baseChar-LOW_A];
1609 PatternMap::~PatternMap() {
1610 for (int32_t i=0; i < MAX_PATTERN_ENTRIES; ++i ) {
1611 if (boot[i]!=NULL ) {
1616 } // PatternMap destructor
1619 PatternMap::add(const UnicodeString& basePattern,
1620 const PtnSkeleton& skeleton,
1621 const UnicodeString& value,// mapped pattern value
1622 UBool skeletonWasSpecified,
1623 UErrorCode &status) {
1624 UChar baseChar = basePattern.charAt(0);
1625 PtnElem *curElem, *baseElem;
1626 status = U_ZERO_ERROR;
1628 // the baseChar must be A-Z or a-z
1629 if ((baseChar >= CAP_A) && (baseChar <= CAP_Z)) {
1630 baseElem = boot[baseChar-CAP_A];
1633 if ((baseChar >=LOW_A) && (baseChar <= LOW_Z)) {
1634 baseElem = boot[26+baseChar-LOW_A];
1637 status = U_ILLEGAL_CHARACTER;
1642 if (baseElem == NULL) {
1643 if ((curElem = new PtnElem(basePattern, value)) == NULL ) {
1645 status = U_MEMORY_ALLOCATION_ERROR;
1648 if (baseChar >= LOW_A) {
1649 boot[26 + (baseChar-LOW_A)] = curElem;
1652 boot[baseChar-CAP_A] = curElem;
1654 curElem->skeleton = new PtnSkeleton(skeleton);
1655 curElem->skeletonWasSpecified = skeletonWasSpecified;
1657 if ( baseElem != NULL ) {
1658 curElem = getDuplicateElem(basePattern, skeleton, baseElem);
1660 if (curElem == NULL) {
1661 // add new element to the list.
1663 while( curElem -> next != NULL )
1665 curElem = curElem->next;
1667 if ((curElem->next = new PtnElem(basePattern, value)) == NULL ) {
1669 status = U_MEMORY_ALLOCATION_ERROR;
1672 curElem=curElem->next;
1673 curElem->skeleton = new PtnSkeleton(skeleton);
1674 curElem->skeletonWasSpecified = skeletonWasSpecified;
1677 // Pattern exists in the list already.
1678 if ( !isDupAllowed ) {
1681 // Overwrite the value.
1682 curElem->pattern = value;
1683 // It was a bug that we were not doing the following previously,
1684 // though that bug hid other problems by making things partly work.
1685 curElem->skeletonWasSpecified = skeletonWasSpecified;
1688 } // PatternMap::add
1690 // Find the pattern from the given basePattern string.
1691 const UnicodeString *
1692 PatternMap::getPatternFromBasePattern(UnicodeString& basePattern, UBool& skeletonWasSpecified) { // key to search for
1695 if ((curElem=getHeader(basePattern.charAt(0)))==NULL) {
1696 return NULL; // no match
1700 if ( basePattern.compare(curElem->basePattern)==0 ) {
1701 skeletonWasSpecified = curElem->skeletonWasSpecified;
1702 return &(curElem->pattern);
1704 curElem=curElem->next;
1705 }while (curElem != NULL);
1708 } // PatternMap::getFromBasePattern
1711 // Find the pattern from the given skeleton.
1712 // At least when this is called from getBestRaw & addPattern (in which case specifiedSkeletonPtr is non-NULL),
1713 // the comparison should be based on skeleton.original (which is unique and tied to the distance measurement in bestRaw)
1714 // and not skeleton.baseOriginal (which is not unique); otherwise we may pick a different skeleton than the one with the
1715 // optimum distance value in getBestRaw. When this is called from public getRedundants (specifiedSkeletonPtr is NULL),
1716 // for now it will continue to compare based on baseOriginal so as not to change the behavior unnecessarily.
1717 const UnicodeString *
1718 PatternMap::getPatternFromSkeleton(PtnSkeleton& skeleton, const PtnSkeleton** specifiedSkeletonPtr) { // key to search for
1721 if (specifiedSkeletonPtr) {
1722 *specifiedSkeletonPtr = NULL;
1726 UChar baseChar = skeleton.getFirstChar();
1727 if ((curElem=getHeader(baseChar))==NULL) {
1728 return NULL; // no match
1733 if (specifiedSkeletonPtr != NULL) { // called from DateTimePatternGenerator::getBestRaw or addPattern, use original
1734 equal = curElem->skeleton->original == skeleton.original;
1735 } else { // called from DateTimePatternGenerator::getRedundants, use baseOriginal
1736 equal = curElem->skeleton->baseOriginal == skeleton.baseOriginal;
1739 if (specifiedSkeletonPtr && curElem->skeletonWasSpecified) {
1740 *specifiedSkeletonPtr = curElem->skeleton;
1742 return &(curElem->pattern);
1744 curElem=curElem->next;
1745 }while (curElem != NULL);
1751 PatternMap::equals(const PatternMap& other) {
1752 if ( this==&other ) {
1755 for (int32_t bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
1756 if ( boot[bootIndex]==other.boot[bootIndex] ) {
1759 if ( (boot[bootIndex]==NULL)||(other.boot[bootIndex]==NULL) ) {
1762 PtnElem *otherElem = other.boot[bootIndex];
1763 PtnElem *myElem = boot[bootIndex];
1764 while ((otherElem!=NULL) || (myElem!=NULL)) {
1765 if ( myElem == otherElem ) {
1768 if ((otherElem==NULL) || (myElem==NULL)) {
1771 if ( (myElem->basePattern != otherElem->basePattern) ||
1772 (myElem->pattern != otherElem->pattern) ) {
1775 if ((myElem->skeleton!=otherElem->skeleton)&&
1776 !myElem->skeleton->equals(*(otherElem->skeleton))) {
1779 myElem = myElem->next;
1780 otherElem=otherElem->next;
1786 // find any key existing in the mapping table already.
1787 // return TRUE if there is an existing key, otherwise return FALSE.
1789 PatternMap::getDuplicateElem(
1790 const UnicodeString &basePattern,
1791 const PtnSkeleton &skeleton,
1792 PtnElem *baseElem) {
1795 if ( baseElem == (PtnElem *)NULL ) {
1796 return (PtnElem*)NULL;
1802 if ( basePattern.compare(curElem->basePattern)==0 ) {
1804 for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
1805 if (curElem->skeleton->type[i] != skeleton.type[i] ) {
1814 curElem = curElem->next;
1815 } while( curElem != (PtnElem *)NULL );
1818 return (PtnElem*)NULL;
1820 } // PatternMap::getDuplicateElem
1822 DateTimeMatcher::DateTimeMatcher(void) {
1825 DateTimeMatcher::~DateTimeMatcher() {}
1827 DateTimeMatcher::DateTimeMatcher(const DateTimeMatcher& other) {
1828 copyFrom(other.skeleton);
1833 DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp) {
1834 PtnSkeleton localSkeleton;
1835 return set(pattern, fp, localSkeleton);
1839 DateTimeMatcher::set(const UnicodeString& pattern, FormatParser* fp, PtnSkeleton& skeletonResult) {
1841 for (i=0; i<UDATPG_FIELD_COUNT; ++i) {
1842 skeletonResult.type[i] = NONE;
1845 for (i=0; i < fp->itemNumber; i++) {
1846 const UnicodeString& value = fp->items[i];
1847 if ( value.charAt(0) == LOW_A ) {
1848 continue; // skip 'a'
1851 if ( fp->isQuoteLiteral(value) ) {
1852 UnicodeString quoteLiteral;
1853 fp->getQuoteLiteral(quoteLiteral, &i);
1856 int32_t canonicalIndex = fp->getCanonicalIndex(value);
1857 if (canonicalIndex < 0 ) {
1860 const dtTypeElem *row = &dtTypes[canonicalIndex];
1861 int32_t field = row->field;
1862 skeletonResult.original.populate(field, value);
1863 UChar repeatChar = row->patternChar;
1864 int32_t repeatCount = row->minLen; // #7930 removes cap at 3
1865 skeletonResult.baseOriginal.populate(field, repeatChar, repeatCount);
1866 int16_t subField = row->type;
1867 if ( row->type > 0) {
1868 subField += value.length();
1870 skeletonResult.type[field] = subField;
1872 copyFrom(skeletonResult);
1876 DateTimeMatcher::getBasePattern(UnicodeString &result ) {
1877 result.remove(); // Reset the result first.
1878 skeleton.baseOriginal.appendTo(result);
1882 DateTimeMatcher::getPattern() {
1883 UnicodeString result;
1884 return skeleton.original.appendTo(result);
1888 DateTimeMatcher::getDistance(const DateTimeMatcher& other, int32_t includeMask, DistanceInfo& distanceInfo) {
1890 distanceInfo.clear();
1891 for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i ) {
1892 int32_t myType = (includeMask&(1<<i))==0 ? 0 : skeleton.type[i];
1893 int32_t otherType = other.skeleton.type[i];
1894 if (myType==otherType) {
1897 if (myType==0) {// and other is not
1898 result += EXTRA_FIELD;
1899 distanceInfo.addExtra(i);
1903 result += MISSING_FIELD;
1904 distanceInfo.addMissing(i);
1907 result += abs(myType - otherType);
1916 DateTimeMatcher::copyFrom(const PtnSkeleton& newSkeleton) {
1917 skeleton.copyFrom(newSkeleton);
1921 DateTimeMatcher::copyFrom() {
1927 DateTimeMatcher::equals(const DateTimeMatcher* other) const {
1928 if (other==NULL) { return FALSE; }
1929 return skeleton.original == other->skeleton.original;
1933 DateTimeMatcher::getFieldMask() {
1936 for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
1937 if (skeleton.type[i]!=0) {
1945 DateTimeMatcher::getSkeletonPtr() {
1949 FormatParser::FormatParser () {
1955 FormatParser::~FormatParser () {
1959 // Find the next token with the starting position and length
1960 // Note: the startPos may
1961 FormatParser::TokenStatus
1962 FormatParser::setTokens(const UnicodeString& pattern, int32_t startPos, int32_t *len) {
1963 int32_t curLoc = startPos;
1964 if ( curLoc >= pattern.length()) {
1967 // check the current char is between A-Z or a-z
1969 UChar c=pattern.charAt(curLoc);
1970 if ( (c>=CAP_A && c<=CAP_Z) || (c>=LOW_A && c<=LOW_Z) ) {
1979 if ( pattern.charAt(curLoc)!= pattern.charAt(startPos) ) {
1980 break; // not the same token
1982 } while(curLoc <= pattern.length());
1983 *len = curLoc-startPos;
1988 FormatParser::set(const UnicodeString& pattern) {
1990 TokenStatus result=START;
1995 result = setTokens( pattern, startPos, &len );
1996 if ( result == ADD_TOKEN )
1998 items[itemNumber++] = UnicodeString(pattern, startPos, len );
2004 } while (result==ADD_TOKEN && itemNumber < MAX_DT_TOKEN);
2008 FormatParser::getCanonicalIndex(const UnicodeString& s, UBool strict) {
2009 int32_t len = s.length();
2013 UChar ch = s.charAt(0);
2015 // Verify that all are the same character.
2016 for (int32_t l = 1; l < len; l++) {
2017 if (ch != s.charAt(l)) {
2022 int32_t bestRow = -1;
2023 while (dtTypes[i].patternChar != 0x0000) {
2024 if ( dtTypes[i].patternChar != ch ) {
2029 if (dtTypes[i].patternChar != dtTypes[i+1].patternChar) {
2032 if (dtTypes[i+1].minLen <= len) {
2038 return strict ? -1 : bestRow;
2042 FormatParser::isQuoteLiteral(const UnicodeString& s) {
2043 return (UBool)(s.charAt(0)==SINGLE_QUOTE);
2046 // This function aussumes the current itemIndex points to the quote literal.
2047 // Please call isQuoteLiteral prior to this function.
2049 FormatParser::getQuoteLiteral(UnicodeString& quote, int32_t *itemIndex) {
2050 int32_t i=*itemIndex;
2053 if (items[i].charAt(0)==SINGLE_QUOTE) {
2057 while ( i < itemNumber ) {
2058 if ( items[i].charAt(0)==SINGLE_QUOTE ) {
2059 if ( (i+1<itemNumber) && (items[i+1].charAt(0)==SINGLE_QUOTE)) {
2060 // two single quotes e.g. 'o''clock'
2061 quote += items[i++];
2062 quote += items[i++];
2079 FormatParser::isPatternSeparator(UnicodeString& field) {
2080 for (int32_t i=0; i<field.length(); ++i ) {
2081 UChar c= field.charAt(i);
2082 if ( (c==SINGLE_QUOTE) || (c==BACKSLASH) || (c==SPACE) || (c==COLON) ||
2083 (c==QUOTATION_MARK) || (c==COMMA) || (c==HYPHEN) ||(items[i].charAt(0)==DOT) ) {
2093 DistanceInfo::~DistanceInfo() {}
2096 DistanceInfo::setTo(DistanceInfo &other) {
2097 missingFieldMask = other.missingFieldMask;
2098 extraFieldMask= other.extraFieldMask;
2101 PatternMapIterator::PatternMapIterator() {
2105 matcher= new DateTimeMatcher();
2109 PatternMapIterator::~PatternMapIterator() {
2114 PatternMapIterator::set(PatternMap& newPatternMap) {
2115 this->patternMap=&newPatternMap;
2119 PatternMapIterator::getSkeleton() {
2120 if ( nodePtr == NULL ) {
2124 return nodePtr->skeleton;
2129 PatternMapIterator::hasNext() {
2130 int32_t headIndex=bootIndex;
2131 PtnElem *curPtr=nodePtr;
2133 if (patternMap==NULL) {
2136 while ( headIndex < MAX_PATTERN_ENTRIES ) {
2137 if ( curPtr != NULL ) {
2138 if ( curPtr->next != NULL ) {
2148 if ( patternMap->boot[headIndex] != NULL ) {
2162 PatternMapIterator::next() {
2163 while ( bootIndex < MAX_PATTERN_ENTRIES ) {
2164 if ( nodePtr != NULL ) {
2165 if ( nodePtr->next != NULL ) {
2166 nodePtr = nodePtr->next;
2176 if ( patternMap->boot[bootIndex] != NULL ) {
2177 nodePtr = patternMap->boot[bootIndex];
2186 if (nodePtr!=NULL) {
2187 matcher->copyFrom(*nodePtr->skeleton);
2190 matcher->copyFrom();
2196 SkeletonFields::SkeletonFields() {
2197 // Set initial values to zero
2201 void SkeletonFields::clear() {
2202 uprv_memset(chars, 0, sizeof(chars));
2203 uprv_memset(lengths, 0, sizeof(lengths));
2206 void SkeletonFields::copyFrom(const SkeletonFields& other) {
2207 uprv_memcpy(chars, other.chars, sizeof(chars));
2208 uprv_memcpy(lengths, other.lengths, sizeof(lengths));
2211 void SkeletonFields::clearField(int32_t field) {
2216 UChar SkeletonFields::getFieldChar(int32_t field) const {
2217 return chars[field];
2220 int32_t SkeletonFields::getFieldLength(int32_t field) const {
2221 return lengths[field];
2224 void SkeletonFields::populate(int32_t field, const UnicodeString& value) {
2225 populate(field, value.charAt(0), value.length());
2228 void SkeletonFields::populate(int32_t field, UChar ch, int32_t length) {
2229 chars[field] = (int8_t) ch;
2230 lengths[field] = (int8_t) length;
2233 UBool SkeletonFields::isFieldEmpty(int32_t field) const {
2234 return lengths[field] == 0;
2237 UnicodeString& SkeletonFields::appendTo(UnicodeString& string) const {
2238 for (int32_t i = 0; i < UDATPG_FIELD_COUNT; ++i) {
2239 appendFieldTo(i, string);
2244 UnicodeString& SkeletonFields::appendFieldTo(int32_t field, UnicodeString& string) const {
2245 UChar ch(chars[field]);
2246 int32_t length = (int32_t) lengths[field];
2248 for (int32_t i=0; i<length; i++) {
2254 UChar SkeletonFields::getFirstChar() const {
2255 for (int32_t i = 0; i < UDATPG_FIELD_COUNT; ++i) {
2256 if (lengths[i] != 0) {
2264 PtnSkeleton::PtnSkeleton() {
2267 PtnSkeleton::PtnSkeleton(const PtnSkeleton& other) {
2271 void PtnSkeleton::copyFrom(const PtnSkeleton& other) {
2272 uprv_memcpy(type, other.type, sizeof(type));
2273 original.copyFrom(other.original);
2274 baseOriginal.copyFrom(other.baseOriginal);
2277 void PtnSkeleton::clear() {
2278 uprv_memset(type, 0, sizeof(type));
2280 baseOriginal.clear();
2284 PtnSkeleton::equals(const PtnSkeleton& other) const {
2285 return (original == other.original)
2286 && (baseOriginal == other.baseOriginal)
2287 && (uprv_memcmp(type, other.type, sizeof(type)) == 0);
2291 PtnSkeleton::getSkeleton() const {
2292 UnicodeString result;
2293 return original.appendTo(result);
2297 PtnSkeleton::getBaseSkeleton() const {
2298 UnicodeString result;
2299 return baseOriginal.appendTo(result);
2303 PtnSkeleton::getFirstChar() const {
2304 return baseOriginal.getFirstChar();
2307 PtnSkeleton::~PtnSkeleton() {
2310 PtnElem::PtnElem(const UnicodeString &basePat, const UnicodeString &pat) :
2311 basePattern(basePat),
2318 PtnElem::~PtnElem() {
2326 DTSkeletonEnumeration::DTSkeletonEnumeration(PatternMap &patternMap, dtStrEnum type, UErrorCode& status) {
2328 PtnSkeleton *curSkeleton;
2333 fSkeletons = new UVector(status);
2334 if (U_FAILURE(status)) {
2338 for (bootIndex=0; bootIndex<MAX_PATTERN_ENTRIES; ++bootIndex ) {
2339 curElem = patternMap.boot[bootIndex];
2340 while (curElem!=NULL) {
2342 case DT_BASESKELETON:
2343 s=curElem->basePattern;
2349 curSkeleton=curElem->skeleton;
2350 s=curSkeleton->getSkeleton();
2353 if ( !isCanonicalItem(s) ) {
2354 fSkeletons->addElement(new UnicodeString(s), status);
2355 if (U_FAILURE(status)) {
2361 curElem = curElem->next;
2364 if ((bootIndex==MAX_PATTERN_ENTRIES) && (curElem!=NULL) ) {
2365 status = U_BUFFER_OVERFLOW_ERROR;
2369 const UnicodeString*
2370 DTSkeletonEnumeration::snext(UErrorCode& status) {
2371 if (U_SUCCESS(status) && pos < fSkeletons->size()) {
2372 return (const UnicodeString*)fSkeletons->elementAt(pos++);
2378 DTSkeletonEnumeration::reset(UErrorCode& /*status*/) {
2383 DTSkeletonEnumeration::count(UErrorCode& /*status*/) const {
2384 return (fSkeletons==NULL) ? 0 : fSkeletons->size();
2388 DTSkeletonEnumeration::isCanonicalItem(const UnicodeString& item) {
2389 if ( item.length() != 1 ) {
2392 for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
2393 if (item.charAt(0)==Canonical_Items[i]) {
2400 DTSkeletonEnumeration::~DTSkeletonEnumeration() {
2402 for (int32_t i=0; i<fSkeletons->size(); ++i) {
2403 if ((s=(UnicodeString *)fSkeletons->elementAt(i))!=NULL) {
2410 DTRedundantEnumeration::DTRedundantEnumeration() {
2416 DTRedundantEnumeration::add(const UnicodeString& pattern, UErrorCode& status) {
2417 if (U_FAILURE(status)) return;
2418 if (fPatterns == NULL) {
2419 fPatterns = new UVector(status);
2420 if (U_FAILURE(status)) {
2426 fPatterns->addElement(new UnicodeString(pattern), status);
2427 if (U_FAILURE(status)) {
2434 const UnicodeString*
2435 DTRedundantEnumeration::snext(UErrorCode& status) {
2436 if (U_SUCCESS(status) && pos < fPatterns->size()) {
2437 return (const UnicodeString*)fPatterns->elementAt(pos++);
2443 DTRedundantEnumeration::reset(UErrorCode& /*status*/) {
2448 DTRedundantEnumeration::count(UErrorCode& /*status*/) const {
2449 return (fPatterns==NULL) ? 0 : fPatterns->size();
2453 DTRedundantEnumeration::isCanonicalItem(const UnicodeString& item) {
2454 if ( item.length() != 1 ) {
2457 for (int32_t i=0; i<UDATPG_FIELD_COUNT; ++i) {
2458 if (item.charAt(0)==Canonical_Items[i]) {
2465 DTRedundantEnumeration::~DTRedundantEnumeration() {
2467 for (int32_t i=0; i<fPatterns->size(); ++i) {
2468 if ((s=(UnicodeString *)fPatterns->elementAt(i))!=NULL) {
2478 #endif /* #if !UCONFIG_NO_FORMATTING */