1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html |
3 | /******************************************************************************* |
4 | * Copyright (C) 2008-2016, International Business Machines Corporation and |
5 | * others. All Rights Reserved. |
6 | ******************************************************************************* |
7 | * |
8 | * File DTITVINF.CPP |
9 | * |
10 | ******************************************************************************* |
11 | */ |
12 | |
13 | #include "unicode/dtitvinf.h" |
14 | |
15 | |
16 | #if !UCONFIG_NO_FORMATTING |
17 | |
18 | //TODO: define it in compiler time |
19 | //#define DTITVINF_DEBUG 1 |
20 | |
21 | |
22 | #ifdef DTITVINF_DEBUG |
23 | #include <iostream> |
24 | #endif |
25 | |
26 | #include "cmemory.h" |
27 | #include "cstring.h" |
28 | #include "unicode/msgfmt.h" |
29 | #include "unicode/uloc.h" |
30 | #include "unicode/ures.h" |
31 | #include "dtitv_impl.h" |
32 | #include "charstr.h" |
33 | #include "hash.h" |
34 | #include "gregoimp.h" |
35 | #include "uresimp.h" |
36 | #include "hash.h" |
37 | #include "gregoimp.h" |
38 | #include "uresimp.h" |
39 | |
40 | |
41 | U_NAMESPACE_BEGIN |
42 | |
43 | |
44 | #ifdef DTITVINF_DEBUG |
45 | #define PRINTMESG(msg) UPRV_BLOCK_MACRO_BEGIN { \ |
46 | std::cout << "(" << __FILE__ << ":" << __LINE__ << ") " << msg << "\n"; \ |
47 | } UPRV_BLOCK_MACRO_END |
48 | #endif |
49 | |
50 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateIntervalInfo) |
51 | |
52 | static const char gCalendarTag[]="calendar" ; |
53 | static const char gGenericTag[]="generic" ; |
54 | static const char gGregorianTag[]="gregorian" ; |
55 | static const char gIntervalDateTimePatternTag[]="intervalFormats" ; |
56 | static const char gFallbackPatternTag[]="fallback" ; |
57 | |
58 | // {0} |
59 | static const UChar gFirstPattern[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET}; |
60 | // {1} |
61 | static const UChar gSecondPattern[] = {LEFT_CURLY_BRACKET, DIGIT_ONE, RIGHT_CURLY_BRACKET}; |
62 | |
63 | // default fall-back |
64 | static const UChar gDefaultFallbackPattern[] = {LEFT_CURLY_BRACKET, DIGIT_ZERO, RIGHT_CURLY_BRACKET, SPACE, EN_DASH, SPACE, LEFT_CURLY_BRACKET, DIGIT_ONE, RIGHT_CURLY_BRACKET, 0}; |
65 | |
66 | DateIntervalInfo::DateIntervalInfo(UErrorCode& status) |
67 | : fFallbackIntervalPattern(gDefaultFallbackPattern), |
68 | fFirstDateInPtnIsLaterDate(false), |
69 | fIntervalPatterns(NULL) |
70 | { |
71 | fIntervalPatterns = initHash(status); |
72 | } |
73 | |
74 | |
75 | |
76 | DateIntervalInfo::DateIntervalInfo(const Locale& locale, UErrorCode& status) |
77 | : fFallbackIntervalPattern(gDefaultFallbackPattern), |
78 | fFirstDateInPtnIsLaterDate(false), |
79 | fIntervalPatterns(NULL) |
80 | { |
81 | initializeData(locale, status); |
82 | } |
83 | |
84 | |
85 | |
86 | void |
87 | DateIntervalInfo::setIntervalPattern(const UnicodeString& skeleton, |
88 | UCalendarDateFields lrgDiffCalUnit, |
89 | const UnicodeString& intervalPattern, |
90 | UErrorCode& status) { |
91 | |
92 | if ( lrgDiffCalUnit == UCAL_HOUR_OF_DAY ) { |
93 | setIntervalPatternInternally(skeleton, UCAL_AM_PM, intervalPattern, status); |
94 | setIntervalPatternInternally(skeleton, UCAL_HOUR, intervalPattern, status); |
95 | } else if ( lrgDiffCalUnit == UCAL_DAY_OF_MONTH || |
96 | lrgDiffCalUnit == UCAL_DAY_OF_WEEK ) { |
97 | setIntervalPatternInternally(skeleton, UCAL_DATE, intervalPattern, status); |
98 | } else { |
99 | setIntervalPatternInternally(skeleton, lrgDiffCalUnit, intervalPattern, status); |
100 | } |
101 | } |
102 | |
103 | |
104 | void |
105 | DateIntervalInfo::setFallbackIntervalPattern( |
106 | const UnicodeString& fallbackPattern, |
107 | UErrorCode& status) { |
108 | if ( U_FAILURE(status) ) { |
109 | return; |
110 | } |
111 | int32_t firstPatternIndex = fallbackPattern.indexOf(gFirstPattern, |
112 | UPRV_LENGTHOF(gFirstPattern), 0); |
113 | int32_t secondPatternIndex = fallbackPattern.indexOf(gSecondPattern, |
114 | UPRV_LENGTHOF(gSecondPattern), 0); |
115 | if ( firstPatternIndex == -1 || secondPatternIndex == -1 ) { |
116 | status = U_ILLEGAL_ARGUMENT_ERROR; |
117 | return; |
118 | } |
119 | if ( firstPatternIndex > secondPatternIndex ) { |
120 | fFirstDateInPtnIsLaterDate = true; |
121 | } |
122 | fFallbackIntervalPattern = fallbackPattern; |
123 | } |
124 | |
125 | |
126 | |
127 | DateIntervalInfo::DateIntervalInfo(const DateIntervalInfo& dtitvinf) |
128 | : UObject(dtitvinf), |
129 | fIntervalPatterns(NULL) |
130 | { |
131 | *this = dtitvinf; |
132 | } |
133 | |
134 | |
135 | |
136 | DateIntervalInfo& |
137 | DateIntervalInfo::operator=(const DateIntervalInfo& dtitvinf) { |
138 | if ( this == &dtitvinf ) { |
139 | return *this; |
140 | } |
141 | |
142 | UErrorCode status = U_ZERO_ERROR; |
143 | deleteHash(fIntervalPatterns); |
144 | fIntervalPatterns = initHash(status); |
145 | copyHash(dtitvinf.fIntervalPatterns, fIntervalPatterns, status); |
146 | if ( U_FAILURE(status) ) { |
147 | return *this; |
148 | } |
149 | |
150 | fFallbackIntervalPattern = dtitvinf.fFallbackIntervalPattern; |
151 | fFirstDateInPtnIsLaterDate = dtitvinf.fFirstDateInPtnIsLaterDate; |
152 | return *this; |
153 | } |
154 | |
155 | |
156 | DateIntervalInfo* |
157 | DateIntervalInfo::clone() const { |
158 | return new DateIntervalInfo(*this); |
159 | } |
160 | |
161 | |
162 | DateIntervalInfo::~DateIntervalInfo() { |
163 | deleteHash(fIntervalPatterns); |
164 | fIntervalPatterns = NULL; |
165 | } |
166 | |
167 | |
168 | UBool |
169 | DateIntervalInfo::operator==(const DateIntervalInfo& other) const { |
170 | UBool equal = ( |
171 | fFallbackIntervalPattern == other.fFallbackIntervalPattern && |
172 | fFirstDateInPtnIsLaterDate == other.fFirstDateInPtnIsLaterDate ); |
173 | |
174 | if ( equal == TRUE ) { |
175 | equal = fIntervalPatterns->equals(*(other.fIntervalPatterns)); |
176 | } |
177 | |
178 | return equal; |
179 | } |
180 | |
181 | |
182 | UnicodeString& |
183 | DateIntervalInfo::getIntervalPattern(const UnicodeString& skeleton, |
184 | UCalendarDateFields field, |
185 | UnicodeString& result, |
186 | UErrorCode& status) const { |
187 | if ( U_FAILURE(status) ) { |
188 | return result; |
189 | } |
190 | |
191 | const UnicodeString* patternsOfOneSkeleton = (UnicodeString*) fIntervalPatterns->get(skeleton); |
192 | if ( patternsOfOneSkeleton != NULL ) { |
193 | IntervalPatternIndex index = calendarFieldToIntervalIndex(field, status); |
194 | if ( U_FAILURE(status) ) { |
195 | return result; |
196 | } |
197 | const UnicodeString& intervalPattern = patternsOfOneSkeleton[index]; |
198 | if ( !intervalPattern.isEmpty() ) { |
199 | result = intervalPattern; |
200 | } |
201 | } |
202 | return result; |
203 | } |
204 | |
205 | |
206 | UBool |
207 | DateIntervalInfo::getDefaultOrder() const { |
208 | return fFirstDateInPtnIsLaterDate; |
209 | } |
210 | |
211 | |
212 | UnicodeString& |
213 | DateIntervalInfo::getFallbackIntervalPattern(UnicodeString& result) const { |
214 | result = fFallbackIntervalPattern; |
215 | return result; |
216 | } |
217 | |
218 | #define ULOC_LOCALE_IDENTIFIER_CAPACITY (ULOC_FULLNAME_CAPACITY + 1 + ULOC_KEYWORD_AND_VALUES_CAPACITY) |
219 | |
220 | |
221 | static const int32_t PATH_PREFIX_LENGTH = 17; |
222 | static const UChar PATH_PREFIX[] = {SOLIDUS, CAP_L, CAP_O, CAP_C, CAP_A, CAP_L, CAP_E, SOLIDUS, |
223 | LOW_C, LOW_A, LOW_L, LOW_E, LOW_N, LOW_D, LOW_A, LOW_R, SOLIDUS}; |
224 | static const int32_t PATH_SUFFIX_LENGTH = 16; |
225 | static const UChar PATH_SUFFIX[] = {SOLIDUS, LOW_I, LOW_N, LOW_T, LOW_E, LOW_R, LOW_V, LOW_A, |
226 | LOW_L, CAP_F, LOW_O, LOW_R, LOW_M, LOW_A, LOW_T, LOW_S}; |
227 | |
228 | /** |
229 | * Sink for enumerating all of the date interval skeletons. |
230 | */ |
231 | struct DateIntervalInfo::DateIntervalSink : public ResourceSink { |
232 | |
233 | // Output data |
234 | DateIntervalInfo &dateIntervalInfo; |
235 | |
236 | // Next calendar type |
237 | UnicodeString nextCalendarType; |
238 | |
239 | DateIntervalSink(DateIntervalInfo &diInfo, const char *currentCalendarType) |
240 | : dateIntervalInfo(diInfo), nextCalendarType(currentCalendarType, -1, US_INV) { } |
241 | virtual ~DateIntervalSink(); |
242 | |
243 | virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &errorCode) { |
244 | if (U_FAILURE(errorCode)) { return; } |
245 | |
246 | // Iterate over all the calendar entries and only pick the 'intervalFormats' table. |
247 | ResourceTable dateIntervalData = value.getTable(errorCode); |
248 | if (U_FAILURE(errorCode)) { return; } |
249 | for (int32_t i = 0; dateIntervalData.getKeyAndValue(i, key, value); i++) { |
250 | if (uprv_strcmp(key, gIntervalDateTimePatternTag) != 0) { |
251 | continue; |
252 | } |
253 | |
254 | // Handle aliases and tables. Ignore the rest. |
255 | if (value.getType() == URES_ALIAS) { |
256 | // Get the calendar type for the alias path. |
257 | const UnicodeString &aliasPath = value.getAliasUnicodeString(errorCode); |
258 | if (U_FAILURE(errorCode)) { return; } |
259 | |
260 | nextCalendarType.remove(); |
261 | getCalendarTypeFromPath(aliasPath, nextCalendarType, errorCode); |
262 | |
263 | if (U_FAILURE(errorCode)) { |
264 | resetNextCalendarType(); |
265 | } |
266 | break; |
267 | |
268 | } else if (value.getType() == URES_TABLE) { |
269 | // Iterate over all the skeletons in the 'intervalFormat' table. |
270 | ResourceTable skeletonData = value.getTable(errorCode); |
271 | if (U_FAILURE(errorCode)) { return; } |
272 | for (int32_t j = 0; skeletonData.getKeyAndValue(j, key, value); j++) { |
273 | if (value.getType() == URES_TABLE) { |
274 | // Process the skeleton |
275 | processSkeletonTable(key, value, errorCode); |
276 | if (U_FAILURE(errorCode)) { return; } |
277 | } |
278 | } |
279 | break; |
280 | } |
281 | } |
282 | } |
283 | |
284 | /** |
285 | * Processes the patterns for a skeleton table |
286 | */ |
287 | void processSkeletonTable(const char *key, ResourceValue &value, UErrorCode &errorCode) { |
288 | if (U_FAILURE(errorCode)) { return; } |
289 | |
290 | // Iterate over all the patterns in the current skeleton table |
291 | const char *currentSkeleton = key; |
292 | ResourceTable patternData = value.getTable(errorCode); |
293 | if (U_FAILURE(errorCode)) { return; } |
294 | for (int32_t k = 0; patternData.getKeyAndValue(k, key, value); k++) { |
295 | if (value.getType() == URES_STRING) { |
296 | // Process the key |
297 | UCalendarDateFields calendarField = validateAndProcessPatternLetter(key); |
298 | |
299 | // If the calendar field has a valid value |
300 | if (calendarField < UCAL_FIELD_COUNT) { |
301 | // Set the interval pattern |
302 | setIntervalPatternIfAbsent(currentSkeleton, calendarField, value, errorCode); |
303 | if (U_FAILURE(errorCode)) { return; } |
304 | } |
305 | } |
306 | } |
307 | } |
308 | |
309 | /** |
310 | * Extracts the calendar type from the path. |
311 | */ |
312 | static void getCalendarTypeFromPath(const UnicodeString &path, UnicodeString &calendarType, |
313 | UErrorCode &errorCode) { |
314 | if (U_FAILURE(errorCode)) { return; } |
315 | |
316 | if (!path.startsWith(PATH_PREFIX, PATH_PREFIX_LENGTH) || !path.endsWith(PATH_SUFFIX, PATH_SUFFIX_LENGTH)) { |
317 | errorCode = U_INVALID_FORMAT_ERROR; |
318 | return; |
319 | } |
320 | |
321 | path.extractBetween(PATH_PREFIX_LENGTH, path.length() - PATH_SUFFIX_LENGTH, calendarType); |
322 | } |
323 | |
324 | /** |
325 | * Validates and processes the pattern letter |
326 | */ |
327 | UCalendarDateFields validateAndProcessPatternLetter(const char *patternLetter) { |
328 | // Check that patternLetter is just one letter |
329 | char c0; |
330 | if ((c0 = patternLetter[0]) != 0 && patternLetter[1] == 0) { |
331 | // Check that the pattern letter is accepted |
332 | if (c0 == 'G') { |
333 | return UCAL_ERA; |
334 | } else if (c0 == 'y') { |
335 | return UCAL_YEAR; |
336 | } else if (c0 == 'M') { |
337 | return UCAL_MONTH; |
338 | } else if (c0 == 'd') { |
339 | return UCAL_DATE; |
340 | } else if (c0 == 'a') { |
341 | return UCAL_AM_PM; |
342 | } else if (c0 == 'h' || c0 == 'H') { |
343 | return UCAL_HOUR; |
344 | } else if (c0 == 'm') { |
345 | return UCAL_MINUTE; |
346 | }// TODO(ticket:12190): Why icu4c doesn't accept the calendar field "s" but icu4j does? |
347 | } |
348 | return UCAL_FIELD_COUNT; |
349 | } |
350 | |
351 | /** |
352 | * Stores the interval pattern for the current skeleton in the internal data structure |
353 | * if it's not present. |
354 | */ |
355 | void setIntervalPatternIfAbsent(const char *currentSkeleton, UCalendarDateFields lrgDiffCalUnit, |
356 | const ResourceValue &value, UErrorCode &errorCode) { |
357 | // Check if the pattern has already been stored on the data structure |
358 | IntervalPatternIndex index = |
359 | dateIntervalInfo.calendarFieldToIntervalIndex(lrgDiffCalUnit, errorCode); |
360 | if (U_FAILURE(errorCode)) { return; } |
361 | |
362 | UnicodeString skeleton(currentSkeleton, -1, US_INV); |
363 | UnicodeString* patternsOfOneSkeleton = |
364 | (UnicodeString*)(dateIntervalInfo.fIntervalPatterns->get(skeleton)); |
365 | |
366 | if (patternsOfOneSkeleton == NULL || patternsOfOneSkeleton[index].isEmpty()) { |
367 | UnicodeString pattern = value.getUnicodeString(errorCode); |
368 | dateIntervalInfo.setIntervalPatternInternally(skeleton, lrgDiffCalUnit, |
369 | pattern, errorCode); |
370 | } |
371 | } |
372 | |
373 | const UnicodeString &getNextCalendarType() { |
374 | return nextCalendarType; |
375 | } |
376 | |
377 | void resetNextCalendarType() { |
378 | nextCalendarType.setToBogus(); |
379 | } |
380 | }; |
381 | |
382 | // Virtual destructors must be defined out of line. |
383 | DateIntervalInfo::DateIntervalSink::~DateIntervalSink() {} |
384 | |
385 | |
386 | |
387 | void |
388 | DateIntervalInfo::initializeData(const Locale& locale, UErrorCode& status) |
389 | { |
390 | fIntervalPatterns = initHash(status); |
391 | if (U_FAILURE(status)) { |
392 | return; |
393 | } |
394 | const char *locName = locale.getName(); |
395 | |
396 | // Get the correct calendar type |
397 | const char * calendarTypeToUse = gGregorianTag; // initial default |
398 | char calendarType[ULOC_KEYWORDS_CAPACITY]; // to be filled in with the type to use, if all goes well |
399 | char localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY]; |
400 | // obtain a locale that always has the calendar key value that should be used |
401 | (void)ures_getFunctionalEquivalent(localeWithCalendarKey, ULOC_LOCALE_IDENTIFIER_CAPACITY, NULL, |
402 | "calendar" , "calendar" , locName, NULL, FALSE, &status); |
403 | localeWithCalendarKey[ULOC_LOCALE_IDENTIFIER_CAPACITY-1] = 0; // ensure null termination |
404 | // now get the calendar key value from that locale |
405 | int32_t calendarTypeLen = uloc_getKeywordValue(localeWithCalendarKey, "calendar" , calendarType, |
406 | ULOC_KEYWORDS_CAPACITY, &status); |
407 | if (U_SUCCESS(status) && calendarTypeLen < ULOC_KEYWORDS_CAPACITY) { |
408 | calendarTypeToUse = calendarType; |
409 | } |
410 | status = U_ZERO_ERROR; |
411 | |
412 | // Instantiate the resource bundles |
413 | UResourceBundle *rb, *calBundle; |
414 | rb = ures_open(NULL, locName, &status); |
415 | if (U_FAILURE(status)) { |
416 | return; |
417 | } |
418 | calBundle = ures_getByKeyWithFallback(rb, gCalendarTag, NULL, &status); |
419 | |
420 | |
421 | if (U_SUCCESS(status)) { |
422 | UResourceBundle *calTypeBundle, *itvDtPtnResource; |
423 | |
424 | // Get the fallback pattern |
425 | const UChar* resStr = nullptr; |
426 | int32_t resStrLen = 0; |
427 | calTypeBundle = ures_getByKeyWithFallback(calBundle, calendarTypeToUse, NULL, &status); |
428 | itvDtPtnResource = ures_getByKeyWithFallback(calTypeBundle, |
429 | gIntervalDateTimePatternTag, NULL, &status); |
430 | // TODO(ICU-20400): After the fixing, we should find the "fallback" from |
431 | // the rb directly by the path "calendar/${calendar}/intervalFormats/fallback". |
432 | if ( U_SUCCESS(status) ) { |
433 | resStr = ures_getStringByKeyWithFallback(itvDtPtnResource, gFallbackPatternTag, |
434 | &resStrLen, &status); |
435 | if ( U_FAILURE(status) ) { |
436 | // Try to find "fallback" from "generic" to work around the bug in |
437 | // ures_getByKeyWithFallback |
438 | UErrorCode localStatus = U_ZERO_ERROR; |
439 | UResourceBundle *genericCalBundle = |
440 | ures_getByKeyWithFallback(calBundle, gGenericTag, NULL, &localStatus); |
441 | UResourceBundle *genericItvDtPtnResource = |
442 | ures_getByKeyWithFallback( |
443 | genericCalBundle, gIntervalDateTimePatternTag, NULL, &localStatus); |
444 | resStr = ures_getStringByKeyWithFallback( |
445 | genericItvDtPtnResource, gFallbackPatternTag, &resStrLen, &localStatus); |
446 | ures_close(genericItvDtPtnResource); |
447 | ures_close(genericCalBundle); |
448 | if ( U_SUCCESS(localStatus) ) { |
449 | status = U_USING_FALLBACK_WARNING;; |
450 | } |
451 | } |
452 | } |
453 | |
454 | if ( U_SUCCESS(status) && (resStr != nullptr)) { |
455 | UnicodeString pattern = UnicodeString(TRUE, resStr, resStrLen); |
456 | setFallbackIntervalPattern(pattern, status); |
457 | } |
458 | ures_close(itvDtPtnResource); |
459 | ures_close(calTypeBundle); |
460 | |
461 | |
462 | // Instantiate the sink |
463 | DateIntervalSink sink(*this, calendarTypeToUse); |
464 | const UnicodeString &calendarTypeToUseUString = sink.getNextCalendarType(); |
465 | |
466 | // Already loaded calendar types |
467 | Hashtable loadedCalendarTypes(FALSE, status); |
468 | |
469 | if (U_SUCCESS(status)) { |
470 | while (!calendarTypeToUseUString.isBogus()) { |
471 | // Set an error when a loop is detected |
472 | if (loadedCalendarTypes.geti(calendarTypeToUseUString) == 1) { |
473 | status = U_INVALID_FORMAT_ERROR; |
474 | break; |
475 | } |
476 | |
477 | // Register the calendar type to avoid loops |
478 | loadedCalendarTypes.puti(calendarTypeToUseUString, 1, status); |
479 | if (U_FAILURE(status)) { break; } |
480 | |
481 | // Get the calendar string |
482 | CharString calTypeBuffer; |
483 | calTypeBuffer.appendInvariantChars(calendarTypeToUseUString, status); |
484 | if (U_FAILURE(status)) { break; } |
485 | const char *calType = calTypeBuffer.data(); |
486 | |
487 | // Reset the next calendar type to load. |
488 | sink.resetNextCalendarType(); |
489 | |
490 | // Get all resources for this calendar type |
491 | ures_getAllItemsWithFallback(calBundle, calType, sink, status); |
492 | } |
493 | } |
494 | } |
495 | |
496 | // Close the opened resource bundles |
497 | ures_close(calBundle); |
498 | ures_close(rb); |
499 | } |
500 | |
501 | void |
502 | DateIntervalInfo::setIntervalPatternInternally(const UnicodeString& skeleton, |
503 | UCalendarDateFields lrgDiffCalUnit, |
504 | const UnicodeString& intervalPattern, |
505 | UErrorCode& status) { |
506 | IntervalPatternIndex index = calendarFieldToIntervalIndex(lrgDiffCalUnit,status); |
507 | if ( U_FAILURE(status) ) { |
508 | return; |
509 | } |
510 | UnicodeString* patternsOfOneSkeleton = (UnicodeString*)(fIntervalPatterns->get(skeleton)); |
511 | UBool emptyHash = false; |
512 | if ( patternsOfOneSkeleton == NULL ) { |
513 | patternsOfOneSkeleton = new UnicodeString[kIPI_MAX_INDEX]; |
514 | emptyHash = true; |
515 | } |
516 | |
517 | patternsOfOneSkeleton[index] = intervalPattern; |
518 | if ( emptyHash == TRUE ) { |
519 | fIntervalPatterns->put(skeleton, patternsOfOneSkeleton, status); |
520 | } |
521 | } |
522 | |
523 | |
524 | |
525 | void |
526 | DateIntervalInfo::parseSkeleton(const UnicodeString& skeleton, |
527 | int32_t* skeletonFieldWidth) { |
528 | const int8_t PATTERN_CHAR_BASE = 0x41; |
529 | int32_t i; |
530 | for ( i = 0; i < skeleton.length(); ++i ) { |
531 | // it is an ASCII char in skeleton |
532 | int8_t ch = (int8_t)skeleton.charAt(i); |
533 | ++skeletonFieldWidth[ch - PATTERN_CHAR_BASE]; |
534 | } |
535 | } |
536 | |
537 | |
538 | |
539 | UBool |
540 | DateIntervalInfo::stringNumeric(int32_t fieldWidth, int32_t anotherFieldWidth, |
541 | char patternLetter) { |
542 | if ( patternLetter == 'M' ) { |
543 | if ( (fieldWidth <= 2 && anotherFieldWidth > 2) || |
544 | (fieldWidth > 2 && anotherFieldWidth <= 2 )) { |
545 | return true; |
546 | } |
547 | } |
548 | return false; |
549 | } |
550 | |
551 | |
552 | |
553 | const UnicodeString* |
554 | DateIntervalInfo::getBestSkeleton(const UnicodeString& skeleton, |
555 | int8_t& bestMatchDistanceInfo) const { |
556 | #ifdef DTITVINF_DEBUG |
557 | char result[1000]; |
558 | char result_1[1000]; |
559 | char mesg[2000]; |
560 | skeleton.extract(0, skeleton.length(), result, "UTF-8" ); |
561 | sprintf(mesg, "in getBestSkeleton: skeleton: %s; \n" , result); |
562 | PRINTMESG(mesg) |
563 | #endif |
564 | |
565 | |
566 | int32_t inputSkeletonFieldWidth[] = |
567 | { |
568 | // A B C D E F G H I J K L M N O |
569 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
570 | // P Q R S T U V W X Y Z |
571 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
572 | // a b c d e f g h i j k l m n o |
573 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
574 | // p q r s t u v w x y z |
575 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
576 | }; |
577 | |
578 | int32_t skeletonFieldWidth[] = |
579 | { |
580 | // A B C D E F G H I J K L M N O |
581 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
582 | // P Q R S T U V W X Y Z |
583 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
584 | // a b c d e f g h i j k l m n o |
585 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
586 | // p q r s t u v w x y z |
587 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
588 | }; |
589 | |
590 | const int32_t DIFFERENT_FIELD = 0x1000; |
591 | const int32_t STRING_NUMERIC_DIFFERENCE = 0x100; |
592 | const int32_t BASE = 0x41; |
593 | const UChar CHAR_V = 0x0076; |
594 | const UChar CHAR_Z = 0x007A; |
595 | |
596 | // hack for 'v' and 'z'. |
597 | // resource bundle only have time skeletons ending with 'v', |
598 | // but not for time skeletons ending with 'z'. |
599 | UBool replaceZWithV = false; |
600 | const UnicodeString* inputSkeleton = &skeleton; |
601 | UnicodeString copySkeleton; |
602 | if ( skeleton.indexOf(CHAR_Z) != -1 ) { |
603 | copySkeleton = skeleton; |
604 | copySkeleton.findAndReplace(UnicodeString(CHAR_Z), UnicodeString(CHAR_V)); |
605 | inputSkeleton = ©Skeleton; |
606 | replaceZWithV = true; |
607 | } |
608 | |
609 | parseSkeleton(*inputSkeleton, inputSkeletonFieldWidth); |
610 | int32_t bestDistance = MAX_POSITIVE_INT; |
611 | const UnicodeString* bestSkeleton = NULL; |
612 | |
613 | // 0 means exact the same skeletons; |
614 | // 1 means having the same field, but with different length, |
615 | // 2 means only z/v differs |
616 | // -1 means having different field. |
617 | bestMatchDistanceInfo = 0; |
618 | int8_t fieldLength = UPRV_LENGTHOF(skeletonFieldWidth); |
619 | |
620 | int32_t pos = UHASH_FIRST; |
621 | const UHashElement* elem = NULL; |
622 | while ( (elem = fIntervalPatterns->nextElement(pos)) != NULL ) { |
623 | const UHashTok keyTok = elem->key; |
624 | UnicodeString* newSkeleton = (UnicodeString*)keyTok.pointer; |
625 | #ifdef DTITVINF_DEBUG |
626 | skeleton->extract(0, skeleton->length(), result, "UTF-8" ); |
627 | sprintf(mesg, "available skeletons: skeleton: %s; \n" , result); |
628 | PRINTMESG(mesg) |
629 | #endif |
630 | |
631 | // clear skeleton field width |
632 | int8_t i; |
633 | for ( i = 0; i < fieldLength; ++i ) { |
634 | skeletonFieldWidth[i] = 0; |
635 | } |
636 | parseSkeleton(*newSkeleton, skeletonFieldWidth); |
637 | // calculate distance |
638 | int32_t distance = 0; |
639 | int8_t fieldDifference = 1; |
640 | for ( i = 0; i < fieldLength; ++i ) { |
641 | int32_t inputFieldWidth = inputSkeletonFieldWidth[i]; |
642 | int32_t fieldWidth = skeletonFieldWidth[i]; |
643 | if ( inputFieldWidth == fieldWidth ) { |
644 | continue; |
645 | } |
646 | if ( inputFieldWidth == 0 ) { |
647 | fieldDifference = -1; |
648 | distance += DIFFERENT_FIELD; |
649 | } else if ( fieldWidth == 0 ) { |
650 | fieldDifference = -1; |
651 | distance += DIFFERENT_FIELD; |
652 | } else if (stringNumeric(inputFieldWidth, fieldWidth, |
653 | (char)(i+BASE) ) ) { |
654 | distance += STRING_NUMERIC_DIFFERENCE; |
655 | } else { |
656 | distance += (inputFieldWidth > fieldWidth) ? |
657 | (inputFieldWidth - fieldWidth) : |
658 | (fieldWidth - inputFieldWidth); |
659 | } |
660 | } |
661 | if ( distance < bestDistance ) { |
662 | bestSkeleton = newSkeleton; |
663 | bestDistance = distance; |
664 | bestMatchDistanceInfo = fieldDifference; |
665 | } |
666 | if ( distance == 0 ) { |
667 | bestMatchDistanceInfo = 0; |
668 | break; |
669 | } |
670 | } |
671 | if ( replaceZWithV && bestMatchDistanceInfo != -1 ) { |
672 | bestMatchDistanceInfo = 2; |
673 | } |
674 | return bestSkeleton; |
675 | } |
676 | |
677 | |
678 | |
679 | DateIntervalInfo::IntervalPatternIndex |
680 | DateIntervalInfo::calendarFieldToIntervalIndex(UCalendarDateFields field, |
681 | UErrorCode& status) { |
682 | if ( U_FAILURE(status) ) { |
683 | return kIPI_MAX_INDEX; |
684 | } |
685 | IntervalPatternIndex index = kIPI_MAX_INDEX; |
686 | switch ( field ) { |
687 | case UCAL_ERA: |
688 | index = kIPI_ERA; |
689 | break; |
690 | case UCAL_YEAR: |
691 | index = kIPI_YEAR; |
692 | break; |
693 | case UCAL_MONTH: |
694 | index = kIPI_MONTH; |
695 | break; |
696 | case UCAL_DATE: |
697 | case UCAL_DAY_OF_WEEK: |
698 | //case UCAL_DAY_OF_MONTH: |
699 | index = kIPI_DATE; |
700 | break; |
701 | case UCAL_AM_PM: |
702 | index = kIPI_AM_PM; |
703 | break; |
704 | case UCAL_HOUR: |
705 | case UCAL_HOUR_OF_DAY: |
706 | index = kIPI_HOUR; |
707 | break; |
708 | case UCAL_MINUTE: |
709 | index = kIPI_MINUTE; |
710 | break; |
711 | case UCAL_SECOND: |
712 | index = kIPI_SECOND; |
713 | break; |
714 | default: |
715 | status = U_ILLEGAL_ARGUMENT_ERROR; |
716 | } |
717 | return index; |
718 | } |
719 | |
720 | |
721 | |
722 | void |
723 | DateIntervalInfo::deleteHash(Hashtable* hTable) |
724 | { |
725 | if ( hTable == NULL ) { |
726 | return; |
727 | } |
728 | int32_t pos = UHASH_FIRST; |
729 | const UHashElement* element = NULL; |
730 | while ( (element = hTable->nextElement(pos)) != NULL ) { |
731 | const UHashTok valueTok = element->value; |
732 | const UnicodeString* value = (UnicodeString*)valueTok.pointer; |
733 | delete[] value; |
734 | } |
735 | delete fIntervalPatterns; |
736 | } |
737 | |
738 | |
739 | U_CDECL_BEGIN |
740 | |
741 | /** |
742 | * set hash table value comparator |
743 | * |
744 | * @param val1 one value in comparison |
745 | * @param val2 the other value in comparison |
746 | * @return TRUE if 2 values are the same, FALSE otherwise |
747 | */ |
748 | static UBool U_CALLCONV dtitvinfHashTableValueComparator(UHashTok val1, UHashTok val2); |
749 | |
750 | static UBool |
751 | U_CALLCONV dtitvinfHashTableValueComparator(UHashTok val1, UHashTok val2) { |
752 | const UnicodeString* pattern1 = (UnicodeString*)val1.pointer; |
753 | const UnicodeString* pattern2 = (UnicodeString*)val2.pointer; |
754 | UBool ret = TRUE; |
755 | int8_t i; |
756 | for ( i = 0; i < DateIntervalInfo::kMaxIntervalPatternIndex && ret == TRUE; ++i ) { |
757 | ret = (pattern1[i] == pattern2[i]); |
758 | } |
759 | return ret; |
760 | } |
761 | |
762 | U_CDECL_END |
763 | |
764 | |
765 | Hashtable* |
766 | DateIntervalInfo::initHash(UErrorCode& status) { |
767 | if ( U_FAILURE(status) ) { |
768 | return NULL; |
769 | } |
770 | Hashtable* hTable; |
771 | if ( (hTable = new Hashtable(FALSE, status)) == NULL ) { |
772 | status = U_MEMORY_ALLOCATION_ERROR; |
773 | return NULL; |
774 | } |
775 | if ( U_FAILURE(status) ) { |
776 | delete hTable; |
777 | return NULL; |
778 | } |
779 | hTable->setValueComparator(dtitvinfHashTableValueComparator); |
780 | return hTable; |
781 | } |
782 | |
783 | |
784 | void |
785 | DateIntervalInfo::copyHash(const Hashtable* source, |
786 | Hashtable* target, |
787 | UErrorCode& status) { |
788 | if ( U_FAILURE(status) ) { |
789 | return; |
790 | } |
791 | int32_t pos = UHASH_FIRST; |
792 | const UHashElement* element = NULL; |
793 | if ( source ) { |
794 | while ( (element = source->nextElement(pos)) != NULL ) { |
795 | const UHashTok keyTok = element->key; |
796 | const UnicodeString* key = (UnicodeString*)keyTok.pointer; |
797 | const UHashTok valueTok = element->value; |
798 | const UnicodeString* value = (UnicodeString*)valueTok.pointer; |
799 | UnicodeString* copy = new UnicodeString[kIPI_MAX_INDEX]; |
800 | int8_t i; |
801 | for ( i = 0; i < kIPI_MAX_INDEX; ++i ) { |
802 | copy[i] = value[i]; |
803 | } |
804 | target->put(UnicodeString(*key), copy, status); |
805 | if ( U_FAILURE(status) ) { |
806 | return; |
807 | } |
808 | } |
809 | } |
810 | } |
811 | |
812 | |
813 | U_NAMESPACE_END |
814 | |
815 | #endif |
816 | |