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