Skip to content

Commit

Permalink
ICU-22696 Update ulocimp_getKeywordValue() to use std::string_view.
Browse files Browse the repository at this point in the history
  • Loading branch information
roubert committed Jul 31, 2024
1 parent 3663cc1 commit dd65ee3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
5 changes: 4 additions & 1 deletion icu4c/source/common/locdispnames.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,10 @@ uloc_getDisplayKeywordValue( const char* locale,
}

/* get the keyword value */
CharString keywordValue = ulocimp_getKeywordValue(locale, keyword, *status);
CharString keywordValue;
if (keyword != nullptr && *keyword != '\0') {
keywordValue = ulocimp_getKeywordValue(locale, keyword, *status);
}

/*
* if the keyword is equal to currency .. then to get the display name
Expand Down
8 changes: 1 addition & 7 deletions icu4c/source/common/locid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2597,13 +2597,7 @@ Locale::getKeywordValue(StringPiece keywordName, ByteSink& sink, UErrorCode& sta
return;
}

// TODO: Remove the need for a const char* to a NUL terminated buffer.
const CharString keywordName_nul(keywordName, status);
if (U_FAILURE(status)) {
return;
}

ulocimp_getKeywordValue(fullName, keywordName_nul.data(), sink, status);
ulocimp_getKeywordValue(fullName, keywordName, sink, status);
}

void
Expand Down
3 changes: 2 additions & 1 deletion icu4c/source/common/loclikely.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* that then do not depend on resource bundle code and likely-subtags data.
*/

#include <string_view>
#include <utility>

#include "unicode/bytestream.h"
Expand Down Expand Up @@ -388,7 +389,7 @@ U_NAMESPACE_END

namespace {
icu::CharString
GetRegionFromKey(const char* localeID, const char* key, UErrorCode& status) {
GetRegionFromKey(const char* localeID, std::string_view key, UErrorCode& status) {
icu::CharString result;

// First check for keyword value
Expand Down
20 changes: 13 additions & 7 deletions icu4c/source/common/uloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/

#include <optional>
#include <string_view>

#include "unicode/bytestream.h"
#include "unicode/errorcode.h"
Expand Down Expand Up @@ -551,17 +552,17 @@ namespace {
* @param status return status (keyword too long)
* @return the keyword name
*/
CharString locale_canonKeywordName(const char* keywordName, UErrorCode& status)
CharString locale_canonKeywordName(std::string_view keywordName, UErrorCode& status)
{
if (U_FAILURE(status)) { return {}; }
CharString result;

for (; *keywordName != 0; keywordName++) {
if (!UPRV_ISALPHANUM(*keywordName)) {
for (char c : keywordName) {
if (!UPRV_ISALPHANUM(c)) {
status = U_ILLEGAL_ARGUMENT_ERROR; /* malformed keyword name */
return {};
}
result.append(uprv_tolower(*keywordName), status);
result.append(uprv_tolower(c), status);
}
if (result.isEmpty()) {
status = U_ILLEGAL_ARGUMENT_ERROR; /* empty keyword name */
Expand Down Expand Up @@ -733,6 +734,11 @@ uloc_getKeywordValue(const char* localeID,
char* buffer, int32_t bufferCapacity,
UErrorCode* status)
{
if (U_FAILURE(*status)) { return 0; }
if (keywordName == nullptr || *keywordName == '\0') {
*status = U_ILLEGAL_ARGUMENT_ERROR;
return 0;
}
return ByteSinkUtil::viaByteSinkToTerminatedChars(
buffer, bufferCapacity,
[&](ByteSink& sink, UErrorCode& status) {
Expand All @@ -743,7 +749,7 @@ uloc_getKeywordValue(const char* localeID,

U_EXPORT CharString
ulocimp_getKeywordValue(const char* localeID,
const char* keywordName,
std::string_view keywordName,
UErrorCode& status)
{
return ByteSinkUtil::viaByteSinkToCharString(
Expand All @@ -755,13 +761,13 @@ ulocimp_getKeywordValue(const char* localeID,

U_EXPORT void
ulocimp_getKeywordValue(const char* localeID,
const char* keywordName,
std::string_view keywordName,
icu::ByteSink& sink,
UErrorCode& status)
{
if (U_FAILURE(status)) { return; }

if (localeID == nullptr || keywordName == nullptr || keywordName[0] == 0) {
if (localeID == nullptr || keywordName.empty()) {
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
Expand Down
5 changes: 3 additions & 2 deletions icu4c/source/common/ulocimp.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define ULOCIMP_H

#include <cstddef>
#include <string_view>

#include "unicode/bytestream.h"
#include "unicode/uloc.h"
Expand Down Expand Up @@ -95,12 +96,12 @@ ulocimp_canonicalize(const char* localeID,

U_EXPORT icu::CharString
ulocimp_getKeywordValue(const char* localeID,
const char* keywordName,
std::string_view keywordName,
UErrorCode& status);

U_EXPORT void
ulocimp_getKeywordValue(const char* localeID,
const char* keywordName,
std::string_view keywordName,
icu::ByteSink& sink,
UErrorCode& status);

Expand Down
9 changes: 6 additions & 3 deletions icu4c/source/common/uresbund.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3074,9 +3074,12 @@ ures_getFunctionalEquivalent(char *result, int32_t resultCapacity,
UErrorCode subStatus = U_ZERO_ERROR;
int32_t length = 0;
if(U_FAILURE(*status)) return 0;
CharString kwVal = ulocimp_getKeywordValue(locid, keyword, subStatus);
if(kwVal == DEFAULT_TAG) {
kwVal.clear();
CharString kwVal;
if (keyword != nullptr && *keyword != '\0') {
kwVal = ulocimp_getKeywordValue(locid, keyword, subStatus);
if (kwVal == DEFAULT_TAG) {
kwVal.clear();
}
}
CharString base = ulocimp_getBaseName(locid, subStatus);
#if defined(URES_TREE_DEBUG)
Expand Down

0 comments on commit dd65ee3

Please sign in to comment.