From: Fredrik Roubert Date: Sat, 15 Sep 2018 00:12:06 +0000 (-0700) Subject: ICU-13417 Add the Locale::get(Unicode)?Keywords() functions. X-Git-Tag: release-63-rc~66 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=46e08431a6d093fb69426536bd9e872f1fbc8004;p=icu ICU-13417 Add the Locale::get(Unicode)?Keywords() functions. They are C++ template wrappers around Locale::createKeywords() and Locale::createUnicodeKeywords() respectively, that write to any container for which an STL style output iterator can be provided. The simplest imaginable usage would just look like this: std::string keys[16]; l.getKeywords(keys, status); The unit test has a more elaborate invocation, writing to an std::set<>. --- diff --git a/icu4c/source/common/unicode/locid.h b/icu4c/source/common/unicode/locid.h index f570391b1bc..f1985667b8a 100644 --- a/icu4c/source/common/unicode/locid.h +++ b/icu4c/source/common/unicode/locid.h @@ -32,6 +32,8 @@ #define LOCID_H #include "unicode/bytestream.h" +#include "unicode/localpointer.h" +#include "unicode/strenum.h" #include "unicode/stringpiece.h" #include "unicode/utypes.h" #include "unicode/uobject.h" @@ -553,6 +555,7 @@ public: * @param status the status code * @return pointer to StringEnumeration class, or NULL if there are no keywords. * Client must dispose of it by calling delete. + * @see getKeywords * @stable ICU 2.8 */ StringEnumeration * createKeywords(UErrorCode &status) const; @@ -565,10 +568,41 @@ public: * @param status the status code * @return pointer to StringEnumeration class, or NULL if there are no keywords. * Client must dispose of it by calling delete. + * @see getUnicodeKeywords * @draft ICU 63 */ StringEnumeration * createUnicodeKeywords(UErrorCode &status) const; + /** + * Gets the set of keywords for this Locale. + * + * A wrapper to call createKeywords() and write the resulting + * keywords as standard strings (or compatible objects) into any kind of + * container that can be written to by an STL style output iterator. + * + * @param iterator an STL style output iterator to write the keywords to. + * @param status error information if creating set of keywords failed. + * @return a set of strings with all keywords. + * @draft ICU 63 + */ + template + inline void getKeywords(OutputIterator iterator, UErrorCode& status) const; + + /** + * Gets the set of Unicode keywords for this Locale. + * + * A wrapper to call createUnicodeKeywords() and write the resulting + * keywords as standard strings (or compatible objects) into any kind of + * container that can be written to by an STL style output iterator. + * + * @param iterator an STL style output iterator to write the keywords to. + * @param status error information if creating set of keywords failed. + * @return a set of strings with all keywords. + * @draft ICU 63 + */ + template + inline void getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const; + #endif // U_HIDE_DRAFT_API /** @@ -1062,6 +1096,40 @@ Locale::getName() const #ifndef U_HIDE_DRAFT_API +template inline void +Locale::getKeywords(OutputIterator iterator, UErrorCode& status) const +{ + LocalPointer keys(createKeywords(status)); + if (U_FAILURE(status)) { + return; + } + for (;;) { + int32_t resultLength; + const char* buffer = keys->next(&resultLength, status); + if (U_FAILURE(status) || buffer == nullptr) { + return; + } + *iterator++ = StringClass(buffer, resultLength); + } +} + +template inline void +Locale::getUnicodeKeywords(OutputIterator iterator, UErrorCode& status) const +{ + LocalPointer keys(createUnicodeKeywords(status)); + if (U_FAILURE(status)) { + return; + } + for (;;) { + int32_t resultLength; + const char* buffer = keys->next(&resultLength, status); + if (U_FAILURE(status) || buffer == nullptr) { + return; + } + *iterator++ = StringClass(buffer, resultLength); + } +} + template inline StringClass Locale::getKeywordValue(StringPiece keywordName, UErrorCode& status) const { diff --git a/icu4c/source/test/intltest/loctest.cpp b/icu4c/source/test/intltest/loctest.cpp index c86db1bed33..0ca3e40e48c 100644 --- a/icu4c/source/test/intltest/loctest.cpp +++ b/icu4c/source/test/intltest/loctest.cpp @@ -6,6 +6,9 @@ * others. All Rights Reserved. ********************************************************************/ +#include +#include + #include "loctest.h" #include "unicode/localpointer.h" #include "unicode/decimfmt.h" @@ -226,6 +229,8 @@ void LocaleTest::runIndexedTest( int32_t index, UBool exec, const char* &name, c TESTCASE_AUTO(TestKeywordVariants); TESTCASE_AUTO(TestCreateUnicodeKeywords); TESTCASE_AUTO(TestKeywordVariantParsing); + TESTCASE_AUTO(TestCreateKeywordSet); + TESTCASE_AUTO(TestCreateUnicodeKeywordSet); TESTCASE_AUTO(TestGetKeywordValueStdString); TESTCASE_AUTO(TestGetUnicodeKeywordValueStdString); TESTCASE_AUTO(TestSetKeywordValue); @@ -1824,6 +1829,44 @@ LocaleTest::TestKeywordVariantParsing(void) { } } +void +LocaleTest::TestCreateKeywordSet(void) { + IcuTestErrorCode status(*this, "TestCreateKeywordSet()"); + + static const Locale l("de@calendar=buddhist;collation=phonebook"); + + std::set result; + l.getKeywords( + std::insert_iterator(result, result.begin()), + status); + status.errIfFailureAndReset("\"%s\"", l.getName()); + + assertEquals("set::size()", 2, result.size()); + assertTrue("set::find(\"calendar\")", + result.find("calendar") != result.end()); + assertTrue("set::find(\"collation\")", + result.find("collation") != result.end()); +} + +void +LocaleTest::TestCreateUnicodeKeywordSet(void) { + IcuTestErrorCode status(*this, "TestCreateUnicodeKeywordSet()"); + + static const Locale l("de@calendar=buddhist;collation=phonebook"); + + std::set result; + l.getUnicodeKeywords( + std::insert_iterator(result, result.begin()), + status); + status.errIfFailureAndReset("\"%s\"", l.getName()); + + assertEquals("set::size()", 2, result.size()); + assertTrue("set::find(\"ca\")", + result.find("ca") != result.end()); + assertTrue("set::find(\"co\")", + result.find("co") != result.end()); +} + void LocaleTest::TestGetKeywordValueStdString(void) { IcuTestErrorCode status(*this, "TestGetKeywordValueStdString()"); diff --git a/icu4c/source/test/intltest/loctest.h b/icu4c/source/test/intltest/loctest.h index e93b17df84a..8c8d7da2eb4 100644 --- a/icu4c/source/test/intltest/loctest.h +++ b/icu4c/source/test/intltest/loctest.h @@ -78,6 +78,8 @@ public: /* Test getting keyword values */ void TestKeywordVariantParsing(void); + void TestCreateKeywordSet(void); + void TestCreateUnicodeKeywordSet(void); void TestGetKeywordValueStdString(void); void TestGetUnicodeKeywordValueStdString(void);