Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICU-22954 USet C++ iter samples no UnicodeString #3316

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion icu4c/source/common/unicode/uniset.h
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,7 @@ class U_COMMON_API UnicodeSet final : public UnicodeFilter {
* for (auto el : set) {
* UnicodeString us(el);
* std::string u8;
* printf("set.string length %ld \"%s\"\n", (long)us.length(), us.toUTF8String(u8).c_str());
* printf("set.element length %ld \"%s\"\n", (long)us.length(), us.toUTF8String(u8).c_str());
* }
* \endcode
*
Expand Down
14 changes: 8 additions & 6 deletions icu4c/source/common/unicode/uset.h
Original file line number Diff line number Diff line change
Expand Up @@ -1700,9 +1700,10 @@ class USetStringIterator {
* using U_HEADER_NESTED_NAMESPACE::USetStrings;
* LocalUSetPointer uset(uset_openPattern(u"[abcçカ🚴{}{abc}{de}]", -1, &errorCode));
* for (auto s : USetStrings(uset.getAlias())) {
* UnicodeString us(s);
* std::string u8;
* printf("uset.string length %ld \"%s\"\n", (long)s.length(), us.toUTF8String(u8).c_str());
* int32_t len32 = s.length();
* char utf8[200];
* u_strToUTF8WithSub(utf8, 199, nullptr, s.data(), len32, 0xFFFD, nullptr, errorCode);
Copy link
Member

@roubert roubert Dec 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering how many of these Frank has fixed just in the past few years, to correct off-by-one errors and hardcoded lengths forgotten when buffer sizes were changed, I think it's a really good idea to use UPRV_LENGTHOF or sizeof expressions even in commented-out sample code.

* printf("uset.string length %ld \"%s\"\n", long{len32}, utf8);
* }
* \endcode
*
Expand Down Expand Up @@ -1854,9 +1855,10 @@ class USetElementIterator {
* using U_HEADER_NESTED_NAMESPACE::USetElements;
* LocalUSetPointer uset(uset_openPattern(u"[abcçカ🚴{}{abc}{de}]", -1, &errorCode));
* for (auto el : USetElements(uset.getAlias())) {
* UnicodeString us(el);
* std::string u8;
* printf("uset.string length %ld \"%s\"\n", (long)us.length(), us.toUTF8String(u8).c_str());
* int32_t len32 = el.length();
* char utf8[200];
* u_strToUTF8WithSub(utf8, 199, nullptr, el.data(), len32, 0xFFFD, nullptr, errorCode);
* printf("uset.element length %ld \"%s\"\n", long{len32}, utf8);
* }
* \endcode
*
Expand Down
27 changes: 14 additions & 13 deletions icu4c/source/test/intltest/usetheaderonlytest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "unicode/utypes.h"
#include "unicode/uset.h"
// #include "unicode/ustring.h"
#include "unicode/utf.h"
#include "unicode/utf16.h"
#include "intltest.h"
Expand Down Expand Up @@ -62,9 +63,9 @@ void USetHeaderOnlyTest::TestUSetCodePointIterator() {
uset_openPattern(u"[abcçカ🚴]", -1, errorCode), &uset_close);
std::u16string result;
for (UChar32 c : USetCodePoints(uset.get())) {
result.append(u" ").append(cpString(c));
// Commented-out sample code for pasting into the API docs.
// printf("uset.codePoint U+%04lx\n", (long)c);
result.append(u" ").append(cpString(c));
}
assertEquals(WHERE, u" a b c ç カ 🚴", result);

Expand Down Expand Up @@ -95,17 +96,17 @@ void USetHeaderOnlyTest::TestUSetRangeIterator() {
uset_openPattern(u"[abcçカ🚴]", -1, errorCode), &uset_close);
std::u16string result;
for (auto [start, end] : USetRanges(uset.get())) {
result.append(u" ").append(cpString(start)).append(u"-").append(cpString(end));
// Commented-out sample code for pasting into the API docs.
// printf("uset.range U+%04lx..U+%04lx\n", (long)start, (long)end);
result.append(u" ").append(cpString(start)).append(u"-").append(cpString(end));
}
assertEquals(WHERE, u" a-c ç-ç カ-カ 🚴-🚴", result);
result.clear();
for (auto range : USetRanges(uset.get())) {
for (UChar32 c : range) {
result.append(u" ").append(cpString(c));
// Commented-out sample code for pasting into the API docs.
// printf("uset.range.c U+%04lx\n", (long)c);
result.append(u" ").append(cpString(c));
}
result.append(u" |");
}
Expand Down Expand Up @@ -162,12 +163,12 @@ void USetHeaderOnlyTest::TestUSetStringIterator() {
uset_openPattern(u"[abcçカ🚴{}{abc}{de}]", -1, errorCode), &uset_close);
std::u16string result;
for (auto s : USetStrings(uset.get())) {
// Commented-out sample code for pasting into the API docs.
// Needs U_SHOW_CPLUSPLUS_API=1 for UnicodeString.
// UnicodeString us(s);
// std::string u8;
// printf("uset.string length %ld \"%s\"\n", (long)s.length(), us.toUTF8String(u8).c_str());
result.append(u" \"").append(s).append(u"\"");
// Commented-out sample code for pasting into the API docs.
// int32_t len32 = s.length();
// char utf8[200];
// u_strToUTF8WithSub(utf8, 199, nullptr, s.data(), len32, 0xFFFD, nullptr, errorCode);
// printf("uset.string length %ld \"%s\"\n", long{len32}, utf8);
}
assertEquals(WHERE, uR"( "" "abc" "de")", result);

Expand All @@ -193,12 +194,12 @@ void USetHeaderOnlyTest::TestUSetElementIterator() {
uset_openPattern(u"[abcçカ🚴{}{abc}{de}]", -1, errorCode), &uset_close);
std::u16string result;
for (auto el : USetElements(uset.get())) {
// Commented-out sample code for pasting into the API docs.
// Needs U_SHOW_CPLUSPLUS_API=1 for UnicodeString.
// UnicodeString us(el);
// std::string u8;
// printf("uset.string length %ld \"%s\"\n", (long)us.length(), us.toUTF8String(u8).c_str());
result.append(u" \"").append(el).append(u"\"");
// Commented-out sample code for pasting into the API docs.
// int32_t len32 = el.length();
// char utf8[200];
// u_strToUTF8WithSub(utf8, 199, nullptr, el.data(), len32, 0xFFFD, nullptr, errorCode);
// printf("uset.element length %ld \"%s\"\n", long{len32}, utf8);
}
assertEquals(WHERE, uR"( "a" "b" "c" "ç" "カ" "🚴" "" "abc" "de")", result);

Expand Down
2 changes: 1 addition & 1 deletion icu4c/source/test/intltest/usettest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4326,7 +4326,7 @@ void UnicodeSetTest::TestElementIterator() {
for (auto el : set) {
// UnicodeString us(el);
// std::string u8;
// printf("set.string length %ld \"%s\"\n", (long)us.length(), us.toUTF8String(u8).c_str());
// printf("set.element length %ld \"%s\"\n", (long)us.length(), us.toUTF8String(u8).c_str());
result.append(u" \"").append(el).append(u'"');
}
assertEquals(WHERE, uR"( "a" "b" "c" "ç" "カ" "🚴" "" "abc" "de")", result);
Expand Down