2 *******************************************************************************
3 * Copyright (C) 2007-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
9 *******************************************************************************
12 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_FORMATTING
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"
37 #include "dtptngen_impl.h"
39 #define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
41 #if U_CHARSET_FAMILY==U_EBCDIC_FAMILY
43 * If we are on EBCDIC, use an iterator which will
44 * traverse the bundles in ASCII order.
46 #define U_USE_ASCII_BUNDLE_ITERATOR
47 #define U_SORT_ASCII_BUNDLE_ITERATOR
50 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
52 #include "unicode/ustring.h"
57 UResourceBundle
*item
;
60 struct UResourceBundleAIterator
{
61 UResourceBundle
*bund
;
67 /* Must be C linkage to pass function pointer to the sort function */
71 static int32_t U_CALLCONV
72 ures_a_codepointSort(const void *context
, const void *left
, const void *right
) {
73 //CompareContext *cmp=(CompareContext *)context;
74 return u_strcmp(((const UResAEntry
*)left
)->key
,
75 ((const UResAEntry
*)right
)->key
);
80 static void ures_a_open(UResourceBundleAIterator
*aiter
, UResourceBundle
*bund
, UErrorCode
*status
) {
81 if(U_FAILURE(*status
)) {
85 aiter
->num
= ures_getSize(aiter
->bund
);
87 #if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
88 aiter
->entries
= NULL
;
90 aiter
->entries
= (UResAEntry
*)uprv_malloc(sizeof(UResAEntry
)*aiter
->num
);
91 for(int i
=0;i
<aiter
->num
;i
++) {
92 aiter
->entries
[i
].item
= ures_getByIndex(aiter
->bund
, i
, NULL
, status
);
93 const char *akey
= ures_getKey(aiter
->entries
[i
].item
);
94 int32_t len
= uprv_strlen(akey
)+1;
95 aiter
->entries
[i
].key
= (UChar
*)uprv_malloc(len
*sizeof(UChar
));
96 u_charsToUChars(akey
, aiter
->entries
[i
].key
, len
);
98 uprv_sortArray(aiter
->entries
, aiter
->num
, sizeof(UResAEntry
), ures_a_codepointSort
, NULL
, TRUE
, status
);
102 static void ures_a_close(UResourceBundleAIterator
*aiter
) {
103 #if defined(U_SORT_ASCII_BUNDLE_ITERATOR)
104 for(int i
=0;i
<aiter
->num
;i
++) {
105 uprv_free(aiter
->entries
[i
].key
);
106 ures_close(aiter
->entries
[i
].item
);
111 static const UChar
*ures_a_getNextString(UResourceBundleAIterator
*aiter
, int32_t *len
, const char **key
, UErrorCode
*err
) {
112 #if !defined(U_SORT_ASCII_BUNDLE_ITERATOR)
113 return ures_getNextString(aiter
->bund
, len
, key
, err
);
115 if(U_FAILURE(*err
)) return NULL
;
116 UResourceBundle
*item
= aiter
->entries
[aiter
->cursor
].item
;
117 const UChar
* ret
= ures_getString(item
, len
, err
);
118 *key
= ures_getKey(item
);
131 // *****************************************************************************
132 // class DateTimePatternGenerator
133 // *****************************************************************************
134 static const UChar Canonical_Items
[] = {
136 CAP_G
, LOW_Y
, CAP_Q
, CAP_M
, LOW_W
, CAP_W
, CAP_E
, LOW_D
, CAP_D
, CAP_F
,
137 CAP_H
, LOW_M
, LOW_S
, CAP_S
, LOW_V
, 0
140 static const dtTypeElem dtTypes
[] = {
141 // patternChar, field, type, minLen, weight
142 {CAP_G
, UDATPG_ERA_FIELD
, DT_SHORT
, 1, 3,},
143 {CAP_G
, UDATPG_ERA_FIELD
, DT_LONG
, 4, 0},
144 {LOW_Y
, UDATPG_YEAR_FIELD
, DT_NUMERIC
, 1, 20},
145 {CAP_Y
, UDATPG_YEAR_FIELD
, DT_NUMERIC
+ DT_DELTA
, 1, 20},
146 {LOW_U
, UDATPG_YEAR_FIELD
, DT_NUMERIC
+ 2*DT_DELTA
, 1, 20},
147 {CAP_U
, UDATPG_YEAR_FIELD
, DT_SHORT
, 1, 3},
148 {CAP_U
, UDATPG_YEAR_FIELD
, DT_LONG
, 4, 0},
149 {CAP_U
, UDATPG_YEAR_FIELD
, DT_NARROW
, 5, 0},
150 {CAP_Q
, UDATPG_QUARTER_FIELD
, DT_NUMERIC
, 1, 2},
151 {CAP_Q
, UDATPG_QUARTER_FIELD
, DT_SHORT
, 3, 0},
152 {CAP_Q
, UDATPG_QUARTER_FIELD
, DT_LONG
, 4, 0},
153 {LOW_Q
, UDATPG_QUARTER_FIELD
, DT_NUMERIC
+ DT_DELTA
, 1, 2},
154 {LOW_Q
, UDATPG_QUARTER_FIELD
, DT_SHORT
+ DT_DELTA
, 3, 0},
155 {LOW_Q
, UDATPG_QUARTER_FIELD
, DT_LONG
+ DT_DELTA
, 4, 0},
156 {CAP_M
, UDATPG_MONTH_FIELD
, DT_NUMERIC
, 1, 2},
157 {CAP_M
, UDATPG_MONTH_FIELD
, DT_SHORT
, 3, 0},
158 {CAP_M
, UDATPG_MONTH_FIELD
, DT_LONG
, 4, 0},
159 {CAP_M
, UDATPG_MONTH_FIELD
, DT_NARROW
, 5, 0},
160 {CAP_L
, UDATPG_MONTH_FIELD
, DT_NUMERIC
+ DT_DELTA
, 1, 2},
161 {CAP_L
, UDATPG_MONTH_FIELD
, DT_SHORT
- DT_DELTA
, 3, 0},
162 {CAP_L
, UDATPG_MONTH_FIELD
, DT_LONG
- DT_DELTA
, 4, 0},
163 {CAP_L
, UDATPG_MONTH_FIELD
, DT_NARROW
- DT_DELTA
, 5, 0},
164 {LOW_L
, UDATPG_MONTH_FIELD
, DT_NUMERIC
+ DT_DELTA
, 1, 1},
165 {LOW_W
, UDATPG_WEEK_OF_YEAR_FIELD
, DT_NUMERIC
, 1, 2},
166 {CAP_W
, UDATPG_WEEK_OF_MONTH_FIELD
, DT_NUMERIC
+ DT_DELTA
, 1, 0},
167 {CAP_E
, UDATPG_WEEKDAY_FIELD
, DT_SHORT
, 1, 3},
168 {CAP_E
, UDATPG_WEEKDAY_FIELD
, DT_LONG
, 4, 0},
169 {CAP_E
, UDATPG_WEEKDAY_FIELD
, DT_NARROW
, 5, 0},
170 {LOW_C
, UDATPG_WEEKDAY_FIELD
, DT_NUMERIC
+ 2*DT_DELTA
, 1, 2},
171 {LOW_C
, UDATPG_WEEKDAY_FIELD
, DT_SHORT
- 2*DT_DELTA
, 3, 0},
172 {LOW_C
, UDATPG_WEEKDAY_FIELD
, DT_LONG
- 2*DT_DELTA
, 4, 0},
173 {LOW_C
, UDATPG_WEEKDAY_FIELD
, DT_NARROW
- 2*DT_DELTA
, 5, 0},
174 {LOW_E
, UDATPG_WEEKDAY_FIELD
, DT_NUMERIC
+ DT_DELTA
, 1, 2}, // LOW_E is currently not used in CLDR data, should not be canonical
175 {LOW_E
, UDATPG_WEEKDAY_FIELD
, DT_SHORT
- DT_DELTA
, 3, 0},
176 {LOW_E
, UDATPG_WEEKDAY_FIELD
, DT_LONG
- DT_DELTA
, 4, 0},
177 {LOW_E
, UDATPG_WEEKDAY_FIELD
, DT_NARROW
- DT_DELTA
, 5, 0},
178 {LOW_D
, UDATPG_DAY_FIELD
, DT_NUMERIC
, 1, 2},
179 {CAP_D
, UDATPG_DAY_OF_YEAR_FIELD
, DT_NUMERIC
+ DT_DELTA
, 1, 3},
180 {CAP_F
, UDATPG_DAY_OF_WEEK_IN_MONTH_FIELD
, DT_NUMERIC
+ 2*DT_DELTA
, 1, 0},
181 {LOW_G
, UDATPG_DAY_FIELD
, DT_NUMERIC
+ 3*DT_DELTA
, 1, 20}, // really internal use, so we don't care
182 {LOW_A
, UDATPG_DAYPERIOD_FIELD
, DT_SHORT
, 1, 0},
183 {CAP_H
, UDATPG_HOUR_FIELD
, DT_NUMERIC
+ 10*DT_DELTA
, 1, 2}, // 24 hour
184 {LOW_K
, UDATPG_HOUR_FIELD
, DT_NUMERIC
+ 11*DT_DELTA
, 1, 2},
185 {LOW_H
, UDATPG_HOUR_FIELD
, DT_NUMERIC
, 1, 2}, // 12 hour
186 {LOW_K
, UDATPG_HOUR_FIELD
, DT_NUMERIC
+ DT_DELTA
, 1, 2},
187 {LOW_M
, UDATPG_MINUTE_FIELD
, DT_NUMERIC
, 1, 2},
188 {LOW_S
, UDATPG_SECOND_FIELD
, DT_NUMERIC
, 1, 2},
189 {CAP_S
, UDATPG_FRACTIONAL_SECOND_FIELD
, DT_NUMERIC
+ DT_DELTA
, 1, 1000},
190 {CAP_A
, UDATPG_SECOND_FIELD
, DT_NUMERIC
+ 2*DT_DELTA
, 1, 1000},
191 {LOW_V
, UDATPG_ZONE_FIELD
, DT_SHORT
- 2*DT_DELTA
, 1, 0},
192 {LOW_V
, UDATPG_ZONE_FIELD
, DT_LONG
- 2*DT_DELTA
, 4, 0},
193 {LOW_Z
, UDATPG_ZONE_FIELD
, DT_SHORT
, 1, 3},
194 {LOW_Z
, UDATPG_ZONE_FIELD
, DT_LONG
, 4, 0},
195 {CAP_Z
, UDATPG_ZONE_FIELD
, DT_SHORT
- DT_DELTA
, 1, 3},
196 {CAP_Z
, UDATPG_ZONE_FIELD
, DT_LONG
- DT_DELTA
, 4, 0},
197 {CAP_V
, UDATPG_ZONE_FIELD
, DT_SHORT
- DT_DELTA
, 1, 3},
198 {CAP_V
, UDATPG_ZONE_FIELD
, DT_LONG
- DT_DELTA
, 4, 0},
199 {0, UDATPG_FIELD_COUNT
, 0, 0, 0} , // last row of dtTypes[]
202 static const char* const CLDR_FIELD_APPEND
[] = {
203 "Era", "Year", "Quarter", "Month", "Week", "*", "Day-Of-Week", "Day", "*", "*", "*",
204 "Hour", "Minute", "Second", "*", "Timezone"
207 static const char* const CLDR_FIELD_NAME
[] = {
208 "era", "year", "quarter", "month", "week", "*", "weekday", "*", "*", "day", "dayperiod",
209 "hour", "minute", "second", "*", "zone"
212 static const char* const Resource_Fields
[] = {
213 "day", "dayperiod", "era", "hour", "minute", "month", "second", "week",
214 "weekday", "year", "zone", "quarter" };
217 static const UChar UDATPG_ItemFormat
[]= {0x7B, 0x30, 0x7D, 0x20, 0x251C, 0x7B, 0x32, 0x7D, 0x3A,
218 0x20, 0x7B, 0x31, 0x7D, 0x2524, 0}; // {0} \u251C{2}: {1}\u2524
220 //static const UChar repeatedPatterns[6]={CAP_G, CAP_E, LOW_Z, LOW_V, CAP_Q, 0}; // "GEzvQ"
222 static const char DT_DateTimePatternsTag
[]="DateTimePatterns";
223 static const char DT_DateTimeCalendarTag
[]="calendar";
224 static const char DT_DateTimeGregorianTag
[]="gregorian";
225 static const char DT_DateTimeAppendItemsTag
[]="appendItems";
226 static const char DT_DateTimeFieldsTag
[]="fields";
227 static const char DT_DateTimeAvailableFormatsTag
[]="availableFormats";
228 //static const UnicodeString repeatedPattern=UnicodeString(repeatedPatterns);
230 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateTimePatternGenerator
)
231 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTSkeletonEnumeration
)
232 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DTRedundantEnumeration
)
234 DateTimePatternGenerator
* U_EXPORT2
235 DateTimePatternGenerator::createInstance(UErrorCode
& status
) {
236 return createInstance(Locale::getDefault(), status
);
239 DateTimePatternGenerator
* U_EXPORT2
240 DateTimePatternGenerator::createInstance(const Locale
& locale
, UErrorCode
& status
) {
241 DateTimePatternGenerator
*result
= new DateTimePatternGenerator(locale
, status
);
242 if (result
== NULL
) {
243 status
= U_MEMORY_ALLOCATION_ERROR
;
245 if (U_FAILURE(status
)) {
252 DateTimePatternGenerator
* U_EXPORT2
253 DateTimePatternGenerator::createEmptyInstance(UErrorCode
& status
) {
254 DateTimePatternGenerator
*result
= new DateTimePatternGenerator(status
);
255 if (result
== NULL
) {
256 status
= U_MEMORY_ALLOCATION_ERROR
;
258 if (U_FAILURE(status
)) {
265 DateTimePatternGenerator::DateTimePatternGenerator(UErrorCode
&status
) :
267 fAvailableFormatKeyHash(NULL
)
269 fp
= new FormatParser();
270 dtMatcher
= new DateTimeMatcher();
271 distanceInfo
= new DistanceInfo();
272 patternMap
= new PatternMap();
273 if (fp
== NULL
|| dtMatcher
== NULL
|| distanceInfo
== NULL
|| patternMap
== NULL
) {
274 status
= U_MEMORY_ALLOCATION_ERROR
;
278 DateTimePatternGenerator::DateTimePatternGenerator(const Locale
& locale
, UErrorCode
&status
) :
280 fAvailableFormatKeyHash(NULL
)
282 fp
= new FormatParser();
283 dtMatcher
= new DateTimeMatcher();
284 distanceInfo
= new DistanceInfo();
285 patternMap
= new PatternMap();
286 if (fp
== NULL
|| dtMatcher
== NULL
|| distanceInfo
== NULL
|| patternMap
== NULL
) {
287 status
= U_MEMORY_ALLOCATION_ERROR
;
290 initData(locale
, status
);
294 DateTimePatternGenerator::DateTimePatternGenerator(const DateTimePatternGenerator
& other
) :
297 fAvailableFormatKeyHash(NULL
)
299 fp
= new FormatParser();
300 dtMatcher
= new DateTimeMatcher();
301 distanceInfo
= new DistanceInfo();
302 patternMap
= new PatternMap();
306 DateTimePatternGenerator
&
307 DateTimePatternGenerator::operator=(const DateTimePatternGenerator
& other
) {
308 pLocale
= other
.pLocale
;
309 fDefaultHourFormatChar
= other
.fDefaultHourFormatChar
;
311 dtMatcher
->copyFrom(other
.dtMatcher
->skeleton
);
312 *distanceInfo
= *(other
.distanceInfo
);
313 dateTimeFormat
= other
.dateTimeFormat
;
314 decimal
= other
.decimal
;
315 // NUL-terminate for the C API.
316 dateTimeFormat
.getTerminatedBuffer();
317 decimal
.getTerminatedBuffer();
319 if ( other
.skipMatcher
== NULL
) {
323 skipMatcher
= new DateTimeMatcher(*other
.skipMatcher
);
325 for (int32_t i
=0; i
< UDATPG_FIELD_COUNT
; ++i
) {
326 appendItemFormats
[i
] = other
.appendItemFormats
[i
];
327 appendItemNames
[i
] = other
.appendItemNames
[i
];
328 // NUL-terminate for the C API.
329 appendItemFormats
[i
].getTerminatedBuffer();
330 appendItemNames
[i
].getTerminatedBuffer();
332 UErrorCode status
= U_ZERO_ERROR
;
333 patternMap
->copyFrom(*other
.patternMap
, status
);
334 copyHashtable(other
.fAvailableFormatKeyHash
, status
);
340 DateTimePatternGenerator::operator==(const DateTimePatternGenerator
& other
) const {
341 if (this == &other
) {
344 if ((pLocale
==other
.pLocale
) && (patternMap
->equals(*other
.patternMap
)) &&
345 (dateTimeFormat
==other
.dateTimeFormat
) && (decimal
==other
.decimal
)) {
346 for ( int32_t i
=0 ; i
<UDATPG_FIELD_COUNT
; ++i
) {
347 if ((appendItemFormats
[i
] != other
.appendItemFormats
[i
]) ||
348 (appendItemNames
[i
] != other
.appendItemNames
[i
]) ) {
360 DateTimePatternGenerator::operator!=(const DateTimePatternGenerator
& other
) const {
361 return !operator==(other
);
364 DateTimePatternGenerator::~DateTimePatternGenerator() {
365 if (fAvailableFormatKeyHash
!=NULL
) {
366 delete fAvailableFormatKeyHash
;
369 if (fp
!= NULL
) delete fp
;
370 if (dtMatcher
!= NULL
) delete dtMatcher
;
371 if (distanceInfo
!= NULL
) delete distanceInfo
;
372 if (patternMap
!= NULL
) delete patternMap
;
373 if (skipMatcher
!= NULL
) delete skipMatcher
;
377 DateTimePatternGenerator::initData(const Locale
& locale
, UErrorCode
&status
) {
378 //const char *baseLangName = locale.getBaseName(); // unused
381 fAvailableFormatKeyHash
=NULL
;
383 addICUPatterns(locale
, status
);
384 if (U_FAILURE(status
)) {
387 addCLDRData(locale
, status
);
388 setDateTimeFromCalendar(locale
, status
);
389 setDecimalSymbols(locale
, status
);
390 } // DateTimePatternGenerator::initData
393 DateTimePatternGenerator::getSkeleton(const UnicodeString
& pattern
, UErrorCode
&
395 dtMatcher
->set(pattern
, fp
);
396 return dtMatcher
->getSkeletonPtr()->getSkeleton();
400 DateTimePatternGenerator::getBaseSkeleton(const UnicodeString
& pattern
, UErrorCode
& /*status*/) {
401 dtMatcher
->set(pattern
, fp
);
402 return dtMatcher
->getSkeletonPtr()->getBaseSkeleton();
406 DateTimePatternGenerator::addICUPatterns(const Locale
& locale
, UErrorCode
& status
) {
407 UnicodeString dfPattern
;
408 UnicodeString conflictingString
;
411 if (U_FAILURE(status
)) {
415 // Load with ICU patterns
416 for (int32_t i
=DateFormat::kFull
; i
<=DateFormat::kShort
; i
++) {
417 DateFormat::EStyle style
= (DateFormat::EStyle
)i
;
418 df
= DateFormat::createDateInstance(style
, locale
);
419 SimpleDateFormat
* sdf
;
420 if (df
!= NULL
&& (sdf
= dynamic_cast<SimpleDateFormat
*>(df
)) != NULL
) {
421 addPattern(sdf
->toPattern(dfPattern
), FALSE
, conflictingString
, status
);
423 // TODO Maybe we should return an error when the date format isn't simple.
425 if (U_FAILURE(status
)) {
429 df
= DateFormat::createTimeInstance(style
, locale
);
430 if (df
!= NULL
&& (sdf
= dynamic_cast<SimpleDateFormat
*>(df
)) != NULL
) {
431 addPattern(sdf
->toPattern(dfPattern
), FALSE
, conflictingString
, status
);
433 if ( i
==DateFormat::kMedium
) {
434 hackPattern
= dfPattern
;
437 // TODO Maybe we should return an error when the date format isn't simple.
439 if (U_FAILURE(status
)) {
446 DateTimePatternGenerator::hackTimes(const UnicodeString
& hackPattern
, UErrorCode
& status
) {
447 UnicodeString conflictingString
;
449 fp
->set(hackPattern
);
452 for (int32_t i
=0; i
<fp
->itemNumber
; ++i
) {
453 UnicodeString field
= fp
->items
[i
];
454 if ( fp
->isQuoteLiteral(field
) ) {
456 UnicodeString quoteLiteral
;
457 fp
->getQuoteLiteral(quoteLiteral
, &i
);
458 mmss
+= quoteLiteral
;
462 if (fp
->isPatternSeparator(field
) && gotMm
) {
466 UChar ch
=field
.charAt(0);
477 addPattern(mmss
, FALSE
, conflictingString
, status
);
481 if (gotMm
|| ch
==LOW_Z
|| ch
==CAP_Z
|| ch
==LOW_V
|| ch
==CAP_V
) {
491 #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY)
493 static const UChar hourFormatChars
[] = { CAP_H
, LOW_H
, CAP_K
, LOW_K
, 0 }; // HhKk, the hour format characters
496 DateTimePatternGenerator::addCLDRData(const Locale
& locale
, UErrorCode
& err
) {
497 UResourceBundle
*rb
, *calTypeBundle
, *calBundle
;
498 UResourceBundle
*patBundle
, *fieldBundle
, *fBundle
;
499 UnicodeString rbPattern
, value
, field
;
500 UnicodeString conflictingPattern
;
501 const char *key
=NULL
;
504 UnicodeString
defaultItemFormat(TRUE
, UDATPG_ItemFormat
, LENGTHOF(UDATPG_ItemFormat
)-1); // Read-only alias.
508 fDefaultHourFormatChar
= 0;
509 for (i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
510 appendItemNames
[i
]=CAP_F
;
512 appendItemNames
[i
]+=(UChar
)(i
+0x30);
515 appendItemNames
[i
]+=(UChar
)0x31;
516 appendItemNames
[i
]+=(UChar
)(i
-10 + 0x30);
518 // NUL-terminate for the C API.
519 appendItemNames
[i
].getTerminatedBuffer();
522 rb
= ures_open(NULL
, locale
.getName(), &err
);
523 if (rb
== NULL
|| U_FAILURE(err
)) {
526 const char *curLocaleName
=ures_getLocaleByType(rb
, ULOC_ACTUAL_LOCALE
, &err
);
527 const char * calendarTypeToUse
= DT_DateTimeGregorianTag
; // initial default
528 char calendarType
[ULOC_KEYWORDS_CAPACITY
]; // to be filled in with the type to use, if all goes well
529 if ( U_SUCCESS(err
) ) {
530 char localeWithCalendarKey
[ULOC_LOCALE_IDENTIFIER_CAPACITY
];
531 // obtain a locale that always has the calendar key value that should be used
532 (void)ures_getFunctionalEquivalent(localeWithCalendarKey
, ULOC_LOCALE_IDENTIFIER_CAPACITY
, NULL
,
533 "calendar", "calendar", locale
.getName(), NULL
, FALSE
, &err
);
534 localeWithCalendarKey
[ULOC_LOCALE_IDENTIFIER_CAPACITY
-1] = 0; // ensure null termination
535 // now get the calendar key value from that locale
536 int32_t calendarTypeLen
= uloc_getKeywordValue(localeWithCalendarKey
, "calendar", calendarType
, ULOC_KEYWORDS_CAPACITY
, &err
);
537 if (U_SUCCESS(err
) && calendarTypeLen
< ULOC_KEYWORDS_CAPACITY
) {
538 calendarTypeToUse
= calendarType
;
542 calBundle
= ures_getByKeyWithFallback(rb
, DT_DateTimeCalendarTag
, NULL
, &err
);
543 calTypeBundle
= ures_getByKeyWithFallback(calBundle
, calendarTypeToUse
, NULL
, &err
);
547 patBundle
= ures_getByKeyWithFallback(calTypeBundle
, DT_DateTimePatternsTag
, NULL
, &err
);
548 while (U_SUCCESS(err
)) {
549 rbPattern
= ures_getNextUnicodeString(patBundle
, &key
, &err
);
551 if (rbPattern
.length()==0 ) {
552 break; // no more pattern
556 setDateTimeFormat(rbPattern
);
557 } else if (dtCount
==4) { // short time format
558 // set fDefaultHourFormatChar to the hour format character from this pattern
559 int32_t tfIdx
, tfLen
= rbPattern
.length();
560 UBool ignoreChars
= FALSE
;
561 for (tfIdx
= 0; tfIdx
< tfLen
; tfIdx
++) {
562 UChar tfChar
= rbPattern
.charAt(tfIdx
);
563 if ( tfChar
== SINGLE_QUOTE
) {
564 ignoreChars
= !ignoreChars
; // toggle (handle quoted literals & '' for single quote)
565 } else if ( !ignoreChars
&& u_strchr(hourFormatChars
, tfChar
) != NULL
) {
566 fDefaultHourFormatChar
= tfChar
;
573 ures_close(patBundle
);
576 patBundle
= ures_getByKeyWithFallback(calTypeBundle
, DT_DateTimeAppendItemsTag
, NULL
, &err
);
578 UnicodeString itemKey
;
579 while (U_SUCCESS(err
)) {
580 rbPattern
= ures_getNextUnicodeString(patBundle
, &key
, &err
);
581 if (rbPattern
.length()==0 ) {
582 break; // no more pattern
585 setAppendItemFormat(getAppendFormatNumber(key
), rbPattern
);
588 ures_close(patBundle
);
592 fBundle
= ures_getByKeyWithFallback(rb
, DT_DateTimeFieldsTag
, NULL
, &err
);
593 for (i
=0; i
<MAX_RESOURCE_FIELD
; ++i
) {
595 patBundle
= ures_getByKeyWithFallback(fBundle
, Resource_Fields
[i
], NULL
, &err
);
596 fieldBundle
= ures_getByKeyWithFallback(patBundle
, "dn", NULL
, &err
);
597 rbPattern
= ures_getNextUnicodeString(fieldBundle
, &key
, &err
);
598 ures_close(fieldBundle
);
599 ures_close(patBundle
);
600 if (rbPattern
.length()==0 ) {
604 setAppendItemName(getAppendNameNumber(Resource_Fields
[i
]), rbPattern
);
609 // add available formats
610 UBool firstTimeThrough
= TRUE
;
613 UBool override
= TRUE
;
615 // At the start of the loop:
616 // - rb is the open resource bundle for the current locale being processed,
617 // whose actual name is in curLocaleName.
618 // - if U_SUCCESS(err), then calBundle and calTypeBundle are open;
619 // process contents of calTypeBundle, then close calBundle and calTypeBundle.
620 if (U_SUCCESS(err
)) {
621 // process contents of calTypeBundle
622 patBundle
= ures_getByKeyWithFallback(calTypeBundle
, DT_DateTimeAvailableFormatsTag
, NULL
, &err
);
623 if (U_SUCCESS(err
)) {
624 int32_t numberKeys
= ures_getSize(patBundle
);
626 const UChar
*retPattern
;
628 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
629 UResourceBundleAIterator aiter
;
630 ures_a_open(&aiter
, patBundle
, &err
);
632 for(i
=0; i
<numberKeys
; ++i
) {
633 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
634 retPattern
=ures_a_getNextString(&aiter
, &len
, &key
, &err
);
636 retPattern
=ures_getNextString(patBundle
, &len
, &key
, &err
);
638 UnicodeString format
=UnicodeString(retPattern
);
639 UnicodeString retKey
=UnicodeString(key
, -1, US_INV
);
640 if ( firstTimeThrough
|| !isAvailableFormatSet(retKey
) ) {
641 setAvailableFormat(retKey
, err
);
642 // Add pattern with its associated skeleton. Override any duplicate derived from std patterns,
643 // but not a previous availableFormats entry:
644 addPatternWithSkeleton(format
, &retKey
, override
, conflictingPattern
, err
);
647 #if defined(U_USE_ASCII_BUNDLE_ITERATOR)
648 ures_a_close(&aiter
);
650 ures_close(patBundle
);
652 firstTimeThrough
= FALSE
;
653 // close calBundle and calTypeBundle
654 ures_close(calTypeBundle
);
655 ures_close(calBundle
);
657 if (uprv_strcmp(curLocaleName
,"root")==0 || uprv_strlen(curLocaleName
)==0) {
658 // we just finished handling root, nothing more to check
662 // Find the name of the appropriate parent locale (from %%Parent if present, else
663 // uloc_getParent on the actual locale name)
664 // (It would be nice to have a ures function that did this...)
666 char parentLocale
[ULOC_FULLNAME_CAPACITY
];
668 const UChar
* parentUName
= ures_getStringByKey(rb
, "%%Parent", &locNameLen
, &err
);
669 if (U_SUCCESS(err
) && err
!= U_USING_FALLBACK_WARNING
&& locNameLen
< ULOC_FULLNAME_CAPACITY
) {
670 u_UCharsToChars(parentUName
, parentLocale
, locNameLen
+ 1);
673 uloc_getParent(curLocaleName
, parentLocale
, ULOC_FULLNAME_CAPACITY
, &err
);
674 if (U_FAILURE(err
) || err
== U_STRING_NOT_TERMINATED_WARNING
) {
675 // just fallback to root, since we are not already there
680 // Close current locale bundle
682 // And open its parent, which becomes the new current locale being processed
683 rb
= ures_open(NULL
, parentLocale
, &err
);
684 if ( U_FAILURE(err
) ) {
688 // Get the name of the parent / new current locale
689 curLocaleName
=ures_getLocaleByType(rb
, ULOC_ACTUAL_LOCALE
, &err
);
690 if ( U_FAILURE(err
) ) {
691 curLocaleName
= parentLocale
;
694 if (uprv_strcmp(curLocaleName
,"root")==0 || uprv_strlen(curLocaleName
)==0) {
697 // Open calBundle and calTypeBundle
698 calBundle
= ures_getByKeyWithFallback(rb
, DT_DateTimeCalendarTag
, NULL
, &err
);
699 if (U_SUCCESS(err
)) {
700 calTypeBundle
= ures_getByKeyWithFallback(calBundle
, calendarTypeToUse
, NULL
, &err
);
701 if ( U_FAILURE(err
) ) {
702 ures_close(calBundle
);
705 // Go to the top of the loop to process contents of calTypeBundle
708 if (hackPattern
.length()>0) {
709 hackTimes(hackPattern
, err
);
714 DateTimePatternGenerator::initHashtable(UErrorCode
& err
) {
715 if (fAvailableFormatKeyHash
!=NULL
) {
718 if ((fAvailableFormatKeyHash
= new Hashtable(FALSE
, err
))==NULL
) {
719 err
=U_MEMORY_ALLOCATION_ERROR
;
726 DateTimePatternGenerator::setAppendItemFormat(UDateTimePatternField field
, const UnicodeString
& value
) {
727 appendItemFormats
[field
] = value
;
728 // NUL-terminate for the C API.
729 appendItemFormats
[field
].getTerminatedBuffer();
733 DateTimePatternGenerator::getAppendItemFormat(UDateTimePatternField field
) const {
734 return appendItemFormats
[field
];
738 DateTimePatternGenerator::setAppendItemName(UDateTimePatternField field
, const UnicodeString
& value
) {
739 appendItemNames
[field
] = value
;
740 // NUL-terminate for the C API.
741 appendItemNames
[field
].getTerminatedBuffer();
745 DateTimePatternGenerator:: getAppendItemName(UDateTimePatternField field
) const {
746 return appendItemNames
[field
];
750 DateTimePatternGenerator::getAppendName(UDateTimePatternField field
, UnicodeString
& value
) {
751 value
= SINGLE_QUOTE
;
752 value
+= appendItemNames
[field
];
753 value
+= SINGLE_QUOTE
;
757 DateTimePatternGenerator::getBestPattern(const UnicodeString
& patternForm
, UErrorCode
& status
) {
758 return getBestPattern(patternForm
, UDATPG_MATCH_NO_OPTIONS
, status
);
762 DateTimePatternGenerator::getBestPattern(const UnicodeString
& patternForm
, UDateTimePatternMatchOptions options
, UErrorCode
& status
) {
763 const UnicodeString
*bestPattern
=NULL
;
764 UnicodeString dtFormat
;
765 UnicodeString resultPattern
;
767 int32_t dateMask
=(1<<UDATPG_DAYPERIOD_FIELD
) - 1;
768 int32_t timeMask
=(1<<UDATPG_FIELD_COUNT
) - 1 - dateMask
;
770 UnicodeString patternFormCopy
= UnicodeString(patternForm
);
771 patternFormCopy
.findAndReplace(UnicodeString(LOW_J
), UnicodeString(fDefaultHourFormatChar
));
773 resultPattern
.remove();
774 dtMatcher
->set(patternFormCopy
, fp
);
775 const PtnSkeleton
* specifiedSkeleton
=NULL
;
776 bestPattern
=getBestRaw(*dtMatcher
, -1, distanceInfo
, &specifiedSkeleton
);
777 if ( distanceInfo
->missingFieldMask
==0 && distanceInfo
->extraFieldMask
==0 ) {
778 resultPattern
= adjustFieldTypes(*bestPattern
, specifiedSkeleton
, FALSE
, options
);
780 return resultPattern
;
782 int32_t neededFields
= dtMatcher
->getFieldMask();
783 UnicodeString datePattern
=getBestAppending(neededFields
& dateMask
, options
);
784 UnicodeString timePattern
=getBestAppending(neededFields
& timeMask
, options
);
785 if (datePattern
.length()==0) {
786 if (timePattern
.length()==0) {
787 resultPattern
.remove();
793 if (timePattern
.length()==0) {
796 resultPattern
.remove();
797 status
= U_ZERO_ERROR
;
798 dtFormat
=getDateTimeFormat();
799 Formattable dateTimeObject
[] = { timePattern
, datePattern
};
800 resultPattern
= MessageFormat::format(dtFormat
, dateTimeObject
, 2, resultPattern
, status
);
801 return resultPattern
;
805 DateTimePatternGenerator::replaceFieldTypes(const UnicodeString
& pattern
,
806 const UnicodeString
& skeleton
,
807 UErrorCode
& status
) {
808 return replaceFieldTypes(pattern
, skeleton
, UDATPG_MATCH_NO_OPTIONS
, status
);
812 DateTimePatternGenerator::replaceFieldTypes(const UnicodeString
& pattern
,
813 const UnicodeString
& skeleton
,
814 UDateTimePatternMatchOptions options
,
815 UErrorCode
& /*status*/) {
816 dtMatcher
->set(skeleton
, fp
);
817 UnicodeString result
= adjustFieldTypes(pattern
, NULL
, FALSE
, options
);
822 DateTimePatternGenerator::setDecimal(const UnicodeString
& newDecimal
) {
823 this->decimal
= newDecimal
;
824 // NUL-terminate for the C API.
825 this->decimal
.getTerminatedBuffer();
829 DateTimePatternGenerator::getDecimal() const {
834 DateTimePatternGenerator::addCanonicalItems() {
835 UnicodeString conflictingPattern
;
836 UErrorCode status
= U_ZERO_ERROR
;
838 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; i
++) {
839 addPattern(UnicodeString(Canonical_Items
[i
]), FALSE
, conflictingPattern
, status
);
844 DateTimePatternGenerator::setDateTimeFormat(const UnicodeString
& dtFormat
) {
845 dateTimeFormat
= dtFormat
;
846 // NUL-terminate for the C API.
847 dateTimeFormat
.getTerminatedBuffer();
851 DateTimePatternGenerator::getDateTimeFormat() const {
852 return dateTimeFormat
;
856 DateTimePatternGenerator::setDateTimeFromCalendar(const Locale
& locale
, UErrorCode
& status
) {
858 int32_t resStrLen
= 0;
860 Calendar
* fCalendar
= Calendar::createInstance(locale
, status
);
861 CalendarData
calData(locale
, fCalendar
?fCalendar
->getType():NULL
, status
);
862 UResourceBundle
*dateTimePatterns
= calData
.getByKey(DT_DateTimePatternsTag
, status
);
863 if (U_FAILURE(status
)) return;
865 if (ures_getSize(dateTimePatterns
) <= DateFormat::kDateTime
)
867 status
= U_INVALID_FORMAT_ERROR
;
870 resStr
= ures_getStringByIndex(dateTimePatterns
, (int32_t)DateFormat::kDateTime
, &resStrLen
, &status
);
871 setDateTimeFormat(UnicodeString(TRUE
, resStr
, resStrLen
));
877 DateTimePatternGenerator::setDecimalSymbols(const Locale
& locale
, UErrorCode
& status
) {
878 DecimalFormatSymbols dfs
= DecimalFormatSymbols(locale
, status
);
879 if(U_SUCCESS(status
)) {
880 decimal
= dfs
.getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol
);
881 // NUL-terminate for the C API.
882 decimal
.getTerminatedBuffer();
886 UDateTimePatternConflict
887 DateTimePatternGenerator::addPattern(
888 const UnicodeString
& pattern
,
890 UnicodeString
&conflictingPattern
,
893 return addPatternWithSkeleton(pattern
, NULL
, override
, conflictingPattern
, status
);
896 // For DateTimePatternGenerator::addPatternWithSkeleton -
897 // If skeletonToUse is specified, then an availableFormats entry is being added. In this case:
898 // 1. We pass that skeleton to matcher.set instead of having it derive a skeleton from the pattern.
899 // 2. If the new entry's skeleton or basePattern does match an existing entry but that entry also had a skeleton specified
900 // (i.e. it was also from availableFormats), then the new entry does not override it regardless of the value of the override
901 // parameter. This prevents later availableFormats entries from a parent locale overriding earlier ones from the actual
902 // specified locale. However, availableFormats entries *should* override entries with matching skeleton whose skeleton was
903 // derived (i.e. entries derived from the standard date/time patters for the specified locale).
904 // 3. When adding the pattern (patternMap->add), we set a new boolean to indicate that the added entry had a
905 // specified skeleton (which sets a new field in the PtnElem in the PatternMap).
906 UDateTimePatternConflict
907 DateTimePatternGenerator::addPatternWithSkeleton(
908 const UnicodeString
& pattern
,
909 const UnicodeString
* skeletonToUse
,
911 UnicodeString
& conflictingPattern
,
915 UnicodeString basePattern
;
916 PtnSkeleton skeleton
;
917 UDateTimePatternConflict conflictingStatus
= UDATPG_NO_CONFLICT
;
919 DateTimeMatcher matcher
;
920 if ( skeletonToUse
== NULL
) {
921 matcher
.set(pattern
, fp
, skeleton
);
922 matcher
.getBasePattern(basePattern
);
924 matcher
.set(*skeletonToUse
, fp
, skeleton
); // no longer trims skeleton fields to max len 3, per #7930
925 matcher
.getBasePattern(basePattern
); // or perhaps instead: basePattern = *skeletonToUse;
927 // We only care about base conflicts - and replacing the pattern associated with a base - if:
928 // 1. the conflicting previous base pattern did *not* have an explicit skeleton; in that case the previous
929 // base + pattern combination was derived from either (a) a canonical item, (b) a standard format, or
930 // (c) a pattern specified programmatically with a previous call to addPattern (which would only happen
931 // if we are getting here from a subsequent call to addPattern).
932 // 2. a skeleton is specified for the current pattern, but override=false; in that case we are checking
933 // availableFormats items from root, which should not override any previous entry with the same base.
934 UBool entryHadSpecifiedSkeleton
;
935 const UnicodeString
*duplicatePattern
= patternMap
->getPatternFromBasePattern(basePattern
, entryHadSpecifiedSkeleton
);
936 if (duplicatePattern
!= NULL
&& (!entryHadSpecifiedSkeleton
|| (skeletonToUse
!= NULL
&& !override
))) {
937 conflictingStatus
= UDATPG_BASE_CONFLICT
;
938 conflictingPattern
= *duplicatePattern
;
940 return conflictingStatus
;
943 // The only time we get here with override=true and skeletonToUse!=null is when adding availableFormats
944 // items from CLDR data. In that case, we don't want an item from a parent locale to replace an item with
945 // same skeleton from the specified locale, so skip the current item if skeletonWasSpecified is true for
946 // the previously-specified conflicting item.
947 const PtnSkeleton
* entrySpecifiedSkeleton
= NULL
;
948 duplicatePattern
= patternMap
->getPatternFromSkeleton(skeleton
, &entrySpecifiedSkeleton
);
949 if (duplicatePattern
!= NULL
) {
950 conflictingStatus
= UDATPG_CONFLICT
;
951 conflictingPattern
= *duplicatePattern
;
952 if (!override
|| (skeletonToUse
!= NULL
&& entrySpecifiedSkeleton
!= NULL
)) {
953 return conflictingStatus
;
956 patternMap
->add(basePattern
, skeleton
, pattern
, skeletonToUse
!= NULL
, status
);
957 if(U_FAILURE(status
)) {
958 return conflictingStatus
;
961 return UDATPG_NO_CONFLICT
;
965 UDateTimePatternField
966 DateTimePatternGenerator::getAppendFormatNumber(const char* field
) const {
967 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
968 if (uprv_strcmp(CLDR_FIELD_APPEND
[i
], field
)==0) {
969 return (UDateTimePatternField
)i
;
972 return UDATPG_FIELD_COUNT
;
975 UDateTimePatternField
976 DateTimePatternGenerator::getAppendNameNumber(const char* field
) const {
977 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
978 if (uprv_strcmp(CLDR_FIELD_NAME
[i
],field
)==0) {
979 return (UDateTimePatternField
)i
;
982 return UDATPG_FIELD_COUNT
;
986 DateTimePatternGenerator::getBestRaw(DateTimeMatcher
& source
,
988 DistanceInfo
* missingFields
,
989 const PtnSkeleton
** specifiedSkeletonPtr
) {
990 int32_t bestDistance
= 0x7fffffff;
991 DistanceInfo tempInfo
;
992 const UnicodeString
*bestPattern
=NULL
;
993 const PtnSkeleton
* specifiedSkeleton
=NULL
;
995 PatternMapIterator it
;
996 for (it
.set(*patternMap
); it
.hasNext(); ) {
997 DateTimeMatcher trial
= it
.next();
998 if (trial
.equals(skipMatcher
)) {
1001 int32_t distance
=source
.getDistance(trial
, includeMask
, tempInfo
);
1002 if (distance
<bestDistance
) {
1003 bestDistance
=distance
;
1004 bestPattern
=patternMap
->getPatternFromSkeleton(*trial
.getSkeletonPtr(), &specifiedSkeleton
);
1005 missingFields
->setTo(tempInfo
);
1012 // If the best raw match had a specified skeleton and that skeleton was requested by the caller,
1013 // then return it too. This generally happens when the caller needs to pass that skeleton
1014 // through to adjustFieldTypes so the latter can do a better job.
1015 if (bestPattern
&& specifiedSkeletonPtr
) {
1016 *specifiedSkeletonPtr
= specifiedSkeleton
;
1022 DateTimePatternGenerator::adjustFieldTypes(const UnicodeString
& pattern
,
1023 const PtnSkeleton
* specifiedSkeleton
,
1024 UBool fixFractionalSeconds
,
1025 UDateTimePatternMatchOptions options
) {
1026 UnicodeString newPattern
;
1028 for (int32_t i
=0; i
< fp
->itemNumber
; i
++) {
1029 UnicodeString field
= fp
->items
[i
];
1030 if ( fp
->isQuoteLiteral(field
) ) {
1032 UnicodeString quoteLiteral
;
1033 fp
->getQuoteLiteral(quoteLiteral
, &i
);
1034 newPattern
+= quoteLiteral
;
1037 if (fp
->isPatternSeparator(field
)) {
1041 int32_t canonicalIndex
= fp
->getCanonicalIndex(field
);
1042 if (canonicalIndex
< 0) {
1044 continue; // don't adjust
1046 const dtTypeElem
*row
= &dtTypes
[canonicalIndex
];
1047 int32_t typeValue
= row
->field
;
1048 if (fixFractionalSeconds
&& typeValue
== UDATPG_SECOND_FIELD
) {
1049 UnicodeString newField
=dtMatcher
->skeleton
.original
[UDATPG_FRACTIONAL_SECOND_FIELD
];
1050 field
= field
+ decimal
+ newField
;
1051 } else if (dtMatcher
->skeleton
.type
[typeValue
]!=0) {
1053 // - "reqField" is the field from the originally requested skeleton, with length
1055 // - "field" is the field from the found pattern.
1057 // The adjusted field should consist of characters from the originally requested
1058 // skeleton, except in the case of UDATPG_HOUR_FIELD or UDATPG_MONTH_FIELD or
1059 // UDATPG_WEEKDAY_FIELD or UDATPG_YEAR_FIELD, in which case it should consist
1060 // of characters from the found pattern.
1062 // The length of the adjusted field (adjFieldLen) should match that in the originally
1063 // requested skeleton, except that in the following cases the length of the adjusted field
1064 // should match that in the found pattern (i.e. the length of this pattern field should
1065 // not be adjusted):
1066 // 1. typeValue is UDATPG_HOUR_FIELD/MINUTE/SECOND and the corresponding bit in options is
1067 // not set (ticket #7180). Note, we may want to implement a similar change for other
1068 // numeric fields (MM, dd, etc.) so the default behavior is to get locale preference for
1069 // field length, but options bits can be used to override this.
1070 // 2. There is a specified skeleton for the found pattern and one of the following is true:
1071 // a) The length of the field in the skeleton (skelFieldLen) is equal to reqFieldLen.
1072 // b) The pattern field is numeric and the skeleton field is not, or vice versa.
1074 UnicodeString reqField
= dtMatcher
->skeleton
.original
[typeValue
];
1075 int32_t reqFieldLen
= reqField
.length();
1076 if (reqField
.charAt(0) == CAP_E
&& reqFieldLen
< 3)
1077 reqFieldLen
= 3; // 1-3 for E are equivalent to 3 for c,e
1078 int32_t adjFieldLen
= reqFieldLen
;
1079 if ( (typeValue
==UDATPG_HOUR_FIELD
&& (options
& UDATPG_MATCH_HOUR_FIELD_LENGTH
)==0) ||
1080 (typeValue
==UDATPG_MINUTE_FIELD
&& (options
& UDATPG_MATCH_MINUTE_FIELD_LENGTH
)==0) ||
1081 (typeValue
==UDATPG_SECOND_FIELD
&& (options
& UDATPG_MATCH_SECOND_FIELD_LENGTH
)==0) ) {
1082 adjFieldLen
= field
.length();
1083 } else if (specifiedSkeleton
) {
1084 UnicodeString skelField
= specifiedSkeleton
->original
[typeValue
];
1085 int32_t skelFieldLen
= skelField
.length();
1086 UBool patFieldIsNumeric
= (row
->type
> 0);
1087 UBool skelFieldIsNumeric
= (specifiedSkeleton
->type
[typeValue
] > 0);
1088 if (skelFieldLen
== reqFieldLen
|| (patFieldIsNumeric
&& !skelFieldIsNumeric
) || (skelFieldIsNumeric
&& !patFieldIsNumeric
)) {
1089 // don't adjust the field length in the found pattern
1090 adjFieldLen
= field
.length();
1093 UChar c
= (typeValue
!= UDATPG_HOUR_FIELD
&& typeValue
!= UDATPG_MONTH_FIELD
&&
1094 typeValue
!= UDATPG_WEEKDAY_FIELD
&& (typeValue
!= UDATPG_YEAR_FIELD
|| reqField
.charAt(0)==CAP_Y
))?
1095 reqField
.charAt(0): field
.charAt(0);
1097 for (int32_t i
=adjFieldLen
; i
>0; --i
) {
1108 DateTimePatternGenerator::getBestAppending(int32_t missingFields
, UDateTimePatternMatchOptions options
) {
1109 UnicodeString resultPattern
, tempPattern
;
1110 UErrorCode err
=U_ZERO_ERROR
;
1111 int32_t lastMissingFieldMask
=0;
1112 if (missingFields
!=0) {
1113 resultPattern
=UnicodeString();
1114 const PtnSkeleton
* specifiedSkeleton
=NULL
;
1115 tempPattern
= *getBestRaw(*dtMatcher
, missingFields
, distanceInfo
, &specifiedSkeleton
);
1116 resultPattern
= adjustFieldTypes(tempPattern
, specifiedSkeleton
, FALSE
, options
);
1117 if ( distanceInfo
->missingFieldMask
==0 ) {
1118 return resultPattern
;
1120 while (distanceInfo
->missingFieldMask
!=0) { // precondition: EVERY single field must work!
1121 if ( lastMissingFieldMask
== distanceInfo
->missingFieldMask
) {
1122 break; // cannot find the proper missing field
1124 if (((distanceInfo
->missingFieldMask
& UDATPG_SECOND_AND_FRACTIONAL_MASK
)==UDATPG_FRACTIONAL_MASK
) &&
1125 ((missingFields
& UDATPG_SECOND_AND_FRACTIONAL_MASK
) == UDATPG_SECOND_AND_FRACTIONAL_MASK
)) {
1126 resultPattern
= adjustFieldTypes(resultPattern
, specifiedSkeleton
, TRUE
, options
);
1127 distanceInfo
->missingFieldMask
&= ~UDATPG_FRACTIONAL_MASK
;
1130 int32_t startingMask
= distanceInfo
->missingFieldMask
;
1131 tempPattern
= *getBestRaw(*dtMatcher
, distanceInfo
->missingFieldMask
, distanceInfo
, &specifiedSkeleton
);
1132 tempPattern
= adjustFieldTypes(tempPattern
, specifiedSkeleton
, FALSE
, options
);
1133 int32_t foundMask
=startingMask
& ~distanceInfo
->missingFieldMask
;
1134 int32_t topField
=getTopBitNumber(foundMask
);
1135 UnicodeString appendName
;
1136 getAppendName((UDateTimePatternField
)topField
, appendName
);
1137 const Formattable formatPattern
[] = {
1142 UnicodeString emptyStr
;
1143 resultPattern
= MessageFormat::format(appendItemFormats
[topField
], formatPattern
, 3, emptyStr
, err
);
1144 lastMissingFieldMask
= distanceInfo
->missingFieldMask
;
1147 return resultPattern
;
1151 DateTimePatternGenerator::getTopBitNumber(int32_t foundMask
) {
1152 if ( foundMask
==0 ) {
1156 while (foundMask
!=0) {
1160 if (i
-1 >UDATPG_ZONE_FIELD
) {
1161 return UDATPG_ZONE_FIELD
;
1168 DateTimePatternGenerator::setAvailableFormat(const UnicodeString
&key
, UErrorCode
& err
)
1170 fAvailableFormatKeyHash
->puti(key
, 1, err
);
1174 DateTimePatternGenerator::isAvailableFormatSet(const UnicodeString
&key
) const {
1175 return (UBool
)(fAvailableFormatKeyHash
->geti(key
) == 1);
1179 DateTimePatternGenerator::copyHashtable(Hashtable
*other
, UErrorCode
&status
) {
1181 if (other
== NULL
) {
1184 if (fAvailableFormatKeyHash
!= NULL
) {
1185 delete fAvailableFormatKeyHash
;
1186 fAvailableFormatKeyHash
= NULL
;
1188 initHashtable(status
);
1189 if(U_FAILURE(status
)){
1193 const UHashElement
* elem
= NULL
;
1194 // walk through the hash table and create a deep clone
1195 while((elem
= other
->nextElement(pos
))!= NULL
){
1196 const UHashTok otherKeyTok
= elem
->key
;
1197 UnicodeString
* otherKey
= (UnicodeString
*)otherKeyTok
.pointer
;
1198 fAvailableFormatKeyHash
->puti(*otherKey
, 1, status
);
1199 if(U_FAILURE(status
)){
1206 DateTimePatternGenerator::getSkeletons(UErrorCode
& status
) const {
1207 StringEnumeration
* skeletonEnumerator
= new DTSkeletonEnumeration(*patternMap
, DT_SKELETON
, status
);
1208 return skeletonEnumerator
;
1211 const UnicodeString
&
1212 DateTimePatternGenerator::getPatternForSkeleton(const UnicodeString
& skeleton
) const {
1215 if (skeleton
.length() ==0) {
1218 curElem
= patternMap
->getHeader(skeleton
.charAt(0));
1219 while ( curElem
!= NULL
) {
1220 if ( curElem
->skeleton
->getSkeleton()==skeleton
) {
1221 return curElem
->pattern
;
1223 curElem
=curElem
->next
;
1229 DateTimePatternGenerator::getBaseSkeletons(UErrorCode
& status
) const {
1230 StringEnumeration
* baseSkeletonEnumerator
= new DTSkeletonEnumeration(*patternMap
, DT_BASESKELETON
, status
);
1231 return baseSkeletonEnumerator
;
1235 DateTimePatternGenerator::getRedundants(UErrorCode
& status
) {
1236 StringEnumeration
* output
= new DTRedundantEnumeration();
1237 const UnicodeString
*pattern
;
1238 PatternMapIterator it
;
1239 for (it
.set(*patternMap
); it
.hasNext(); ) {
1240 DateTimeMatcher current
= it
.next();
1241 pattern
= patternMap
->getPatternFromSkeleton(*(it
.getSkeleton()));
1242 if ( isCanonicalItem(*pattern
) ) {
1245 if ( skipMatcher
== NULL
) {
1246 skipMatcher
= new DateTimeMatcher(current
);
1249 *skipMatcher
= current
;
1251 UnicodeString trial
= getBestPattern(current
.getPattern(), status
);
1252 if (trial
== *pattern
) {
1253 ((DTRedundantEnumeration
*)output
)->add(*pattern
, status
);
1255 if (current
.equals(skipMatcher
)) {
1263 DateTimePatternGenerator::isCanonicalItem(const UnicodeString
& item
) const {
1264 if ( item
.length() != 1 ) {
1267 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1268 if (item
.charAt(0)==Canonical_Items
[i
]) {
1276 DateTimePatternGenerator
*
1277 DateTimePatternGenerator::clone() const {
1278 return new DateTimePatternGenerator(*this);
1281 PatternMap::PatternMap() {
1282 for (int32_t i
=0; i
< MAX_PATTERN_ENTRIES
; ++i
) {
1285 isDupAllowed
= TRUE
;
1289 PatternMap::copyFrom(const PatternMap
& other
, UErrorCode
& status
) {
1290 this->isDupAllowed
= other
.isDupAllowed
;
1291 for (int32_t bootIndex
=0; bootIndex
<MAX_PATTERN_ENTRIES
; ++bootIndex
) {
1292 PtnElem
*curElem
, *otherElem
, *prevElem
=NULL
;
1293 otherElem
= other
.boot
[bootIndex
];
1294 while (otherElem
!=NULL
) {
1295 if ((curElem
= new PtnElem(otherElem
->basePattern
, otherElem
->pattern
))==NULL
) {
1297 status
= U_MEMORY_ALLOCATION_ERROR
;
1300 if ( this->boot
[bootIndex
]== NULL
) {
1301 this->boot
[bootIndex
] = curElem
;
1303 if ((curElem
->skeleton
=new PtnSkeleton(*(otherElem
->skeleton
))) == NULL
) {
1305 status
= U_MEMORY_ALLOCATION_ERROR
;
1309 if (prevElem
!=NULL
) {
1310 prevElem
->next
=curElem
;
1314 otherElem
= otherElem
->next
;
1321 PatternMap::getHeader(UChar baseChar
) {
1324 if ( (baseChar
>= CAP_A
) && (baseChar
<= CAP_Z
) ) {
1325 curElem
= boot
[baseChar
-CAP_A
];
1328 if ( (baseChar
>=LOW_A
) && (baseChar
<= LOW_Z
) ) {
1329 curElem
= boot
[26+baseChar
-LOW_A
];
1338 PatternMap::~PatternMap() {
1339 for (int32_t i
=0; i
< MAX_PATTERN_ENTRIES
; ++i
) {
1340 if (boot
[i
]!=NULL
) {
1345 } // PatternMap destructor
1348 PatternMap::add(const UnicodeString
& basePattern
,
1349 const PtnSkeleton
& skeleton
,
1350 const UnicodeString
& value
,// mapped pattern value
1351 UBool skeletonWasSpecified
,
1352 UErrorCode
&status
) {
1353 UChar baseChar
= basePattern
.charAt(0);
1354 PtnElem
*curElem
, *baseElem
;
1355 status
= U_ZERO_ERROR
;
1357 // the baseChar must be A-Z or a-z
1358 if ((baseChar
>= CAP_A
) && (baseChar
<= CAP_Z
)) {
1359 baseElem
= boot
[baseChar
-CAP_A
];
1362 if ((baseChar
>=LOW_A
) && (baseChar
<= LOW_Z
)) {
1363 baseElem
= boot
[26+baseChar
-LOW_A
];
1366 status
= U_ILLEGAL_CHARACTER
;
1371 if (baseElem
== NULL
) {
1372 if ((curElem
= new PtnElem(basePattern
, value
)) == NULL
) {
1374 status
= U_MEMORY_ALLOCATION_ERROR
;
1377 if (baseChar
>= LOW_A
) {
1378 boot
[26 + (baseChar
-LOW_A
)] = curElem
;
1381 boot
[baseChar
-CAP_A
] = curElem
;
1383 curElem
->skeleton
= new PtnSkeleton(skeleton
);
1384 curElem
->skeletonWasSpecified
= skeletonWasSpecified
;
1386 if ( baseElem
!= NULL
) {
1387 curElem
= getDuplicateElem(basePattern
, skeleton
, baseElem
);
1389 if (curElem
== NULL
) {
1390 // add new element to the list.
1392 while( curElem
-> next
!= NULL
)
1394 curElem
= curElem
->next
;
1396 if ((curElem
->next
= new PtnElem(basePattern
, value
)) == NULL
) {
1398 status
= U_MEMORY_ALLOCATION_ERROR
;
1401 curElem
=curElem
->next
;
1402 curElem
->skeleton
= new PtnSkeleton(skeleton
);
1403 curElem
->skeletonWasSpecified
= skeletonWasSpecified
;
1406 // Pattern exists in the list already.
1407 if ( !isDupAllowed
) {
1410 // Overwrite the value.
1411 curElem
->pattern
= value
;
1412 // It was a bug that we were not doing the following previously,
1413 // though that bug hid other problems by making things partly work.
1414 curElem
->skeletonWasSpecified
= skeletonWasSpecified
;
1417 } // PatternMap::add
1419 // Find the pattern from the given basePattern string.
1420 const UnicodeString
*
1421 PatternMap::getPatternFromBasePattern(UnicodeString
& basePattern
, UBool
& skeletonWasSpecified
) { // key to search for
1424 if ((curElem
=getHeader(basePattern
.charAt(0)))==NULL
) {
1425 return NULL
; // no match
1429 if ( basePattern
.compare(curElem
->basePattern
)==0 ) {
1430 skeletonWasSpecified
= curElem
->skeletonWasSpecified
;
1431 return &(curElem
->pattern
);
1433 curElem
=curElem
->next
;
1434 }while (curElem
!= NULL
);
1437 } // PatternMap::getFromBasePattern
1440 // Find the pattern from the given skeleton.
1441 // At least when this is called from getBestRaw & addPattern (in which case specifiedSkeletonPtr is non-NULL),
1442 // the comparison should be based on skeleton.original (which is unique and tied to the distance measurement in bestRaw)
1443 // and not skeleton.baseOriginal (which is not unique); otherwise we may pick a different skeleton than the one with the
1444 // optimum distance value in getBestRaw. When this is called from public getRedundants (specifiedSkeletonPtr is NULL),
1445 // for now it will continue to compare based on baseOriginal so as not to change the behavior unnecessarily.
1446 const UnicodeString
*
1447 PatternMap::getPatternFromSkeleton(PtnSkeleton
& skeleton
, const PtnSkeleton
** specifiedSkeletonPtr
) { // key to search for
1450 if (specifiedSkeletonPtr
) {
1451 *specifiedSkeletonPtr
= NULL
;
1455 UChar baseChar
='\0';
1456 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1457 if (skeleton
.baseOriginal
[i
].length() !=0 ) {
1458 baseChar
= skeleton
.baseOriginal
[i
].charAt(0);
1463 if ((curElem
=getHeader(baseChar
))==NULL
) {
1464 return NULL
; // no match
1469 if (specifiedSkeletonPtr
!= NULL
) { // called from DateTimePatternGenerator::getBestRaw or addPattern, use original
1470 for (i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1471 if (curElem
->skeleton
->original
[i
].compare(skeleton
.original
[i
]) != 0 )
1476 } else { // called from DateTimePatternGenerator::getRedundants, use baseOriginal
1477 for (i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1478 if (curElem
->skeleton
->baseOriginal
[i
].compare(skeleton
.baseOriginal
[i
]) != 0 )
1484 if (i
== UDATPG_FIELD_COUNT
) {
1485 if (specifiedSkeletonPtr
&& curElem
->skeletonWasSpecified
) {
1486 *specifiedSkeletonPtr
= curElem
->skeleton
;
1488 return &(curElem
->pattern
);
1490 curElem
=curElem
->next
;
1491 }while (curElem
!= NULL
);
1497 PatternMap::equals(const PatternMap
& other
) {
1498 if ( this==&other
) {
1501 for (int32_t bootIndex
=0; bootIndex
<MAX_PATTERN_ENTRIES
; ++bootIndex
) {
1502 if ( boot
[bootIndex
]==other
.boot
[bootIndex
] ) {
1505 if ( (boot
[bootIndex
]==NULL
)||(other
.boot
[bootIndex
]==NULL
) ) {
1508 PtnElem
*otherElem
= other
.boot
[bootIndex
];
1509 PtnElem
*myElem
= boot
[bootIndex
];
1510 while ((otherElem
!=NULL
) || (myElem
!=NULL
)) {
1511 if ( myElem
== otherElem
) {
1514 if ((otherElem
==NULL
) || (myElem
==NULL
)) {
1517 if ( (myElem
->basePattern
!= otherElem
->basePattern
) ||
1518 (myElem
->pattern
!= otherElem
->pattern
) ) {
1521 if ((myElem
->skeleton
!=otherElem
->skeleton
)&&
1522 !myElem
->skeleton
->equals(*(otherElem
->skeleton
))) {
1525 myElem
= myElem
->next
;
1526 otherElem
=otherElem
->next
;
1532 // find any key existing in the mapping table already.
1533 // return TRUE if there is an existing key, otherwise return FALSE.
1535 PatternMap::getDuplicateElem(
1536 const UnicodeString
&basePattern
,
1537 const PtnSkeleton
&skeleton
,
1538 PtnElem
*baseElem
) {
1541 if ( baseElem
== (PtnElem
*)NULL
) {
1542 return (PtnElem
*)NULL
;
1548 if ( basePattern
.compare(curElem
->basePattern
)==0 ) {
1550 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1551 if (curElem
->skeleton
->type
[i
] != skeleton
.type
[i
] ) {
1560 curElem
= curElem
->next
;
1561 } while( curElem
!= (PtnElem
*)NULL
);
1564 return (PtnElem
*)NULL
;
1566 } // PatternMap::getDuplicateElem
1568 DateTimeMatcher::DateTimeMatcher(void) {
1571 DateTimeMatcher::~DateTimeMatcher() {}
1573 DateTimeMatcher::DateTimeMatcher(const DateTimeMatcher
& other
) {
1574 copyFrom(other
.skeleton
);
1579 DateTimeMatcher::set(const UnicodeString
& pattern
, FormatParser
* fp
) {
1580 PtnSkeleton localSkeleton
;
1581 return set(pattern
, fp
, localSkeleton
);
1585 DateTimeMatcher::set(const UnicodeString
& pattern
, FormatParser
* fp
, PtnSkeleton
& skeletonResult
) {
1587 for (i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1588 skeletonResult
.type
[i
]=NONE
;
1591 for (i
=0; i
< fp
->itemNumber
; i
++) {
1592 UnicodeString field
= fp
->items
[i
];
1593 if ( field
.charAt(0) == LOW_A
) {
1594 continue; // skip 'a'
1597 if ( fp
->isQuoteLiteral(field
) ) {
1598 UnicodeString quoteLiteral
;
1599 fp
->getQuoteLiteral(quoteLiteral
, &i
);
1602 int32_t canonicalIndex
= fp
->getCanonicalIndex(field
);
1603 if (canonicalIndex
< 0 ) {
1606 const dtTypeElem
*row
= &dtTypes
[canonicalIndex
];
1607 int32_t typeValue
= row
->field
;
1608 skeletonResult
.original
[typeValue
]=field
;
1609 UChar repeatChar
= row
->patternChar
;
1610 int32_t repeatCount
= row
->minLen
; // #7930 removes cap at 3
1611 while (repeatCount
-- > 0) {
1612 skeletonResult
.baseOriginal
[typeValue
] += repeatChar
;
1614 int16_t subTypeValue
= row
->type
;
1615 if ( row
->type
> 0) {
1616 subTypeValue
+= field
.length();
1618 skeletonResult
.type
[typeValue
] = subTypeValue
;
1620 copyFrom(skeletonResult
);
1624 DateTimeMatcher::getBasePattern(UnicodeString
&result
) {
1625 result
.remove(); // Reset the result first.
1626 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1627 if (skeleton
.baseOriginal
[i
].length()!=0) {
1628 result
+= skeleton
.baseOriginal
[i
];
1634 DateTimeMatcher::getPattern() {
1635 UnicodeString result
;
1637 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1638 if (skeleton
.original
[i
].length()!=0) {
1639 result
+= skeleton
.original
[i
];
1646 DateTimeMatcher::getDistance(const DateTimeMatcher
& other
, int32_t includeMask
, DistanceInfo
& distanceInfo
) {
1648 distanceInfo
.clear();
1649 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1650 int32_t myType
= (includeMask
&(1<<i
))==0 ? 0 : skeleton
.type
[i
];
1651 int32_t otherType
= other
.skeleton
.type
[i
];
1652 if (myType
==otherType
) {
1655 if (myType
==0) {// and other is not
1656 result
+= EXTRA_FIELD
;
1657 distanceInfo
.addExtra(i
);
1661 result
+= MISSING_FIELD
;
1662 distanceInfo
.addMissing(i
);
1665 result
+= abs(myType
- otherType
);
1674 DateTimeMatcher::copyFrom(const PtnSkeleton
& newSkeleton
) {
1675 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1676 this->skeleton
.type
[i
]=newSkeleton
.type
[i
];
1677 this->skeleton
.original
[i
]=newSkeleton
.original
[i
];
1678 this->skeleton
.baseOriginal
[i
]=newSkeleton
.baseOriginal
[i
];
1683 DateTimeMatcher::copyFrom() {
1685 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1686 this->skeleton
.type
[i
]=0;
1687 this->skeleton
.original
[i
].remove();
1688 this->skeleton
.baseOriginal
[i
].remove();
1693 DateTimeMatcher::equals(const DateTimeMatcher
* other
) const {
1697 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1698 if (this->skeleton
.original
[i
]!=other
->skeleton
.original
[i
] ) {
1706 DateTimeMatcher::getFieldMask() {
1709 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1710 if (skeleton
.type
[i
]!=0) {
1718 DateTimeMatcher::getSkeletonPtr() {
1722 FormatParser::FormatParser () {
1728 FormatParser::~FormatParser () {
1732 // Find the next token with the starting position and length
1733 // Note: the startPos may
1734 FormatParser::TokenStatus
1735 FormatParser::setTokens(const UnicodeString
& pattern
, int32_t startPos
, int32_t *len
) {
1736 int32_t curLoc
= startPos
;
1737 if ( curLoc
>= pattern
.length()) {
1740 // check the current char is between A-Z or a-z
1742 UChar c
=pattern
.charAt(curLoc
);
1743 if ( (c
>=CAP_A
&& c
<=CAP_Z
) || (c
>=LOW_A
&& c
<=LOW_Z
) ) {
1752 if ( pattern
.charAt(curLoc
)!= pattern
.charAt(startPos
) ) {
1753 break; // not the same token
1755 } while(curLoc
<= pattern
.length());
1756 *len
= curLoc
-startPos
;
1761 FormatParser::set(const UnicodeString
& pattern
) {
1763 TokenStatus result
=START
;
1768 result
= setTokens( pattern
, startPos
, &len
);
1769 if ( result
== ADD_TOKEN
)
1771 items
[itemNumber
++] = UnicodeString(pattern
, startPos
, len
);
1777 } while (result
==ADD_TOKEN
&& itemNumber
< MAX_DT_TOKEN
);
1781 FormatParser::getCanonicalIndex(const UnicodeString
& s
, UBool strict
) {
1782 int32_t len
= s
.length();
1786 UChar ch
= s
.charAt(0);
1788 // Verify that all are the same character.
1789 for (int32_t l
= 1; l
< len
; l
++) {
1790 if (ch
!= s
.charAt(l
)) {
1795 int32_t bestRow
= -1;
1796 while (dtTypes
[i
].patternChar
!= '\0') {
1797 if ( dtTypes
[i
].patternChar
!= ch
) {
1802 if (dtTypes
[i
].patternChar
!= dtTypes
[i
+1].patternChar
) {
1805 if (dtTypes
[i
+1].minLen
<= len
) {
1811 return strict
? -1 : bestRow
;
1815 FormatParser::isQuoteLiteral(const UnicodeString
& s
) const {
1816 return (UBool
)(s
.charAt(0)==SINGLE_QUOTE
);
1819 // This function aussumes the current itemIndex points to the quote literal.
1820 // Please call isQuoteLiteral prior to this function.
1822 FormatParser::getQuoteLiteral(UnicodeString
& quote
, int32_t *itemIndex
) {
1823 int32_t i
=*itemIndex
;
1826 if (items
[i
].charAt(0)==SINGLE_QUOTE
) {
1830 while ( i
< itemNumber
) {
1831 if ( items
[i
].charAt(0)==SINGLE_QUOTE
) {
1832 if ( (i
+1<itemNumber
) && (items
[i
+1].charAt(0)==SINGLE_QUOTE
)) {
1833 // two single quotes e.g. 'o''clock'
1834 quote
+= items
[i
++];
1835 quote
+= items
[i
++];
1852 FormatParser::isPatternSeparator(UnicodeString
& field
) {
1853 for (int32_t i
=0; i
<field
.length(); ++i
) {
1854 UChar c
= field
.charAt(i
);
1855 if ( (c
==SINGLE_QUOTE
) || (c
==BACKSLASH
) || (c
==SPACE
) || (c
==COLON
) ||
1856 (c
==QUOTATION_MARK
) || (c
==COMMA
) || (c
==HYPHEN
) ||(items
[i
].charAt(0)==DOT
) ) {
1866 DistanceInfo::~DistanceInfo() {}
1869 DistanceInfo::setTo(DistanceInfo
&other
) {
1870 missingFieldMask
= other
.missingFieldMask
;
1871 extraFieldMask
= other
.extraFieldMask
;
1874 PatternMapIterator::PatternMapIterator() {
1878 matcher
= new DateTimeMatcher();
1882 PatternMapIterator::~PatternMapIterator() {
1887 PatternMapIterator::set(PatternMap
& newPatternMap
) {
1888 this->patternMap
=&newPatternMap
;
1892 PatternMapIterator::getSkeleton() {
1893 if ( nodePtr
== NULL
) {
1897 return nodePtr
->skeleton
;
1902 PatternMapIterator::hasNext() {
1903 int32_t headIndex
=bootIndex
;
1904 PtnElem
*curPtr
=nodePtr
;
1906 if (patternMap
==NULL
) {
1909 while ( headIndex
< MAX_PATTERN_ENTRIES
) {
1910 if ( curPtr
!= NULL
) {
1911 if ( curPtr
->next
!= NULL
) {
1921 if ( patternMap
->boot
[headIndex
] != NULL
) {
1935 PatternMapIterator::next() {
1936 while ( bootIndex
< MAX_PATTERN_ENTRIES
) {
1937 if ( nodePtr
!= NULL
) {
1938 if ( nodePtr
->next
!= NULL
) {
1939 nodePtr
= nodePtr
->next
;
1949 if ( patternMap
->boot
[bootIndex
] != NULL
) {
1950 nodePtr
= patternMap
->boot
[bootIndex
];
1959 if (nodePtr
!=NULL
) {
1960 matcher
->copyFrom(*nodePtr
->skeleton
);
1963 matcher
->copyFrom();
1968 PtnSkeleton::PtnSkeleton() {
1972 PtnSkeleton::PtnSkeleton(const PtnSkeleton
& other
) {
1973 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1974 this->type
[i
]=other
.type
[i
];
1975 this->original
[i
]=other
.original
[i
];
1976 this->baseOriginal
[i
]=other
.baseOriginal
[i
];
1981 PtnSkeleton::equals(const PtnSkeleton
& other
) {
1982 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
1983 if ( (type
[i
]!= other
.type
[i
]) ||
1984 (original
[i
]!=other
.original
[i
]) ||
1985 (baseOriginal
[i
]!=other
.baseOriginal
[i
]) ) {
1993 PtnSkeleton::getSkeleton() {
1994 UnicodeString result
;
1996 for(int32_t i
=0; i
< UDATPG_FIELD_COUNT
; ++i
) {
1997 if (original
[i
].length()!=0) {
1998 result
+= original
[i
];
2005 PtnSkeleton::getBaseSkeleton() {
2006 UnicodeString result
;
2008 for(int32_t i
=0; i
< UDATPG_FIELD_COUNT
; ++i
) {
2009 if (baseOriginal
[i
].length()!=0) {
2010 result
+= baseOriginal
[i
];
2016 PtnSkeleton::~PtnSkeleton() {
2019 PtnElem::PtnElem(const UnicodeString
&basePat
, const UnicodeString
&pat
) :
2020 basePattern(basePat
),
2027 PtnElem::~PtnElem() {
2035 DTSkeletonEnumeration::DTSkeletonEnumeration(PatternMap
&patternMap
, dtStrEnum type
, UErrorCode
& status
) {
2037 PtnSkeleton
*curSkeleton
;
2042 fSkeletons
= new UVector(status
);
2043 if (U_FAILURE(status
)) {
2047 for (bootIndex
=0; bootIndex
<MAX_PATTERN_ENTRIES
; ++bootIndex
) {
2048 curElem
= patternMap
.boot
[bootIndex
];
2049 while (curElem
!=NULL
) {
2051 case DT_BASESKELETON
:
2052 s
=curElem
->basePattern
;
2058 curSkeleton
=curElem
->skeleton
;
2059 s
=curSkeleton
->getSkeleton();
2062 if ( !isCanonicalItem(s
) ) {
2063 fSkeletons
->addElement(new UnicodeString(s
), status
);
2064 if (U_FAILURE(status
)) {
2070 curElem
= curElem
->next
;
2073 if ((bootIndex
==MAX_PATTERN_ENTRIES
) && (curElem
!=NULL
) ) {
2074 status
= U_BUFFER_OVERFLOW_ERROR
;
2078 const UnicodeString
*
2079 DTSkeletonEnumeration::snext(UErrorCode
& status
) {
2080 if (U_SUCCESS(status
) && pos
< fSkeletons
->size()) {
2081 return (const UnicodeString
*)fSkeletons
->elementAt(pos
++);
2087 DTSkeletonEnumeration::reset(UErrorCode
& /*status*/) {
2092 DTSkeletonEnumeration::count(UErrorCode
& /*status*/) const {
2093 return (fSkeletons
==NULL
) ? 0 : fSkeletons
->size();
2097 DTSkeletonEnumeration::isCanonicalItem(const UnicodeString
& item
) {
2098 if ( item
.length() != 1 ) {
2101 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
2102 if (item
.charAt(0)==Canonical_Items
[i
]) {
2109 DTSkeletonEnumeration::~DTSkeletonEnumeration() {
2111 for (int32_t i
=0; i
<fSkeletons
->size(); ++i
) {
2112 if ((s
=(UnicodeString
*)fSkeletons
->elementAt(i
))!=NULL
) {
2119 DTRedundantEnumeration::DTRedundantEnumeration() {
2125 DTRedundantEnumeration::add(const UnicodeString
& pattern
, UErrorCode
& status
) {
2126 if (U_FAILURE(status
)) return;
2127 if (fPatterns
== NULL
) {
2128 fPatterns
= new UVector(status
);
2129 if (U_FAILURE(status
)) {
2135 fPatterns
->addElement(new UnicodeString(pattern
), status
);
2136 if (U_FAILURE(status
)) {
2143 const UnicodeString
*
2144 DTRedundantEnumeration::snext(UErrorCode
& status
) {
2145 if (U_SUCCESS(status
) && pos
< fPatterns
->size()) {
2146 return (const UnicodeString
*)fPatterns
->elementAt(pos
++);
2152 DTRedundantEnumeration::reset(UErrorCode
& /*status*/) {
2157 DTRedundantEnumeration::count(UErrorCode
& /*status*/) const {
2158 return (fPatterns
==NULL
) ? 0 : fPatterns
->size();
2162 DTRedundantEnumeration::isCanonicalItem(const UnicodeString
& item
) {
2163 if ( item
.length() != 1 ) {
2166 for (int32_t i
=0; i
<UDATPG_FIELD_COUNT
; ++i
) {
2167 if (item
.charAt(0)==Canonical_Items
[i
]) {
2174 DTRedundantEnumeration::~DTRedundantEnumeration() {
2176 for (int32_t i
=0; i
<fPatterns
->size(); ++i
) {
2177 if ((s
=(UnicodeString
*)fPatterns
->elementAt(i
))!=NULL
) {
2187 #endif /* #if !UCONFIG_NO_FORMATTING */