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