ICU4C: Update to version 77.1

This commit is contained in:
Pāvels Nadtočajevs 2025-03-13 23:09:36 +02:00
parent 7e4f6bdb59
commit a25696fd90
No known key found for this signature in database
GPG key ID: 8413210218EF35D2
50 changed files with 2086 additions and 1668 deletions

View file

@ -420,13 +420,13 @@ Files extracted from upstream source:
## icu4c ## icu4c
- Upstream: https://github.com/unicode-org/icu - Upstream: https://github.com/unicode-org/icu
- Version: 76.1 (8eca245c7484ac6cc179e3e5f7c1ea7680810f39, 2024) - Version: 77.1 (457157a92aa053e632cc7fcfd0e12f8a943b2d11, 2025)
- License: Unicode - License: Unicode
Files extracted from upstream source: Files extracted from upstream source:
- The `common` folder - The `common` folder
- `scriptset.*`, `ucln_in.*`, `uspoof.cpp"` and `uspoof_impl.cpp` from the `i18n` folder - `scriptset.*`, `ucln_in.*`, `uspoof.cpp` and `uspoof_impl.*` from the `i18n` folder
- `uspoof.h` from the `i18n/unicode` folder - `uspoof.h` from the `i18n/unicode` folder
- `LICENSE` - `LICENSE`

View file

@ -2,7 +2,7 @@ UNICODE LICENSE V3
COPYRIGHT AND PERMISSION NOTICE COPYRIGHT AND PERMISSION NOTICE
Copyright © 2016-2024 Unicode, Inc. Copyright © 2016-2025 Unicode, Inc.
NOTICE TO USER: Carefully read the following legal agreement. BY NOTICE TO USER: Carefully read the following legal agreement. BY
DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR

View file

@ -59,7 +59,7 @@ BreakIterator::buildInstance(const Locale& loc, const char *type, UErrorCode &st
{ {
char fnbuff[256]; char fnbuff[256];
char ext[4]={'\0'}; char ext[4]={'\0'};
CharString actualLocale; CharString actual;
int32_t size; int32_t size;
const char16_t* brkfname = nullptr; const char16_t* brkfname = nullptr;
UResourceBundle brkRulesStack; UResourceBundle brkRulesStack;
@ -94,7 +94,7 @@ BreakIterator::buildInstance(const Locale& loc, const char *type, UErrorCode &st
// Use the string if we found it // Use the string if we found it
if (U_SUCCESS(status) && brkfname) { if (U_SUCCESS(status) && brkfname) {
actualLocale.append(ures_getLocaleInternal(brkName, &status), -1, status); actual.append(ures_getLocaleInternal(brkName, &status), -1, status);
char16_t* extStart=u_strchr(brkfname, 0x002e); char16_t* extStart=u_strchr(brkfname, 0x002e);
int len = 0; int len = 0;
@ -124,9 +124,8 @@ BreakIterator::buildInstance(const Locale& loc, const char *type, UErrorCode &st
U_LOCALE_BASED(locBased, *(BreakIterator*)result); U_LOCALE_BASED(locBased, *(BreakIterator*)result);
locBased.setLocaleIDs(ures_getLocaleByType(b, ULOC_VALID_LOCALE, &status), locBased.setLocaleIDs(ures_getLocaleByType(b, ULOC_VALID_LOCALE, &status),
actualLocale.data()); actual.data(), status);
uprv_strncpy(result->requestLocale, loc.getName(), ULOC_FULLNAME_CAPACITY); LocaleBased::setLocaleID(loc.getName(), result->requestLocale, status);
result->requestLocale[ULOC_FULLNAME_CAPACITY-1] = 0; // always terminate
} }
ures_close(b); ures_close(b);
@ -206,26 +205,32 @@ BreakIterator::getAvailableLocales(int32_t& count)
BreakIterator::BreakIterator() BreakIterator::BreakIterator()
{ {
*validLocale = *actualLocale = *requestLocale = 0;
} }
BreakIterator::BreakIterator(const BreakIterator &other) : UObject(other) { BreakIterator::BreakIterator(const BreakIterator &other) : UObject(other) {
uprv_strncpy(actualLocale, other.actualLocale, sizeof(actualLocale)); UErrorCode status = U_ZERO_ERROR;
uprv_strncpy(validLocale, other.validLocale, sizeof(validLocale)); U_LOCALE_BASED(locBased, *this);
uprv_strncpy(requestLocale, other.requestLocale, sizeof(requestLocale)); locBased.setLocaleIDs(other.validLocale, other.actualLocale, status);
LocaleBased::setLocaleID(other.requestLocale, requestLocale, status);
U_ASSERT(U_SUCCESS(status));
} }
BreakIterator &BreakIterator::operator =(const BreakIterator &other) { BreakIterator &BreakIterator::operator =(const BreakIterator &other) {
if (this != &other) { if (this != &other) {
uprv_strncpy(actualLocale, other.actualLocale, sizeof(actualLocale)); UErrorCode status = U_ZERO_ERROR;
uprv_strncpy(validLocale, other.validLocale, sizeof(validLocale)); U_LOCALE_BASED(locBased, *this);
uprv_strncpy(requestLocale, other.requestLocale, sizeof(requestLocale)); locBased.setLocaleIDs(other.validLocale, other.actualLocale, status);
LocaleBased::setLocaleID(other.requestLocale, requestLocale, status);
U_ASSERT(U_SUCCESS(status));
} }
return *this; return *this;
} }
BreakIterator::~BreakIterator() BreakIterator::~BreakIterator()
{ {
delete validLocale;
delete actualLocale;
delete requestLocale;
} }
// ------------------------------------------ // ------------------------------------------
@ -394,7 +399,7 @@ BreakIterator::createInstance(const Locale& loc, int32_t kind, UErrorCode& statu
// revisit this in ICU 3.0 and clean it up/fix it/remove it. // revisit this in ICU 3.0 and clean it up/fix it/remove it.
if (U_SUCCESS(status) && (result != nullptr) && *actualLoc.getName() != 0) { if (U_SUCCESS(status) && (result != nullptr) && *actualLoc.getName() != 0) {
U_LOCALE_BASED(locBased, *result); U_LOCALE_BASED(locBased, *result);
locBased.setLocaleIDs(actualLoc.getName(), actualLoc.getName()); locBased.setLocaleIDs(actualLoc.getName(), actualLoc.getName(), status);
} }
return result; return result;
} }
@ -488,6 +493,7 @@ BreakIterator::makeInstance(const Locale& loc, int32_t kind, UErrorCode& status)
} }
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
delete result;
return nullptr; return nullptr;
} }
@ -496,20 +502,25 @@ BreakIterator::makeInstance(const Locale& loc, int32_t kind, UErrorCode& status)
Locale Locale
BreakIterator::getLocale(ULocDataLocaleType type, UErrorCode& status) const { BreakIterator::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
if (type == ULOC_REQUESTED_LOCALE) { if (U_FAILURE(status)) {
return {requestLocale}; return Locale::getRoot();
} }
U_LOCALE_BASED(locBased, *this); if (type == ULOC_REQUESTED_LOCALE) {
return locBased.getLocale(type, status); return requestLocale == nullptr ?
Locale::getRoot() : Locale(requestLocale->data());
}
return LocaleBased::getLocale(validLocale, actualLocale, type, status);
} }
const char * const char *
BreakIterator::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const { BreakIterator::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
if (type == ULOC_REQUESTED_LOCALE) { if (U_FAILURE(status)) {
return requestLocale; return nullptr;
} }
U_LOCALE_BASED(locBased, *this); if (type == ULOC_REQUESTED_LOCALE) {
return locBased.getLocaleID(type, status); return requestLocale == nullptr ? "" : requestLocale->data();
}
return LocaleBased::getLocaleID(validLocale, actualLocale, type, status);
} }
@ -536,8 +547,10 @@ int32_t BreakIterator::getRuleStatusVec(int32_t *fillInVec, int32_t capacity, UE
} }
BreakIterator::BreakIterator (const Locale& valid, const Locale& actual) { BreakIterator::BreakIterator (const Locale& valid, const Locale& actual) {
UErrorCode status = U_ZERO_ERROR;
U_LOCALE_BASED(locBased, (*this)); U_LOCALE_BASED(locBased, (*this));
locBased.setLocaleIDs(valid, actual); locBased.setLocaleIDs(valid.getName(), actual.getName(), status);
U_ASSERT(U_SUCCESS(status));
} }
U_NAMESPACE_END U_NAMESPACE_END

View file

