Skip to content

Commit

Permalink
ICU-22260 Allow relative datetime formatting without context adjustme…
Browse files Browse the repository at this point in the history
…nt with UCONFIG_NO_BREAK_ITERATION
  • Loading branch information
hsivonen authored and FrankYFTang committed Jun 21, 2023
1 parent c94469b commit 3fec4e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
30 changes: 28 additions & 2 deletions icu4c/source/i18n/reldatefmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "unicode/reldatefmt.h"

#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION
#if !UCONFIG_NO_FORMATTING

#include <cmath>
#include <functional>
Expand Down Expand Up @@ -701,7 +701,7 @@ static UBool getDateTimePattern(
return getStringByIndex(topLevel.getAlias(), dateTimeFormatOffset, result, status);
}

template<>
template<>
const RelativeDateTimeCacheData *LocaleCacheKey<RelativeDateTimeCacheData>::createObject(const void * /*unused*/, UErrorCode &status) const {
const char *localeId = fLoc.getName();
LocalUResourceBundlePointer topLevel(ures_open(nullptr, localeId, &status));
Expand Down Expand Up @@ -761,6 +761,7 @@ RelativeDateTimeFormatter::RelativeDateTimeFormatter(UErrorCode& status) :
fStyle(UDAT_STYLE_LONG),
fContext(UDISPCTX_CAPITALIZATION_NONE),
fOptBreakIterator(nullptr) {
(void)fOptBreakIterator; // suppress unused field warning
init(nullptr, nullptr, status);
}

Expand Down Expand Up @@ -809,11 +810,16 @@ RelativeDateTimeFormatter::RelativeDateTimeFormatter(
return;
}
if (capitalizationContext == UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE) {
#if !UCONFIG_NO_BREAK_ITERATION
BreakIterator *bi = BreakIterator::createSentenceInstance(locale, status);
if (U_FAILURE(status)) {
return;
}
init(nfToAdopt, bi, status);
#else
status = U_UNSUPPORTED_ERROR;
return;
#endif // !UCONFIG_NO_BREAK_ITERATION
} else {
init(nfToAdopt, nullptr, status);
}
Expand All @@ -832,9 +838,11 @@ RelativeDateTimeFormatter::RelativeDateTimeFormatter(
fCache->addRef();
fNumberFormat->addRef();
fPluralRules->addRef();
#if !UCONFIG_NO_BREAK_ITERATION
if (fOptBreakIterator != nullptr) {
fOptBreakIterator->addRef();
}
#endif // !UCONFIG_NO_BREAK_ITERATION
}

RelativeDateTimeFormatter& RelativeDateTimeFormatter::operator=(
Expand All @@ -843,7 +851,9 @@ RelativeDateTimeFormatter& RelativeDateTimeFormatter::operator=(
SharedObject::copyPtr(other.fCache, fCache);
SharedObject::copyPtr(other.fNumberFormat, fNumberFormat);
SharedObject::copyPtr(other.fPluralRules, fPluralRules);
#if !UCONFIG_NO_BREAK_ITERATION
SharedObject::copyPtr(other.fOptBreakIterator, fOptBreakIterator);
#endif // !UCONFIG_NO_BREAK_ITERATION
fStyle = other.fStyle;
fContext = other.fContext;
fLocale = other.fLocale;
Expand All @@ -861,9 +871,11 @@ RelativeDateTimeFormatter::~RelativeDateTimeFormatter() {
if (fPluralRules != nullptr) {
fPluralRules->removeRef();
}
#if !UCONFIG_NO_BREAK_ITERATION
if (fOptBreakIterator != nullptr) {
fOptBreakIterator->removeRef();
}
#endif // !UCONFIG_NO_BREAK_ITERATION
}

const NumberFormat& RelativeDateTimeFormatter::getNumberFormat() const {
Expand Down Expand Up @@ -1191,6 +1203,7 @@ UnicodeString& RelativeDateTimeFormatter::combineDateAndTime(
}

UnicodeString& RelativeDateTimeFormatter::adjustForContext(UnicodeString &str) const {
#if !UCONFIG_NO_BREAK_ITERATION
if (fOptBreakIterator == nullptr
|| str.length() == 0 || !u_islower(str.char32At(0))) {
return str;
Expand All @@ -1204,25 +1217,36 @@ UnicodeString& RelativeDateTimeFormatter::adjustForContext(UnicodeString &str) c
fOptBreakIterator->get(),
fLocale,
U_TITLECASE_NO_LOWERCASE | U_TITLECASE_NO_BREAK_ADJUSTMENT);
#endif // !UCONFIG_NO_BREAK_ITERATION
return str;
}

UBool RelativeDateTimeFormatter::checkNoAdjustForContext(UErrorCode& status) const {
#if !UCONFIG_NO_BREAK_ITERATION
// This is unsupported because it's hard to keep fields in sync with title
// casing. The code could be written and tested if there is demand.
if (fOptBreakIterator != nullptr) {
status = U_UNSUPPORTED_ERROR;
return false;
}
#else
(void)status; // suppress unused argument warning
#endif // !UCONFIG_NO_BREAK_ITERATION
return true;
}

void RelativeDateTimeFormatter::init(
NumberFormat *nfToAdopt,
#if !UCONFIG_NO_BREAK_ITERATION
BreakIterator *biToAdopt,
#else
std::nullptr_t,
#endif // !UCONFIG_NO_BREAK_ITERATION
UErrorCode &status) {
LocalPointer<NumberFormat> nf(nfToAdopt);
#if !UCONFIG_NO_BREAK_ITERATION
LocalPointer<BreakIterator> bi(biToAdopt);
#endif // !UCONFIG_NO_BREAK_ITERATION
UnifiedCache::getByLocale(fLocale, fCache, status);
if (U_FAILURE(status)) {
return;
Expand Down Expand Up @@ -1251,6 +1275,7 @@ void RelativeDateTimeFormatter::init(
nf.orphan();
SharedObject::copyPtr(shared, fNumberFormat);
}
#if !UCONFIG_NO_BREAK_ITERATION
if (bi.isNull()) {
SharedObject::clearPtr(fOptBreakIterator);
} else {
Expand All @@ -1262,6 +1287,7 @@ void RelativeDateTimeFormatter::init(
bi.orphan();
SharedObject::copyPtr(shared, fOptBreakIterator);
}
#endif // !UCONFIG_NO_BREAK_ITERATION
}

U_NAMESPACE_END
Expand Down
15 changes: 10 additions & 5 deletions icu4c/source/i18n/unicode/reldatefmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,6 @@ typedef enum UDateDirection {
#endif // U_HIDE_DEPRECATED_API
} UDateDirection;

#if !UCONFIG_NO_BREAK_ITERATION

U_NAMESPACE_BEGIN

class BreakIterator;
Expand Down Expand Up @@ -560,7 +558,7 @@ class U_I18N_API RelativeDateTimeFormatter : public UObject {
*
* This method returns a String. To get more information about the
* formatting result, use formatNumericToValue().
*
*
* @param offset The signed offset for the specified unit. This
* will be formatted according to this object's
* NumberFormat object.
Expand All @@ -586,7 +584,7 @@ class U_I18N_API RelativeDateTimeFormatter : public UObject {
*
* This method returns a FormattedRelativeDateTime, which exposes more
* information than the String returned by formatNumeric().
*
*
* @param offset The signed offset for the specified unit. This
* will be formatted according to this object's
* NumberFormat object.
Expand Down Expand Up @@ -696,11 +694,19 @@ class U_I18N_API RelativeDateTimeFormatter : public UObject {
const SharedPluralRules *fPluralRules;
UDateRelativeDateTimeFormatterStyle fStyle;
UDisplayContext fContext;
#if !UCONFIG_NO_BREAK_ITERATION
const SharedBreakIterator *fOptBreakIterator;
#else
std::nullptr_t fOptBreakIterator = nullptr;
#endif // !UCONFIG_NO_BREAK_ITERATION
Locale fLocale;
void init(
NumberFormat *nfToAdopt,
#if !UCONFIG_NO_BREAK_ITERATION
BreakIterator *brkIter,
#else
std::nullptr_t,
#endif // !UCONFIG_NO_BREAK_ITERATION
UErrorCode &status);
UnicodeString& adjustForContext(UnicodeString &) const;
UBool checkNoAdjustForContext(UErrorCode& status) const;
Expand Down Expand Up @@ -743,7 +749,6 @@ class U_I18N_API RelativeDateTimeFormatter : public UObject {

U_NAMESPACE_END

#endif /* !UCONFIG_NO_BREAK_ITERATION */
#endif /* !UCONFIG_NO_FORMATTING */

#endif /* U_SHOW_CPLUSPLUS_API */
Expand Down
4 changes: 2 additions & 2 deletions icu4c/source/i18n/unicode/ureldatefmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "unicode/utypes.h"

#if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION
#if !UCONFIG_NO_FORMATTING

#include "unicode/unum.h"
#include "unicode/udisplaycontext.h"
Expand Down Expand Up @@ -505,6 +505,6 @@ ureldatefmt_combineDateAndTime( const URelativeDateTimeFormatter* reldatefmt,
int32_t resultCapacity,
UErrorCode* status );

#endif /* !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION */
#endif /* !UCONFIG_NO_FORMATTING */

#endif

0 comments on commit 3fec4e7

Please sign in to comment.