X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/2ca993e82fb37b597a3c73ecd1586a139a6579c5..refs/heads/master:/icuSources/i18n/dtitvfmt.cpp diff --git a/icuSources/i18n/dtitvfmt.cpp b/icuSources/i18n/dtitvfmt.cpp index 40ad3d08..7a4082b5 100644 --- a/icuSources/i18n/dtitvfmt.cpp +++ b/icuSources/i18n/dtitvfmt.cpp @@ -1,9 +1,11 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /******************************************************************************* * Copyright (C) 2008-2016, International Business Machines Corporation and * others. All Rights Reserved. ******************************************************************************* * -* File DTITVFMT.CPP +* File DTITVFMT.CPP * ******************************************************************************* */ @@ -20,15 +22,17 @@ #include "unicode/calendar.h" #include "unicode/dtptngen.h" #include "unicode/dtitvinf.h" -#include "unicode/udateintervalformat.h" #include "unicode/simpleformatter.h" #include "cmemory.h" #include "cstring.h" #include "dtitv_impl.h" -#include "gregoimp.h" #include "mutex.h" +#include "uresimp.h" +#include "formattedval_impl.h" +// Apple addition +#include "unicode/udateintervalformat.h" -#ifdef DTITVFMT_DEBUG +#ifdef DTITVFMT_DEBUG #include #endif @@ -36,7 +40,7 @@ U_NAMESPACE_BEGIN -#ifdef DTITVFMT_DEBUG +#ifdef DTITVFMT_DEBUG #define PRINTMESG(msg) { std::cout << "(" << __FILE__ << ":" << __LINE__ << ") " << msg << "\n"; } #endif @@ -52,7 +56,9 @@ static const UChar gDateFormatSkeleton[][11] = { {LOW_Y, CAP_M, LOW_D, 0} }; -static const char gDateTimePatternsTag[]="DateTimePatterns"; +static const char gCalendarTag[] = "calendar"; +static const char gGregorianTag[] = "gregorian"; +static const char gDateTimePatternsTag[] = "DateTimePatterns"; // latestFirst: @@ -62,23 +68,34 @@ static const UChar gLaterFirstPrefix[] = {LOW_L, LOW_A, LOW_T, LOW_E, LOW_S,LOW_ static const UChar gEarlierFirstPrefix[] = {LOW_E, LOW_A, LOW_R, LOW_L, LOW_I, LOW_E, LOW_S, LOW_T, CAP_F, LOW_I, LOW_R, LOW_S, LOW_T, COLON}; +class FormattedDateIntervalData : public FormattedValueFieldPositionIteratorImpl { +public: + FormattedDateIntervalData(UErrorCode& status) : FormattedValueFieldPositionIteratorImpl(5, status) {} + virtual ~FormattedDateIntervalData(); +}; + +FormattedDateIntervalData::~FormattedDateIntervalData() = default; + +UPRV_FORMATTED_VALUE_SUBCLASS_AUTO_IMPL(FormattedDateInterval) + + UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateIntervalFormat) -// Mutex, protects access to fDateFormat, fFromCalendar and fToCalendar. +// Mutex, protects access to fDateFormat, fFromCalendar and fToCalendar. // Needed because these data members are modified by const methods of DateIntervalFormat. -static UMutex gFormatterMutex = U_MUTEX_INITIALIZER; +static UMutex gFormatterMutex; DateIntervalFormat* U_EXPORT2 -DateIntervalFormat::createInstance(const UnicodeString& skeleton, +DateIntervalFormat::createInstance(const UnicodeString& skeleton, UErrorCode& status) { return createInstance(skeleton, Locale::getDefault(), status); } DateIntervalFormat* U_EXPORT2 -DateIntervalFormat::createInstance(const UnicodeString& skeleton, - const Locale& locale, +DateIntervalFormat::createInstance(const UnicodeString& skeleton, + const Locale& locale, UErrorCode& status) { #ifdef DTITVFMT_DEBUG char result[1000]; @@ -125,7 +142,8 @@ DateIntervalFormat::DateIntervalFormat() fDatePattern(NULL), fTimePattern(NULL), fDateTimeFormat(NULL), - fMinimizeType(UDTITVFMT_MINIMIZE_NONE) + fMinimizeType(UDTITVFMT_MINIMIZE_NONE), + fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) {} @@ -139,7 +157,8 @@ DateIntervalFormat::DateIntervalFormat(const DateIntervalFormat& itvfmt) fDatePattern(NULL), fTimePattern(NULL), fDateTimeFormat(NULL), - fMinimizeType(UDTITVFMT_MINIMIZE_NONE) { + fMinimizeType(UDTITVFMT_MINIMIZE_NONE), + fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) { *this = itvfmt; } @@ -157,7 +176,7 @@ DateIntervalFormat::operator=(const DateIntervalFormat& itvfmt) { { Mutex lock(&gFormatterMutex); if ( itvfmt.fDateFormat ) { - fDateFormat = (SimpleDateFormat*)itvfmt.fDateFormat->clone(); + fDateFormat = itvfmt.fDateFormat->clone(); } else { fDateFormat = NULL; } @@ -186,6 +205,8 @@ DateIntervalFormat::operator=(const DateIntervalFormat& itvfmt) { fDatePattern = (itvfmt.fDatePattern)? (UnicodeString*)itvfmt.fDatePattern->clone(): NULL; fTimePattern = (itvfmt.fTimePattern)? (UnicodeString*)itvfmt.fTimePattern->clone(): NULL; fDateTimeFormat = (itvfmt.fDateTimeFormat)? (UnicodeString*)itvfmt.fDateTimeFormat->clone(): NULL; + fMinimizeType = itvfmt.fMinimizeType; + fCapitalizationContext = itvfmt.fCapitalizationContext; } return *this; } @@ -202,8 +223,8 @@ DateIntervalFormat::~DateIntervalFormat() { } -Format* -DateIntervalFormat::clone(void) const { +DateIntervalFormat* +DateIntervalFormat::clone() const { return new DateIntervalFormat(*this); } @@ -220,15 +241,9 @@ DateIntervalFormat::operator==(const Format& other) const { Mutex lock(&gFormatterMutex); if (fDateFormat != fmt->fDateFormat && (fDateFormat == NULL || fmt->fDateFormat == NULL)) {return FALSE;} if (fDateFormat && fmt->fDateFormat && (*fDateFormat != *fmt->fDateFormat)) {return FALSE;} - - // TODO: should operator == ignore the From and ToCalendar? They hold transient values during - // formatting of a DateInterval. - if (fFromCalendar != fmt->fFromCalendar && (fFromCalendar == NULL || fmt->fFromCalendar == NULL)) {return FALSE;} - if (fFromCalendar && fmt->fFromCalendar && !fFromCalendar->isEquivalentTo(*fmt->fFromCalendar)) {return FALSE;} - - if (fToCalendar != fmt->fToCalendar && (fToCalendar == NULL || fmt->fToCalendar == NULL)) {return FALSE;} - if (fToCalendar && fmt->fToCalendar && !fToCalendar->isEquivalentTo(*fmt->fToCalendar)) {return FALSE;} } + // note: fFromCalendar and fToCalendar hold no persistent state, and therefore do not participate in operator ==. + // fDateFormat has the master calendar for the DateIntervalFormat. if (fSkeleton != fmt->fSkeleton) {return FALSE;} if (fDatePattern != fmt->fDatePattern && (fDatePattern == NULL || fmt->fDatePattern == NULL)) {return FALSE;} if (fDatePattern && fmt->fDatePattern && (*fDatePattern != *fmt->fDatePattern)) {return FALSE;} @@ -243,6 +258,8 @@ DateIntervalFormat::operator==(const Format& other) const { if (fIntervalPatterns[i].secondPart != fmt->fIntervalPatterns[i].secondPart ) {return FALSE;} if (fIntervalPatterns[i].laterDateFirst != fmt->fIntervalPatterns[i].laterDateFirst) {return FALSE;} } + if (fMinimizeType != fmt->fMinimizeType) {return FALSE;} + if (fCapitalizationContext != fmt->fCapitalizationContext) {return FALSE;} return TRUE; } @@ -276,15 +293,51 @@ DateIntervalFormat::format(const DateInterval* dtInterval, if ( U_FAILURE(status) ) { return appendTo; } - if (fFromCalendar == NULL || fToCalendar == NULL || fDateFormat == NULL || fInfo == NULL) { + if (fDateFormat == NULL || fInfo == NULL) { status = U_INVALID_STATE_ERROR; return appendTo; } + FieldPositionOnlyHandler handler(fieldPosition); + handler.setAcceptFirstOnly(TRUE); + int8_t ignore; + Mutex lock(&gFormatterMutex); - fFromCalendar->setTime(dtInterval->getFromDate(), status); - fToCalendar->setTime(dtInterval->getToDate(), status); - return formatImpl(*fFromCalendar, *fToCalendar, appendTo,fieldPosition, status); + return formatIntervalImpl(*dtInterval, appendTo, ignore, handler, status); +} + + +FormattedDateInterval DateIntervalFormat::formatToValue( + const DateInterval& dtInterval, + UErrorCode& status) const { + LocalPointer result(new FormattedDateIntervalData(status), status); + if (U_FAILURE(status)) { + return FormattedDateInterval(status); + } + UnicodeString string; + int8_t firstIndex; + auto handler = result->getHandler(status); + handler.setCategory(UFIELD_CATEGORY_DATE); + { + Mutex lock(&gFormatterMutex); + formatIntervalImpl(dtInterval, string, firstIndex, handler, status); + } + handler.getError(status); + result->appendString(string, status); + if (U_FAILURE(status)) { + return FormattedDateInterval(status); + } + + // Compute the span fields and sort them into place: + if (firstIndex != -1) { + result->addOverlapSpans(UFIELD_CATEGORY_DATE_INTERVAL_SPAN, firstIndex, status); + if (U_FAILURE(status)) { + return FormattedDateInterval(status); + } + result->sort(); + } + + return FormattedDateInterval(result.orphan()); } @@ -294,21 +347,80 @@ DateIntervalFormat::format(Calendar& fromCalendar, UnicodeString& appendTo, FieldPosition& pos, UErrorCode& status) const { + FieldPositionOnlyHandler handler(pos); + handler.setAcceptFirstOnly(TRUE); + int8_t ignore; + Mutex lock(&gFormatterMutex); - return formatImpl(fromCalendar, toCalendar, appendTo, pos, status); + return formatImpl(fromCalendar, toCalendar, appendTo, ignore, handler, status); +} + + +FormattedDateInterval DateIntervalFormat::formatToValue( + Calendar& fromCalendar, + Calendar& toCalendar, + UErrorCode& status) const { + LocalPointer result(new FormattedDateIntervalData(status), status); + if (U_FAILURE(status)) { + return FormattedDateInterval(status); + } + UnicodeString string; + int8_t firstIndex; + auto handler = result->getHandler(status); + handler.setCategory(UFIELD_CATEGORY_DATE); + { + Mutex lock(&gFormatterMutex); + formatImpl(fromCalendar, toCalendar, string, firstIndex, handler, status); + } + handler.getError(status); + result->appendString(string, status); + if (U_FAILURE(status)) { + return FormattedDateInterval(status); + } + + // Compute the span fields and sort them into place: + if (firstIndex != -1) { + result->addOverlapSpans(UFIELD_CATEGORY_DATE_INTERVAL_SPAN, firstIndex, status); + result->sort(); + } + + return FormattedDateInterval(result.orphan()); +} + + +UnicodeString& DateIntervalFormat::formatIntervalImpl( + const DateInterval& dtInterval, + UnicodeString& appendTo, + int8_t& firstIndex, + FieldPositionHandler& fphandler, + UErrorCode& status) const { + if (U_FAILURE(status)) { + return appendTo; + } + if (fFromCalendar == nullptr || fToCalendar == nullptr) { + status = U_INVALID_STATE_ERROR; + return appendTo; + } + fFromCalendar->setTime(dtInterval.getFromDate(), status); + fToCalendar->setTime(dtInterval.getToDate(), status); + return formatImpl(*fFromCalendar, *fToCalendar, appendTo, firstIndex, fphandler, status); } - + UnicodeString& DateIntervalFormat::formatImpl(Calendar& fromCalendar, Calendar& toCalendar, UnicodeString& appendTo, - FieldPosition& pos, + int8_t& firstIndex, + FieldPositionHandler& fphandler, UErrorCode& status) const { if ( U_FAILURE(status) ) { return appendTo; } + // Initialize firstIndex to -1 (single date, no range) + firstIndex = -1; + // not support different calendar types and time zones //if ( fromCalendar.getType() != toCalendar.getType() ) { if ( !fromCalendar.isEquivalentTo(toCalendar) ) { @@ -318,16 +430,28 @@ DateIntervalFormat::formatImpl(Calendar& fromCalendar, // First, find the largest different calendar field. UCalendarDateFields field = UCAL_FIELD_COUNT; + UChar patternDay = 0x0064; // d + UChar patternYear = 0x0079; // y if ( fromCalendar.get(UCAL_ERA,status) != toCalendar.get(UCAL_ERA,status)) { field = UCAL_ERA; - } else if ( fromCalendar.get(UCAL_YEAR, status) != + } else if ( fromCalendar.get(UCAL_YEAR, status) != toCalendar.get(UCAL_YEAR, status) ) { field = UCAL_YEAR; + if (fMinimizeType == UDTITVFMT_MINIMIZE_ADJACENT_MONTHS && fSkeleton.indexOf(patternDay) >= 0 && fSkeleton.indexOf(patternYear) < 0) { + UDate fromDate = fromCalendar.getTime(status); + UDate toDate = toCalendar.getTime(status); + int32_t fromDay = fromCalendar.get(UCAL_DATE, status); + int32_t toDay = toCalendar.get(UCAL_DATE, status); + fromCalendar.add(UCAL_MONTH, 1, status); + if ( fromDate < toDate && fromCalendar.getTime(status) > toDate && fromDay > toDay ) { + field = UCAL_DATE; + } + fromCalendar.setTime(fromDate, status); + } } else if ( fromCalendar.get(UCAL_MONTH, status) != toCalendar.get(UCAL_MONTH, status) ) { field = UCAL_MONTH; - UChar patternDay = 0x0064; // d if (fMinimizeType == UDTITVFMT_MINIMIZE_ADJACENT_MONTHS && fSkeleton.indexOf(patternDay) >= 0) { UDate fromDate = fromCalendar.getTime(status); UDate toDate = toCalendar.getTime(status); @@ -376,10 +500,11 @@ DateIntervalFormat::formatImpl(Calendar& fromCalendar, /* ignore the millisecond etc. small fields' difference. * use single date when all the above are the same. */ - return fDateFormat->format(fromCalendar, appendTo, pos); + fDateFormat->setContext(fCapitalizationContext, status); + return fDateFormat->_format(fromCalendar, appendTo, fphandler, status); } UBool fromToOnSameDay = (field==UCAL_AM_PM || field==UCAL_HOUR || field==UCAL_MINUTE || field==UCAL_SECOND); - + // following call should not set wrong status, // all the pass-in fields are valid till here int32_t itvPtnIndex = DateIntervalInfo::calendarFieldToIntervalIndex(field, @@ -393,11 +518,12 @@ DateIntervalFormat::formatImpl(Calendar& fromCalendar, * the smallest calendar field in pattern, * return single date format. */ - return fDateFormat->format(fromCalendar, appendTo, pos); + fDateFormat->setContext(fCapitalizationContext, status); + return fDateFormat->_format(fromCalendar, appendTo, fphandler, status); } - return fallbackFormat(fromCalendar, toCalendar, fromToOnSameDay, appendTo, pos, status); + return fallbackFormat(fromCalendar, toCalendar, fromToOnSameDay, appendTo, firstIndex, fphandler, status); } - // If the first part in interval pattern is empty, + // If the first part in interval pattern is empty, // the 2nd part of it saves the full-pattern used in fall-back. // For a 'real' interval pattern, the first part will never be empty. if ( intervalPattern.firstPart.isEmpty() ) { @@ -405,7 +531,7 @@ DateIntervalFormat::formatImpl(Calendar& fromCalendar, UnicodeString originalPattern; fDateFormat->toPattern(originalPattern); fDateFormat->applyPattern(intervalPattern.secondPart); - appendTo = fallbackFormat(fromCalendar, toCalendar, fromToOnSameDay, appendTo, pos, status); + appendTo = fallbackFormat(fromCalendar, toCalendar, fromToOnSameDay, appendTo, firstIndex, fphandler, status); fDateFormat->applyPattern(originalPattern); return appendTo; } @@ -414,24 +540,24 @@ DateIntervalFormat::formatImpl(Calendar& fromCalendar, if ( intervalPattern.laterDateFirst ) { firstCal = &toCalendar; secondCal = &fromCalendar; + firstIndex = 1; } else { firstCal = &fromCalendar; secondCal = &toCalendar; + firstIndex = 0; } // break the interval pattern into 2 parts, - // first part should not be empty, + // first part should not be empty, UnicodeString originalPattern; fDateFormat->toPattern(originalPattern); fDateFormat->applyPattern(intervalPattern.firstPart); - fDateFormat->format(*firstCal, appendTo, pos); + fDateFormat->setContext(fCapitalizationContext, status); + fDateFormat->_format(*firstCal, appendTo, fphandler, status); + if ( !intervalPattern.secondPart.isEmpty() ) { fDateFormat->applyPattern(intervalPattern.secondPart); - FieldPosition otherPos; - otherPos.setField(pos.getField()); - fDateFormat->format(*secondCal, appendTo, otherPos); - if (pos.getEndIndex() == 0 && otherPos.getEndIndex() > 0) { - pos = otherPos; - } + fDateFormat->setContext(UDISPCTX_CAPITALIZATION_NONE, status); + fDateFormat->_format(*secondCal, appendTo, fphandler, status); } fDateFormat->applyPattern(originalPattern); return appendTo; @@ -440,11 +566,11 @@ DateIntervalFormat::formatImpl(Calendar& fromCalendar, void -DateIntervalFormat::parseObject(const UnicodeString& /* source */, +DateIntervalFormat::parseObject(const UnicodeString& /* source */, Formattable& /* result */, ParsePosition& /* parse_pos */) const { // parseObject(const UnicodeString&, Formattable&, UErrorCode&) const - // will set status as U_INVALID_FORMAT_ERROR if + // will set status as U_INVALID_FORMAT_ERROR if // parse_pos is still 0 } @@ -477,7 +603,7 @@ DateIntervalFormat::setDateIntervalInfo(const DateIntervalInfo& newItvPattern, } - + const DateFormat* DateIntervalFormat::getDateFormat() const { return fDateFormat; @@ -544,10 +670,34 @@ DateIntervalFormat::setAttribute(UDateIntervalFormatAttribute attr, } } +void +DateIntervalFormat::setContext(UDisplayContext value, UErrorCode& status) +{ + if (U_FAILURE(status)) + return; + if ( (UDisplayContextType)((uint32_t)value >> 8) == UDISPCTX_TYPE_CAPITALIZATION ) { + fCapitalizationContext = value; + } else { + status = U_ILLEGAL_ARGUMENT_ERROR; + } +} + +UDisplayContext +DateIntervalFormat::getContext(UDisplayContextType type, UErrorCode& status) const +{ + if (U_FAILURE(status)) + return (UDisplayContext)0; + if (type != UDISPCTX_TYPE_CAPITALIZATION) { + status = U_ILLEGAL_ARGUMENT_ERROR; + return (UDisplayContext)0; + } + return fCapitalizationContext; +} + DateIntervalFormat::DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo, const UnicodeString* skeleton, - UErrorCode& status) + UErrorCode& status) : fInfo(NULL), fDateFormat(NULL), fFromCalendar(NULL), @@ -556,7 +706,8 @@ DateIntervalFormat::DateIntervalFormat(const Locale& locale, fDatePattern(NULL), fTimePattern(NULL), fDateTimeFormat(NULL), - fMinimizeType(UDTITVFMT_MINIMIZE_NONE) + fMinimizeType(UDTITVFMT_MINIMIZE_NONE), + fCapitalizationContext(UDISPCTX_CAPITALIZATION_NONE) { LocalPointer info(dtItvInfo, status); LocalPointer dtfmt(static_cast( @@ -582,7 +733,7 @@ DateIntervalFormat::create(const Locale& locale, DateIntervalInfo* dtitvinf, const UnicodeString* skeleton, UErrorCode& status) { - DateIntervalFormat* f = new DateIntervalFormat(locale, dtitvinf, + DateIntervalFormat* f = new DateIntervalFormat(locale, dtitvinf, skeleton, status); if ( f == NULL ) { status = U_MEMORY_ALLOCATION_ERROR; @@ -597,10 +748,10 @@ DateIntervalFormat::create(const Locale& locale, -/** +/** * Initialize interval patterns locale to this formatter - * - * This code is a bit complicated since + * + * This code is a bit complicated since * 1. the interval patterns saved in resource bundle files are interval * patterns based on date or time only. * It does not have interval patterns based on both date and time. @@ -608,30 +759,30 @@ DateIntervalFormat::create(const Locale& locale, * * For example, it has interval patterns on skeleton "dMy" and "hm", * but it does not have interval patterns on skeleton "dMyhm". - * + * * The rule to genearte interval patterns for both date and time skeleton are - * 1) when the year, month, or day differs, concatenate the two original - * expressions with a separator between, - * For example, interval pattern from "Jan 10, 2007 10:10 am" - * to "Jan 11, 2007 10:10am" is - * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am" + * 1) when the year, month, or day differs, concatenate the two original + * expressions with a separator between, + * For example, interval pattern from "Jan 10, 2007 10:10 am" + * to "Jan 11, 2007 10:10am" is + * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am" * - * 2) otherwise, present the date followed by the range expression + * 2) otherwise, present the date followed by the range expression * for the time. - * For example, interval pattern from "Jan 10, 2007 10:10 am" - * to "Jan 10, 2007 11:10am" is - * "Jan 10, 2007 10:10 am - 11:10am" + * For example, interval pattern from "Jan 10, 2007 10:10 am" + * to "Jan 10, 2007 11:10am" is + * "Jan 10, 2007 10:10 am - 11:10am" * * 2. even a pattern does not request a certion calendar field, * the interval pattern needs to include such field if such fields are * different between 2 dates. - * For example, a pattern/skeleton is "hm", but the interval pattern + * For example, a pattern/skeleton is "hm", but the interval pattern * includes year, month, and date when year, month, and date differs. - * + * * @param status output param set to success/failure code on exit - * @stable ICU 4.0 + * @stable ICU 4.0 */ -void +void DateIntervalFormat::initializePattern(UErrorCode& status) { if ( U_FAILURE(status) ) { return; @@ -653,7 +804,7 @@ DateIntervalFormat::initializePattern(UErrorCode& status) { fSkeleton = DateTimePatternGenerator::staticGetSkeleton( fullPattern, status); if ( U_FAILURE(status) ) { - return; + return; } } @@ -675,7 +826,7 @@ DateIntervalFormat::initializePattern(UErrorCode& status) { /* the difference between time skeleton and normalizedTimeSkeleton are: * 1. (Formerly, normalized time skeleton folded 'H' to 'h'; no longer true) * 2. 'a' is omitted in normalized time skeleton. - * 3. there is only one appearance for 'h' or 'H', 'm','v', 'z' in normalized + * 3. there is only one appearance for 'h' or 'H', 'm','v', 'z' in normalized * time skeleton * * The difference between date skeleton and normalizedDateSkeleton are: @@ -701,30 +852,25 @@ DateIntervalFormat::initializePattern(UErrorCode& status) { // with the time interval. // The date/time pattern ( such as {0} {1} ) is saved in // calendar, that is why need to get the CalendarData here. - CalendarData* calData = new CalendarData(locale, NULL, status); - if ( U_FAILURE(status) ) { - delete calData; - return; - } - if ( calData == NULL ) { - status = U_MEMORY_ALLOCATION_ERROR; - return; - } - - const UResourceBundle* dateTimePatternsRes = calData->getByKey( - gDateTimePatternsTag, status); + LocalUResourceBundlePointer dateTimePatternsRes(ures_open(NULL, locale.getBaseName(), &status)); + ures_getByKey(dateTimePatternsRes.getAlias(), gCalendarTag, + dateTimePatternsRes.getAlias(), &status); + ures_getByKeyWithFallback(dateTimePatternsRes.getAlias(), gGregorianTag, + dateTimePatternsRes.getAlias(), &status); + ures_getByKeyWithFallback(dateTimePatternsRes.getAlias(), gDateTimePatternsTag, + dateTimePatternsRes.getAlias(), &status); + int32_t dateTimeFormatLength; const UChar* dateTimeFormat = ures_getStringByIndex( - dateTimePatternsRes, + dateTimePatternsRes.getAlias(), (int32_t)DateFormat::kDateTime, &dateTimeFormatLength, &status); if ( U_SUCCESS(status) && dateTimeFormatLength >= 3 ) { fDateTimeFormat = new UnicodeString(dateTimeFormat, dateTimeFormatLength); } - delete calData; } - UBool found = setSeparateDateTimePtn(normalizedDateSkeleton, + UBool found = setSeparateDateTimePtn(normalizedDateSkeleton, normalizedTimeSkeleton); // for skeletons with seconds, found is false and we enter this block @@ -738,15 +884,15 @@ DateIntervalFormat::initializePattern(UErrorCode& status) { UnicodeString pattern = DateFormat::getBestPattern( locale, timeSkeleton, status); if ( U_FAILURE(status) ) { - return; + return; } // for fall back interval patterns, // the first part of the pattern is empty, // the second part of the pattern is the full-pattern // should be used in fall-back. - setPatternInfo(UCAL_DATE, NULL, &pattern, fInfo->getDefaultOrder()); - setPatternInfo(UCAL_MONTH, NULL, &pattern, fInfo->getDefaultOrder()); - setPatternInfo(UCAL_YEAR, NULL, &pattern, fInfo->getDefaultOrder()); + setPatternInfo(UCAL_DATE, NULL, &pattern, fInfo->getDefaultOrder()); + setPatternInfo(UCAL_MONTH, NULL, &pattern, fInfo->getDefaultOrder()); + setPatternInfo(UCAL_YEAR, NULL, &pattern, fInfo->getDefaultOrder()); } else { // TODO: fall back } @@ -755,7 +901,7 @@ DateIntervalFormat::initializePattern(UErrorCode& status) { } return; } // end of skeleton not found - // interval patterns for skeleton are found in resource + // interval patterns for skeleton are found in resource if ( timeSkeleton.length() == 0 ) { // done } else if ( dateSkeleton.length() == 0 ) { @@ -764,25 +910,25 @@ DateIntervalFormat::initializePattern(UErrorCode& status) { UnicodeString pattern = DateFormat::getBestPattern( locale, timeSkeleton, status); if ( U_FAILURE(status) ) { - return; + return; } // for fall back interval patterns, // the first part of the pattern is empty, // the second part of the pattern is the full-pattern // should be used in fall-back. - setPatternInfo(UCAL_DATE, NULL, &pattern, fInfo->getDefaultOrder()); - setPatternInfo(UCAL_MONTH, NULL, &pattern, fInfo->getDefaultOrder()); - setPatternInfo(UCAL_YEAR, NULL, &pattern, fInfo->getDefaultOrder()); + setPatternInfo(UCAL_DATE, NULL, &pattern, fInfo->getDefaultOrder()); + setPatternInfo(UCAL_MONTH, NULL, &pattern, fInfo->getDefaultOrder()); + setPatternInfo(UCAL_YEAR, NULL, &pattern, fInfo->getDefaultOrder()); } else { /* if both present, - * 1) when the year, month, or day differs, - * concatenate the two original expressions with a separator between, - * 2) otherwise, present the date followed by the - * range expression for the time. + * 1) when the year, month, or day differs, + * concatenate the two original expressions with a separator between, + * 2) otherwise, present the date followed by the + * range expression for the time. */ /* - * 1) when the year, month, or day differs, - * concatenate the two original expressions with a separator between, + * 1) when the year, month, or day differs, + * concatenate the two original expressions with a separator between, */ // if field exists, use fall back UnicodeString skeleton = fSkeleton; @@ -801,10 +947,10 @@ DateIntervalFormat::initializePattern(UErrorCode& status) { skeleton.insert(0, LOW_Y); setFallbackPattern(UCAL_YEAR, skeleton, status); } - + /* - * 2) otherwise, present the date followed by the - * range expression for the time. + * 2) otherwise, present the date followed by the + * range expression for the time. */ if ( fDateTimeFormat == NULL ) { @@ -823,10 +969,10 @@ DateIntervalFormat::initializePattern(UErrorCode& status) { -void U_EXPORT2 -DateIntervalFormat::getDateTimeSkeleton(const UnicodeString& skeleton, - UnicodeString& dateSkeleton, - UnicodeString& normalizedDateSkeleton, +void U_EXPORT2 +DateIntervalFormat::getDateTimeSkeleton(const UnicodeString& skeleton, + UnicodeString& dateSkeleton, + UnicodeString& normalizedDateSkeleton, UnicodeString& timeSkeleton, UnicodeString& normalizedTimeSkeleton) { // dateSkeleton follows the sequence of y*M*E*d* @@ -881,7 +1027,7 @@ DateIntervalFormat::getDateTimeSkeleton(const UnicodeString& skeleton, dateSkeleton.append(ch); break; case LOW_A: - // 'a' is implicitly handled + // 'a' is implicitly handled timeSkeleton.append(ch); break; case LOW_H: @@ -914,7 +1060,7 @@ DateIntervalFormat::getDateTimeSkeleton(const UnicodeString& skeleton, case CAP_A: timeSkeleton.append(ch); normalizedTimeSkeleton.append(ch); - break; + break; } } @@ -928,8 +1074,7 @@ DateIntervalFormat::getDateTimeSkeleton(const UnicodeString& skeleton, if ( MCount < 3 ) { normalizedDateSkeleton.append(CAP_M); } else { - int32_t i; - for ( i = 0; i < MCount && i < MAX_M_COUNT; ++i ) { + for ( int32_t j = 0; j < MCount && j < MAX_M_COUNT; ++j) { normalizedDateSkeleton.append(CAP_M); } } @@ -938,8 +1083,7 @@ DateIntervalFormat::getDateTimeSkeleton(const UnicodeString& skeleton, if ( ECount <= 3 ) { normalizedDateSkeleton.append(CAP_E); } else { - int32_t i; - for ( i = 0; i < ECount && i < MAX_E_COUNT; ++i ) { + for ( int32_t j = 0; j < ECount && j < MAX_E_COUNT; ++j ) { normalizedDateSkeleton.append(CAP_E); } } @@ -971,7 +1115,7 @@ DateIntervalFormat::getDateTimeSkeleton(const UnicodeString& skeleton, * Generate date or time interval pattern from resource, * and set them into the interval pattern locale to this formatter. * - * It needs to handle the following: + * It needs to handle the following: * 1. need to adjust field width. * For example, the interval patterns saved in DateIntervalInfo * includes "dMMMy", but not "dMMMMy". @@ -991,7 +1135,7 @@ DateIntervalFormat::getDateTimeSkeleton(const UnicodeString& skeleton, * FALSE otherwise. * @stable ICU 4.0 */ -UBool +UBool DateIntervalFormat::setSeparateDateTimePtn( const UnicodeString& dateSkeleton, const UnicodeString& timeSkeleton) { @@ -1007,17 +1151,17 @@ DateIntervalFormat::setSeparateDateTimePtn( skeleton = &dateSkeleton; } - /* interval patterns for skeleton "dMMMy" (but not "dMMMMy") + /* interval patterns for skeleton "dMMMy" (but not "dMMMMy") * are defined in resource, * interval patterns for skeleton "dMMMMy" are calculated by * 1. get the best match skeleton for "dMMMMy", which is "dMMMy" * 2. get the interval patterns for "dMMMy", - * 3. extend "MMM" to "MMMM" in above interval patterns for "dMMMMy" + * 3. extend "MMM" to "MMMM" in above interval patterns for "dMMMMy" * getBestSkeleton() is step 1. */ // best skeleton, and the difference information int8_t differenceInfo = 0; - const UnicodeString* bestSkeleton = fInfo->getBestSkeleton(*skeleton, + const UnicodeString* bestSkeleton = fInfo->getBestSkeleton(*skeleton, differenceInfo); /* best skeleton could be NULL. For example: in "ca" resource file, @@ -1029,9 +1173,9 @@ DateIntervalFormat::setSeparateDateTimePtn( and the best skeleton match could be NULL */ if ( bestSkeleton == NULL ) { - return false; - } - + return false; + } + // Set patterns for fallback use, need to do this // before returning if differenceInfo == -1 UErrorCode status; @@ -1050,10 +1194,10 @@ DateIntervalFormat::setSeparateDateTimePtn( // 0 means the best matched skeleton is the same as input skeleton // 1 means the fields are the same, but field width are different // 2 means the only difference between fields are v/z, - // -1 means there are other fields difference + // -1 means there are other fields difference // (this will happen, for instance, if the supplied skeleton has seconds, // but no skeletons in the intervalFormats data do) - if ( differenceInfo == -1 ) { + if ( differenceInfo == -1 ) { // skeleton has different fields, not only v/z difference return false; } @@ -1065,16 +1209,18 @@ DateIntervalFormat::setSeparateDateTimePtn( setIntervalPattern(UCAL_DATE, skeleton, bestSkeleton, differenceInfo, &extendedSkeleton, &extendedBestSkeleton); - UBool extended = setIntervalPattern(UCAL_MONTH, skeleton, bestSkeleton, + UBool extended = setIntervalPattern(UCAL_MONTH, skeleton, bestSkeleton, differenceInfo, &extendedSkeleton, &extendedBestSkeleton); - + if ( extended ) { bestSkeleton = &extendedBestSkeleton; skeleton = &extendedSkeleton; } setIntervalPattern(UCAL_YEAR, skeleton, bestSkeleton, differenceInfo, &extendedSkeleton, &extendedBestSkeleton); + setIntervalPattern(UCAL_ERA, skeleton, bestSkeleton, differenceInfo, + &extendedSkeleton, &extendedBestSkeleton); } else { setIntervalPattern(UCAL_MINUTE, skeleton, bestSkeleton, differenceInfo); setIntervalPattern(UCAL_HOUR, skeleton, bestSkeleton, differenceInfo); @@ -1104,9 +1250,9 @@ DateIntervalFormat::setFallbackPattern(UCalendarDateFields field, void -DateIntervalFormat::setPatternInfo(UCalendarDateFields field, +DateIntervalFormat::setPatternInfo(UCalendarDateFields field, const UnicodeString* firstPart, - const UnicodeString* secondPart, + const UnicodeString* secondPart, UBool laterDateFirst) { // for fall back interval patterns, // the first part of the pattern is empty, @@ -1149,7 +1295,7 @@ DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, UnicodeString realPattern; if ( intervalPattern.startsWith(gLaterFirstPrefix, prefixLength) ) { order = true; - intervalPattern.extract(prefixLength, + intervalPattern.extract(prefixLength, intervalPattern.length() - prefixLength, realPattern); pattern = &realPattern; @@ -1163,7 +1309,7 @@ DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, } int32_t splitPoint = splitPatternInto2Part(*pattern); - + UnicodeString firstPart; UnicodeString secondPart; pattern->extract(0, splitPoint, firstPart); @@ -1190,11 +1336,11 @@ DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, * 0 means the best matched skeleton is the same as input skeleton * 1 means the fields are the same, but field width are different * 2 means the only difference between fields are v/z, - * -1 means there are other fields difference + * -1 means there are other fields difference * * @param extendedSkeleton extended skeleton * @param extendedBestSkeleton extended best match skeleton - * @return whether the interval pattern is found + * @return whether the interval pattern is found * through extending skeleton or not. * TRUE if interval pattern is found by * extending skeleton, FALSE otherwise. @@ -1219,7 +1365,7 @@ DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, } // for 24 hour system, interval patterns in resource file - // might not include pattern when am_pm differ, + // might not include pattern when am_pm differ, // which should be the same as hour differ. // add it here for simplicity if ( field == UCAL_AM_PM ) { @@ -1228,7 +1374,7 @@ DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, setIntervalPattern(field, pattern); } return false; - } + } // else, looking for pattern when 'y' differ for 'dMMMM' skeleton, // first, get best match pattern "MMMd", // since there is no pattern for 'y' differs for skeleton 'MMMd', @@ -1242,11 +1388,11 @@ DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, extendedSkeleton->insert(0, fieldLetter); extendedBestSkeleton->insert(0, fieldLetter); // for example, looking for patterns when 'y' differ for - // skeleton "MMMM". + // skeleton "MMMM". fInfo->getIntervalPattern(*extendedBestSkeleton,field,pattern,status); if ( pattern.isEmpty() && differenceInfo == 0 ) { // if there is no skeleton "yMMMM" defined, - // look for the best match skeleton, for example: "yMMM" + // look for the best match skeleton, for example: "yMMM" const UnicodeString* tmpBest = fInfo->getBestSkeleton( *extendedBestSkeleton, differenceInfo); if ( tmpBest != 0 && differenceInfo != -1 ) { @@ -1255,7 +1401,7 @@ DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, } } } - } + } if ( !pattern.isEmpty() ) { if ( differenceInfo != 0 ) { UnicodeString adjustIntervalPattern; @@ -1274,7 +1420,7 @@ DateIntervalFormat::setIntervalPattern(UCalendarDateFields field, -int32_t U_EXPORT2 +int32_t U_EXPORT2 DateIntervalFormat::splitPatternInto2Part(const UnicodeString& intervalPattern) { UBool inQuote = false; UChar prevCh = 0; @@ -1284,7 +1430,7 @@ DateIntervalFormat::splitPatternInto2Part(const UnicodeString& intervalPattern) It is a pattern applies to first calendar if it is first time seen, otherwise, it is a pattern applies to the second calendar */ - UBool patternRepeated[] = + UBool patternRepeated[] = { // A B C D E F G H I J K L M N O 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -1297,16 +1443,16 @@ DateIntervalFormat::splitPatternInto2Part(const UnicodeString& intervalPattern) }; int8_t PATTERN_CHAR_BASE = 0x41; - + /* loop through the pattern string character by character looking for * the first repeated pattern letter, which breaks the interval pattern - * into 2 parts. + * into 2 parts. */ int32_t i; UBool foundRepetition = false; for (i = 0; i < intervalPattern.length(); ++i) { UChar ch = intervalPattern.charAt(i); - + if (ch != prevCh && count > 0) { // check the repeativeness of pattern letter UBool repeated = patternRepeated[(int)(prevCh - PATTERN_CHAR_BASE)]; @@ -1318,26 +1464,26 @@ DateIntervalFormat::splitPatternInto2Part(const UnicodeString& intervalPattern) } count = 0; } - if (ch == '\'') { + if (ch == 0x0027 /*'*/) { // Consecutive single quotes are a single quote literal, // either outside of quotes or between quotes - if ((i+1) < intervalPattern.length() && - intervalPattern.charAt(i+1) == '\'') { + if ((i+1) < intervalPattern.length() && + intervalPattern.charAt(i+1) == 0x0027 /*'*/) { ++i; } else { inQuote = ! inQuote; } - } + } else if (!inQuote && ((ch >= 0x0061 /*'a'*/ && ch <= 0x007A /*'z'*/) || (ch >= 0x0041 /*'A'*/ && ch <= 0x005A /*'Z'*/))) { - // ch is a date-time pattern character + // ch is a date-time pattern character prevCh = ch; ++count; } } // check last pattern char, distinguish - // "dd MM" ( no repetition ), - // "d-d"(last char repeated ), and + // "dd MM" ( no repetition ), + // "d-d"(last char repeated ), and // "d-d MM" ( repetition found ) if ( count > 0 && foundRepetition == FALSE ) { if ( patternRepeated[(int)(prevCh - PATTERN_CHAR_BASE)] == FALSE ) { @@ -1347,95 +1493,97 @@ DateIntervalFormat::splitPatternInto2Part(const UnicodeString& intervalPattern) return (i - count); } -static const UChar bracketedZero[] = {0x7B,0x30,0x7D}; -static const UChar bracketedOne[] = {0x7B,0x31,0x7D}; - -void -DateIntervalFormat::adjustPosition(UnicodeString& combiningPattern, // has {0} and {1} in it - UnicodeString& pat0, FieldPosition& pos0, // pattern and pos corresponding to {0} - UnicodeString& pat1, FieldPosition& pos1, // pattern and pos corresponding to {1} - FieldPosition& posResult) { - int32_t index0 = combiningPattern.indexOf(bracketedZero, 3, 0); - int32_t index1 = combiningPattern.indexOf(bracketedOne, 3, 0); - if (index0 < 0 || index1 < 0) { +void DateIntervalFormat::fallbackFormatRange( + Calendar& fromCalendar, + Calendar& toCalendar, + UnicodeString& appendTo, + int8_t& firstIndex, + FieldPositionHandler& fphandler, + UErrorCode& status) const { + UnicodeString fallbackPattern; + fInfo->getFallbackIntervalPattern(fallbackPattern); + SimpleFormatter sf(fallbackPattern, 2, 2, status); + if (U_FAILURE(status)) { return; } - int32_t placeholderLen = 3; // length of "{0}" or "{1}" - if (index0 < index1) { - if (pos0.getEndIndex() > 0) { - posResult.setBeginIndex(pos0.getBeginIndex() + index0); - posResult.setEndIndex(pos0.getEndIndex() + index0); - } else if (pos1.getEndIndex() > 0) { - // here index1 >= 3 - index1 += pat0.length() - placeholderLen; // adjust for pat0 replacing {0} - posResult.setBeginIndex(pos1.getBeginIndex() + index1); - posResult.setEndIndex(pos1.getEndIndex() + index1); - } + int32_t offsets[2]; + UnicodeString patternBody = sf.getTextWithNoArguments(offsets, 2); + + // TODO(ICU-20406): Use SimpleFormatter Iterator interface when available. + // The context for the first of the _format calls in each pair is set by caller. + // This function always leaves _format context as UDISPCTX_CAPITALIZATION_NONE. + if (offsets[0] < offsets[1]) { + firstIndex = 0; + appendTo.append(patternBody.tempSubStringBetween(0, offsets[0])); + fDateFormat->_format(fromCalendar, appendTo, fphandler, status); + appendTo.append(patternBody.tempSubStringBetween(offsets[0], offsets[1])); + fDateFormat->setContext(UDISPCTX_CAPITALIZATION_NONE, status); + fDateFormat->_format(toCalendar, appendTo, fphandler, status); + appendTo.append(patternBody.tempSubStringBetween(offsets[1])); } else { - if (pos1.getEndIndex() > 0) { - posResult.setBeginIndex(pos1.getBeginIndex() + index1); - posResult.setEndIndex(pos1.getEndIndex() + index1); - } else if (pos0.getEndIndex() > 0) { - // here index0 >= 3 - index0 += pat1.length() - placeholderLen; // adjust for pat1 replacing {1} - posResult.setBeginIndex(pos0.getBeginIndex() + index0); - posResult.setEndIndex(pos0.getEndIndex() + index0); - } + firstIndex = 1; + appendTo.append(patternBody.tempSubStringBetween(0, offsets[1])); + fDateFormat->_format(toCalendar, appendTo, fphandler, status); + appendTo.append(patternBody.tempSubStringBetween(offsets[1], offsets[0])); + fDateFormat->setContext(UDISPCTX_CAPITALIZATION_NONE, status); + fDateFormat->_format(fromCalendar, appendTo, fphandler, status); + appendTo.append(patternBody.tempSubStringBetween(offsets[0])); } } -UnicodeString& +UnicodeString& DateIntervalFormat::fallbackFormat(Calendar& fromCalendar, Calendar& toCalendar, UBool fromToOnSameDay, // new UnicodeString& appendTo, - FieldPosition& pos, + int8_t& firstIndex, + FieldPositionHandler& fphandler, UErrorCode& status) const { if ( U_FAILURE(status) ) { return appendTo; } - UnicodeString fullPattern; // for saving the pattern in fDateFormat + UBool formatDatePlusTimeRange = (fromToOnSameDay && fDatePattern && fTimePattern); - // the fall back if (formatDatePlusTimeRange) { + SimpleFormatter sf(*fDateTimeFormat, 2, 2, TRUE, status); + if (U_FAILURE(status)) { + return appendTo; + } + int32_t offsets[2]; + UnicodeString patternBody = sf.getTextWithNoArguments(offsets, 2); + + UnicodeString fullPattern; // for saving the pattern in fDateFormat fDateFormat->toPattern(fullPattern); // save current pattern, restore later - fDateFormat->applyPattern(*fTimePattern); - } - FieldPosition otherPos; - otherPos.setField(pos.getField()); - UnicodeString earlierDate; - fDateFormat->format(fromCalendar, earlierDate, pos); - UnicodeString laterDate; - fDateFormat->format(toCalendar, laterDate, otherPos); - UnicodeString fallbackPattern; - fInfo->getFallbackIntervalPattern(fallbackPattern); - adjustPosition(fallbackPattern, earlierDate, pos, laterDate, otherPos, pos); - UnicodeString fallbackRange; - SimpleFormatter(fallbackPattern, 2, 2, status). - format(earlierDate, laterDate, fallbackRange, status); - if ( U_SUCCESS(status) && formatDatePlusTimeRange ) { - // fallbackRange has just the time range, need to format the date part and combine that - UnicodeString dateTimeFormatNoQuote(*fDateTimeFormat); - dateTimeFormatNoQuote.findAndReplace(UnicodeString(0x0027), UnicodeString()); - fDateFormat->applyPattern(*fDatePattern); - UnicodeString datePortion; - otherPos.setBeginIndex(0); - otherPos.setEndIndex(0); - fDateFormat->format(fromCalendar, datePortion, otherPos); - adjustPosition(dateTimeFormatNoQuote, fallbackRange, pos, datePortion, otherPos, pos); - const UnicodeString *values[2] = { - &fallbackRange, // {0} is time range - &datePortion, // {1} is single date portion - }; - SimpleFormatter(dateTimeFormatNoQuote, 2, 2, status). - formatAndReplace(values, 2, fallbackRange, NULL, 0, status); - } - if ( U_SUCCESS(status) ) { - appendTo.append(fallbackRange); - } - if (formatDatePlusTimeRange) { + + // {0} is time range + // {1} is single date portion + // TODO(ICU-20406): Use SimpleFormatter Iterator interface when available. + if (offsets[0] < offsets[1]) { + appendTo.append(patternBody.tempSubStringBetween(0, offsets[0])); + fDateFormat->applyPattern(*fTimePattern); + fDateFormat->setContext(fCapitalizationContext, status); + fallbackFormatRange(fromCalendar, toCalendar, appendTo, firstIndex, fphandler, status); + appendTo.append(patternBody.tempSubStringBetween(offsets[0], offsets[1])); + fDateFormat->applyPattern(*fDatePattern); + fDateFormat->_format(fromCalendar, appendTo, fphandler, status); + appendTo.append(patternBody.tempSubStringBetween(offsets[1])); + } else { + appendTo.append(patternBody.tempSubStringBetween(0, offsets[1])); + fDateFormat->applyPattern(*fDatePattern); + fDateFormat->setContext(fCapitalizationContext, status); + fDateFormat->_format(fromCalendar, appendTo, fphandler, status); + appendTo.append(patternBody.tempSubStringBetween(offsets[1], offsets[0])); + fDateFormat->applyPattern(*fTimePattern); + fDateFormat->setContext(UDISPCTX_CAPITALIZATION_NONE, status); + fallbackFormatRange(fromCalendar, toCalendar, appendTo, firstIndex, fphandler, status); + appendTo.append(patternBody.tempSubStringBetween(offsets[0])); + } + // restore full pattern fDateFormat->applyPattern(fullPattern); + } else { + fDateFormat->setContext(fCapitalizationContext, status); + fallbackFormatRange(fromCalendar, toCalendar, appendTo, firstIndex, fphandler, status); } return appendTo; } @@ -1443,7 +1591,7 @@ DateIntervalFormat::fallbackFormat(Calendar& fromCalendar, -UBool U_EXPORT2 +UBool U_EXPORT2 DateIntervalFormat::fieldExistsInSkeleton(UCalendarDateFields field, const UnicodeString& skeleton) { @@ -1453,14 +1601,14 @@ DateIntervalFormat::fieldExistsInSkeleton(UCalendarDateFields field, -void U_EXPORT2 +void U_EXPORT2 DateIntervalFormat::adjustFieldWidth(const UnicodeString& inputSkeleton, const UnicodeString& bestMatchSkeleton, const UnicodeString& bestIntervalPattern, int8_t differenceInfo, UnicodeString& adjustedPtn) { adjustedPtn = bestIntervalPattern; - int32_t inputSkeletonFieldWidth[] = + int32_t inputSkeletonFieldWidth[] = { // A B C D E F G H I J K L M N O 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -1472,7 +1620,7 @@ DateIntervalFormat::adjustFieldWidth(const UnicodeString& inputSkeleton, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - int32_t bestMatchSkeletonFieldWidth[] = + int32_t bestMatchSkeletonFieldWidth[] = { // A B C D E F G H I J K L M N O 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -1496,8 +1644,8 @@ DateIntervalFormat::adjustFieldWidth(const UnicodeString& inputSkeleton, int32_t count = 0; const int8_t PATTERN_CHAR_BASE = 0x41; - - // loop through the pattern string character by character + + // loop through the pattern string character by character int32_t adjustedPtnLength = adjustedPtn.length(); int32_t i; for (i = 0; i < adjustedPtnLength; ++i) { @@ -1506,9 +1654,9 @@ DateIntervalFormat::adjustFieldWidth(const UnicodeString& inputSkeleton, // check the repeativeness of pattern letter UChar skeletonChar = prevCh; if ( skeletonChar == CAP_L ) { - // there is no "L" (always be "M") in skeleton, + // there is no "L" (always be "M") in skeleton, // but there is "L" in pattern. - // for skeleton "M+", the pattern might be "...L..." + // for skeleton "M+", the pattern might be "...L..." skeletonChar = CAP_M; } int32_t fieldCount = bestMatchSkeletonFieldWidth[(int)(skeletonChar - PATTERN_CHAR_BASE)]; @@ -1517,25 +1665,25 @@ DateIntervalFormat::adjustFieldWidth(const UnicodeString& inputSkeleton, count = inputFieldCount - fieldCount; int32_t j; for ( j = 0; j < count; ++j ) { - adjustedPtn.insert(i, prevCh); - } + adjustedPtn.insert(i, prevCh); + } i += count; adjustedPtnLength += count; } count = 0; } - if (ch == '\'') { + if (ch == 0x0027 /*'*/) { // Consecutive single quotes are a single quote literal, // either outside of quotes or between quotes - if ((i+1) < adjustedPtn.length() && adjustedPtn.charAt(i+1) == '\'') { + if ((i+1) < adjustedPtn.length() && adjustedPtn.charAt(i+1) == 0x0027 /* ' */) { ++i; } else { inQuote = ! inQuote; } - } - else if ( ! inQuote && ((ch >= 0x0061 /*'a'*/ && ch <= 0x007A /*'z'*/) + } + else if ( ! inQuote && ((ch >= 0x0061 /*'a'*/ && ch <= 0x007A /*'z'*/) || (ch >= 0x0041 /*'A'*/ && ch <= 0x005A /*'Z'*/))) { - // ch is a date-time pattern character + // ch is a date-time pattern character prevCh = ch; ++count; } @@ -1545,9 +1693,9 @@ DateIntervalFormat::adjustFieldWidth(const UnicodeString& inputSkeleton, // check the repeativeness of pattern letter UChar skeletonChar = prevCh; if ( skeletonChar == CAP_L ) { - // there is no "L" (always be "M") in skeleton, + // there is no "L" (always be "M") in skeleton, // but there is "L" in pattern. - // for skeleton "M+", the pattern might be "...L..." + // for skeleton "M+", the pattern might be "...L..." skeletonChar = CAP_M; } int32_t fieldCount = bestMatchSkeletonFieldWidth[(int)(skeletonChar - PATTERN_CHAR_BASE)]; @@ -1556,15 +1704,15 @@ DateIntervalFormat::adjustFieldWidth(const UnicodeString& inputSkeleton, count = inputFieldCount - fieldCount; int32_t j; for ( j = 0; j < count; ++j ) { - adjustedPtn.append(prevCh); - } + adjustedPtn.append(prevCh); + } } } } -void +void DateIntervalFormat::concatSingleDate2TimeInterval(UnicodeString& format, const UnicodeString& datePattern, UCalendarDateFields field, @@ -1586,7 +1734,7 @@ DateIntervalFormat::concatSingleDate2TimeInterval(UnicodeString& format, return; } setIntervalPattern(field, combinedPattern, timeItvPtnInfo.laterDateFirst); - } + } // else: fall back // it should not happen if the interval format defined is valid } @@ -1607,6 +1755,7 @@ DateIntervalFormat::fgCalendarFieldToPatternLetter[] = }; + U_NAMESPACE_END #endif