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