From 6f932b744dd7c11bffd97588aadb6b73558c3697 Mon Sep 17 00:00:00 2001 From: Fredrik Roubert Date: Mon, 19 Nov 2018 15:12:05 +0100 Subject: [PATCH] ICU-20276 Accept empty strings in Locale::setUnicodeKeywordValue(). The API documentation is perfectly clear about this, an empty string for the value means that the keyword should be removed: @param keywordValue value of the keyword to be set. If 0-length or NULL, will result in the keyword being removed. No error is given if that keyword does not exist. --- icu4c/source/common/locid.cpp | 14 +++++++++----- icu4c/source/test/intltest/loctest.cpp | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/icu4c/source/common/locid.cpp b/icu4c/source/common/locid.cpp index 6d38e4668bf..72224c2f982 100644 --- a/icu4c/source/common/locid.cpp +++ b/icu4c/source/common/locid.cpp @@ -1448,12 +1448,16 @@ Locale::setUnicodeKeywordValue(StringPiece keywordName, return; } - const char* legacy_value = - uloc_toLegacyType(keywordName_nul.data(), keywordValue_nul.data()); + const char* legacy_value = nullptr; - if (legacy_value == nullptr) { - status = U_ILLEGAL_ARGUMENT_ERROR; - return; + if (!keywordValue_nul.isEmpty()) { + legacy_value = + uloc_toLegacyType(keywordName_nul.data(), keywordValue_nul.data()); + + if (legacy_value == nullptr) { + status = U_ILLEGAL_ARGUMENT_ERROR; + return; + } } setKeywordValue(legacy_key, legacy_value, status); diff --git a/icu4c/source/test/intltest/loctest.cpp b/icu4c/source/test/intltest/loctest.cpp index 08b3c6af020..11240de213a 100644 --- a/icu4c/source/test/intltest/loctest.cpp +++ b/icu4c/source/test/intltest/loctest.cpp @@ -1969,6 +1969,26 @@ LocaleTest::TestSetUnicodeKeywordValueStringPiece(void) { static const char expected[] = "de@calendar=buddhist;collation=phonebook"; assertEquals("", expected, l.getName()); + + l.setUnicodeKeywordValue("cu", nullptr, status); + status.errIfFailureAndReset(); + assertEquals("", expected, l.getName()); + + l.setUnicodeKeywordValue("!!", nullptr, status); + assertEquals("status", U_ILLEGAL_ARGUMENT_ERROR, status.reset()); + assertEquals("", expected, l.getName()); + + l.setUnicodeKeywordValue("co", "!!", status); + assertEquals("status", U_ILLEGAL_ARGUMENT_ERROR, status.reset()); + assertEquals("", expected, l.getName()); + + l.setUnicodeKeywordValue("co", nullptr, status); + status.errIfFailureAndReset(); + + l.setUnicodeKeywordValue("ca", "", status); + status.errIfFailureAndReset(); + + assertEquals("", Locale::getGerman().getName(), l.getName()); } void -- 2.49.0