@ -70,6 +70,15 @@ CharString &CharString::copyFrom(const CharString &s, UErrorCode &errorCode) {
return *this; return *this;
} }
CharString &CharString::copyFrom(StringPiece s, UErrorCode &errorCode) {
if (U_FAILURE(errorCode)) {
return *this;
}
len = 0;
append(s, errorCode);
return *this;
}
int32_t CharString::lastIndexOf(char c) const { int32_t CharString::lastIndexOf(char c) const {
for(int32_t i=len; i>0;) { for(int32_t i=len; i>0;) {
if(buffer[--i]==c) { if(buffer[--i]==c) {
@ -143,7 +152,7 @@ CharString &CharString::append(const char *s, int32_t sLength, UErrorCode &error
return *this; return *this;
} }
CharString &CharString::appendNumber(int32_t number, UErrorCode &status) { CharString &CharString::appendNumber(int64_t number, UErrorCode &status) {
if (number < 0) { if (number < 0) {
this->append('-', status); this->append('-', status);
if (U_FAILURE(status)) { if (U_FAILURE(status)) {

View file

@ -74,6 +74,7 @@ public:
* use a UErrorCode where memory allocations might be needed. * use a UErrorCode where memory allocations might be needed.
*/ */
CharString &copyFrom(const CharString &other, UErrorCode &errorCode); CharString &copyFrom(const CharString &other, UErrorCode &errorCode);
CharString &copyFrom(StringPiece s, UErrorCode &errorCode);
UBool isEmpty() const { return len==0; } UBool isEmpty() const { return len==0; }
int32_t length() const { return len; } int32_t length() const { return len; }
@ -135,7 +136,7 @@ public:
} }
CharString &append(const char *s, int32_t sLength, UErrorCode &status); CharString &append(const char *s, int32_t sLength, UErrorCode &status);
CharString &appendNumber(int32_t number, UErrorCode &status); CharString &appendNumber(int64_t number, UErrorCode &status);
/** /**
* Returns a writable buffer for appending and writes the buffer's capacity to * Returns a writable buffer for appending and writes the buffer's capacity to

File diff suppressed because it is too large Load diff

View file

@ -12,44 +12,84 @@
*/ */
#include "locbased.h" #include "locbased.h"
#include "cstring.h" #include "cstring.h"
#include "charstr.h"
U_NAMESPACE_BEGIN U_NAMESPACE_BEGIN
Locale LocaleBased::getLocale(ULocDataLocaleType type, UErrorCode& status) const { Locale LocaleBased::getLocale(const CharString* valid, const CharString* actual,
const char* id = getLocaleID(type, status); ULocDataLocaleType type, UErrorCode& status) {
const char* id = getLocaleID(valid, actual, type, status);
return Locale(id != nullptr ? id : ""); return Locale(id != nullptr ? id : "");
} }
const char* LocaleBased::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const { const char* LocaleBased::getLocaleID(const CharString* valid, const CharString* actual,
ULocDataLocaleType type, UErrorCode& status) {
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
return nullptr; return nullptr;
} }
switch(type) { switch(type) {
case ULOC_VALID_LOCALE: case ULOC_VALID_LOCALE:
return valid; return valid == nullptr ? "" : valid->data();
case ULOC_ACTUAL_LOCALE: case ULOC_ACTUAL_LOCALE:
return actual; return actual == nullptr ? "" : actual->data();
default: default:
status = U_ILLEGAL_ARGUMENT_ERROR; status = U_ILLEGAL_ARGUMENT_ERROR;
return nullptr; return nullptr;
} }
} }
void LocaleBased::setLocaleIDs(const char* validID, const char* actualID) { void LocaleBased::setLocaleIDs(const CharString* validID, const CharString* actualID, UErrorCode& status) {
if (validID != nullptr) { setValidLocaleID(validID, status);
uprv_strncpy(valid, validID, ULOC_FULLNAME_CAPACITY); setActualLocaleID(actualID,status);
valid[ULOC_FULLNAME_CAPACITY-1] = 0; // always terminate }
void LocaleBased::setLocaleIDs(const char* validID, const char* actualID, UErrorCode& status) {
setValidLocaleID(validID, status);
setActualLocaleID(actualID,status);
}
void LocaleBased::setLocaleID(const char* id, CharString*& dest, UErrorCode& status) {
if (U_FAILURE(status)) { return; }
if (id == nullptr || *id == 0) {
delete dest;
dest = nullptr;
} else {
if (dest == nullptr) {
dest = new CharString(id, status);
if (dest == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
} else {
dest->copyFrom(id, status);
} }
if (actualID != nullptr) {
uprv_strncpy(actual, actualID, ULOC_FULLNAME_CAPACITY);
actual[ULOC_FULLNAME_CAPACITY-1] = 0; // always terminate
} }
} }
void LocaleBased::setLocaleIDs(const Locale& validID, const Locale& actualID) { void LocaleBased::setLocaleID(const CharString* id, CharString*& dest, UErrorCode& status) {
uprv_strcpy(valid, validID.getName()); if (U_FAILURE(status)) { return; }
uprv_strcpy(actual, actualID.getName()); if (id == nullptr || id->isEmpty()) {
delete dest;
dest = nullptr;
} else {
if (dest == nullptr) {
dest = new CharString(*id, status);
if (dest == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
} else {
dest->copyFrom(*id, status);
}
}
}
bool LocaleBased::equalIDs(const CharString* left, const CharString* right) {
// true if both are nullptr
if (left == nullptr && right == nullptr) return true;
// false if only one is nullptr
if (left == nullptr || right == nullptr) return false;
return *left == *right;
} }
U_NAMESPACE_END U_NAMESPACE_END

View file

@ -19,13 +19,14 @@
/** /**
* Macro to declare a locale LocaleBased wrapper object for the given * Macro to declare a locale LocaleBased wrapper object for the given
* object, which must have two members named `validLocale' and * object, which must have two members named `validLocale' and
* `actualLocale' of size ULOC_FULLNAME_CAPACITY * `actualLocale' of which are pointers to the internal icu::CharString.
*/ */
#define U_LOCALE_BASED(varname, objname) \ #define U_LOCALE_BASED(varname, objname) \
LocaleBased varname((objname).validLocale, (objname).actualLocale) LocaleBased varname((objname).validLocale, (objname).actualLocale)
U_NAMESPACE_BEGIN U_NAMESPACE_BEGIN
class CharString;
/** /**
* A utility class that unifies the implementation of getLocale() by * A utility class that unifies the implementation of getLocale() by
* various ICU services. This class is likely to be removed in the * various ICU services. This class is likely to be removed in the
@ -41,33 +42,35 @@ class U_COMMON_API LocaleBased : public UMemory {
* Construct a LocaleBased wrapper around the two pointers. These * Construct a LocaleBased wrapper around the two pointers. These
* will be aliased for the lifetime of this object. * will be aliased for the lifetime of this object.
*/ */
inline LocaleBased(char* validAlias, char* actualAlias); inline LocaleBased(CharString*& validAlias, CharString*& actualAlias);
/**
* Construct a LocaleBased wrapper around the two const pointers.
* These will be aliased for the lifetime of this object.
*/
inline LocaleBased(const char* validAlias, const char* actualAlias);
/** /**
* Return locale meta-data for the service object wrapped by this * Return locale meta-data for the service object wrapped by this
* object. Either the valid or the actual locale may be * object. Either the valid or the actual locale may be
* retrieved. * retrieved.
* @param valid The valid locale.
* @param actual The actual locale.
* @param type either ULOC_VALID_LOCALE or ULOC_ACTUAL_LOCALE * @param type either ULOC_VALID_LOCALE or ULOC_ACTUAL_LOCALE
* @param status input-output error code * @param status input-output error code
* @return the indicated locale * @return the indicated locale
*/ */
Locale getLocale(ULocDataLocaleType type, UErrorCode& status) const; static Locale getLocale(
const CharString* valid, const CharString* actual,
ULocDataLocaleType type, UErrorCode& status);
/** /**
* Return the locale ID for the service object wrapped by this * Return the locale ID for the service object wrapped by this
* object. Either the valid or the actual locale may be * object. Either the valid or the actual locale may be
* retrieved. * retrieved.
* @param valid The valid locale.
* @param actual The actual locale.
* @param type either ULOC_VALID_LOCALE or ULOC_ACTUAL_LOCALE * @param type either ULOC_VALID_LOCALE or ULOC_ACTUAL_LOCALE
* @param status input-output error code * @param status input-output error code
* @return the indicated locale ID * @return the indicated locale ID
*/ */
const char* getLocaleID(ULocDataLocaleType type, UErrorCode& status) const; static const char* getLocaleID(
const CharString* valid, const CharString* actual,
ULocDataLocaleType type, UErrorCode& status);
/** /**
* Set the locale meta-data for the service object wrapped by this * Set the locale meta-data for the service object wrapped by this
@ -75,31 +78,40 @@ class U_COMMON_API LocaleBased : public UMemory {
* @param valid the ID of the valid locale * @param valid the ID of the valid locale
* @param actual the ID of the actual locale * @param actual the ID of the actual locale
*/ */
void setLocaleIDs(const char* valid, const char* actual); void setLocaleIDs(const char* valid, const char* actual, UErrorCode& status);
void setLocaleIDs(const CharString* valid, const CharString* actual, UErrorCode& status);
/** static void setLocaleID(const char* id, CharString*& dest, UErrorCode& status);
* Set the locale meta-data for the service object wrapped by this static void setLocaleID(const CharString* id, CharString*& dest, UErrorCode& status);
* object.
* @param valid the ID of the valid locale static bool equalIDs(const CharString* left, const CharString* right);
* @param actual the ID of the actual locale
*/
void setLocaleIDs(const Locale& valid, const Locale& actual);
private: private:
char* valid; void setValidLocaleID(const CharString* id, UErrorCode& status);
void setActualLocaleID(const CharString* id, UErrorCode& status);
void setValidLocaleID(const char* id, UErrorCode& status);
void setActualLocaleID(const char* id, UErrorCode& status);
char* actual; CharString*& valid;
CharString*& actual;
}; };
inline LocaleBased::LocaleBased(char* validAlias, char* actualAlias) : inline LocaleBased::LocaleBased(CharString*& validAlias, CharString*& actualAlias) :
valid(validAlias), actual(actualAlias) { valid(validAlias), actual(actualAlias) {
} }
inline LocaleBased::LocaleBased(const char* validAlias, inline void LocaleBased::setValidLocaleID(const CharString* id, UErrorCode& status) {
const char* actualAlias) : setLocaleID(id, valid, status);
// ugh: cast away const }
valid(const_cast<char*>(validAlias)), actual(const_cast<char*>(actualAlias)) { inline void LocaleBased::setActualLocaleID(const CharString* id, UErrorCode& status) {
setLocaleID(id, actual, status);
}
inline void LocaleBased::setValidLocaleID(const char* id, UErrorCode& status) {
setLocaleID(id, valid, status);
}
inline void LocaleBased::setActualLocaleID(const char* id, UErrorCode& status) {
setLocaleID(id, actual, status);
} }
U_NAMESPACE_END U_NAMESPACE_END

View file

@ -19,6 +19,8 @@
* that then do not depend on resource bundle code and display name data. * that then do not depend on resource bundle code and display name data.
*/ */
#include <string_view>
#include "unicode/utypes.h" #include "unicode/utypes.h"
#include "unicode/brkiter.h" #include "unicode/brkiter.h"
#include "unicode/locid.h" #include "unicode/locid.h"
@ -359,7 +361,7 @@ _getStringOrCopyKey(const char *path, const char *locale,
return u_terminateUChars(dest, destCapacity, length, &errorCode); return u_terminateUChars(dest, destCapacity, length, &errorCode);
} }
using UDisplayNameGetter = icu::CharString(const char*, UErrorCode&); using UDisplayNameGetter = icu::CharString(std::string_view, UErrorCode&);
int32_t int32_t
_getDisplayNameForComponent(const char *locale, _getDisplayNameForComponent(const char *locale,
@ -377,6 +379,10 @@ _getDisplayNameForComponent(const char *locale,
return 0; return 0;
} }
if (locale == nullptr) {
locale = uloc_getDefault();
}
localStatus = U_ZERO_ERROR; localStatus = U_ZERO_ERROR;
icu::CharString localeBuffer = (*getter)(locale, localStatus); icu::CharString localeBuffer = (*getter)(locale, localStatus);
if (U_FAILURE(localStatus)) { if (U_FAILURE(localStatus)) {

View file

@ -1828,8 +1828,13 @@ ulocimp_isCanonicalizedLocaleForTest(const char* localeName)
U_NAMESPACE_BEGIN U_NAMESPACE_BEGIN
/*This function initializes a Locale from a C locale ID*/
Locale& Locale::init(const char* localeID, UBool canonicalize) Locale& Locale::init(const char* localeID, UBool canonicalize)
{
return localeID == nullptr ? *this = getDefault() : init(StringPiece{localeID}, canonicalize);
}
/*This function initializes a Locale from a C locale ID*/
Locale& Locale::init(StringPiece localeID, UBool canonicalize)
{ {
fIsBogus = false; fIsBogus = false;
/* Free our current storage */ /* Free our current storage */
@ -1854,19 +1859,28 @@ Locale& Locale::init(const char* localeID, UBool canonicalize)
int32_t length; int32_t length;
UErrorCode err; UErrorCode err;
if(localeID == nullptr) {
// not an error, just set the default locale
return *this = getDefault();
}
/* preset all fields to empty */ /* preset all fields to empty */
language[0] = script[0] = country[0] = 0; language[0] = script[0] = country[0] = 0;
const auto parse = [canonicalize](std::string_view localeID,
char* name,
int32_t nameCapacity,
UErrorCode& status) {
return ByteSinkUtil::viaByteSinkToTerminatedChars(
name, nameCapacity,
[&](ByteSink& sink, UErrorCode& status) {
if (canonicalize) {
ulocimp_canonicalize(localeID, sink, status);
} else {
ulocimp_getName(localeID, sink, status);
}
},
status);
};
// "canonicalize" the locale ID to ICU/Java format // "canonicalize" the locale ID to ICU/Java format
err = U_ZERO_ERROR; err = U_ZERO_ERROR;
length = canonicalize ? length = parse(localeID, fullName, sizeof fullNameBuffer, err);
uloc_canonicalize(localeID, fullName, sizeof(fullNameBuffer), &err) :
uloc_getName(localeID, fullName, sizeof(fullNameBuffer), &err);
if (err == U_BUFFER_OVERFLOW_ERROR || length >= static_cast<int32_t>(sizeof(fullNameBuffer))) { if (err == U_BUFFER_OVERFLOW_ERROR || length >= static_cast<int32_t>(sizeof(fullNameBuffer))) {
U_ASSERT(baseName == nullptr); U_ASSERT(baseName == nullptr);
@ -1877,9 +1891,7 @@ Locale& Locale::init(const char* localeID, UBool canonicalize)
} }
fullName = newFullName; fullName = newFullName;
err = U_ZERO_ERROR; err = U_ZERO_ERROR;
length = canonicalize ? length = parse(localeID, fullName, length + 1, err);
uloc_canonicalize(localeID, fullName, length+1, &err) :
uloc_getName(localeID, fullName, length+1, &err);
} }
if(U_FAILURE(err) || err == U_STRING_NOT_TERMINATED_WARNING) { if(U_FAILURE(err) || err == U_STRING_NOT_TERMINATED_WARNING) {
/* should never occur */ /* should never occur */
@ -2200,6 +2212,13 @@ Locale::createFromName (const char *name)
} }
} }
Locale U_EXPORT2
Locale::createFromName(StringPiece name) {
Locale loc("");
loc.init(name, false);
return loc;
}
Locale U_EXPORT2 Locale U_EXPORT2
Locale::createCanonical(const char* name) { Locale::createCanonical(const char* name) {
Locale loc(""); Locale loc("");

View file

@ -300,6 +300,9 @@ ulocimp_addLikelySubtags(const char* localeID,
icu::ByteSink& sink, icu::ByteSink& sink,
UErrorCode& status) { UErrorCode& status) {
if (U_FAILURE(status)) { return; } if (U_FAILURE(status)) { return; }
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
icu::CharString localeBuffer = ulocimp_canonicalize(localeID, status); icu::CharString localeBuffer = ulocimp_canonicalize(localeID, status);
_uloc_addLikelySubtags(localeBuffer.data(), sink, status); _uloc_addLikelySubtags(localeBuffer.data(), sink, status);
} }
@ -334,6 +337,9 @@ ulocimp_minimizeSubtags(const char* localeID,
bool favorScript, bool favorScript,
UErrorCode& status) { UErrorCode& status) {
if (U_FAILURE(status)) { return; } if (U_FAILURE(status)) { return; }
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
icu::CharString localeBuffer = ulocimp_canonicalize(localeID, status); icu::CharString localeBuffer = ulocimp_canonicalize(localeID, status);
_uloc_minimizeSubtags(localeBuffer.data(), sink, favorScript, status); _uloc_minimizeSubtags(localeBuffer.data(), sink, favorScript, status);
} }
@ -349,7 +355,9 @@ uloc_isRightToLeft(const char *locale) {
UErrorCode errorCode = U_ZERO_ERROR; UErrorCode errorCode = U_ZERO_ERROR;
icu::CharString lang; icu::CharString lang;
icu::CharString script; icu::CharString script;
ulocimp_getSubtags(locale, &lang, &script, nullptr, nullptr, nullptr, errorCode); ulocimp_getSubtags(
locale == nullptr ? uloc_getDefault() : locale,
&lang, &script, nullptr, nullptr, nullptr, errorCode);
if (U_FAILURE(errorCode) || script.isEmpty()) { if (U_FAILURE(errorCode) || script.isEmpty()) {
// Fastpath: We know the likely scripts and their writing direction // Fastpath: We know the likely scripts and their writing direction
// for some common languages. // for some common languages.
@ -369,7 +377,7 @@ uloc_isRightToLeft(const char *locale) {
if (U_FAILURE(errorCode)) { if (U_FAILURE(errorCode)) {
return false; return false;
} }
ulocimp_getSubtags(likely.data(), nullptr, &script, nullptr, nullptr, nullptr, errorCode); ulocimp_getSubtags(likely.toStringPiece(), nullptr, &script, nullptr, nullptr, nullptr, errorCode);
if (U_FAILURE(errorCode) || script.isEmpty()) { if (U_FAILURE(errorCode) || script.isEmpty()) {
return false; return false;
} }
@ -430,7 +438,7 @@ ulocimp_getRegionForSupplementalData(const char *localeID, bool inferRegion,
icu::CharString rgBuf = GetRegionFromKey(localeID, "rg", status); icu::CharString rgBuf = GetRegionFromKey(localeID, "rg", status);
if (U_SUCCESS(status) && rgBuf.isEmpty()) { if (U_SUCCESS(status) && rgBuf.isEmpty()) {
// No valid rg keyword value, try for unicode_region_subtag // No valid rg keyword value, try for unicode_region_subtag
rgBuf = ulocimp_getRegion(localeID, status); rgBuf = ulocimp_getRegion(localeID == nullptr ? uloc_getDefault() : localeID, status);
if (U_SUCCESS(status) && rgBuf.isEmpty() && inferRegion) { if (U_SUCCESS(status) && rgBuf.isEmpty() && inferRegion) {
// Second check for sd keyword value // Second check for sd keyword value
rgBuf = GetRegionFromKey(localeID, "sd", status); rgBuf = GetRegionFromKey(localeID, "sd", status);
@ -439,7 +447,7 @@ ulocimp_getRegionForSupplementalData(const char *localeID, bool inferRegion,
UErrorCode rgStatus = U_ZERO_ERROR; UErrorCode rgStatus = U_ZERO_ERROR;
icu::CharString locBuf = ulocimp_addLikelySubtags(localeID, rgStatus); icu::CharString locBuf = ulocimp_addLikelySubtags(localeID, rgStatus);
if (U_SUCCESS(rgStatus)) { if (U_SUCCESS(rgStatus)) {
rgBuf = ulocimp_getRegion(locBuf.data(), status); rgBuf = ulocimp_getRegion(locBuf.toStringPiece(), status);
} }
} }
} }

View file

@ -527,7 +527,7 @@ LSR LikelySubtags::makeMaximizedLsrFrom(const Locale &locale,
return {}; return {};
} }
const char *name = locale.getName(); const char *name = locale.getName();
if (uprv_isAtSign(name[0]) && name[1] == 'x' && name[2] == '=') { // name.startsWith("@x=") if (!returnInputIfUnmatch && uprv_isAtSign(name[0]) && name[1] == 'x' && name[2] == '=') { // name.startsWith("@x=")
// Private use language tag x-subtag-subtag... which CLDR changes to // Private use language tag x-subtag-subtag... which CLDR changes to
// und-x-subtag-subtag... // und-x-subtag-subtag...
return LSR(name, "", "", LSR::EXPLICIT_LSR); return LSR(name, "", "", LSR::EXPLICIT_LSR);

View file

@ -161,6 +161,9 @@ _uloc_getOrientationHelper(const char* localeId,
if (U_FAILURE(status)) { return result; } if (U_FAILURE(status)) { return result; }
if (localeId == nullptr) {
localeId = uloc_getDefault();
}
icu::CharString localeBuffer = ulocimp_canonicalize(localeId, status); icu::CharString localeBuffer = ulocimp_canonicalize(localeId, status);
if (U_FAILURE(status)) { return result; } if (U_FAILURE(status)) { return result; }

View file

@ -193,7 +193,7 @@ u_strToPunycode(const char16_t *src, int32_t srcLength,
return 0; return 0;
} }
if(src==nullptr || srcLength<-1 || (dest==nullptr && destCapacity!=0)) { if(src==nullptr || srcLength<-1 || destCapacity<0 || (dest==nullptr && destCapacity!=0)) {
*pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
return 0; return 0;
} }

View file

@ -76,7 +76,7 @@
#include <float.h> #include <float.h>
#ifndef U_COMMON_IMPLEMENTATION #ifndef U_COMMON_IMPLEMENTATION
#error U_COMMON_IMPLEMENTATION not set - must be set for all ICU source files in common/ - see https://unicode-org.github.io/icu/userguide/howtouseicu #error U_COMMON_IMPLEMENTATION not set - must be set for all ICU source files in common/ - see https://unicode-org.github.io/icu/userguide/icu/howtouseicu.html
#endif #endif

View file

@ -47,7 +47,10 @@ static int gLastSerial = 0;
// Constructor. Just set the fields to reasonable default values. // Constructor. Just set the fields to reasonable default values.
// //
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
RBBINode::RBBINode(NodeType t) : UMemory() { RBBINode::RBBINode(NodeType t, UErrorCode& status) : UMemory() {
if (U_FAILURE(status)) {
return;
}
#ifdef RBBI_DEBUG #ifdef RBBI_DEBUG
fSerialNum = ++gLastSerial; fSerialNum = ++gLastSerial;
#endif #endif
@ -65,10 +68,13 @@ RBBINode::RBBINode(NodeType t) : UMemory() {
fVal = 0; fVal = 0;
fPrecedence = precZero; fPrecedence = precZero;
UErrorCode status = U_ZERO_ERROR; fFirstPosSet = new UVector(status);
fFirstPosSet = new UVector(status); // TODO - get a real status from somewhere
fLastPosSet = new UVector(status); fLastPosSet = new UVector(status);
fFollowPos = new UVector(status); fFollowPos = new UVector(status);
if (U_SUCCESS(status) &&
(fFirstPosSet == nullptr || fLastPosSet == nullptr || fFollowPos == nullptr)) {
status = U_MEMORY_ALLOCATION_ERROR;
}
if (t==opCat) {fPrecedence = precOpCat;} if (t==opCat) {fPrecedence = precOpCat;}
else if (t==opOr) {fPrecedence = precOpOr;} else if (t==opOr) {fPrecedence = precOpOr;}
else if (t==opStart) {fPrecedence = precStart;} else if (t==opStart) {fPrecedence = precStart;}
@ -77,7 +83,10 @@ RBBINode::RBBINode(NodeType t) : UMemory() {
} }
RBBINode::RBBINode(const RBBINode &other) : UMemory(other) { RBBINode::RBBINode(const RBBINode &other, UErrorCode& status) : UMemory(other) {
if (U_FAILURE(status)) {
return;
}
#ifdef RBBI_DEBUG #ifdef RBBI_DEBUG
fSerialNum = ++gLastSerial; fSerialNum = ++gLastSerial;
#endif #endif
@ -94,10 +103,13 @@ RBBINode::RBBINode(const RBBINode &other) : UMemory(other) {
fVal = other.fVal; fVal = other.fVal;
fRuleRoot = false; fRuleRoot = false;
fChainIn = other.fChainIn; fChainIn = other.fChainIn;
UErrorCode status = U_ZERO_ERROR;
fFirstPosSet = new UVector(status); // TODO - get a real status from somewhere fFirstPosSet = new UVector(status); // TODO - get a real status from somewhere
fLastPosSet = new UVector(status); fLastPosSet = new UVector(status);
fFollowPos = new UVector(status); fFollowPos = new UVector(status);
if (U_SUCCESS(status) &&
(fFirstPosSet == nullptr || fLastPosSet == nullptr || fFollowPos == nullptr)) {
status = U_MEMORY_ALLOCATION_ERROR;
}
} }
@ -193,27 +205,54 @@ void RBBINode::NRDeleteNode(RBBINode *node) {
// references in preparation for generating the DFA tables. // references in preparation for generating the DFA tables.
// //
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
RBBINode *RBBINode::cloneTree() { constexpr int kRecursiveDepthLimit = 3500;
RBBINode *RBBINode::cloneTree(UErrorCode &status, int depth) {
if (U_FAILURE(status)) {
return nullptr;
}
// If the depth of the stack is too deep, we return U_INPUT_TOO_LONG_ERROR
// to avoid stack overflow crash.
if (depth > kRecursiveDepthLimit) {
status = U_INPUT_TOO_LONG_ERROR;
return nullptr;
}
RBBINode *n; RBBINode *n;
if (fType == RBBINode::varRef) { if (fType == RBBINode::varRef) {
// If the current node is a variable reference, skip over it // If the current node is a variable reference, skip over it
// and clone the definition of the variable instead. // and clone the definition of the variable instead.
n = fLeftChild->cloneTree(); n = fLeftChild->cloneTree(status, depth+1);
if (U_FAILURE(status)) {
return nullptr;
}
} else if (fType == RBBINode::uset) { } else if (fType == RBBINode::uset) {
n = this; n = this;
} else { } else {
n = new RBBINode(*this); n = new RBBINode(*this, status);
if (U_FAILURE(status)) {
delete n;
return nullptr;
}
// Check for null pointer. // Check for null pointer.
if (n != nullptr) { if (n == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
return nullptr;
}
if (fLeftChild != nullptr) { if (fLeftChild != nullptr) {
n->fLeftChild = fLeftChild->cloneTree(); n->fLeftChild = fLeftChild->cloneTree(status, depth+1);
if (U_FAILURE(status)) {
delete n;
return nullptr;
}
n->fLeftChild->fParent = n; n->fLeftChild->fParent = n;
} }
if (fRightChild != nullptr) { if (fRightChild != nullptr) {
n->fRightChild = fRightChild->cloneTree(); n->fRightChild = fRightChild->cloneTree(status, depth+1);
n->fRightChild->fParent = n; if (U_FAILURE(status)) {
delete n;
return nullptr;
} }
n->fRightChild->fParent = n;
} }
} }
return n; return n;
@ -239,7 +278,6 @@ RBBINode *RBBINode::cloneTree() {
// nested references are handled by cloneTree(), not here. // nested references are handled by cloneTree(), not here.
// //
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
constexpr int kRecursiveDepthLimit = 3500;
RBBINode *RBBINode::flattenVariables(UErrorCode& status, int depth) { RBBINode *RBBINode::flattenVariables(UErrorCode& status, int depth) {
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
return this; return this;
@ -251,21 +289,34 @@ RBBINode *RBBINode::flattenVariables(UErrorCode& status, int depth) {
return this; return this;
} }
if (fType == varRef) { if (fType == varRef) {
RBBINode *retNode = fLeftChild->cloneTree(); RBBINode *retNode = fLeftChild->cloneTree(status, depth+1);
if (retNode != nullptr) { if (U_FAILURE(status)) {
return this;
}
retNode->fRuleRoot = this->fRuleRoot; retNode->fRuleRoot = this->fRuleRoot;
retNode->fChainIn = this->fChainIn; retNode->fChainIn = this->fChainIn;
}
delete this; // TODO: undefined behavior. Fix. delete this; // TODO: undefined behavior. Fix.
return retNode; return retNode;
} }
if (fLeftChild != nullptr) { if (fLeftChild != nullptr) {
fLeftChild = fLeftChild->flattenVariables(status, depth+1); fLeftChild = fLeftChild->flattenVariables(status, depth+1);
if (fLeftChild == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
}
if (U_FAILURE(status)) {
return this;
}
fLeftChild->fParent = this; fLeftChild->fParent = this;
} }
if (fRightChild != nullptr) { if (fRightChild != nullptr) {
fRightChild = fRightChild->flattenVariables(status, depth+1); fRightChild = fRightChild->flattenVariables(status, depth+1);
if (fRightChild == nullptr) {
status = U_MEMORY_ALLOCATION_ERROR;
}
if (U_FAILURE(status)) {
return this;
}
fRightChild->fParent = this; fRightChild->fParent = this;
} }
return this; return this;
@ -280,7 +331,16 @@ RBBINode *RBBINode::flattenVariables(UErrorCode& status, int depth) {
// the left child of the uset node. // the left child of the uset node.
// //
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
void RBBINode::flattenSets() { void RBBINode::flattenSets(UErrorCode &status, int depth) {
if (U_FAILURE(status)) {
return;
}
// If the depth of the stack is too deep, we return U_INPUT_TOO_LONG_ERROR
// to avoid stack overflow crash.
if (depth > kRecursiveDepthLimit) {
status = U_INPUT_TOO_LONG_ERROR;
return;
}
U_ASSERT(fType != setRef); U_ASSERT(fType != setRef);
if (fLeftChild != nullptr) { if (fLeftChild != nullptr) {
@ -288,11 +348,15 @@ void RBBINode::flattenSets() {
RBBINode *setRefNode = fLeftChild; RBBINode *setRefNode = fLeftChild;
RBBINode *usetNode = setRefNode->fLeftChild; RBBINode *usetNode = setRefNode->fLeftChild;
RBBINode *replTree = usetNode->fLeftChild; RBBINode *replTree = usetNode->fLeftChild;
fLeftChild = replTree->cloneTree(); fLeftChild = replTree->cloneTree(status, depth+1);
if (U_FAILURE(status)) {
delete setRefNode;
return;
}
fLeftChild->fParent = this; fLeftChild->fParent = this;
delete setRefNode; delete setRefNode;
} else { } else {
fLeftChild->flattenSets(); fLeftChild->flattenSets(status, depth+1);
} }
} }
@ -301,11 +365,15 @@ void RBBINode::flattenSets() {
RBBINode *setRefNode = fRightChild; RBBINode *setRefNode = fRightChild;
RBBINode *usetNode = setRefNode->fLeftChild; RBBINode *usetNode = setRefNode->fLeftChild;
RBBINode *replTree = usetNode->fLeftChild; RBBINode *replTree = usetNode->fLeftChild;
fRightChild = replTree->cloneTree(); fRightChild = replTree->cloneTree(status, depth+1);
if (U_FAILURE(status)) {
delete setRefNode;
return;
}
fRightChild->fParent = this; fRightChild->fParent = this;
delete setRefNode; delete setRefNode;
} else { } else {
fRightChild->flattenSets(); fRightChild->flattenSets(status, depth+1);
} }
} }
} }

View file

@ -91,14 +91,14 @@ class RBBINode : public UMemory {
UVector *fFollowPos; UVector *fFollowPos;
RBBINode(NodeType t); RBBINode(NodeType t, UErrorCode& status);
RBBINode(const RBBINode &other); RBBINode(const RBBINode &other, UErrorCode& status);
~RBBINode(); ~RBBINode();
static void NRDeleteNode(RBBINode *node); static void NRDeleteNode(RBBINode *node);
RBBINode *cloneTree(); RBBINode *cloneTree(UErrorCode &status, int depth=0);
RBBINode *flattenVariables(UErrorCode &status, int depth=0); RBBINode *flattenVariables(UErrorCode &status, int depth=0);
void flattenSets(); void flattenSets(UErrorCode &status, int depth=0);
void findNodes(UVector *dest, RBBINode::NodeType kind, UErrorCode &status); void findNodes(UVector *dest, RBBINode::NodeType kind, UErrorCode &status);
#ifdef RBBI_DEBUG #ifdef RBBI_DEBUG

View file

@ -767,15 +767,24 @@ void RBBIRuleScanner::findSetFor(const UnicodeString &s, RBBINode *node, Unicode
c = s.char32At(0); c = s.char32At(0);
setToAdopt = new UnicodeSet(c, c); setToAdopt = new UnicodeSet(c, c);
} }
if (setToAdopt == nullptr) {
error(U_MEMORY_ALLOCATION_ERROR);
return;
}
} }
// //
// Make a new uset node to refer to this UnicodeSet // Make a new uset node to refer to this UnicodeSet
// This new uset node becomes the child of the caller's setReference node. // This new uset node becomes the child of the caller's setReference node.
// //
RBBINode *usetNode = new RBBINode(RBBINode::uset); UErrorCode localStatus = U_ZERO_ERROR;
RBBINode *usetNode = new RBBINode(RBBINode::uset, localStatus);
if (usetNode == nullptr) { if (usetNode == nullptr) {
error(U_MEMORY_ALLOCATION_ERROR); localStatus = U_MEMORY_ALLOCATION_ERROR;
}
if (U_FAILURE(localStatus)) {
delete usetNode;
error(localStatus);
delete setToAdopt; delete setToAdopt;
return; return;
} }
@ -1191,7 +1200,7 @@ RBBINode *RBBIRuleScanner::pushNewNode(RBBINode::NodeType t) {
return nullptr; return nullptr;
} }
fNodeStackPtr++; fNodeStackPtr++;
fNodeStack[fNodeStackPtr] = new RBBINode(t); fNodeStack[fNodeStackPtr] = new RBBINode(t, *fRB->fStatus);
if (fNodeStack[fNodeStackPtr] == nullptr) { if (fNodeStack[fNodeStackPtr] == nullptr) {
*fRB->fStatus = U_MEMORY_ALLOCATION_ERROR; *fRB->fStatus = U_MEMORY_ALLOCATION_ERROR;
} }

View file

@ -375,7 +375,11 @@ void RBBISetBuilder::addValToSets(UVector *sets, uint32_t val) {
} }
void RBBISetBuilder::addValToSet(RBBINode *usetNode, uint32_t val) { void RBBISetBuilder::addValToSet(RBBINode *usetNode, uint32_t val) {
RBBINode *leafNode = new RBBINode(RBBINode::leafChar); RBBINode *leafNode = new RBBINode(RBBINode::leafChar, *fStatus);
if (U_FAILURE(*fStatus)) {
delete leafNode;
return;
}
if (leafNode == nullptr) { if (leafNode == nullptr) {
*fStatus = U_MEMORY_ALLOCATION_ERROR; *fStatus = U_MEMORY_ALLOCATION_ERROR;
return; return;
@ -388,9 +392,13 @@ void RBBISetBuilder::addValToSet(RBBINode *usetNode, uint32_t val) {
// There are already input symbols present for this set. // There are already input symbols present for this set.
// Set up an OR node, with the previous stuff as the left child // Set up an OR node, with the previous stuff as the left child
// and the new value as the right child. // and the new value as the right child.
RBBINode *orNode = new RBBINode(RBBINode::opOr); RBBINode *orNode = new RBBINode(RBBINode::opOr, *fStatus);
if (orNode == nullptr) { if (orNode == nullptr) {
*fStatus = U_MEMORY_ALLOCATION_ERROR; *fStatus = U_MEMORY_ALLOCATION_ERROR;
}
if (U_FAILURE(*fStatus)) {
delete orNode;
delete leafNode;
return; return;
} }
orNode->fLeftChild = usetNode->fLeftChild; orNode->fLeftChild = usetNode->fLeftChild;

View file

@ -99,13 +99,22 @@ void RBBITableBuilder::buildForwardTable() {
// {bof} fake character. // {bof} fake character.
// //
if (fRB->fSetBuilder->sawBOF()) { if (fRB->fSetBuilder->sawBOF()) {
RBBINode *bofTop = new RBBINode(RBBINode::opCat); RBBINode *bofTop = new RBBINode(RBBINode::opCat, *fStatus);
RBBINode *bofLeaf = new RBBINode(RBBINode::leafChar); if (bofTop == nullptr) {
// Delete and exit if memory allocation failed.
if (bofTop == nullptr || bofLeaf == nullptr) {
*fStatus = U_MEMORY_ALLOCATION_ERROR; *fStatus = U_MEMORY_ALLOCATION_ERROR;
}
if (U_FAILURE(*fStatus)) {
delete bofTop; delete bofTop;
return;
}
RBBINode *bofLeaf = new RBBINode(RBBINode::leafChar, *fStatus);
// Delete and exit if memory allocation failed.
if (bofLeaf == nullptr) {
*fStatus = U_MEMORY_ALLOCATION_ERROR;
}
if (U_FAILURE(*fStatus)) {
delete bofLeaf; delete bofLeaf;
delete bofTop;
return; return;
} }
bofTop->fLeftChild = bofLeaf; bofTop->fLeftChild = bofLeaf;
@ -120,18 +129,23 @@ void RBBITableBuilder::buildForwardTable() {
// Appears as a cat-node, left child being the original tree, // Appears as a cat-node, left child being the original tree,
// right child being the end marker. // right child being the end marker.
// //
RBBINode *cn = new RBBINode(RBBINode::opCat); RBBINode *cn = new RBBINode(RBBINode::opCat, *fStatus);
// Exit if memory allocation failed. // Exit if memory allocation failed.
if (cn == nullptr) { if (cn == nullptr) {
*fStatus = U_MEMORY_ALLOCATION_ERROR; *fStatus = U_MEMORY_ALLOCATION_ERROR;
}
if (U_FAILURE(*fStatus)) {
delete cn;
return; return;
} }
cn->fLeftChild = fTree; cn->fLeftChild = fTree;
fTree->fParent = cn; fTree->fParent = cn;
RBBINode *endMarkerNode = cn->fRightChild = new RBBINode(RBBINode::endMark); RBBINode *endMarkerNode = cn->fRightChild = new RBBINode(RBBINode::endMark, *fStatus);
// Delete and exit if memory allocation failed. // Delete and exit if memory allocation failed.
if (cn->fRightChild == nullptr) { if (cn->fRightChild == nullptr) {
*fStatus = U_MEMORY_ALLOCATION_ERROR; *fStatus = U_MEMORY_ALLOCATION_ERROR;
}
if (U_FAILURE(*fStatus)) {
delete cn; delete cn;
return; return;
} }
@ -142,7 +156,7 @@ void RBBITableBuilder::buildForwardTable() {
// Replace all references to UnicodeSets with the tree for the equivalent // Replace all references to UnicodeSets with the tree for the equivalent
// expression. // expression.
// //
fTree->flattenSets(); fTree->flattenSets(*fStatus, 0);
#ifdef RBBI_DEBUG #ifdef RBBI_DEBUG
if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "stree")) { if (fRB->fDebugEnv && uprv_strstr(fRB->fDebugEnv, "stree")) {
RBBIDebugPuts("\nParse tree after flattening Unicode Set references."); RBBIDebugPuts("\nParse tree after flattening Unicode Set references.");

View file

@ -388,7 +388,7 @@ const Locale &ResourceBundle::getLocale() const {
return ncThis->fLocale != nullptr ? *ncThis->fLocale : Locale::getDefault(); return ncThis->fLocale != nullptr ? *ncThis->fLocale : Locale::getDefault();
} }
const Locale ResourceBundle::getLocale(ULocDataLocaleType type, UErrorCode &status) const Locale ResourceBundle::getLocale(ULocDataLocaleType type, UErrorCode &status) const
{ {
return ures_getLocaleByType(fResource, type, &status); return ures_getLocaleByType(fResource, type, &status);
} }

View file

@ -3146,11 +3146,8 @@ ucnv_MBCSGetNextUChar(UConverterToUnicodeArgs *pArgs,
if(c<0) { if(c<0) {
if(U_SUCCESS(*pErrorCode) && source==sourceLimit && lastSource<source) { if(U_SUCCESS(*pErrorCode) && source==sourceLimit && lastSource<source) {
/* incomplete character byte sequence */ /* incomplete character byte sequence */
uint8_t *bytes=cnv->toUBytes;
cnv->toULength = static_cast<int8_t>(source - lastSource); cnv->toULength = static_cast<int8_t>(source - lastSource);
do { uprv_memcpy(cnv->toUBytes, lastSource, cnv->toULength);
*bytes++=*lastSource++;
} while(lastSource<source);
*pErrorCode=U_TRUNCATED_CHAR_FOUND; *pErrorCode=U_TRUNCATED_CHAR_FOUND;
} else if(U_FAILURE(*pErrorCode)) { } else if(U_FAILURE(*pErrorCode)) {
/* callback(illegal) */ /* callback(illegal) */

View file

@ -372,12 +372,8 @@ struct CReg : public icu::UMemory {
CReg(const char16_t* _iso, const char* _id) CReg(const char16_t* _iso, const char* _id)
: next(nullptr) : next(nullptr)
{ {
int32_t len = static_cast<int32_t>(uprv_strlen(_id)); uprv_strncpy(id, _id, sizeof(id)-1);
if (len > static_cast<int32_t>(sizeof(id) - 1)) { id[sizeof(id)-1] = 0;
len = (sizeof(id)-1);
}
uprv_strncpy(id, _id, len);
id[len] = 0;
u_memcpy(iso, _iso, ISO_CURRENCY_CODE_LENGTH); u_memcpy(iso, _iso, ISO_CURRENCY_CODE_LENGTH);
iso[ISO_CURRENCY_CODE_LENGTH] = 0; iso[ISO_CURRENCY_CODE_LENGTH] = 0;
} }
@ -682,6 +678,9 @@ ucurr_getName(const char16_t* currency,
// this function. // this function.
UErrorCode ec2 = U_ZERO_ERROR; UErrorCode ec2 = U_ZERO_ERROR;
if (locale == nullptr) {
locale = uloc_getDefault();
}
CharString loc = ulocimp_getName(locale, ec2); CharString loc = ulocimp_getName(locale, ec2);
if (U_FAILURE(ec2)) { if (U_FAILURE(ec2)) {
*ec = U_ILLEGAL_ARGUMENT_ERROR; *ec = U_ILLEGAL_ARGUMENT_ERROR;
@ -780,6 +779,9 @@ ucurr_getPluralName(const char16_t* currency,
// this function. // this function.
UErrorCode ec2 = U_ZERO_ERROR; UErrorCode ec2 = U_ZERO_ERROR;
if (locale == nullptr) {
locale = uloc_getDefault();
}
CharString loc = ulocimp_getName(locale, ec2); CharString loc = ulocimp_getName(locale, ec2);
if (U_FAILURE(ec2)) { if (U_FAILURE(ec2)) {
*ec = U_ILLEGAL_ARGUMENT_ERROR; *ec = U_ILLEGAL_ARGUMENT_ERROR;
@ -973,6 +975,9 @@ collectCurrencyNames(const char* locale,
// Look up the Currencies resource for the given locale. // Look up the Currencies resource for the given locale.
UErrorCode ec2 = U_ZERO_ERROR; UErrorCode ec2 = U_ZERO_ERROR;
if (locale == nullptr) {
locale = uloc_getDefault();
}
CharString loc = ulocimp_getName(locale, ec2); CharString loc = ulocimp_getName(locale, ec2);
if (U_FAILURE(ec2)) { if (U_FAILURE(ec2)) {
ec = U_ILLEGAL_ARGUMENT_ERROR; ec = U_ILLEGAL_ARGUMENT_ERROR;

View file

@ -482,8 +482,8 @@ constexpr CanonicalizationMap CANONICALIZE_MAP[] = {
/* ### BCP47 Conversion *******************************************/ /* ### BCP47 Conversion *******************************************/
/* Gets the size of the shortest subtag in the given localeID. */ /* Gets the size of the shortest subtag in the given localeID. */
int32_t getShortestSubtagLength(const char *localeID) { int32_t getShortestSubtagLength(std::string_view localeID) {
int32_t localeIDLength = static_cast<int32_t>(uprv_strlen(localeID)); int32_t localeIDLength = static_cast<int32_t>(localeID.length());
int32_t length = localeIDLength; int32_t length = localeIDLength;
int32_t tmpLength = 0; int32_t tmpLength = 0;
int32_t i; int32_t i;
@ -507,8 +507,8 @@ int32_t getShortestSubtagLength(const char *localeID) {
return length; return length;
} }
/* Test if the locale id has BCP47 u extension and does not have '@' */ /* Test if the locale id has BCP47 u extension and does not have '@' */
inline bool _hasBCP47Extension(const char *id) { inline bool _hasBCP47Extension(std::string_view id) {
return id != nullptr && uprv_strstr(id, "@") == nullptr && getShortestSubtagLength(id) == 1; return id.find('@') == std::string_view::npos && getShortestSubtagLength(id) == 1;
} }
/* ### Keywords **************************************************/ /* ### Keywords **************************************************/
@ -523,10 +523,9 @@ inline bool UPRV_OK_VALUE_PUNCTUATION(char c) { return c == '_' || c == '-' || c
#define ULOC_MAX_NO_KEYWORDS 25 #define ULOC_MAX_NO_KEYWORDS 25
U_CAPI const char * U_EXPORT2 U_CAPI const char * U_EXPORT2
locale_getKeywordsStart(const char *localeID) { locale_getKeywordsStart(std::string_view localeID) {
const char *result = nullptr; if (size_t pos = localeID.find('@'); pos != std::string_view::npos) {
if((result = uprv_strchr(localeID, '@')) != nullptr) { return localeID.data() + pos;
return result;
} }
#if (U_CHARSET_FAMILY == U_EBCDIC_FAMILY) #if (U_CHARSET_FAMILY == U_EBCDIC_FAMILY)
else { else {
@ -536,8 +535,8 @@ locale_getKeywordsStart(const char *localeID) {
static const uint8_t ebcdicSigns[] = { 0x7C, 0x44, 0x66, 0x80, 0xAC, 0xAE, 0xAF, 0xB5, 0xEC, 0xEF, 0x00 }; static const uint8_t ebcdicSigns[] = { 0x7C, 0x44, 0x66, 0x80, 0xAC, 0xAE, 0xAF, 0xB5, 0xEC, 0xEF, 0x00 };
const uint8_t *charToFind = ebcdicSigns; const uint8_t *charToFind = ebcdicSigns;
while(*charToFind) { while(*charToFind) {
if((result = uprv_strchr(localeID, *charToFind)) != nullptr) { if (size_t pos = localeID.find(*charToFind); pos != std::string_view::npos) {
return result; return localeID.data() + pos;
} }
charToFind++; charToFind++;
} }
@ -590,7 +589,7 @@ compareKeywordStructs(const void * /*context*/, const void *left, const void *ri
} // namespace } // namespace
U_EXPORT CharString U_EXPORT CharString
ulocimp_getKeywords(const char* localeID, ulocimp_getKeywords(std::string_view localeID,
char prev, char prev,
bool valuesToo, bool valuesToo,
UErrorCode& status) UErrorCode& status)
@ -607,7 +606,7 @@ ulocimp_getKeywords(const char* localeID,
} }
U_EXPORT void U_EXPORT void
ulocimp_getKeywords(const char* localeID, ulocimp_getKeywords(std::string_view localeID,
char prev, char prev,
ByteSink& sink, ByteSink& sink,
bool valuesToo, bool valuesToo,
@ -619,9 +618,8 @@ ulocimp_getKeywords(const char* localeID,
int32_t maxKeywords = ULOC_MAX_NO_KEYWORDS; int32_t maxKeywords = ULOC_MAX_NO_KEYWORDS;
int32_t numKeywords = 0; int32_t numKeywords = 0;
const char* pos = localeID; size_t equalSign = std::string_view::npos;
const char* equalSign = nullptr; size_t semicolon = std::string_view::npos;
const char* semicolon = nullptr;
int32_t i = 0, j, n; int32_t i = 0, j, n;
if(prev == '@') { /* start of keyword definition */ if(prev == '@') { /* start of keyword definition */
@ -629,74 +627,72 @@ ulocimp_getKeywords(const char* localeID,
do { do {
bool duplicate = false; bool duplicate = false;
/* skip leading spaces */ /* skip leading spaces */
while(*pos == ' ') { while (localeID.front() == ' ') {
pos++; localeID.remove_prefix(1);
} }
if (!*pos) { /* handle trailing "; " */ if (localeID.empty()) { /* handle trailing "; " */
break; break;
} }
if(numKeywords == maxKeywords) { if(numKeywords == maxKeywords) {
status = U_INTERNAL_PROGRAM_ERROR; status = U_INTERNAL_PROGRAM_ERROR;
return; return;
} }
equalSign = uprv_strchr(pos, '='); equalSign = localeID.find('=');
semicolon = uprv_strchr(pos, ';'); semicolon = localeID.find(';');
/* lack of '=' [foo@currency] is illegal */ /* lack of '=' [foo@currency] is illegal */
/* ';' before '=' [foo@currency;collation=pinyin] is illegal */ /* ';' before '=' [foo@currency;collation=pinyin] is illegal */
if(!equalSign || (semicolon && semicolon<equalSign)) { if (equalSign == std::string_view::npos ||
(semicolon != std::string_view::npos && semicolon < equalSign)) {
status = U_INVALID_FORMAT_ERROR;
return;
}
/* zero-length keyword is an error. */
if (equalSign == 0) {
status = U_INVALID_FORMAT_ERROR; status = U_INVALID_FORMAT_ERROR;
return; return;
} }
/* need to normalize both keyword and keyword name */ /* need to normalize both keyword and keyword name */
if(equalSign - pos >= ULOC_KEYWORD_BUFFER_LEN) { if (equalSign >= ULOC_KEYWORD_BUFFER_LEN) {
/* keyword name too long for internal buffer */ /* keyword name too long for internal buffer */
status = U_INTERNAL_PROGRAM_ERROR; status = U_INTERNAL_PROGRAM_ERROR;
return; return;
} }
for(i = 0, n = 0; i < equalSign - pos; ++i) { for (i = 0, n = 0; static_cast<size_t>(i) < equalSign; ++i) {
if (pos[i] != ' ') { if (localeID[i] != ' ') {
keywordList[numKeywords].keyword[n++] = uprv_tolower(pos[i]); keywordList[numKeywords].keyword[n++] = uprv_tolower(localeID[i]);
} }
} }
/* zero-length keyword is an error. */
if (n == 0) {
status = U_INVALID_FORMAT_ERROR;
return;
}
keywordList[numKeywords].keyword[n] = 0; keywordList[numKeywords].keyword[n] = 0;
keywordList[numKeywords].keywordLen = n; keywordList[numKeywords].keywordLen = n;
/* now grab the value part. First we skip the '=' */ /* now grab the value part. First we skip the '=' */
equalSign++; equalSign++;
/* then we leading spaces */ /* then we leading spaces */
while(*equalSign == ' ') { while (equalSign < localeID.length() && localeID[equalSign] == ' ') {
equalSign++; equalSign++;
} }
/* Premature end or zero-length value */ /* Premature end or zero-length value */
if (!*equalSign || equalSign == semicolon) { if (equalSign == localeID.length() || equalSign == semicolon) {
status = U_INVALID_FORMAT_ERROR; status = U_INVALID_FORMAT_ERROR;
return; return;
} }
keywordList[numKeywords].valueStart = equalSign; keywordList[numKeywords].valueStart = localeID.data() + equalSign;
pos = semicolon; std::string_view value = localeID;
i = 0; if (semicolon != std::string_view::npos) {
if(pos) { value.remove_suffix(value.length() - semicolon);
while(*(pos - i - 1) == ' ') { localeID.remove_prefix(semicolon + 1);
i++;
}
keywordList[numKeywords].valueLen = static_cast<int32_t>(pos - equalSign - i);
pos++;
} else { } else {
i = static_cast<int32_t>(uprv_strlen(equalSign)); localeID = {};
while(i && equalSign[i-1] == ' ') {
i--;
} }
keywordList[numKeywords].valueLen = i; value.remove_prefix(equalSign);
if (size_t last = value.find_last_not_of(' '); last != std::string_view::npos) {
value.remove_suffix(value.length() - last - 1);
} }
keywordList[numKeywords].valueLen = static_cast<int32_t>(value.length());
/* If this is a duplicate keyword, then ignore it */ /* If this is a duplicate keyword, then ignore it */
for (j=0; j<numKeywords; ++j) { for (j=0; j<numKeywords; ++j) {
if (uprv_strcmp(keywordList[j].keyword, keywordList[numKeywords].keyword) == 0) { if (uprv_strcmp(keywordList[j].keyword, keywordList[numKeywords].keyword) == 0) {
@ -707,7 +703,7 @@ ulocimp_getKeywords(const char* localeID,
if (!duplicate) { if (!duplicate) {
++numKeywords; ++numKeywords;
} }
} while(pos); } while (!localeID.empty());
/* now we have a list of keywords */ /* now we have a list of keywords */
/* we need to sort it */ /* we need to sort it */
@ -784,7 +780,7 @@ ulocimp_getKeywordValue(const char* localeID,
return; return;
} }
if (_hasBCP47Extension(localeID)) { if (localeID != nullptr && _hasBCP47Extension(localeID)) {
tempBuffer = ulocimp_forLanguageTag(localeID, -1, nullptr, status); tempBuffer = ulocimp_forLanguageTag(localeID, -1, nullptr, status);
tmpLocaleID = U_SUCCESS(status) && !tempBuffer.isEmpty() ? tempBuffer.data() : localeID; tmpLocaleID = U_SUCCESS(status) && !tempBuffer.isEmpty() ? tempBuffer.data() : localeID;
} else { } else {
@ -889,7 +885,8 @@ uloc_setKeywordValue(const char* keywordName,
return 0; return 0;
} }
char* keywords = const_cast<char*>(locale_getKeywordsStart(buffer)); char* keywords = const_cast<char*>(
locale_getKeywordsStart({buffer, static_cast<std::string_view::size_type>(bufLen)}));
int32_t baseLen = keywords == nullptr ? bufLen : keywords - buffer; int32_t baseLen = keywords == nullptr ? bufLen : keywords - buffer;
// Remove -1 from the capacity so that this function can guarantee NUL termination. // Remove -1 from the capacity so that this function can guarantee NUL termination.
CheckedArrayByteSink sink(keywords == nullptr ? buffer + bufLen : keywords, CheckedArrayByteSink sink(keywords == nullptr ? buffer + bufLen : keywords,
@ -921,7 +918,7 @@ ulocimp_setKeywordValue(std::string_view keywordName,
{ {
if (U_FAILURE(status)) { return; } if (U_FAILURE(status)) { return; }
std::string_view keywords; std::string_view keywords;
if (const char* start = locale_getKeywordsStart(localeID.data()); start != nullptr) { if (const char* start = locale_getKeywordsStart(localeID.toStringPiece()); start != nullptr) {
// This is safe because CharString::truncate() doesn't actually erase any // This is safe because CharString::truncate() doesn't actually erase any
// data, but simply sets the position for where new data will be written. // data, but simply sets the position for where new data will be written.
int32_t size = start - localeID.data(); int32_t size = start - localeID.data();
@ -1138,15 +1135,18 @@ inline bool _isPrefixLetter(char a) { return a == 'x' || a == 'X' || a == 'i' ||
/*returns true if one of the special prefixes is here (s=string) /*returns true if one of the special prefixes is here (s=string)
'x-' or 'i-' */ 'x-' or 'i-' */
inline bool _isIDPrefix(const char *s) { return _isPrefixLetter(s[0]) && _isIDSeparator(s[1]); } inline bool _isIDPrefix(std::string_view s) {
return s.size() >= 2 && _isPrefixLetter(s[0]) && _isIDSeparator(s[1]);
}
/* Dot terminates it because of POSIX form where dot precedes the codepage /* Dot terminates it because of POSIX form where dot precedes the codepage
* except for variant * except for variant
*/ */
inline bool _isTerminator(char a) { return a == 0 || a == '.' || a == '@'; } inline bool _isTerminator(char a) { return a == '.' || a == '@'; }
inline bool _isBCP47Extension(const char* p) { inline bool _isBCP47Extension(std::string_view p) {
return p[0] == '-' && return p.size() >= 3 &&
p[0] == '-' &&
(p[1] == 't' || p[1] == 'T' || (p[1] == 't' || p[1] == 'T' ||
p[1] == 'u' || p[1] == 'U' || p[1] == 'u' || p[1] == 'U' ||
p[1] == 'x' || p[1] == 'X') && p[1] == 'x' || p[1] == 'X') &&
@ -1202,49 +1202,44 @@ namespace {
* TODO try to use this in Locale * TODO try to use this in Locale
*/ */
void size_t _getLanguage(std::string_view localeID, ByteSink* sink, UErrorCode& status) {
_getLanguage(const char* localeID, size_t skip = 0;
ByteSink* sink, if (localeID.size() == 4 && uprv_strnicmp(localeID.data(), "root", 4) == 0) {
const char** pEnd, skip = 4;
UErrorCode& status) { localeID.remove_prefix(skip);
U_ASSERT(pEnd != nullptr); } else if (localeID.size() >= 3 && uprv_strnicmp(localeID.data(), "und", 3) == 0 &&
*pEnd = localeID; (localeID.size() == 3 ||
if (uprv_stricmp(localeID, "root") == 0) {
localeID += 4;
} else if (uprv_strnicmp(localeID, "und", 3) == 0 &&
(localeID[3] == '\0' ||
localeID[3] == '-' || localeID[3] == '-' ||
localeID[3] == '_' || localeID[3] == '_' ||
localeID[3] == '@')) { localeID[3] == '@')) {
localeID += 3; skip = 3;
localeID.remove_prefix(skip);
} }
constexpr int32_t MAXLEN = ULOC_LANG_CAPACITY - 1; // Minus NUL. constexpr int32_t MAXLEN = ULOC_LANG_CAPACITY - 1; // Minus NUL.
/* if it starts with i- or x- then copy that prefix */ /* if it starts with i- or x- then copy that prefix */
int32_t len = _isIDPrefix(localeID) ? 2 : 0; size_t len = _isIDPrefix(localeID) ? 2 : 0;
while (!_isTerminator(localeID[len]) && !_isIDSeparator(localeID[len])) { while (len < localeID.size() && !_isTerminator(localeID[len]) && !_isIDSeparator(localeID[len])) {
if (len == MAXLEN) { if (len == MAXLEN) {
status = U_ILLEGAL_ARGUMENT_ERROR; status = U_ILLEGAL_ARGUMENT_ERROR;
return; return 0;
} }
len++; len++;
} }
*pEnd = localeID + len; if (sink == nullptr || len == 0) { return skip + len; }
if (sink == nullptr || len == 0) { return; }
int32_t minCapacity = uprv_max(len, 4); // Minimum 3 letters plus NUL. int32_t minCapacity = uprv_max(static_cast<int32_t>(len), 4); // Minimum 3 letters plus NUL.
char scratch[MAXLEN]; char scratch[MAXLEN];
int32_t capacity = 0; int32_t capacity = 0;
char* buffer = sink->GetAppendBuffer( char* buffer = sink->GetAppendBuffer(
minCapacity, minCapacity, scratch, UPRV_LENGTHOF(scratch), &capacity); minCapacity, minCapacity, scratch, UPRV_LENGTHOF(scratch), &capacity);
for (int32_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
buffer[i] = uprv_tolower(localeID[i]); buffer[i] = uprv_tolower(localeID[i]);
} }
if (_isIDSeparator(localeID[1])) { if (localeID.size() >= 2 && _isIDSeparator(localeID[1])) {
buffer[1] = '-'; buffer[1] = '-';
} }
@ -1256,32 +1251,26 @@ _getLanguage(const char* localeID,
if (offset.has_value()) { if (offset.has_value()) {
const char* const alias = LANGUAGES[*offset]; const char* const alias = LANGUAGES[*offset];
sink->Append(alias, static_cast<int32_t>(uprv_strlen(alias))); sink->Append(alias, static_cast<int32_t>(uprv_strlen(alias)));
return; return skip + len;
} }
} }
sink->Append(buffer, len); sink->Append(buffer, static_cast<int32_t>(len));
return skip + len;
} }
void size_t _getScript(std::string_view localeID, ByteSink* sink) {
_getScript(const char* localeID,
ByteSink* sink,
const char** pEnd) {
U_ASSERT(pEnd != nullptr);
*pEnd = localeID;
constexpr int32_t LENGTH = 4; constexpr int32_t LENGTH = 4;
int32_t len = 0; size_t len = 0;
while (!_isTerminator(localeID[len]) && !_isIDSeparator(localeID[len]) && while (len < localeID.size() && !_isTerminator(localeID[len]) && !_isIDSeparator(localeID[len]) &&
uprv_isASCIILetter(localeID[len])) { uprv_isASCIILetter(localeID[len])) {
if (len == LENGTH) { return; } if (len == LENGTH) { return 0; }
len++; len++;
} }
if (len != LENGTH) { return; } if (len != LENGTH) { return 0; }
*pEnd = localeID + LENGTH; if (sink == nullptr) { return len; }
if (sink == nullptr) { return; }
char scratch[LENGTH]; char scratch[LENGTH];
int32_t capacity = 0; int32_t capacity = 0;
@ -1294,27 +1283,21 @@ _getScript(const char* localeID,
} }
sink->Append(buffer, LENGTH); sink->Append(buffer, LENGTH);
return len;
} }
void size_t _getRegion(std::string_view localeID, ByteSink* sink) {
_getRegion(const char* localeID,
ByteSink* sink,
const char** pEnd) {
U_ASSERT(pEnd != nullptr);
*pEnd = localeID;
constexpr int32_t MINLEN = 2; constexpr int32_t MINLEN = 2;
constexpr int32_t MAXLEN = ULOC_COUNTRY_CAPACITY - 1; // Minus NUL. constexpr int32_t MAXLEN = ULOC_COUNTRY_CAPACITY - 1; // Minus NUL.
int32_t len = 0; size_t len = 0;
while (!_isTerminator(localeID[len]) && !_isIDSeparator(localeID[len])) { while (len < localeID.size() && !_isTerminator(localeID[len]) && !_isIDSeparator(localeID[len])) {
if (len == MAXLEN) { return; } if (len == MAXLEN) { return 0; }
len++; len++;
} }
if (len < MINLEN) { return; } if (len < MINLEN) { return 0; }
*pEnd = localeID + len; if (sink == nullptr) { return len; }
if (sink == nullptr) { return; }
char scratch[ULOC_COUNTRY_CAPACITY]; char scratch[ULOC_COUNTRY_CAPACITY];
int32_t capacity = 0; int32_t capacity = 0;
@ -1325,7 +1308,7 @@ _getRegion(const char* localeID,
UPRV_LENGTHOF(scratch), UPRV_LENGTHOF(scratch),
&capacity); &capacity);
for (int32_t i = 0; i < len; ++i) { for (size_t i = 0; i < len; ++i) {
buffer[i] = uprv_toupper(localeID[i]); buffer[i] = uprv_toupper(localeID[i]);
} }
@ -1337,26 +1320,25 @@ _getRegion(const char* localeID,
if (offset.has_value()) { if (offset.has_value()) {
const char* const alias = COUNTRIES[*offset]; const char* const alias = COUNTRIES[*offset];
sink->Append(alias, static_cast<int32_t>(uprv_strlen(alias))); sink->Append(alias, static_cast<int32_t>(uprv_strlen(alias)));
return; return len;
} }
} }
sink->Append(buffer, len); sink->Append(buffer, static_cast<int32_t>(len));
return len;
} }
/** /**
* @param needSeparator if true, then add leading '_' if any variants * @param needSeparator if true, then add leading '_' if any variants
* are added to 'variant' * are added to 'variant'
*/ */
void size_t
_getVariant(const char* localeID, _getVariant(std::string_view localeID,
char prev, char prev,
ByteSink* sink, ByteSink* sink,
const char** pEnd,
bool needSeparator, bool needSeparator,
UErrorCode& status) { UErrorCode& status) {
if (U_FAILURE(status)) return; if (U_FAILURE(status) || localeID.empty()) return 0;
if (pEnd != nullptr) { *pEnd = localeID; }
// Reasonable upper limit for variants // Reasonable upper limit for variants
// There are no strict limitation of the syntax of variant in the legacy // There are no strict limitation of the syntax of variant in the legacy
@ -1369,42 +1351,62 @@ _getVariant(const char* localeID,
constexpr int32_t MAX_VARIANTS_LENGTH = 179; constexpr int32_t MAX_VARIANTS_LENGTH = 179;
/* get one or more variant tags and separate them with '_' */ /* get one or more variant tags and separate them with '_' */
int32_t index = 0; size_t index = 0;
if (_isIDSeparator(prev)) { if (_isIDSeparator(prev)) {
/* get a variant string after a '-' or '_' */ /* get a variant string after a '-' or '_' */
for (index=0; !_isTerminator(localeID[index]); index++) { for (std::string_view sub = localeID;;) {
if (index >= MAX_VARIANTS_LENGTH) { // same as length > MAX_VARIANTS_LENGTH size_t next = sub.find_first_of(".@_-");
// For historical reasons, a trailing separator is included in the variant.
bool finished = next == std::string_view::npos || next + 1 == sub.length();
size_t limit = finished ? sub.length() : next;
index += limit;
if (index > MAX_VARIANTS_LENGTH) {
status = U_ILLEGAL_ARGUMENT_ERROR; status = U_ILLEGAL_ARGUMENT_ERROR;
return; return 0;
}
if (needSeparator) {
if (sink != nullptr) {
sink->Append("_", 1);
}
needSeparator = false;
}
if (sink != nullptr) {
char c = uprv_toupper(localeID[index]);
if (c == '-') c = '_';
sink->Append(&c, 1);
}
}
if (pEnd != nullptr) { *pEnd = localeID+index; }
} }
if (sink != nullptr) {
if (needSeparator) {
sink->Append("_", 1);
} else {
needSeparator = true;
}
int32_t length = static_cast<int32_t>(limit);
int32_t minCapacity = uprv_min(length, MAX_VARIANTS_LENGTH);
char scratch[MAX_VARIANTS_LENGTH];
int32_t capacity = 0;
char* buffer = sink->GetAppendBuffer(
minCapacity, minCapacity, scratch, UPRV_LENGTHOF(scratch), &capacity);
for (size_t i = 0; i < limit; ++i) {
buffer[i] = uprv_toupper(sub[i]);
}
sink->Append(buffer, length);
}
if (finished) { return index; }
sub.remove_prefix(next);
if (_isTerminator(sub.front()) || _isBCP47Extension(sub)) { return index; }
sub.remove_prefix(1);
index++;
}
}
size_t skip = 0;
/* if there is no variant tag after a '-' or '_' then look for '@' */ /* if there is no variant tag after a '-' or '_' then look for '@' */
if (index == 0) {
if (prev == '@') { if (prev == '@') {
/* keep localeID */ /* keep localeID */
} else if((localeID=locale_getKeywordsStart(localeID))!=nullptr) { } else if (const char* p = locale_getKeywordsStart(localeID); p != nullptr) {
++localeID; /* point after the '@' */ skip = 1 + p - localeID.data(); /* point after the '@' */
localeID.remove_prefix(skip);
} else { } else {
return; return 0;
} }
for(; !_isTerminator(localeID[index]); index++) { for (; index < localeID.size() && !_isTerminator(localeID[index]); index++) {
if (index >= MAX_VARIANTS_LENGTH) { // same as length > MAX_VARIANTS_LENGTH if (index >= MAX_VARIANTS_LENGTH) { // same as length > MAX_VARIANTS_LENGTH
status = U_ILLEGAL_ARGUMENT_ERROR; status = U_ILLEGAL_ARGUMENT_ERROR;
return; return 0;
} }
if (needSeparator) { if (needSeparator) {
if (sink != nullptr) { if (sink != nullptr) {
@ -1418,14 +1420,13 @@ _getVariant(const char* localeID,
sink->Append(&c, 1); sink->Append(&c, 1);
} }
} }
if (pEnd != nullptr) { *pEnd = localeID + index; } return skip + index;
}
} }
} // namespace } // namespace
U_EXPORT CharString U_EXPORT CharString
ulocimp_getLanguage(const char* localeID, UErrorCode& status) { ulocimp_getLanguage(std::string_view localeID, UErrorCode& status) {
return ByteSinkUtil::viaByteSinkToCharString( return ByteSinkUtil::viaByteSinkToCharString(
[&](ByteSink& sink, UErrorCode& status) { [&](ByteSink& sink, UErrorCode& status) {
ulocimp_getSubtags( ulocimp_getSubtags(
@ -1441,7 +1442,7 @@ ulocimp_getLanguage(const char* localeID, UErrorCode& status) {
} }
U_EXPORT CharString U_EXPORT CharString
ulocimp_getScript(const char* localeID, UErrorCode& status) { ulocimp_getScript(std::string_view localeID, UErrorCode& status) {
return ByteSinkUtil::viaByteSinkToCharString( return ByteSinkUtil::viaByteSinkToCharString(
[&](ByteSink& sink, UErrorCode& status) { [&](ByteSink& sink, UErrorCode& status) {
ulocimp_getSubtags( ulocimp_getSubtags(
@ -1457,7 +1458,7 @@ ulocimp_getScript(const char* localeID, UErrorCode& status) {
} }
U_EXPORT CharString U_EXPORT CharString
ulocimp_getRegion(const char* localeID, UErrorCode& status) { ulocimp_getRegion(std::string_view localeID, UErrorCode& status) {
return ByteSinkUtil::viaByteSinkToCharString( return ByteSinkUtil::viaByteSinkToCharString(
[&](ByteSink& sink, UErrorCode& status) { [&](ByteSink& sink, UErrorCode& status) {
ulocimp_getSubtags( ulocimp_getSubtags(
@ -1473,7 +1474,7 @@ ulocimp_getRegion(const char* localeID, UErrorCode& status) {
} }
U_EXPORT CharString U_EXPORT CharString
ulocimp_getVariant(const char* localeID, UErrorCode& status) { ulocimp_getVariant(std::string_view localeID, UErrorCode& status) {
return ByteSinkUtil::viaByteSinkToCharString( return ByteSinkUtil::viaByteSinkToCharString(
[&](ByteSink& sink, UErrorCode& status) { [&](ByteSink& sink, UErrorCode& status) {
ulocimp_getSubtags( ulocimp_getSubtags(
@ -1490,7 +1491,7 @@ ulocimp_getVariant(const char* localeID, UErrorCode& status) {
U_EXPORT void U_EXPORT void
ulocimp_getSubtags( ulocimp_getSubtags(
const char* localeID, std::string_view localeID,
CharString* language, CharString* language,
CharString* script, CharString* script,
CharString* region, CharString* region,
@ -1521,7 +1522,7 @@ ulocimp_getSubtags(
U_EXPORT void U_EXPORT void
ulocimp_getSubtags( ulocimp_getSubtags(
const char* localeID, std::string_view localeID,
ByteSink* language, ByteSink* language,
ByteSink* script, ByteSink* script,
ByteSink* region, ByteSink* region,
@ -1531,7 +1532,7 @@ ulocimp_getSubtags(
if (U_FAILURE(status)) { return; } if (U_FAILURE(status)) { return; }
if (pEnd != nullptr) { if (pEnd != nullptr) {
*pEnd = localeID; *pEnd = localeID.data();
} else if (language == nullptr && } else if (language == nullptr &&
script == nullptr && script == nullptr &&
region == nullptr && region == nullptr &&
@ -1539,62 +1540,94 @@ ulocimp_getSubtags(
return; return;
} }
if (localeID.empty()) { return; }
bool hasRegion = false; bool hasRegion = false;
if (localeID == nullptr) { {
localeID = uloc_getDefault(); size_t len = _getLanguage(localeID, language, status);
if (U_FAILURE(status)) { return; }
if (len > 0) {
localeID.remove_prefix(len);
}
} }
_getLanguage(localeID, language, &localeID, status);
if (U_FAILURE(status)) { return; }
U_ASSERT(localeID != nullptr);
if (pEnd != nullptr) { if (pEnd != nullptr) {
*pEnd = localeID; *pEnd = localeID.data();
} else if (script == nullptr && } else if (script == nullptr &&
region == nullptr && region == nullptr &&
variant == nullptr) { variant == nullptr) {
return; return;
} }
if (_isIDSeparator(*localeID)) { if (localeID.empty()) { return; }
const char* begin = localeID + 1;
const char* end = nullptr; if (_isIDSeparator(localeID.front())) {
_getScript(begin, script, &end); std::string_view sub = localeID;
U_ASSERT(end != nullptr); sub.remove_prefix(1);
if (end != begin) { size_t len = _getScript(sub, script);
localeID = end; if (len > 0) {
if (pEnd != nullptr) { *pEnd = localeID; } localeID.remove_prefix(len + 1);
if (pEnd != nullptr) { *pEnd = localeID.data(); }
} }
} }
if (region == nullptr && variant == nullptr && pEnd == nullptr) { return; } if ((region == nullptr && variant == nullptr && pEnd == nullptr) || localeID.empty()) { return; }
if (_isIDSeparator(*localeID)) { if (_isIDSeparator(localeID.front())) {
const char* begin = localeID + 1; std::string_view sub = localeID;
const char* end = nullptr; sub.remove_prefix(1);
_getRegion(begin, region, &end); size_t len = _getRegion(sub, region);
U_ASSERT(end != nullptr); if (len > 0) {
if (end != begin) {
hasRegion = true; hasRegion = true;
localeID = end; localeID.remove_prefix(len + 1);
if (pEnd != nullptr) { *pEnd = localeID; } if (pEnd != nullptr) { *pEnd = localeID.data(); }
} }
} }
if (variant == nullptr && pEnd == nullptr) { return; } if ((variant == nullptr && pEnd == nullptr) || localeID.empty()) { return; }
if (_isIDSeparator(*localeID) && !_isBCP47Extension(localeID)) { bool hasVariant = false;
if (_isIDSeparator(localeID.front()) && !_isBCP47Extension(localeID)) {
std::string_view sub = localeID;
/* If there was no country ID, skip a possible extra IDSeparator */ /* If there was no country ID, skip a possible extra IDSeparator */
if (!hasRegion && _isIDSeparator(localeID[1])) { size_t skip = !hasRegion && localeID.size() > 1 && _isIDSeparator(localeID[1]) ? 2 : 1;
localeID++; sub.remove_prefix(skip);
} size_t len = _getVariant(sub, localeID[0], variant, false, status);
const char* begin = localeID + 1;
const char* end = nullptr;
_getVariant(begin, *localeID, variant, &end, false, status);
if (U_FAILURE(status)) { return; } if (U_FAILURE(status)) { return; }
U_ASSERT(end != nullptr); if (len > 0) {
if (end != begin && pEnd != nullptr) { *pEnd = end; } hasVariant = true;
localeID.remove_prefix(skip + len);
if (pEnd != nullptr) { *pEnd = localeID.data(); }
}
}
if ((variant == nullptr && pEnd == nullptr) || localeID.empty()) { return; }
if (_isBCP47Extension(localeID)) {
localeID.remove_prefix(2);
constexpr char vaposix[] = "-va-posix";
constexpr size_t length = sizeof vaposix - 1;
for (size_t next;; localeID.remove_prefix(next)) {
next = localeID.find('-', 1);
if (next == std::string_view::npos) { break; }
next = localeID.find('-', next + 1);
bool finished = next == std::string_view::npos;
std::string_view sub = localeID;
if (!finished) { sub.remove_suffix(sub.length() - next); }
if (sub.length() == length && uprv_strnicmp(sub.data(), vaposix, length) == 0) {
if (variant != nullptr) {
if (hasVariant) { variant->Append("_", 1); }
constexpr char posix[] = "POSIX";
variant->Append(posix, sizeof posix - 1);
}
if (pEnd != nullptr) { *pEnd = localeID.data() + length; }
}
if (finished) { break; }
}
} }
} }
@ -1700,7 +1733,7 @@ uloc_openKeywords(const char* localeID,
CharString tempBuffer; CharString tempBuffer;
const char* tmpLocaleID; const char* tmpLocaleID;
if (_hasBCP47Extension(localeID)) { if (localeID != nullptr && _hasBCP47Extension(localeID)) {
tempBuffer = ulocimp_forLanguageTag(localeID, -1, nullptr, *status); tempBuffer = ulocimp_forLanguageTag(localeID, -1, nullptr, *status);
tmpLocaleID = U_SUCCESS(*status) && !tempBuffer.isEmpty() ? tempBuffer.data() : localeID; tmpLocaleID = U_SUCCESS(*status) && !tempBuffer.isEmpty() ? tempBuffer.data() : localeID;
} else { } else {
@ -1753,7 +1786,7 @@ constexpr int32_t I_DEFAULT_LENGTH = UPRV_LENGTHOF(i_default);
* This is the code underlying uloc_getName and uloc_canonicalize. * This is the code underlying uloc_getName and uloc_canonicalize.
*/ */
void void
_canonicalize(const char* localeID, _canonicalize(std::string_view localeID,
ByteSink& sink, ByteSink& sink,
uint32_t options, uint32_t options,
UErrorCode& err) { UErrorCode& err) {
@ -1764,33 +1797,30 @@ _canonicalize(const char* localeID,
int32_t j, fieldCount=0; int32_t j, fieldCount=0;
CharString tempBuffer; // if localeID has a BCP47 extension, tmpLocaleID points to this CharString tempBuffer; // if localeID has a BCP47 extension, tmpLocaleID points to this
CharString localeIDWithHyphens; // if localeID has a BPC47 extension and have _, tmpLocaleID points to this CharString localeIDWithHyphens; // if localeID has a BPC47 extension and have _, tmpLocaleID points to this
const char* origLocaleID; std::string_view origLocaleID;
const char* tmpLocaleID; std::string_view tmpLocaleID;
const char* keywordAssign = nullptr; size_t keywordAssign = std::string_view::npos;
const char* separatorIndicator = nullptr; size_t separatorIndicator = std::string_view::npos;
if (_hasBCP47Extension(localeID)) { if (_hasBCP47Extension(localeID)) {
const char* localeIDPtr = localeID; std::string_view localeIDPtr = localeID;
// convert all underbars to hyphens, unless the "BCP47 extension" comes at the beginning of the string // convert all underbars to hyphens, unless the "BCP47 extension" comes at the beginning of the string
if (uprv_strchr(localeID, '_') != nullptr && localeID[1] != '-' && localeID[1] != '_') { if (localeID.size() >= 2 && localeID.find('_') != std::string_view::npos && localeID[1] != '-' && localeID[1] != '_') {
localeIDWithHyphens.append(localeID, -1, err); localeIDWithHyphens.append(localeID, err);
if (U_SUCCESS(err)) { if (U_SUCCESS(err)) {
for (char* p = localeIDWithHyphens.data(); *p != '\0'; ++p) { for (char* p = localeIDWithHyphens.data(); *p != '\0'; ++p) {
if (*p == '_') { if (*p == '_') {
*p = '-'; *p = '-';
} }
} }
localeIDPtr = localeIDWithHyphens.data(); localeIDPtr = localeIDWithHyphens.toStringPiece();
} }
} }
tempBuffer = ulocimp_forLanguageTag(localeIDPtr, -1, nullptr, err); tempBuffer = ulocimp_forLanguageTag(localeIDPtr.data(), static_cast<int32_t>(localeIDPtr.size()), nullptr, err);
tmpLocaleID = U_SUCCESS(err) && !tempBuffer.isEmpty() ? tempBuffer.data() : localeIDPtr; tmpLocaleID = U_SUCCESS(err) && !tempBuffer.isEmpty() ? static_cast<std::string_view>(tempBuffer.toStringPiece()) : localeIDPtr;
} else { } else {
if (localeID==nullptr) {
localeID=uloc_getDefault();
}
tmpLocaleID=localeID; tmpLocaleID=localeID;
} }
@ -1801,20 +1831,25 @@ _canonicalize(const char* localeID,
CharString script; CharString script;
CharString country; CharString country;
CharString variant; CharString variant;
const char* end = nullptr;
ulocimp_getSubtags( ulocimp_getSubtags(
tmpLocaleID, tmpLocaleID,
&tag, &tag,
&script, &script,
&country, &country,
&variant, &variant,
&tmpLocaleID, &end,
err); err);
if (U_FAILURE(err)) { if (U_FAILURE(err)) {
return; return;
} }
U_ASSERT(end != nullptr);
if (end > tmpLocaleID.data()) {
tmpLocaleID.remove_prefix(end - tmpLocaleID.data());
}
if (tag.length() == I_DEFAULT_LENGTH && if (tag.length() == I_DEFAULT_LENGTH && origLocaleID.length() >= I_DEFAULT_LENGTH &&
uprv_strncmp(origLocaleID, i_default, I_DEFAULT_LENGTH) == 0) { uprv_strncmp(origLocaleID.data(), i_default, I_DEFAULT_LENGTH) == 0) {
tag.clear(); tag.clear();
tag.append(uloc_getDefault(), err); tag.append(uloc_getDefault(), err);
} else { } else {
@ -1839,15 +1874,14 @@ _canonicalize(const char* localeID,
} }
/* Copy POSIX-style charset specifier, if any [mr.utf8] */ /* Copy POSIX-style charset specifier, if any [mr.utf8] */
if (!OPTION_SET(options, _ULOC_CANONICALIZE) && *tmpLocaleID == '.') { if (!OPTION_SET(options, _ULOC_CANONICALIZE) && !tmpLocaleID.empty() && tmpLocaleID.front() == '.') {
tag.append('.', err); tag.append('.', err);
++tmpLocaleID; tmpLocaleID.remove_prefix(1);
const char *atPos = nullptr;
size_t length; size_t length;
if((atPos = uprv_strchr(tmpLocaleID, '@')) != nullptr) { if (size_t atPos = tmpLocaleID.find('@'); atPos != std::string_view::npos) {
length = atPos - tmpLocaleID; length = atPos;
} else { } else {
length = uprv_strlen(tmpLocaleID); length = tmpLocaleID.length();
} }
// The longest charset name we found in IANA charset registry // The longest charset name we found in IANA charset registry
// https://www.iana.org/assignments/character-sets/ is // https://www.iana.org/assignments/character-sets/ is
@ -1859,33 +1893,34 @@ _canonicalize(const char* localeID,
err = U_ILLEGAL_ARGUMENT_ERROR; /* malformed keyword name */ err = U_ILLEGAL_ARGUMENT_ERROR; /* malformed keyword name */
return; return;
} }
tag.append(tmpLocaleID, static_cast<int32_t>(length), err); if (length > 0) {
tmpLocaleID += length; tag.append(tmpLocaleID.data(), static_cast<int32_t>(length), err);
tmpLocaleID.remove_prefix(length);
}
} }
/* Scan ahead to next '@' and determine if it is followed by '=' and/or ';' /* Scan ahead to next '@' and determine if it is followed by '=' and/or ';'
After this, tmpLocaleID either points to '@' or is nullptr */ After this, tmpLocaleID either starts at '@' or is empty. */
if ((tmpLocaleID=locale_getKeywordsStart(tmpLocaleID))!=nullptr) { if (const char* start = locale_getKeywordsStart(tmpLocaleID); start != nullptr) {
keywordAssign = uprv_strchr(tmpLocaleID, '='); if (start > tmpLocaleID.data()) {
separatorIndicator = uprv_strchr(tmpLocaleID, ';'); tmpLocaleID.remove_prefix(start - tmpLocaleID.data());
}
keywordAssign = tmpLocaleID.find('=');
separatorIndicator = tmpLocaleID.find(';');
} else {
tmpLocaleID = {};
} }
/* Copy POSIX-style variant, if any [mr@FOO] */ /* Copy POSIX-style variant, if any [mr@FOO] */
if (!OPTION_SET(options, _ULOC_CANONICALIZE) && if (!OPTION_SET(options, _ULOC_CANONICALIZE) &&
tmpLocaleID != nullptr && keywordAssign == nullptr) { !tmpLocaleID.empty() && keywordAssign == std::string_view::npos) {
for (;;) { tag.append(tmpLocaleID, err);
char c = *tmpLocaleID; tmpLocaleID = {};
if (c == 0) {
break;
}
tag.append(c, err);
++tmpLocaleID;
}
} }
if (OPTION_SET(options, _ULOC_CANONICALIZE)) { if (OPTION_SET(options, _ULOC_CANONICALIZE)) {
/* Handle @FOO variant if @ is present and not followed by = */ /* Handle @FOO variant if @ is present and not followed by = */
if (tmpLocaleID!=nullptr && keywordAssign==nullptr) { if (!tmpLocaleID.empty() && keywordAssign == std::string_view::npos) {
/* Add missing '_' if needed */ /* Add missing '_' if needed */
if (fieldCount < 2 || (fieldCount < 3 && !script.isEmpty())) { if (fieldCount < 2 || (fieldCount < 3 && !script.isEmpty())) {
do { do {
@ -1895,7 +1930,9 @@ _canonicalize(const char* localeID,
} }
CharStringByteSink s(&tag); CharStringByteSink s(&tag);
_getVariant(tmpLocaleID+1, '@', &s, nullptr, !variant.isEmpty(), err); std::string_view sub = tmpLocaleID;
sub.remove_prefix(1);
_getVariant(sub, '@', &s, !variant.isEmpty(), err);
if (U_FAILURE(err)) { return; } if (U_FAILURE(err)) { return; }
} }
@ -1903,7 +1940,7 @@ _canonicalize(const char* localeID,
for (j=0; j<UPRV_LENGTHOF(CANONICALIZE_MAP); j++) { for (j=0; j<UPRV_LENGTHOF(CANONICALIZE_MAP); j++) {
StringPiece id(CANONICALIZE_MAP[j].id); StringPiece id(CANONICALIZE_MAP[j].id);
if (tag == id) { if (tag == id) {
if (id.empty() && tmpLocaleID != nullptr) { if (id.empty() && !tmpLocaleID.empty()) {
break; /* Don't remap "" if keywords present */ break; /* Don't remap "" if keywords present */
} }
tag.clear(); tag.clear();
@ -1916,11 +1953,12 @@ _canonicalize(const char* localeID,
sink.Append(tag.data(), tag.length()); sink.Append(tag.data(), tag.length());
if (!OPTION_SET(options, _ULOC_STRIP_KEYWORDS)) { if (!OPTION_SET(options, _ULOC_STRIP_KEYWORDS)) {
if (tmpLocaleID!=nullptr && keywordAssign!=nullptr && if (!tmpLocaleID.empty() && keywordAssign != std::string_view::npos &&
(!separatorIndicator || separatorIndicator > keywordAssign)) { (separatorIndicator == std::string_view::npos || separatorIndicator > keywordAssign)) {
sink.Append("@", 1); sink.Append("@", 1);
++fieldCount; ++fieldCount;
ulocimp_getKeywords(tmpLocaleID+1, '@', sink, true, err); tmpLocaleID.remove_prefix(1);
ulocimp_getKeywords(tmpLocaleID, '@', sink, true, err);
} }
} }
} }
@ -1989,6 +2027,10 @@ uloc_getLanguage(const char* localeID,
int32_t languageCapacity, int32_t languageCapacity,
UErrorCode* err) UErrorCode* err)
{ {
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
/* uloc_getLanguage will return a 2 character iso-639 code if one exists. *CWB*/ /* uloc_getLanguage will return a 2 character iso-639 code if one exists. *CWB*/
return ByteSinkUtil::viaByteSinkToTerminatedChars( return ByteSinkUtil::viaByteSinkToTerminatedChars(
language, languageCapacity, language, languageCapacity,
@ -2011,6 +2053,10 @@ uloc_getScript(const char* localeID,
int32_t scriptCapacity, int32_t scriptCapacity,
UErrorCode* err) UErrorCode* err)
{ {
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
return ByteSinkUtil::viaByteSinkToTerminatedChars( return ByteSinkUtil::viaByteSinkToTerminatedChars(
script, scriptCapacity, script, scriptCapacity,
[&](ByteSink& sink, UErrorCode& status) { [&](ByteSink& sink, UErrorCode& status) {
@ -2032,6 +2078,10 @@ uloc_getCountry(const char* localeID,
int32_t countryCapacity, int32_t countryCapacity,
UErrorCode* err) UErrorCode* err)
{ {
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
return ByteSinkUtil::viaByteSinkToTerminatedChars( return ByteSinkUtil::viaByteSinkToTerminatedChars(
country, countryCapacity, country, countryCapacity,
[&](ByteSink& sink, UErrorCode& status) { [&](ByteSink& sink, UErrorCode& status) {
@ -2053,6 +2103,10 @@ uloc_getVariant(const char* localeID,
int32_t variantCapacity, int32_t variantCapacity,
UErrorCode* err) UErrorCode* err)
{ {
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
return ByteSinkUtil::viaByteSinkToTerminatedChars( return ByteSinkUtil::viaByteSinkToTerminatedChars(
variant, variantCapacity, variant, variantCapacity,
[&](ByteSink& sink, UErrorCode& status) { [&](ByteSink& sink, UErrorCode& status) {
@ -2074,6 +2128,9 @@ uloc_getName(const char* localeID,
int32_t nameCapacity, int32_t nameCapacity,
UErrorCode* err) UErrorCode* err)
{ {
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
return ByteSinkUtil::viaByteSinkToTerminatedChars( return ByteSinkUtil::viaByteSinkToTerminatedChars(
name, nameCapacity, name, nameCapacity,
[&](ByteSink& sink, UErrorCode& status) { [&](ByteSink& sink, UErrorCode& status) {
@ -2083,7 +2140,7 @@ uloc_getName(const char* localeID,
} }
U_EXPORT CharString U_EXPORT CharString
ulocimp_getName(const char* localeID, ulocimp_getName(std::string_view localeID,
UErrorCode& err) UErrorCode& err)
{ {
return ByteSinkUtil::viaByteSinkToCharString( return ByteSinkUtil::viaByteSinkToCharString(
@ -2094,7 +2151,7 @@ ulocimp_getName(const char* localeID,
} }
U_EXPORT void U_EXPORT void
ulocimp_getName(const char* localeID, ulocimp_getName(std::string_view localeID,
ByteSink& sink, ByteSink& sink,
UErrorCode& err) UErrorCode& err)
{ {
@ -2107,6 +2164,9 @@ uloc_getBaseName(const char* localeID,
int32_t nameCapacity, int32_t nameCapacity,
UErrorCode* err) UErrorCode* err)
{ {
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
return ByteSinkUtil::viaByteSinkToTerminatedChars( return ByteSinkUtil::viaByteSinkToTerminatedChars(
name, nameCapacity, name, nameCapacity,
[&](ByteSink& sink, UErrorCode& status) { [&](ByteSink& sink, UErrorCode& status) {
@ -2116,7 +2176,7 @@ uloc_getBaseName(const char* localeID,
} }
U_EXPORT CharString U_EXPORT CharString
ulocimp_getBaseName(const char* localeID, ulocimp_getBaseName(std::string_view localeID,
UErrorCode& err) UErrorCode& err)
{ {
return ByteSinkUtil::viaByteSinkToCharString( return ByteSinkUtil::viaByteSinkToCharString(
@ -2127,7 +2187,7 @@ ulocimp_getBaseName(const char* localeID,
} }
U_EXPORT void U_EXPORT void
ulocimp_getBaseName(const char* localeID, ulocimp_getBaseName(std::string_view localeID,
ByteSink& sink, ByteSink& sink,
UErrorCode& err) UErrorCode& err)
{ {
@ -2140,6 +2200,9 @@ uloc_canonicalize(const char* localeID,
int32_t nameCapacity, int32_t nameCapacity,
UErrorCode* err) UErrorCode* err)
{ {
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
return ByteSinkUtil::viaByteSinkToTerminatedChars( return ByteSinkUtil::viaByteSinkToTerminatedChars(
name, nameCapacity, name, nameCapacity,
[&](ByteSink& sink, UErrorCode& status) { [&](ByteSink& sink, UErrorCode& status) {
@ -2149,7 +2212,7 @@ uloc_canonicalize(const char* localeID,
} }
U_EXPORT CharString U_EXPORT CharString
ulocimp_canonicalize(const char* localeID, ulocimp_canonicalize(std::string_view localeID,
UErrorCode& err) UErrorCode& err)
{ {
return ByteSinkUtil::viaByteSinkToCharString( return ByteSinkUtil::viaByteSinkToCharString(
@ -2160,7 +2223,7 @@ ulocimp_canonicalize(const char* localeID,
} }
U_EXPORT void U_EXPORT void
ulocimp_canonicalize(const char* localeID, ulocimp_canonicalize(std::string_view localeID,
ByteSink& sink, ByteSink& sink,
UErrorCode& err) UErrorCode& err)
{ {

View file

@ -1043,7 +1043,7 @@ _initializeULanguageTag(ULanguageTag* langtag) {
} }
void void
_appendLanguageToLanguageTag(const char* localeID, icu::ByteSink& sink, bool strict, UErrorCode& status) { _appendLanguageToLanguageTag(std::string_view localeID, icu::ByteSink& sink, bool strict, UErrorCode& status) {
UErrorCode tmpStatus = U_ZERO_ERROR; UErrorCode tmpStatus = U_ZERO_ERROR;
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
@ -1088,7 +1088,7 @@ _appendLanguageToLanguageTag(const char* localeID, icu::ByteSink& sink, bool str
} }
void void
_appendScriptToLanguageTag(const char* localeID, icu::ByteSink& sink, bool strict, UErrorCode& status) { _appendScriptToLanguageTag(std::string_view localeID, icu::ByteSink& sink, bool strict, UErrorCode& status) {
UErrorCode tmpStatus = U_ZERO_ERROR; UErrorCode tmpStatus = U_ZERO_ERROR;
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
@ -1118,7 +1118,7 @@ _appendScriptToLanguageTag(const char* localeID, icu::ByteSink& sink, bool stric
} }
void void
_appendRegionToLanguageTag(const char* localeID, icu::ByteSink& sink, bool strict, UErrorCode& status) { _appendRegionToLanguageTag(std::string_view localeID, icu::ByteSink& sink, bool strict, UErrorCode& status) {
UErrorCode tmpStatus = U_ZERO_ERROR; UErrorCode tmpStatus = U_ZERO_ERROR;
if (U_FAILURE(status)) { if (U_FAILURE(status)) {
@ -1169,7 +1169,7 @@ void _sortVariants(VariantListEntry* first) {
} }
void void
_appendVariantsToLanguageTag(const char* localeID, icu::ByteSink& sink, bool strict, bool& hadPosix, UErrorCode& status) { _appendVariantsToLanguageTag(std::string_view localeID, icu::ByteSink& sink, bool strict, bool& hadPosix, UErrorCode& status) {
if (U_FAILURE(status)) { return; } if (U_FAILURE(status)) { return; }
UErrorCode tmpStatus = U_ZERO_ERROR; UErrorCode tmpStatus = U_ZERO_ERROR;
@ -1872,7 +1872,7 @@ _appendKeywords(ULanguageTag* langtag, icu::ByteSink& sink, UErrorCode& status)
} }
void void
_appendPrivateuseToLanguageTag(const char* localeID, icu::ByteSink& sink, bool strict, bool /*hadPosix*/, UErrorCode& status) { _appendPrivateuseToLanguageTag(std::string_view localeID, icu::ByteSink& sink, bool strict, bool /*hadPosix*/, UErrorCode& status) {
if (U_FAILURE(status)) { return; } if (U_FAILURE(status)) { return; }
UErrorCode tmpStatus = U_ZERO_ERROR; UErrorCode tmpStatus = U_ZERO_ERROR;
@ -2596,6 +2596,9 @@ ulocimp_toLanguageTag(const char* localeID,
bool hadPosix = false; bool hadPosix = false;
const char* pKeywordStart; const char* pKeywordStart;
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
/* Note: uloc_canonicalize returns "en_US_POSIX" for input locale ID "". See #6835 */ /* Note: uloc_canonicalize returns "en_US_POSIX" for input locale ID "". See #6835 */
icu::CharString canonical = ulocimp_canonicalize(localeID, tmpStatus); icu::CharString canonical = ulocimp_canonicalize(localeID, tmpStatus);
if (U_FAILURE(tmpStatus)) { if (U_FAILURE(tmpStatus)) {
@ -2604,7 +2607,7 @@ ulocimp_toLanguageTag(const char* localeID,
} }
/* For handling special case - private use only tag */ /* For handling special case - private use only tag */
pKeywordStart = locale_getKeywordsStart(canonical.data()); pKeywordStart = locale_getKeywordsStart(canonical.toStringPiece());
if (pKeywordStart == canonical.data()) { if (pKeywordStart == canonical.data()) {
int kwdCnt = 0; int kwdCnt = 0;
bool done = false; bool done = false;
@ -2642,12 +2645,12 @@ ulocimp_toLanguageTag(const char* localeID,
} }
} }
_appendLanguageToLanguageTag(canonical.data(), sink, strict, status); _appendLanguageToLanguageTag(canonical.toStringPiece(), sink, strict, status);
_appendScriptToLanguageTag(canonical.data(), sink, strict, status); _appendScriptToLanguageTag(canonical.toStringPiece(), sink, strict, status);
_appendRegionToLanguageTag(canonical.data(), sink, strict, status); _appendRegionToLanguageTag(canonical.toStringPiece(), sink, strict, status);
_appendVariantsToLanguageTag(canonical.data(), sink, strict, hadPosix, status); _appendVariantsToLanguageTag(canonical.toStringPiece(), sink, strict, hadPosix, status);
_appendKeywordsToLanguageTag(canonical.data(), sink, strict, hadPosix, status); _appendKeywordsToLanguageTag(canonical.data(), sink, strict, hadPosix, status);
_appendPrivateuseToLanguageTag(canonical.data(), sink, strict, hadPosix, status); _appendPrivateuseToLanguageTag(canonical.toStringPiece(), sink, strict, hadPosix, status);
} }

View file

@ -10,7 +10,6 @@
#include "unicode/locid.h" #include "unicode/locid.h"
#include "bytesinkutil.h" #include "bytesinkutil.h"
#include "charstr.h"
#include "cmemory.h" #include "cmemory.h"
U_NAMESPACE_USE U_NAMESPACE_USE
@ -24,9 +23,7 @@ ulocale_openForLocaleID(const char* localeID, int32_t length, UErrorCode* err) {
if (length < 0) { if (length < 0) {
return EXTERNAL(icu::Locale::createFromName(localeID).clone()); return EXTERNAL(icu::Locale::createFromName(localeID).clone());
} }
CharString str(localeID, length, *err); // Make a NUL terminated copy. return EXTERNAL(icu::Locale::createFromName(StringPiece{localeID, length}).clone());
if (U_FAILURE(*err)) { return nullptr; }
return EXTERNAL(icu::Locale::createFromName(str.data()).clone());
} }
ULocale* ULocale*

View file

@ -68,42 +68,42 @@ U_EXPORT std::optional<std::string_view>
ulocimp_toLegacyTypeWithFallback(std::string_view keyword, std::string_view value); ulocimp_toLegacyTypeWithFallback(std::string_view keyword, std::string_view value);
U_EXPORT icu::CharString U_EXPORT icu::CharString
ulocimp_getKeywords(const char* localeID, ulocimp_getKeywords(std::string_view localeID,
char prev, char prev,
bool valuesToo, bool valuesToo,
UErrorCode& status); UErrorCode& status);
U_EXPORT void U_EXPORT void
ulocimp_getKeywords(const char* localeID, ulocimp_getKeywords(std::string_view localeID,
char prev, char prev,
icu::ByteSink& sink, icu::ByteSink& sink,
bool valuesToo, bool valuesToo,
UErrorCode& status); UErrorCode& status);
U_EXPORT icu::CharString U_EXPORT icu::CharString
ulocimp_getName(const char* localeID, ulocimp_getName(std::string_view localeID,
UErrorCode& err); UErrorCode& err);
U_EXPORT void U_EXPORT void
ulocimp_getName(const char* localeID, ulocimp_getName(std::string_view localeID,
icu::ByteSink& sink, icu::ByteSink& sink,
UErrorCode& err); UErrorCode& err);
U_EXPORT icu::CharString U_EXPORT icu::CharString
ulocimp_getBaseName(const char* localeID, ulocimp_getBaseName(std::string_view localeID,
UErrorCode& err); UErrorCode& err);
U_EXPORT void U_EXPORT void
ulocimp_getBaseName(const char* localeID, ulocimp_getBaseName(std::string_view localeID,
icu::ByteSink& sink, icu::ByteSink& sink,
UErrorCode& err); UErrorCode& err);
U_EXPORT icu::CharString U_EXPORT icu::CharString
ulocimp_canonicalize(const char* localeID, ulocimp_canonicalize(std::string_view localeID,
UErrorCode& err); UErrorCode& err);
U_EXPORT void U_EXPORT void
ulocimp_canonicalize(const char* localeID, ulocimp_canonicalize(std::string_view localeID,
icu::ByteSink& sink, icu::ByteSink& sink,
UErrorCode& err); UErrorCode& err);
@ -119,16 +119,16 @@ ulocimp_getKeywordValue(const char* localeID,
UErrorCode& status); UErrorCode& status);
U_EXPORT icu::CharString U_EXPORT icu::CharString
ulocimp_getLanguage(const char* localeID, UErrorCode& status); ulocimp_getLanguage(std::string_view localeID, UErrorCode& status);
U_EXPORT icu::CharString U_EXPORT icu::CharString
ulocimp_getScript(const char* localeID, UErrorCode& status); ulocimp_getScript(std::string_view localeID, UErrorCode& status);
U_EXPORT icu::CharString U_EXPORT icu::CharString
ulocimp_getRegion(const char* localeID, UErrorCode& status); ulocimp_getRegion(std::string_view localeID, UErrorCode& status);
U_EXPORT icu::CharString U_EXPORT icu::CharString
ulocimp_getVariant(const char* localeID, UErrorCode& status); ulocimp_getVariant(std::string_view localeID, UErrorCode& status);
U_EXPORT void U_EXPORT void
ulocimp_setKeywordValue(std::string_view keywordName, ulocimp_setKeywordValue(std::string_view keywordName,
@ -145,7 +145,7 @@ ulocimp_setKeywordValue(std::string_view keywords,
U_EXPORT void U_EXPORT void
ulocimp_getSubtags( ulocimp_getSubtags(
const char* localeID, std::string_view localeID,
icu::CharString* language, icu::CharString* language,
icu::CharString* script, icu::CharString* script,
icu::CharString* region, icu::CharString* region,
@ -155,7 +155,7 @@ ulocimp_getSubtags(
U_EXPORT void U_EXPORT void
ulocimp_getSubtags( ulocimp_getSubtags(
const char* localeID, std::string_view localeID,
icu::ByteSink* language, icu::ByteSink* language,
icu::ByteSink* script, icu::ByteSink* script,
icu::ByteSink* region, icu::ByteSink* region,
@ -165,7 +165,7 @@ ulocimp_getSubtags(
inline void inline void
ulocimp_getSubtags( ulocimp_getSubtags(
const char* localeID, std::string_view localeID,
std::nullptr_t, std::nullptr_t,
std::nullptr_t, std::nullptr_t,
std::nullptr_t, std::nullptr_t,
@ -364,7 +364,7 @@ ulocimp_minimizeSubtags(const char* localeID,
UErrorCode& err); UErrorCode& err);
U_CAPI const char * U_EXPORT2 U_CAPI const char * U_EXPORT2
locale_getKeywordsStart(const char *localeID); locale_getKeywordsStart(std::string_view localeID);
bool bool
ultag_isExtensionSubtags(const char* s, int32_t len); ultag_isExtensionSubtags(const char* s, int32_t len);

View file

@ -237,8 +237,13 @@ typedef HANDLE MemoryMap;
pData->map = (char *)data + length; pData->map = (char *)data + length;
pData->pHeader=(const DataHeader *)data; pData->pHeader=(const DataHeader *)data;
pData->mapAddr = data; pData->mapAddr = data;
#if U_PLATFORM == U_PF_IPHONE #if U_PLATFORM == U_PF_IPHONE || U_PLATFORM == U_PF_ANDROID
// Apparently supported from Android 23 and higher:
// https://github.com/ggml-org/llama.cpp/pull/3631
// Checking for the flag itself is safer than checking for __ANDROID_API__.
# ifdef POSIX_MADV_RANDOM
posix_madvise(data, length, POSIX_MADV_RANDOM); posix_madvise(data, length, POSIX_MADV_RANDOM);
# endif
#endif #endif
return true; return true;
} }

View file

@ -58,6 +58,8 @@ U_NAMESPACE_END
U_NAMESPACE_BEGIN U_NAMESPACE_BEGIN
class CharString;
/** /**
* The BreakIterator class implements methods for finding the location * The BreakIterator class implements methods for finding the location
* of boundaries in text. BreakIterator is an abstract base class. * of boundaries in text. BreakIterator is an abstract base class.
@ -646,9 +648,9 @@ protected:
private: private:
/** @internal (private) */ /** @internal (private) */
char actualLocale[ULOC_FULLNAME_CAPACITY]; CharString* actualLocale = nullptr;
char validLocale[ULOC_FULLNAME_CAPACITY]; CharString* validLocale = nullptr;
char requestLocale[ULOC_FULLNAME_CAPACITY]; CharString* requestLocale = nullptr;
}; };
#ifndef U_HIDE_DEPRECATED_API #ifndef U_HIDE_DEPRECATED_API

View file

@ -9,10 +9,13 @@
#include "unicode/utypes.h" #include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API #if U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API
#include <cstddef> #include <cstddef>
#include <string_view> #include <string_view>
#include <type_traits>
#endif
/** /**
* \file * \file
@ -21,8 +24,6 @@
* Also conversion functions from char16_t * to UChar * and OldUChar *. * Also conversion functions from char16_t * to UChar * and OldUChar *.
*/ */
U_NAMESPACE_BEGIN
/** /**
* \def U_ALIASING_BARRIER * \def U_ALIASING_BARRIER
* Barrier for pointer anti-aliasing optimizations even across function boundaries. * Barrier for pointer anti-aliasing optimizations even across function boundaries.
@ -36,6 +37,11 @@ U_NAMESPACE_BEGIN
# define U_ALIASING_BARRIER(ptr) # define U_ALIASING_BARRIER(ptr)
#endif #endif
// ICU DLL-exported
#if U_SHOW_CPLUSPLUS_API
U_NAMESPACE_BEGIN
/** /**
* char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types. * char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
* @stable ICU 59 * @stable ICU 59
@ -251,6 +257,60 @@ const char16_t *ConstChar16Ptr::get() const { return u_.cp; }
#endif #endif
/// \endcond /// \endcond
U_NAMESPACE_END
#endif // U_SHOW_CPLUSPLUS_API
// Usable in header-only definitions
#if U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API
namespace U_ICU_NAMESPACE_OR_INTERNAL {
#ifndef U_FORCE_HIDE_INTERNAL_API
/** @internal */
template<typename T, typename = std::enable_if_t<std::is_same_v<T, UChar>>>
inline const char16_t *uprv_char16PtrFromUChar(const T *p) {
if constexpr (std::is_same_v<UChar, char16_t>) {
return p;
} else {
#if U_SHOW_CPLUSPLUS_API
return ConstChar16Ptr(p).get();
#else
#ifdef U_ALIASING_BARRIER
U_ALIASING_BARRIER(p);
#endif
return reinterpret_cast<const char16_t *>(p);
#endif
}
}
#if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000)
/** @internal */
inline const char16_t *uprv_char16PtrFromUint16(const uint16_t *p) {
#if U_SHOW_CPLUSPLUS_API
return ConstChar16Ptr(p).get();
#else
#ifdef U_ALIASING_BARRIER
U_ALIASING_BARRIER(p);
#endif
return reinterpret_cast<const char16_t *>(p);
#endif
}
#endif
#if U_SIZEOF_WCHAR_T==2
/** @internal */
inline const char16_t *uprv_char16PtrFromWchar(const wchar_t *p) {
#if U_SHOW_CPLUSPLUS_API
return ConstChar16Ptr(p).get();
#else
#ifdef U_ALIASING_BARRIER
U_ALIASING_BARRIER(p);
#endif
return reinterpret_cast<const char16_t *>(p);
#endif
}
#endif
#endif
/** /**
* Converts from const char16_t * to const UChar *. * Converts from const char16_t * to const UChar *.
* Includes an aliasing barrier if available. * Includes an aliasing barrier if available.
@ -307,6 +367,15 @@ inline OldUChar *toOldUCharPtr(char16_t *p) {
return reinterpret_cast<OldUChar *>(p); return reinterpret_cast<OldUChar *>(p);
} }
} // U_ICU_NAMESPACE_OR_INTERNAL
#endif // U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API
// ICU DLL-exported
#if U_SHOW_CPLUSPLUS_API
U_NAMESPACE_BEGIN
#ifndef U_FORCE_HIDE_INTERNAL_API #ifndef U_FORCE_HIDE_INTERNAL_API
/** /**
* Is T convertible to a std::u16string_view or some other 16-bit string view? * Is T convertible to a std::u16string_view or some other 16-bit string view?
@ -379,6 +448,6 @@ inline std::u16string_view toU16StringViewNullable(const T& text) {
U_NAMESPACE_END U_NAMESPACE_END
#endif /* U_SHOW_CPLUSPLUS_API */ #endif // U_SHOW_CPLUSPLUS_API
#endif // __CHAR16PTR_H__ #endif // __CHAR16PTR_H__

View file

@ -449,6 +449,11 @@ public:
*/ */
static Locale U_EXPORT2 createFromName(const char *name); static Locale U_EXPORT2 createFromName(const char *name);
#ifndef U_HIDE_INTERNAL_API
/** @internal */
static Locale U_EXPORT2 createFromName(StringPiece name);
#endif /* U_HIDE_INTERNAL_API */
/** /**
* Creates a locale from the given string after canonicalizing * Creates a locale from the given string after canonicalizing
* the string according to CLDR by calling uloc_canonicalize(). * the string according to CLDR by calling uloc_canonicalize().
@ -1133,7 +1138,9 @@ private:
* @param cLocaleID The new locale name. * @param cLocaleID The new locale name.
* @param canonicalize whether to call uloc_canonicalize on cLocaleID * @param canonicalize whether to call uloc_canonicalize on cLocaleID
*/ */
Locale& init(const char* cLocaleID, UBool canonicalize); Locale& init(const char* localeID, UBool canonicalize);
/** @internal */
Locale& init(StringPiece localeID, UBool canonicalize);
/* /*
* Internal constructor to allow construction of a locale object with * Internal constructor to allow construction of a locale object with

View file

@ -450,7 +450,7 @@ public:
* @return a Locale object * @return a Locale object
* @stable ICU 2.8 * @stable ICU 2.8
*/ */
const Locale Locale
getLocale(ULocDataLocaleType type, UErrorCode &status) const; getLocale(ULocDataLocaleType type, UErrorCode &status) const;
#ifndef U_HIDE_INTERNAL_API #ifndef U_HIDE_INTERNAL_API
/** /**

View file

@ -675,14 +675,14 @@ typedef enum UProperty {
* @stable ICU 63 * @stable ICU 63
*/ */
UCHAR_VERTICAL_ORIENTATION=0x1018, UCHAR_VERTICAL_ORIENTATION=0x1018,
#ifndef U_HIDE_DRAFT_API
/** /**
* Enumerated property Identifier_Status. * Enumerated property Identifier_Status.
* Used for UTS #39 General Security Profile for Identifiers * Used for UTS #39 General Security Profile for Identifiers
* (https://www.unicode.org/reports/tr39/#General_Security_Profile). * (https://www.unicode.org/reports/tr39/#General_Security_Profile).
* @draft ICU 75 * @stable ICU 75
*/ */
UCHAR_IDENTIFIER_STATUS=0x1019, UCHAR_IDENTIFIER_STATUS=0x1019,
#ifndef U_HIDE_DRAFT_API
/** /**
* Enumerated property Indic_Conjunct_Break. * Enumerated property Indic_Conjunct_Break.
* Used in the grapheme cluster break algorithm in UAX #29. * Used in the grapheme cluster break algorithm in UAX #29.
@ -796,7 +796,6 @@ typedef enum UProperty {
UCHAR_SCRIPT_EXTENSIONS=0x7000, UCHAR_SCRIPT_EXTENSIONS=0x7000,
/** First constant for Unicode properties with unusual value types. @stable ICU 4.6 */ /** First constant for Unicode properties with unusual value types. @stable ICU 4.6 */
UCHAR_OTHER_PROPERTY_START=UCHAR_SCRIPT_EXTENSIONS, UCHAR_OTHER_PROPERTY_START=UCHAR_SCRIPT_EXTENSIONS,
#ifndef U_HIDE_DRAFT_API
/** /**
* Miscellaneous property Identifier_Type. * Miscellaneous property Identifier_Type.
* Used for UTS #39 General Security Profile for Identifiers * Used for UTS #39 General Security Profile for Identifiers
@ -808,10 +807,9 @@ typedef enum UProperty {
* *
* @see u_hasIDType * @see u_hasIDType
* @see u_getIDTypes * @see u_getIDTypes
* @draft ICU 75 * @stable ICU 75
*/ */
UCHAR_IDENTIFIER_TYPE=0x7001, UCHAR_IDENTIFIER_TYPE=0x7001,
#endif // U_HIDE_DRAFT_API
#ifndef U_HIDE_DEPRECATED_API #ifndef U_HIDE_DEPRECATED_API
/** /**
* One more than the last constant for Unicode properties with unusual value types. * One more than the last constant for Unicode properties with unusual value types.
@ -2791,13 +2789,12 @@ typedef enum UVerticalOrientation {
U_VO_UPRIGHT, U_VO_UPRIGHT,
} UVerticalOrientation; } UVerticalOrientation;
#ifndef U_HIDE_DRAFT_API
/** /**
* Identifier Status constants. * Identifier Status constants.
* See https://www.unicode.org/reports/tr39/#Identifier_Status_and_Type. * See https://www.unicode.org/reports/tr39/#Identifier_Status_and_Type.
* *
* @see UCHAR_IDENTIFIER_STATUS * @see UCHAR_IDENTIFIER_STATUS
* @draft ICU 75 * @stable ICU 75
*/ */
typedef enum UIdentifierStatus { typedef enum UIdentifierStatus {
/* /*
@ -2806,9 +2803,9 @@ typedef enum UIdentifierStatus {
* U_ID_STATUS_<Unicode Identifier_Status value name> * U_ID_STATUS_<Unicode Identifier_Status value name>
*/ */
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_STATUS_RESTRICTED, U_ID_STATUS_RESTRICTED,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_STATUS_ALLOWED, U_ID_STATUS_ALLOWED,
} UIdentifierStatus; } UIdentifierStatus;
@ -2817,7 +2814,7 @@ typedef enum UIdentifierStatus {
* See https://www.unicode.org/reports/tr39/#Identifier_Status_and_Type. * See https://www.unicode.org/reports/tr39/#Identifier_Status_and_Type.
* *
* @see UCHAR_IDENTIFIER_TYPE * @see UCHAR_IDENTIFIER_TYPE
* @draft ICU 75 * @stable ICU 75
*/ */
typedef enum UIdentifierType { typedef enum UIdentifierType {
/* /*
@ -2826,32 +2823,31 @@ typedef enum UIdentifierType {
* U_ID_TYPE_<Unicode Identifier_Type value name> * U_ID_TYPE_<Unicode Identifier_Type value name>
*/ */
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_NOT_CHARACTER, U_ID_TYPE_NOT_CHARACTER,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_DEPRECATED, U_ID_TYPE_DEPRECATED,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_DEFAULT_IGNORABLE, U_ID_TYPE_DEFAULT_IGNORABLE,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_NOT_NFKC, U_ID_TYPE_NOT_NFKC,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_NOT_XID, U_ID_TYPE_NOT_XID,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_EXCLUSION, U_ID_TYPE_EXCLUSION,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_OBSOLETE, U_ID_TYPE_OBSOLETE,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_TECHNICAL, U_ID_TYPE_TECHNICAL,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_UNCOMMON_USE, U_ID_TYPE_UNCOMMON_USE,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_LIMITED_USE, U_ID_TYPE_LIMITED_USE,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_INCLUSION, U_ID_TYPE_INCLUSION,
/** @draft ICU 75 */ /** @stable ICU 75 */
U_ID_TYPE_RECOMMENDED, U_ID_TYPE_RECOMMENDED,
} UIdentifierType; } UIdentifierType;
#endif // U_HIDE_DRAFT_API
/** /**
* Check a binary Unicode property for a code point. * Check a binary Unicode property for a code point.
@ -4057,7 +4053,6 @@ u_isIDStart(UChar32 c);
U_CAPI UBool U_EXPORT2 U_CAPI UBool U_EXPORT2
u_isIDPart(UChar32 c); u_isIDPart(UChar32 c);
#ifndef U_HIDE_DRAFT_API
/** /**
* Does the set of Identifier_Type values code point c contain the given type? * Does the set of Identifier_Type values code point c contain the given type?
* *
@ -4069,7 +4064,7 @@ u_isIDPart(UChar32 c);
* @param c code point * @param c code point
* @param type Identifier_Type to check * @param type Identifier_Type to check
* @return true if type is in Identifier_Type(c) * @return true if type is in Identifier_Type(c)
* @draft ICU 75 * @stable ICU 75
*/ */
U_CAPI bool U_EXPORT2 U_CAPI bool U_EXPORT2
u_hasIDType(UChar32 c, UIdentifierType type); u_hasIDType(UChar32 c, UIdentifierType type);
@ -4104,11 +4099,10 @@ u_hasIDType(UChar32 c, UIdentifierType type);
* function chaining. (See User Guide for details.) * function chaining. (See User Guide for details.)
* @return number of values in c's Identifier_Type, * @return number of values in c's Identifier_Type,
* written to types unless U_BUFFER_OVERFLOW_ERROR indicates insufficient capacity * written to types unless U_BUFFER_OVERFLOW_ERROR indicates insufficient capacity
* @draft ICU 75 * @stable ICU 75
*/ */
U_CAPI int32_t U_EXPORT2 U_CAPI int32_t U_EXPORT2
u_getIDTypes(UChar32 c, UIdentifierType *types, int32_t capacity, UErrorCode *pErrorCode); u_getIDTypes(UChar32 c, UIdentifierType *types, int32_t capacity, UErrorCode *pErrorCode);
#endif // U_HIDE_DRAFT_API
/** /**
* Determines if the specified character should be regarded * Determines if the specified character should be regarded

View file

@ -1173,10 +1173,12 @@ public:
inline U_HEADER_NESTED_NAMESPACE::USetStrings strings() const { inline U_HEADER_NESTED_NAMESPACE::USetStrings strings() const {
return U_HEADER_NESTED_NAMESPACE::USetStrings(toUSet()); return U_HEADER_NESTED_NAMESPACE::USetStrings(toUSet());
} }
#endif // U_HIDE_DRAFT_API
#ifndef U_HIDE_DRAFT_API
/** /**
* Returns a C++ iterator for iterating over all of the elements of this set. * Returns a C++ iterator for iterating over all of the elements of this set.
* Convenient all-in one iteration, but creates a UnicodeString for each * Convenient all-in one iteration, but creates a std::u16string for each
* code point or string. * code point or string.
* (Similar to how Java UnicodeSet *is an* Iterable&lt;String&gt;.) * (Similar to how Java UnicodeSet *is an* Iterable&lt;String&gt;.)
* *
@ -1185,13 +1187,14 @@ public:
* \code * \code
* UnicodeSet set(u"[abcçカ🚴{}{abc}{de}]", errorCode); * UnicodeSet set(u"[abcçカ🚴{}{abc}{de}]", errorCode);
* for (auto el : set) { * for (auto el : set) {
* UnicodeString us(el);
* std::string u8; * std::string u8;
* printf("set.string length %ld \"%s\"\n", (long)el.length(), el.toUTF8String(u8).c_str()); * printf("set.element length %ld \"%s\"\n", (long)us.length(), us.toUTF8String(u8).c_str());
* } * }
* \endcode * \endcode
* *
* @return an all-elements iterator. * @return an all-elements iterator.
* @draft ICU 76 * @draft ICU 77
* @see end * @see end
* @see codePoints * @see codePoints
* @see ranges * @see ranges
@ -1203,7 +1206,7 @@ public:
/** /**
* @return an exclusive-end sentinel for iterating over all of the elements of this set. * @return an exclusive-end sentinel for iterating over all of the elements of this set.
* @draft ICU 76 * @draft ICU 77
* @see begin * @see begin
* @see codePoints * @see codePoints
* @see ranges * @see ranges

View file

@ -32,12 +32,13 @@
#include "unicode/utypes.h" #include "unicode/utypes.h"
#include "unicode/uchar.h" #include "unicode/uchar.h"
#if U_SHOW_CPLUSPLUS_API #if U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API
#include <string>
#include <string_view> #include <string_view>
#include "unicode/char16ptr.h" #include "unicode/char16ptr.h"
#include "unicode/localpointer.h" #include "unicode/localpointer.h"
#include "unicode/unistr.h" #include "unicode/utf16.h"
#endif // U_SHOW_CPLUSPLUS_API #endif
#ifndef USET_DEFINED #ifndef USET_DEFINED
@ -1392,8 +1393,8 @@ public:
private: private:
friend class USetCodePoints; friend class USetCodePoints;
USetCodePointIterator(const USet *uset, int32_t rangeIndex, int32_t rangeCount) USetCodePointIterator(const USet *pUset, int32_t nRangeIndex, int32_t nRangeCount)
: uset(uset), rangeIndex(rangeIndex), rangeCount(rangeCount), : uset(pUset), rangeIndex(nRangeIndex), rangeCount(nRangeCount),
c(U_SENTINEL), end(U_SENTINEL) { c(U_SENTINEL), end(U_SENTINEL) {
// Fetch the first range. // Fetch the first range.
operator++(); operator++();
@ -1429,7 +1430,7 @@ public:
* Constructs a C++ "range" object over the code points of the USet. * Constructs a C++ "range" object over the code points of the USet.
* @draft ICU 76 * @draft ICU 76
*/ */
USetCodePoints(const USet *uset) : uset(uset), rangeCount(uset_getRangeCount(uset)) {} USetCodePoints(const USet *pUset) : uset(pUset), rangeCount(uset_getRangeCount(pUset)) {}
/** @draft ICU 76 */ /** @draft ICU 76 */
USetCodePoints(const USetCodePoints &other) = default; USetCodePoints(const USetCodePoints &other) = default;
@ -1460,7 +1461,7 @@ struct CodePointRange {
/** @draft ICU 76 */ /** @draft ICU 76 */
struct iterator { struct iterator {
/** @draft ICU 76 */ /** @draft ICU 76 */
iterator(UChar32 c) : c(c) {} iterator(UChar32 aC) : c(aC) {}
/** @draft ICU 76 */ /** @draft ICU 76 */
bool operator==(const iterator &other) const { return c == other.c; } bool operator==(const iterator &other) const { return c == other.c; }
@ -1573,8 +1574,8 @@ public:
private: private:
friend class USetRanges; friend class USetRanges;
USetRangeIterator(const USet *uset, int32_t rangeIndex, int32_t rangeCount) USetRangeIterator(const USet *pUset, int32_t nRangeIndex, int32_t nRangeCount)
: uset(uset), rangeIndex(rangeIndex), rangeCount(rangeCount) {} : uset(pUset), rangeIndex(nRangeIndex), rangeCount(nRangeCount) {}
const USet *uset; const USet *uset;
int32_t rangeIndex; int32_t rangeIndex;
@ -1610,7 +1611,7 @@ public:
* Constructs a C++ "range" object over the code point ranges of the USet. * Constructs a C++ "range" object over the code point ranges of the USet.
* @draft ICU 76 * @draft ICU 76
*/ */
USetRanges(const USet *uset) : uset(uset), rangeCount(uset_getRangeCount(uset)) {} USetRanges(const USet *pUset) : uset(pUset), rangeCount(uset_getRangeCount(pUset)) {}
/** @draft ICU 76 */ /** @draft ICU 76 */
USetRanges(const USetRanges &other) = default; USetRanges(const USetRanges &other) = default;
@ -1657,7 +1658,7 @@ public:
int32_t length; int32_t length;
const UChar *uchars = uset_getString(uset, index, &length); const UChar *uchars = uset_getString(uset, index, &length);
// assert uchars != nullptr; // assert uchars != nullptr;
return {ConstChar16Ptr(uchars), static_cast<uint32_t>(length)}; return {uprv_char16PtrFromUChar(uchars), static_cast<size_t>(length)};
} }
return {}; return {};
} }
@ -1684,8 +1685,8 @@ public:
private: private:
friend class USetStrings; friend class USetStrings;
USetStringIterator(const USet *uset, int32_t index, int32_t count) USetStringIterator(const USet *pUset, int32_t nIndex, int32_t nCount)
: uset(uset), index(index), count(count) {} : uset(pUset), index(nIndex), count(nCount) {}
const USet *uset; const USet *uset;
int32_t index; int32_t index;
@ -1699,9 +1700,11 @@ private:
* using U_HEADER_NESTED_NAMESPACE::USetStrings; * using U_HEADER_NESTED_NAMESPACE::USetStrings;
* LocalUSetPointer uset(uset_openPattern(u"[abcçカ🚴{}{abc}{de}]", -1, &errorCode)); * LocalUSetPointer uset(uset_openPattern(u"[abcçカ🚴{}{abc}{de}]", -1, &errorCode));
* for (auto s : USetStrings(uset.getAlias())) { * for (auto s : USetStrings(uset.getAlias())) {
* UnicodeString us(s); * int32_t len32 = s.length();
* std::string u8; * char utf8[200];
* printf("uset.string length %ld \"%s\"\n", (long)s.length(), us.toUTF8String(u8).c_str()); * u_strToUTF8WithSub(utf8, int32_t{sizeof(utf8) - 1}, nullptr,
* s.data(), len32, 0xFFFD, nullptr, errorCode);
* printf("uset.string length %ld \"%s\"\n", long{len32}, utf8);
* } * }
* \endcode * \endcode
* *
@ -1718,7 +1721,7 @@ public:
* Constructs a C++ "range" object over the strings of the USet. * Constructs a C++ "range" object over the strings of the USet.
* @draft ICU 76 * @draft ICU 76
*/ */
USetStrings(const USet *uset) : uset(uset), count(uset_getStringCount(uset)) {} USetStrings(const USet *pUset) : uset(pUset), count(uset_getStringCount(pUset)) {}
/** @draft ICU 76 */ /** @draft ICU 76 */
USetStrings(const USetStrings &other) = default; USetStrings(const USetStrings &other) = default;
@ -1737,17 +1740,19 @@ private:
const USet *uset; const USet *uset;
int32_t count; int32_t count;
}; };
#endif // U_HIDE_DRAFT_API
#ifndef U_HIDE_DRAFT_API
/** /**
* Iterator returned by USetElements. * Iterator returned by USetElements.
* @draft ICU 76 * @draft ICU 77
*/ */
class USetElementIterator { class USetElementIterator {
public: public:
/** @draft ICU 76 */ /** @draft ICU 77 */
USetElementIterator(const USetElementIterator &other) = default; USetElementIterator(const USetElementIterator &other) = default;
/** @draft ICU 76 */ /** @draft ICU 77 */
bool operator==(const USetElementIterator &other) const { bool operator==(const USetElementIterator &other) const {
// No need to compare rangeCount & end given private constructor // No need to compare rangeCount & end given private constructor
// and assuming we don't compare iterators across the set being modified. // and assuming we don't compare iterators across the set being modified.
@ -1756,26 +1761,28 @@ public:
return uset == other.uset && c == other.c && index == other.index; return uset == other.uset && c == other.c && index == other.index;
} }
/** @draft ICU 76 */ /** @draft ICU 77 */
bool operator!=(const USetElementIterator &other) const { return !operator==(other); } bool operator!=(const USetElementIterator &other) const { return !operator==(other); }
/** @draft ICU 76 */ /** @draft ICU 77 */
UnicodeString operator*() const { std::u16string operator*() const {
if (c >= 0) { if (c >= 0) {
return UnicodeString(c); return c <= 0xffff ?
std::u16string({static_cast<char16_t>(c)}) :
std::u16string({U16_LEAD(c), U16_TRAIL(c)});
} else if (index < totalCount) { } else if (index < totalCount) {
int32_t length; int32_t length;
const UChar *uchars = uset_getString(uset, index - rangeCount, &length); const UChar *uchars = uset_getString(uset, index - rangeCount, &length);
// assert uchars != nullptr; // assert uchars != nullptr;
return UnicodeString(uchars, length); return {uprv_char16PtrFromUChar(uchars), static_cast<size_t>(length)};
} else { } else {
return UnicodeString(); return {};
} }
} }
/** /**
* Pre-increment. * Pre-increment.
* @draft ICU 76 * @draft ICU 77
*/ */
USetElementIterator &operator++() { USetElementIterator &operator++() {
if (c < end) { if (c < end) {
@ -1800,7 +1807,7 @@ public:
/** /**
* Post-increment. * Post-increment.
* @draft ICU 76 * @draft ICU 77
*/ */
USetElementIterator operator++(int) { USetElementIterator operator++(int) {
USetElementIterator result(*this); USetElementIterator result(*this);
@ -1811,8 +1818,8 @@ public:
private: private:
friend class USetElements; friend class USetElements;
USetElementIterator(const USet *uset, int32_t index, int32_t rangeCount, int32_t totalCount) USetElementIterator(const USet *pUset, int32_t nIndex, int32_t nRangeCount, int32_t nTotalCount)
: uset(uset), index(index), rangeCount(rangeCount), totalCount(totalCount), : uset(pUset), index(nIndex), rangeCount(nRangeCount), totalCount(nTotalCount),
c(U_SENTINEL), end(U_SENTINEL) { c(U_SENTINEL), end(U_SENTINEL) {
if (index < rangeCount) { if (index < rangeCount) {
// Fetch the first range. // Fetch the first range.
@ -1840,7 +1847,7 @@ private:
/** /**
* A C++ "range" for iterating over all of the elements of a USet. * A C++ "range" for iterating over all of the elements of a USet.
* Convenient all-in one iteration, but creates a UnicodeString for each * Convenient all-in one iteration, but creates a std::u16string for each
* code point or string. * code point or string.
* *
* Code points are returned first, then empty and multi-character strings. * Code points are returned first, then empty and multi-character strings.
@ -1849,15 +1856,18 @@ private:
* using U_HEADER_NESTED_NAMESPACE::USetElements; * using U_HEADER_NESTED_NAMESPACE::USetElements;
* LocalUSetPointer uset(uset_openPattern(u"[abcçカ🚴{}{abc}{de}]", -1, &errorCode)); * LocalUSetPointer uset(uset_openPattern(u"[abcçカ🚴{}{abc}{de}]", -1, &errorCode));
* for (auto el : USetElements(uset.getAlias())) { * for (auto el : USetElements(uset.getAlias())) {
* std::string u8; * int32_t len32 = el.length();
* printf("uset.string length %ld \"%s\"\n", (long)el.length(), el.toUTF8String(u8).c_str()); * char utf8[200];
* u_strToUTF8WithSub(utf8, int32_t{sizeof(utf8) - 1}, nullptr,
* el.data(), len32, 0xFFFD, nullptr, errorCode);
* printf("uset.element length %ld \"%s\"\n", long{len32}, utf8);
* } * }
* \endcode * \endcode
* *
* C++ UnicodeSet has member functions for iteration, including begin() and end(). * C++ UnicodeSet has member functions for iteration, including begin() and end().
* *
* @return an all-elements iterator. * @return an all-elements iterator.
* @draft ICU 76 * @draft ICU 77
* @see USetCodePoints * @see USetCodePoints
* @see USetRanges * @see USetRanges
* @see USetStrings * @see USetStrings
@ -1866,21 +1876,21 @@ class USetElements {
public: public:
/** /**
* Constructs a C++ "range" object over all of the elements of the USet. * Constructs a C++ "range" object over all of the elements of the USet.
* @draft ICU 76 * @draft ICU 77
*/ */
USetElements(const USet *uset) USetElements(const USet *pUset)
: uset(uset), rangeCount(uset_getRangeCount(uset)), : uset(pUset), rangeCount(uset_getRangeCount(pUset)),
stringCount(uset_getStringCount(uset)) {} stringCount(uset_getStringCount(pUset)) {}
/** @draft ICU 76 */ /** @draft ICU 77 */
USetElements(const USetElements &other) = default; USetElements(const USetElements &other) = default;
/** @draft ICU 76 */ /** @draft ICU 77 */
USetElementIterator begin() const { USetElementIterator begin() const {
return USetElementIterator(uset, 0, rangeCount, rangeCount + stringCount); return USetElementIterator(uset, 0, rangeCount, rangeCount + stringCount);
} }
/** @draft ICU 76 */ /** @draft ICU 77 */
USetElementIterator end() const { USetElementIterator end() const {
return USetElementIterator(uset, rangeCount + stringCount, rangeCount, rangeCount + stringCount); return USetElementIterator(uset, rangeCount + stringCount, rangeCount, rangeCount + stringCount);
} }

View file

@ -124,7 +124,7 @@
* @internal * @internal
*/ */
U_CAPI UChar32 U_EXPORT2 U_CAPI UChar32 U_EXPORT2
utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict); utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, int8_t strict);
/** /**
* Function for handling "append code point" with error-checking. * Function for handling "append code point" with error-checking.
@ -148,7 +148,7 @@ utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool
* @internal * @internal
*/ */
U_CAPI UChar32 U_EXPORT2 U_CAPI UChar32 U_EXPORT2
utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict); utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, int8_t strict);
/** /**
* Function for handling "skip backward one code point" with error-checking. * Function for handling "skip backward one code point" with error-checking.

View file

@ -598,12 +598,13 @@ typedef enum UErrorCode {
U_MF_DUPLICATE_DECLARATION_ERROR, /**< The same variable is declared in more than one .local or .input declaration. @internal ICU 75 technology preview @deprecated This API is for technology preview only. */ U_MF_DUPLICATE_DECLARATION_ERROR, /**< The same variable is declared in more than one .local or .input declaration. @internal ICU 75 technology preview @deprecated This API is for technology preview only. */
U_MF_OPERAND_MISMATCH_ERROR, /**< An operand provided to a function does not have the required form for that function @internal ICU 75 technology preview @deprecated This API is for technology preview only. */ U_MF_OPERAND_MISMATCH_ERROR, /**< An operand provided to a function does not have the required form for that function @internal ICU 75 technology preview @deprecated This API is for technology preview only. */
U_MF_DUPLICATE_VARIANT_ERROR, /**< A message includes a variant with the same key list as another variant. @internal ICU 76 technology preview @deprecated This API is for technology preview only. */ U_MF_DUPLICATE_VARIANT_ERROR, /**< A message includes a variant with the same key list as another variant. @internal ICU 76 technology preview @deprecated This API is for technology preview only. */
U_MF_BAD_OPTION, /**< An option value provided to a function does not have the required form for that option. @internal ICU 77 technology preview @deprecated This API is for technology preview only. */
#ifndef U_HIDE_DEPRECATED_API #ifndef U_HIDE_DEPRECATED_API
/** /**
* One more than the highest normal formatting API error code. * One more than the highest normal formatting API error code.
* @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420. * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
*/ */
U_FMT_PARSE_ERROR_LIMIT = 0x10120, U_FMT_PARSE_ERROR_LIMIT = 0x10121,
#endif // U_HIDE_DEPRECATED_API #endif // U_HIDE_DEPRECATED_API
/* /*

View file

@ -53,7 +53,7 @@
* This value will change in the subsequent releases of ICU * This value will change in the subsequent releases of ICU
* @stable ICU 2.4 * @stable ICU 2.4
*/ */
#define U_ICU_VERSION_MAJOR_NUM 76 #define U_ICU_VERSION_MAJOR_NUM 77
/** The current ICU minor version as an integer. /** The current ICU minor version as an integer.
* This value will change in the subsequent releases of ICU * This value will change in the subsequent releases of ICU
@ -79,7 +79,7 @@
* This value will change in the subsequent releases of ICU * This value will change in the subsequent releases of ICU
* @stable ICU 2.6 * @stable ICU 2.6
*/ */
#define U_ICU_VERSION_SUFFIX _76 #define U_ICU_VERSION_SUFFIX _77
/** /**
* \def U_DEF2_ICU_ENTRY_POINT_RENAME * \def U_DEF2_ICU_ENTRY_POINT_RENAME
@ -132,7 +132,7 @@
* This value will change in the subsequent releases of ICU * This value will change in the subsequent releases of ICU
* @stable ICU 2.4 * @stable ICU 2.4
*/ */
#define U_ICU_VERSION "76.1" #define U_ICU_VERSION "77.1"
/** /**
* The current ICU library major version number as a string, for library name suffixes. * The current ICU library major version number as a string, for library name suffixes.
@ -145,13 +145,13 @@
* *
* @stable ICU 2.6 * @stable ICU 2.6
*/ */
#define U_ICU_VERSION_SHORT "76" #define U_ICU_VERSION_SHORT "77"
#ifndef U_HIDE_INTERNAL_API #ifndef U_HIDE_INTERNAL_API
/** Data version in ICU4C. /** Data version in ICU4C.
* @internal ICU 4.4 Internal Use Only * @internal ICU 4.4 Internal Use Only
**/ **/
#define U_ICU_DATA_VERSION "76.1" #define U_ICU_DATA_VERSION "77.1"
#endif /* U_HIDE_INTERNAL_API */ #endif /* U_HIDE_INTERNAL_API */
/*=========================================================================== /*===========================================================================

View file

@ -125,7 +125,7 @@ typedef uint8_t UVersionInfo[U_MAX_VERSION_LENGTH];
U_NAMESPACE_USE U_NAMESPACE_USE
# endif # endif
#ifndef U_HIDE_DRAFT_API #ifndef U_FORCE_HIDE_DRAFT_API
/** /**
* \def U_HEADER_NESTED_NAMESPACE * \def U_HEADER_NESTED_NAMESPACE
* Nested namespace used inside U_ICU_NAMESPACE for header-only APIs. * Nested namespace used inside U_ICU_NAMESPACE for header-only APIs.
@ -150,22 +150,37 @@ typedef uint8_t UVersionInfo[U_MAX_VERSION_LENGTH];
* @draft ICU 76 * @draft ICU 76
*/ */
/**
* \def U_ICU_NAMESPACE_OR_INTERNAL
* Namespace used for header-only APIs that used to be regular C++ APIs.
* Different when used inside ICU to prevent public use of internal instantiations.
* Similar to U_HEADER_ONLY_NAMESPACE, but the public definition is the same as U_ICU_NAMESPACE.
* "U_ICU_NAMESPACE" or "U_ICU_NAMESPACE::internal".
*
* @draft ICU 77
*/
// The first test is the same as for defining U_EXPORT for Windows. // The first test is the same as for defining U_EXPORT for Windows.
#if defined(_MSC_VER) || (UPRV_HAS_DECLSPEC_ATTRIBUTE(__dllexport__) && \ #if defined(_MSC_VER) || (UPRV_HAS_DECLSPEC_ATTRIBUTE(__dllexport__) && \
UPRV_HAS_DECLSPEC_ATTRIBUTE(__dllimport__)) UPRV_HAS_DECLSPEC_ATTRIBUTE(__dllimport__))
# define U_HEADER_NESTED_NAMESPACE header # define U_HEADER_NESTED_NAMESPACE header
# define U_ICU_NAMESPACE_OR_INTERNAL U_ICU_NAMESPACE
#elif defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || \ #elif defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || \
defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION) || \ defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION) || \
defined(U_LAYOUTEX_IMPLEMENTATION) || defined(U_TOOLUTIL_IMPLEMENTATION) defined(U_LAYOUTEX_IMPLEMENTATION) || defined(U_TOOLUTIL_IMPLEMENTATION)
# define U_HEADER_NESTED_NAMESPACE internal # define U_HEADER_NESTED_NAMESPACE internal
# define U_ICU_NAMESPACE_OR_INTERNAL U_ICU_NAMESPACE::internal
namespace U_ICU_NAMESPACE_OR_INTERNAL {}
using namespace U_ICU_NAMESPACE_OR_INTERNAL;
#else #else
# define U_HEADER_NESTED_NAMESPACE header # define U_HEADER_NESTED_NAMESPACE header
# define U_ICU_NAMESPACE_OR_INTERNAL U_ICU_NAMESPACE
#endif #endif
#define U_HEADER_ONLY_NAMESPACE U_ICU_NAMESPACE::U_HEADER_NESTED_NAMESPACE #define U_HEADER_ONLY_NAMESPACE U_ICU_NAMESPACE::U_HEADER_NESTED_NAMESPACE
namespace U_HEADER_ONLY_NAMESPACE {} namespace U_HEADER_ONLY_NAMESPACE {}
#endif // U_HIDE_DRAFT_API #endif // U_FORCE_HIDE_DRAFT_API
#endif /* __cplusplus */ #endif /* __cplusplus */

View file

@ -1945,6 +1945,13 @@ UnicodeString::cloneArrayIfNeeded(int32_t newCapacity,
growCapacity = newCapacity; growCapacity = newCapacity;
} else if(newCapacity <= US_STACKBUF_SIZE && growCapacity > US_STACKBUF_SIZE) { } else if(newCapacity <= US_STACKBUF_SIZE && growCapacity > US_STACKBUF_SIZE) {
growCapacity = US_STACKBUF_SIZE; growCapacity = US_STACKBUF_SIZE;
} else if(newCapacity > growCapacity) {
setToBogus();
return false; // bad inputs
}
if(growCapacity > kMaxCapacity) {
setToBogus();
return false;
} }
// save old values // save old values

View file

@ -2716,6 +2716,9 @@ ures_openWithType(UResourceBundle *r, const char* path, const char* localeID,
UResourceDataEntry *entry; UResourceDataEntry *entry;
if(openType != URES_OPEN_DIRECT) { if(openType != URES_OPEN_DIRECT) {
if (localeID == nullptr) {
localeID = uloc_getDefault();
}
/* first "canonicalize" the locale ID */ /* first "canonicalize" the locale ID */
CharString canonLocaleID = ulocimp_getBaseName(localeID, *status); CharString canonLocaleID = ulocimp_getBaseName(localeID, *status);
if(U_FAILURE(*status)) { if(U_FAILURE(*status)) {
@ -3080,6 +3083,9 @@ ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
kwVal.clear(); kwVal.clear();
} }
} }
if (locid == nullptr) {
locid = uloc_getDefault();
}
CharString base = ulocimp_getBaseName(locid, subStatus); CharString base = ulocimp_getBaseName(locid, subStatus);
#if defined(URES_TREE_DEBUG) #if defined(URES_TREE_DEBUG)
fprintf(stderr, "getFunctionalEquivalent: \"%s\" [%s=%s] in %s - %s\n", fprintf(stderr, "getFunctionalEquivalent: \"%s\" [%s=%s] in %s - %s\n",
@ -3244,7 +3250,7 @@ ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
const char *validLoc = ures_getLocaleByType(res, ULOC_VALID_LOCALE, &subStatus); const char *validLoc = ures_getLocaleByType(res, ULOC_VALID_LOCALE, &subStatus);
if (U_SUCCESS(subStatus) && validLoc != nullptr && validLoc[0] != 0 && uprv_strcmp(validLoc, "root") != 0) { if (U_SUCCESS(subStatus) && validLoc != nullptr && validLoc[0] != 0 && uprv_strcmp(validLoc, "root") != 0) {
CharString validLang = ulocimp_getLanguage(validLoc, subStatus); CharString validLang = ulocimp_getLanguage(validLoc, subStatus);
CharString parentLang = ulocimp_getLanguage(parent.data(), subStatus); CharString parentLang = ulocimp_getLanguage(parent.toStringPiece(), subStatus);
if (U_SUCCESS(subStatus) && validLang != parentLang) { if (U_SUCCESS(subStatus) && validLang != parentLang) {
// validLoc is not root and has a different language than parent, use it instead // validLoc is not root and has a different language than parent, use it instead
found.clear().append(validLoc, subStatus); found.clear().append(validLoc, subStatus);

View file

@ -59,6 +59,9 @@ getCodesFromLocale(const char *locale,
if (U_FAILURE(*err)) { return 0; } if (U_FAILURE(*err)) { return 0; }
icu::CharString lang; icu::CharString lang;
icu::CharString script; icu::CharString script;
if (locale == nullptr) {
locale = uloc_getDefault();
}
ulocimp_getSubtags(locale, &lang, &script, nullptr, nullptr, nullptr, *err); ulocimp_getSubtags(locale, &lang, &script, nullptr, nullptr, nullptr, *err);
if (U_FAILURE(*err)) { return 0; } if (U_FAILURE(*err)) { return 0; }
// Multi-script languages, equivalent to the LocaleScript data // Multi-script languages, equivalent to the LocaleScript data

View file

@ -28,6 +28,7 @@
#include "ubidi_props.h" #include "ubidi_props.h"
#include "uassert.h" #include "uassert.h"
#include <limits>
/* /*
* This implementation is designed for 16-bit Unicode strings. * This implementation is designed for 16-bit Unicode strings.
* The main assumption is that the Arabic characters and their * The main assumption is that the Arabic characters and their
@ -747,6 +748,10 @@ handleGeneratedSpaces(char16_t *dest, int32_t sourceLength,
} }
} }
if (static_cast<size_t>(sourceLength) + 1 > std::numeric_limits<size_t>::max() / U_SIZEOF_UCHAR) {
*pErrorCode = U_INDEX_OUTOFBOUNDS_ERROR;
return 0;
}
tempbuffer = static_cast<char16_t*>(uprv_malloc((sourceLength + 1) * U_SIZEOF_UCHAR)); tempbuffer = static_cast<char16_t*>(uprv_malloc((sourceLength + 1) * U_SIZEOF_UCHAR));
/* Test for nullptr */ /* Test for nullptr */
if(tempbuffer == nullptr) { if(tempbuffer == nullptr) {

View file

@ -126,7 +126,7 @@ compareEntries(const UHashTok p1, const UHashTok p2) {
name2.pointer = b2->name; name2.pointer = b2->name;
path1.pointer = b1->path; path1.pointer = b1->path;
path2.pointer = b2->path; path2.pointer = b2->path;
return uhash_compareChars(name1, name2) & uhash_compareChars(path1, path2); return uhash_compareChars(name1, name2) && uhash_compareChars(path1, path2);
} }
static void static void

View file

@ -124,11 +124,9 @@ errorValue(int32_t count, int8_t strict) {
* >0 Obsolete "strict" behavior of UTF8_NEXT_CHAR_SAFE(..., true): * >0 Obsolete "strict" behavior of UTF8_NEXT_CHAR_SAFE(..., true):
* Same as the obsolete "safe" behavior, but non-characters are also treated * Same as the obsolete "safe" behavior, but non-characters are also treated
* like illegal sequences. * like illegal sequences.
*
* Note that a UBool is the same as an int8_t.
*/ */
U_CAPI UChar32 U_EXPORT2 U_CAPI UChar32 U_EXPORT2
utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, UBool strict) { utf8_nextCharSafeBody(const uint8_t *s, int32_t *pi, int32_t length, UChar32 c, int8_t strict) {
// *pi is one after byte c. // *pi is one after byte c.
int32_t i=*pi; int32_t i=*pi;
// length can be negative for NUL-terminated strings: Read and validate one byte at a time. // length can be negative for NUL-terminated strings: Read and validate one byte at a time.
@ -233,7 +231,7 @@ utf8_appendCharSafeBody(uint8_t *s, int32_t i, int32_t length, UChar32 c, UBool
} }
U_CAPI UChar32 U_EXPORT2 U_CAPI UChar32 U_EXPORT2
utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, UBool strict) { utf8_prevCharSafeBody(const uint8_t *s, int32_t start, int32_t *pi, UChar32 c, int8_t strict) {
// *pi is the index of byte c. // *pi is the index of byte c.
int32_t i=*pi; int32_t i=*pi;
if(U8_IS_TRAIL(c) && i>start) { if(U8_IS_TRAIL(c) && i>start) {

View file

@ -140,7 +140,8 @@ _uFmtErrorName[U_FMT_PARSE_ERROR_LIMIT - U_FMT_PARSE_ERROR_START] = {
"U_MF_MISSING_SELECTOR_ANNOTATION_ERROR", "U_MF_MISSING_SELECTOR_ANNOTATION_ERROR",
"U_MF_DUPLICATE_DECLARATION_ERROR", "U_MF_DUPLICATE_DECLARATION_ERROR",
"U_MF_OPERAND_MISMATCH_ERROR", "U_MF_OPERAND_MISMATCH_ERROR",
"U_MF_DUPLICATE_VARIANT_ERROR" "U_MF_DUPLICATE_VARIANT_ERROR",
"U_MF_BAD_OPTION"
}; };
static const char * const static const char * const

View file

@ -285,19 +285,19 @@ uhash_equalsScriptSet(const UElement key1, const UElement key2) {
return (*s1 == *s2); return (*s1 == *s2);
} }
U_CAPI int8_t U_EXPORT2 U_CAPI int32_t U_EXPORT2
uhash_compareScriptSet(UElement key0, UElement key1) { uhash_compareScriptSet(UElement key0, UElement key1) {
icu::ScriptSet *s0 = static_cast<icu::ScriptSet *>(key0.pointer); icu::ScriptSet *s0 = static_cast<icu::ScriptSet *>(key0.pointer);
icu::ScriptSet *s1 = static_cast<icu::ScriptSet *>(key1.pointer); icu::ScriptSet *s1 = static_cast<icu::ScriptSet *>(key1.pointer);
int32_t diff = s0->countMembers() - s1->countMembers(); int32_t diff = s0->countMembers() - s1->countMembers();
if (diff != 0) return static_cast<UBool>(diff); if (diff != 0) return diff;
int32_t i0 = s0->nextSetBit(0); int32_t i0 = s0->nextSetBit(0);
int32_t i1 = s1->nextSetBit(0); int32_t i1 = s1->nextSetBit(0);
while ((diff = i0-i1) == 0 && i0 > 0) { while ((diff = i0-i1) == 0 && i0 > 0) {
i0 = s0->nextSetBit(i0+1); i0 = s0->nextSetBit(i0+1);
i1 = s1->nextSetBit(i1+1); i1 = s1->nextSetBit(i1+1);
} }
return (int8_t)diff; return diff;
} }
U_CAPI int32_t U_EXPORT2 U_CAPI int32_t U_EXPORT2

View file

@ -74,7 +74,7 @@ class U_I18N_API ScriptSet: public UMemory {
U_NAMESPACE_END U_NAMESPACE_END
U_CAPI UBool U_EXPORT2 U_CAPI int32_t U_EXPORT2
uhash_compareScriptSet(const UElement key1, const UElement key2); uhash_compareScriptSet(const UElement key1, const UElement key2);
U_CAPI int32_t U_EXPORT2 U_CAPI int32_t U_EXPORT2

View file

@ -39,6 +39,7 @@ typedef enum ECleanupI18NType {
UCLN_I18N_HEBREW_CALENDAR, UCLN_I18N_HEBREW_CALENDAR,
UCLN_I18N_ASTRO_CALENDAR, UCLN_I18N_ASTRO_CALENDAR,
UCLN_I18N_DANGI_CALENDAR, UCLN_I18N_DANGI_CALENDAR,
UCLN_I18N_PERSIAN_CALENDAR,
UCLN_I18N_CALENDAR, UCLN_I18N_CALENDAR,
UCLN_I18N_TIMEZONEFORMAT, UCLN_I18N_TIMEZONEFORMAT,
UCLN_I18N_TZDBTIMEZONENAMES, UCLN_I18N_TZDBTIMEZONENAMES,
@ -62,6 +63,7 @@ typedef enum ECleanupI18NType {
UCLN_I18N_REGION, UCLN_I18N_REGION,
UCLN_I18N_LIST_FORMATTER, UCLN_I18N_LIST_FORMATTER,
UCLN_I18N_NUMSYS, UCLN_I18N_NUMSYS,
UCLN_I18N_MF2_UNISETS,
UCLN_I18N_COUNT /* This must be last */ UCLN_I18N_COUNT /* This must be last */
} ECleanupI18NType; } ECleanupI18NType;

Binary file not shown.