Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(248)

Side by Side Diff: source/i18n/reldtfmt.cpp

Issue 845603002: Update ICU to 54.1 step 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/icu.git@master
Patch Set: remove unusued directories Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « source/i18n/reldtfmt.h ('k') | source/i18n/rematch.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 ******************************************************************************* 2 *******************************************************************************
3 * Copyright (C) 2007-2013, International Business Machines Corporation and 3 * Copyright (C) 2007-2014, International Business Machines Corporation and
4 * others. All Rights Reserved. 4 * others. All Rights Reserved.
5 ******************************************************************************* 5 *******************************************************************************
6 */ 6 */
7 7
8 #include "unicode/utypes.h" 8 #include "unicode/utypes.h"
9 9
10 #if !UCONFIG_NO_FORMATTING 10 #if !UCONFIG_NO_FORMATTING
11 11
12 #include <stdlib.h> 12 #include <stdlib.h>
13 13
14 #include "reldtfmt.h" 14 #include "reldtfmt.h"
15 #include "unicode/datefmt.h" 15 #include "unicode/datefmt.h"
16 #include "unicode/smpdtfmt.h" 16 #include "unicode/smpdtfmt.h"
17 #include "unicode/msgfmt.h" 17 #include "unicode/msgfmt.h"
18 #include "unicode/udisplaycontext.h"
19 #include "unicode/uchar.h"
20 #include "unicode/brkiter.h"
18 21
19 #include "gregoimp.h" // for CalendarData 22 #include "gregoimp.h" // for CalendarData
20 #include "cmemory.h" 23 #include "cmemory.h"
21 #include "uresimp.h" 24 #include "uresimp.h"
22 25
23 U_NAMESPACE_BEGIN 26 U_NAMESPACE_BEGIN
24 27
25 28
26 /** 29 /**
27 * An array of URelativeString structs is used to store the resource data loaded out of the bundle. 30 * An array of URelativeString structs is used to store the resource data loaded out of the bundle.
28 */ 31 */
29 struct URelativeString { 32 struct URelativeString {
30 int32_t offset; /** offset of this item, such as, the relative date **/ 33 int32_t offset; /** offset of this item, such as, the relative date **/
31 int32_t len; /** length of the string **/ 34 int32_t len; /** length of the string **/
32 const UChar* string; /** string, or NULL if not set **/ 35 const UChar* string; /** string, or NULL if not set **/
33 }; 36 };
34 37
35 static const char DT_DateTimePatternsTag[]="DateTimePatterns"; 38 static const char DT_DateTimePatternsTag[]="DateTimePatterns";
36 39
37 40
38 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RelativeDateFormat) 41 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RelativeDateFormat)
39 42
40 RelativeDateFormat::RelativeDateFormat(const RelativeDateFormat& other) : 43 RelativeDateFormat::RelativeDateFormat(const RelativeDateFormat& other) :
41 DateFormat(other), fDateTimeFormatter(NULL), fDatePattern(other.fDatePattern), 44 DateFormat(other), fDateTimeFormatter(NULL), fDatePattern(other.fDatePattern),
42 fTimePattern(other.fTimePattern), fCombinedFormat(NULL), 45 fTimePattern(other.fTimePattern), fCombinedFormat(NULL),
43 fDateStyle(other.fDateStyle), fLocale(other.fLocale), 46 fDateStyle(other.fDateStyle), fLocale(other.fLocale),
44 fDayMin(other.fDayMin), fDayMax(other.fDayMax), 47 fDayMin(other.fDayMin), fDayMax(other.fDayMax),
45 fDatesLen(other.fDatesLen), fDates(NULL) 48 fDatesLen(other.fDatesLen), fDates(NULL),
49 fCombinedHasDateAtStart(other.fCombinedHasDateAtStart),
50 fCapitalizationInfoSet(other.fCapitalizationInfoSet),
51 fCapitalizationOfRelativeUnitsForUIListMenu(other.fCapitalizationOfRelativeUnit sForUIListMenu),
52 fCapitalizationOfRelativeUnitsForStandAlone(other.fCapitalizationOfRelativeUnit sForStandAlone),
53 fCapitalizationBrkIter(NULL)
46 { 54 {
47 if(other.fDateTimeFormatter != NULL) { 55 if(other.fDateTimeFormatter != NULL) {
48 fDateTimeFormatter = (SimpleDateFormat*)other.fDateTimeFormatter->clone( ); 56 fDateTimeFormatter = (SimpleDateFormat*)other.fDateTimeFormatter->clone( );
49 } 57 }
50 if(other.fCombinedFormat != NULL) { 58 if(other.fCombinedFormat != NULL) {
51 fCombinedFormat = (MessageFormat*)other.fCombinedFormat->clone(); 59 fCombinedFormat = (MessageFormat*)other.fCombinedFormat->clone();
52 } 60 }
53 if (fDatesLen > 0) { 61 if (fDatesLen > 0) {
54 fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen); 62 fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen);
55 uprv_memcpy(fDates, other.fDates, sizeof(fDates[0])*fDatesLen); 63 uprv_memcpy(fDates, other.fDates, sizeof(fDates[0])*fDatesLen);
56 } 64 }
65 #if !UCONFIG_NO_BREAK_ITERATION
66 if (other.fCapitalizationBrkIter != NULL) {
67 fCapitalizationBrkIter = (other.fCapitalizationBrkIter)->clone();
68 }
69 #endif
57 } 70 }
58 71
59 RelativeDateFormat::RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatS tyle dateStyle, 72 RelativeDateFormat::RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatS tyle dateStyle,
60 const Locale& locale, UErrorCode& status ) : 73 const Locale& locale, UErrorCode& status ) :
61 DateFormat(), fDateTimeFormatter(NULL), fDatePattern(), fTimePattern(), fCombin edFormat(NULL), 74 DateFormat(), fDateTimeFormatter(NULL), fDatePattern(), fTimePattern(), fCombin edFormat(NULL),
62 fDateStyle(dateStyle), fLocale(locale), fDatesLen(0), fDates(NULL) 75 fDateStyle(dateStyle), fLocale(locale), fDayMin(0), fDayMax(0), fDatesLen(0), f Dates(NULL),
76 fCombinedHasDateAtStart(FALSE), fCapitalizationInfoSet(FALSE),
77 fCapitalizationOfRelativeUnitsForUIListMenu(FALSE), fCapitalizationOfRelativeUn itsForStandAlone(FALSE),
78 fCapitalizationBrkIter(NULL)
63 { 79 {
64 if(U_FAILURE(status) ) { 80 if(U_FAILURE(status) ) {
65 return; 81 return;
66 } 82 }
67 83
68 if (timeStyle < UDAT_NONE || timeStyle > UDAT_SHORT) { 84 if (timeStyle < UDAT_NONE || timeStyle > UDAT_SHORT) {
69 // don't support other time styles (e.g. relative styles), for now 85 // don't support other time styles (e.g. relative styles), for now
70 status = U_ILLEGAL_ARGUMENT_ERROR; 86 status = U_ILLEGAL_ARGUMENT_ERROR;
71 return; 87 return;
72 } 88 }
(...skipping 30 matching lines...) Expand all
103 119
104 // Initialize the parent fCalendar, so that parse() works correctly. 120 // Initialize the parent fCalendar, so that parse() works correctly.
105 initializeCalendar(NULL, locale, status); 121 initializeCalendar(NULL, locale, status);
106 loadDates(status); 122 loadDates(status);
107 } 123 }
108 124
109 RelativeDateFormat::~RelativeDateFormat() { 125 RelativeDateFormat::~RelativeDateFormat() {
110 delete fDateTimeFormatter; 126 delete fDateTimeFormatter;
111 delete fCombinedFormat; 127 delete fCombinedFormat;
112 uprv_free(fDates); 128 uprv_free(fDates);
129 #if !UCONFIG_NO_BREAK_ITERATION
130 delete fCapitalizationBrkIter;
131 #endif
113 } 132 }
114 133
115 134
116 Format* RelativeDateFormat::clone(void) const { 135 Format* RelativeDateFormat::clone(void) const {
117 return new RelativeDateFormat(*this); 136 return new RelativeDateFormat(*this);
118 } 137 }
119 138
120 UBool RelativeDateFormat::operator==(const Format& other) const { 139 UBool RelativeDateFormat::operator==(const Format& other) const {
121 if(DateFormat::operator==(other)) { 140 if(DateFormat::operator==(other)) {
141 // The DateFormat::operator== check for fCapitalizationContext equality above
142 // is sufficient to check equality of all derived context-related data .
122 // DateFormat::operator== guarantees following cast is safe 143 // DateFormat::operator== guarantees following cast is safe
123 RelativeDateFormat* that = (RelativeDateFormat*)&other; 144 RelativeDateFormat* that = (RelativeDateFormat*)&other;
124 return (fDateStyle==that->fDateStyle && 145 return (fDateStyle==that->fDateStyle &&
125 fDatePattern==that->fDatePattern && 146 fDatePattern==that->fDatePattern &&
126 fTimePattern==that->fTimePattern && 147 fTimePattern==that->fTimePattern &&
127 fLocale==that->fLocale); 148 fLocale==that->fLocale );
128 } 149 }
129 return FALSE; 150 return FALSE;
130 } 151 }
131 152
132 static const UChar APOSTROPHE = (UChar)0x0027; 153 static const UChar APOSTROPHE = (UChar)0x0027;
133 154
134 UnicodeString& RelativeDateFormat::format( Calendar& cal, 155 UnicodeString& RelativeDateFormat::format( Calendar& cal,
135 UnicodeString& appendTo, 156 UnicodeString& appendTo,
136 FieldPosition& pos) const { 157 FieldPosition& pos) const {
137 158
138 UErrorCode status = U_ZERO_ERROR; 159 UErrorCode status = U_ZERO_ERROR;
139 UnicodeString relativeDayString; 160 UnicodeString relativeDayString;
161 UDisplayContext capitalizationContext = getContext(UDISPCTX_TYPE_CAPITALIZAT ION, status);
140 162
141 // calculate the difference, in days, between 'cal' and now. 163 // calculate the difference, in days, between 'cal' and now.
142 int dayDiff = dayDifference(cal, status); 164 int dayDiff = dayDifference(cal, status);
143 165
144 // look up string 166 // look up string
145 int32_t len = 0; 167 int32_t len = 0;
146 const UChar *theString = getStringForDay(dayDiff, len, status); 168 const UChar *theString = getStringForDay(dayDiff, len, status);
147 if(U_SUCCESS(status) && (theString!=NULL)) { 169 if(U_SUCCESS(status) && (theString!=NULL)) {
148 // found a relative string 170 // found a relative string
149 relativeDayString.setTo(theString, len); 171 relativeDayString.setTo(theString, len);
150 } 172 }
151 173
174 if ( relativeDayString.length() > 0 && !fDatePattern.isEmpty() &&
175 (fTimePattern.isEmpty() || fCombinedFormat == NULL || fCombinedHasDateA tStart)) {
176 #if !UCONFIG_NO_BREAK_ITERATION
177 // capitalize relativeDayString according to context for relative, set f ormatter no context
178 if ( u_islower(relativeDayString.char32At(0)) && fCapitalizationBrkIter! = NULL &&
179 ( capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_S ENTENCE ||
180 (capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_ME NU && fCapitalizationOfRelativeUnitsForUIListMenu) ||
181 (capitalizationContext==UDISPCTX_CAPITALIZATION_FOR_STANDALONE && fCapitalizationOfRelativeUnitsForStandAlone) ) ) {
182 // titlecase first word of relativeDayString
183 relativeDayString.toTitle(fCapitalizationBrkIter, fLocale, U_TITLECA SE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT);
184 }
185 #endif
186 fDateTimeFormatter->setContext(UDISPCTX_CAPITALIZATION_NONE, status);
187 } else {
188 // set our context for the formatter
189 fDateTimeFormatter->setContext(capitalizationContext, status);
190 }
191
152 if (fDatePattern.isEmpty()) { 192 if (fDatePattern.isEmpty()) {
153 fDateTimeFormatter->applyPattern(fTimePattern); 193 fDateTimeFormatter->applyPattern(fTimePattern);
154 fDateTimeFormatter->format(cal,appendTo,pos); 194 fDateTimeFormatter->format(cal,appendTo,pos);
155 } else if (fTimePattern.isEmpty() || fCombinedFormat == NULL) { 195 } else if (fTimePattern.isEmpty() || fCombinedFormat == NULL) {
156 if (relativeDayString.length() > 0) { 196 if (relativeDayString.length() > 0) {
157 appendTo.append(relativeDayString); 197 appendTo.append(relativeDayString);
158 } else { 198 } else {
159 fDateTimeFormatter->applyPattern(fDatePattern); 199 fDateTimeFormatter->applyPattern(fDatePattern);
160 fDateTimeFormatter->format(cal,appendTo,pos); 200 fDateTimeFormatter->format(cal,appendTo,pos);
161 } 201 }
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 fTimePattern.setTo(timePattern); 417 fTimePattern.setTo(timePattern);
378 } 418 }
379 } 419 }
380 420
381 const DateFormatSymbols* 421 const DateFormatSymbols*
382 RelativeDateFormat::getDateFormatSymbols() const 422 RelativeDateFormat::getDateFormatSymbols() const
383 { 423 {
384 return fDateTimeFormatter->getDateFormatSymbols(); 424 return fDateTimeFormatter->getDateFormatSymbols();
385 } 425 }
386 426
427 // override the DateFormat implementation in order to
428 // lazily initialize relevant items
429 void
430 RelativeDateFormat::setContext(UDisplayContext value, UErrorCode& status)
431 {
432 DateFormat::setContext(value, status);
433 if (U_SUCCESS(status)) {
434 if (!fCapitalizationInfoSet &&
435 (value==UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU || value==UD ISPCTX_CAPITALIZATION_FOR_STANDALONE)) {
436 initCapitalizationContextInfo(fLocale);
437 fCapitalizationInfoSet = TRUE;
438 }
439 #if !UCONFIG_NO_BREAK_ITERATION
440 if ( fCapitalizationBrkIter == NULL && (value==UDISPCTX_CAPITALIZATION_F OR_BEGINNING_OF_SENTENCE ||
441 (value==UDISPCTX_CAPITALIZATION_FOR_UI_LIST_OR_MENU && fCapitali zationOfRelativeUnitsForUIListMenu) ||
442 (value==UDISPCTX_CAPITALIZATION_FOR_STANDALONE && fCapitalizatio nOfRelativeUnitsForStandAlone)) ) {
443 UErrorCode status = U_ZERO_ERROR;
444 fCapitalizationBrkIter = BreakIterator::createSentenceInstance(fLoca le, status);
445 if (U_FAILURE(status)) {
446 delete fCapitalizationBrkIter;
447 fCapitalizationBrkIter = NULL;
448 }
449 }
450 #endif
451 }
452 }
453
454 void
455 RelativeDateFormat::initCapitalizationContextInfo(const Locale& thelocale)
456 {
457 #if !UCONFIG_NO_BREAK_ITERATION
458 const char * localeID = (thelocale != NULL)? thelocale.getBaseName(): NULL;
459 UErrorCode status = U_ZERO_ERROR;
460 UResourceBundle *rb = ures_open(NULL, localeID, &status);
461 rb = ures_getByKeyWithFallback(rb, "contextTransforms", rb, &status);
462 rb = ures_getByKeyWithFallback(rb, "relative", rb, &status);
463 if (U_SUCCESS(status) && rb != NULL) {
464 int32_t len = 0;
465 const int32_t * intVector = ures_getIntVector(rb, &len, &status);
466 if (U_SUCCESS(status) && intVector != NULL && len >= 2) {
467 fCapitalizationOfRelativeUnitsForUIListMenu = intVector[0];
468 fCapitalizationOfRelativeUnitsForStandAlone = intVector[1];
469 }
470 }
471 ures_close(rb);
472 #endif
473 }
474
475 static const UChar patItem1[] = {0x7B,0x31,0x7D}; // "{1}"
476 static const int32_t patItem1Len = 3;
477
387 void RelativeDateFormat::loadDates(UErrorCode &status) { 478 void RelativeDateFormat::loadDates(UErrorCode &status) {
388 CalendarData calData(fLocale, "gregorian", status); 479 CalendarData calData(fLocale, "gregorian", status);
389 480
390 UErrorCode tempStatus = status; 481 UErrorCode tempStatus = status;
391 UResourceBundle *dateTimePatterns = calData.getByKey(DT_DateTimePatternsTag, tempStatus); 482 UResourceBundle *dateTimePatterns = calData.getByKey(DT_DateTimePatternsTag, tempStatus);
392 if(U_SUCCESS(tempStatus)) { 483 if(U_SUCCESS(tempStatus)) {
393 int32_t patternsSize = ures_getSize(dateTimePatterns); 484 int32_t patternsSize = ures_getSize(dateTimePatterns);
394 if (patternsSize > kDateTime) { 485 if (patternsSize > kDateTime) {
395 int32_t resStrLen = 0; 486 int32_t resStrLen = 0;
396 487
(...skipping 16 matching lines...) Expand all
413 case kShortRelative: 504 case kShortRelative:
414 case kShort: 505 case kShort:
415 glueIndex = kDateTimeOffset + kShort; 506 glueIndex = kDateTimeOffset + kShort;
416 break; 507 break;
417 default: 508 default:
418 break; 509 break;
419 } 510 }
420 } 511 }
421 512
422 const UChar *resStr = ures_getStringByIndex(dateTimePatterns, glueIn dex, &resStrLen, &tempStatus); 513 const UChar *resStr = ures_getStringByIndex(dateTimePatterns, glueIn dex, &resStrLen, &tempStatus);
514 if (U_SUCCESS(tempStatus) && resStrLen >= patItem1Len && u_strncmp(r esStr,patItem1,patItem1Len)==0) {
515 fCombinedHasDateAtStart = TRUE;
516 }
423 fCombinedFormat = new MessageFormat(UnicodeString(TRUE, resStr, resS trLen), fLocale, tempStatus); 517 fCombinedFormat = new MessageFormat(UnicodeString(TRUE, resStr, resS trLen), fLocale, tempStatus);
424 } 518 }
425 } 519 }
426 520
427 UResourceBundle *rb = ures_open(NULL, fLocale.getBaseName(), &status); 521 UResourceBundle *rb = ures_open(NULL, fLocale.getBaseName(), &status);
428 UResourceBundle *sb = ures_getByKeyWithFallback(rb, "fields", NULL, &status) ; 522 rb = ures_getByKeyWithFallback(rb, "fields", rb, &status);
429 rb = ures_getByKeyWithFallback(sb, "day", rb, &status); 523 rb = ures_getByKeyWithFallback(rb, "day", rb, &status);
430 sb = ures_getByKeyWithFallback(rb, "relative", sb, &status); 524 rb = ures_getByKeyWithFallback(rb, "relative", rb, &status);
431 ures_close(rb);
432 // set up min/max 525 // set up min/max
433 fDayMin=-1; 526 fDayMin=-1;
434 fDayMax=1; 527 fDayMax=1;
435 528
436 if(U_FAILURE(status)) { 529 if(U_FAILURE(status)) {
437 fDatesLen=0; 530 fDatesLen=0;
438 ures_close(sb); 531 ures_close(rb);
439 return; 532 return;
440 } 533 }
441 534
442 fDatesLen = ures_getSize(sb); 535 fDatesLen = ures_getSize(rb);
443 fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen); 536 fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen);
444 537
445 // Load in each item into the array... 538 // Load in each item into the array...
446 int n = 0; 539 int n = 0;
447 540
448 UResourceBundle *subString = NULL; 541 UResourceBundle *subString = NULL;
449 542
450 while(ures_hasNext(sb) && U_SUCCESS(status)) { // iterate over items 543 while(ures_hasNext(rb) && U_SUCCESS(status)) { // iterate over items
451 subString = ures_getNextResource(sb, subString, &status); 544 subString = ures_getNextResource(rb, subString, &status);
452 545
453 if(U_FAILURE(status) || (subString==NULL)) break; 546 if(U_FAILURE(status) || (subString==NULL)) break;
454 547
455 // key = offset # 548 // key = offset #
456 const char *key = ures_getKey(subString); 549 const char *key = ures_getKey(subString);
457 550
458 // load the string and length 551 // load the string and length
459 int32_t aLen; 552 int32_t aLen;
460 const UChar* aString = ures_getString(subString, &aLen, &status); 553 const UChar* aString = ures_getString(subString, &aLen, &status);
461 554
(...skipping 11 matching lines...) Expand all
473 } 566 }
474 567
475 // copy the string pointer 568 // copy the string pointer
476 fDates[n].offset = offset; 569 fDates[n].offset = offset;
477 fDates[n].string = aString; 570 fDates[n].string = aString;
478 fDates[n].len = aLen; 571 fDates[n].len = aLen;
479 572
480 n++; 573 n++;
481 } 574 }
482 ures_close(subString); 575 ures_close(subString);
483 ures_close(sb); 576 ures_close(rb);
484 577
485 // the fDates[] array could be sorted here, for direct access. 578 // the fDates[] array could be sorted here, for direct access.
486 } 579 }
487 580
581 //----------------------------------------------------------------------
488 582
489 // this should to be in DateFormat, instead it was copied from SimpleDateFormat. 583 // this should to be in DateFormat, instead it was copied from SimpleDateFormat.
490 584
491 Calendar* 585 Calendar*
492 RelativeDateFormat::initializeCalendar(TimeZone* adoptZone, const Locale& locale , UErrorCode& status) 586 RelativeDateFormat::initializeCalendar(TimeZone* adoptZone, const Locale& locale , UErrorCode& status)
493 { 587 {
494 if(!U_FAILURE(status)) { 588 if(!U_FAILURE(status)) {
495 fCalendar = Calendar::createInstance(adoptZone?adoptZone:TimeZone::creat eDefault(), locale, status); 589 fCalendar = Calendar::createInstance(adoptZone?adoptZone:TimeZone::creat eDefault(), locale, status);
496 } 590 }
497 if (U_SUCCESS(status) && fCalendar == NULL) { 591 if (U_SUCCESS(status) && fCalendar == NULL) {
(...skipping 16 matching lines...) Expand all
514 int32_t dayDiff = cal.get(UCAL_JULIAN_DAY, status) - nowCal->get(UCAL_JULIAN _DAY, status); 608 int32_t dayDiff = cal.get(UCAL_JULIAN_DAY, status) - nowCal->get(UCAL_JULIAN _DAY, status);
515 609
516 delete nowCal; 610 delete nowCal;
517 return dayDiff; 611 return dayDiff;
518 } 612 }
519 613
520 U_NAMESPACE_END 614 U_NAMESPACE_END
521 615
522 #endif 616 #endif
523 617
OLDNEW
« no previous file with comments | « source/i18n/reldtfmt.h ('k') | source/i18n/rematch.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698