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