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