diff --git a/icu4c/source/common/unicode/unistr.h b/icu4c/source/common/unicode/unistr.h index 00e173862d6..5f785fef942 100644 --- a/icu4c/source/common/unicode/unistr.h +++ b/icu4c/source/common/unicode/unistr.h @@ -364,6 +364,30 @@ public: */ inline bool operator!= (const UnicodeString& text) const; +#ifndef U_HIDE_DRAFT_API + /** + * Inequality operator. Performs only bitwise comparison with `text` + * which is, or which is implicitly convertible to, + * a std::u16string_view or (if U_SIZEOF_WCHAR_T==2) std::wstring_view. + * + * For performance, you can use std::u16string_view literals with compile-time + * length determination: + * \code + * #include <string_view> + * using namespace std::string_view_literals; + * UnicodeString str = ...; + * if (str != u"literal"sv) { ... } + * \endcode + * @param text The string view to compare to this string. + * @return false if `text` contains the same characters as this one, true otherwise. + * @draft ICU 76 + */ + template>> + inline bool operator!=(const S &text) const { + return !operator==(text); + } +#endif // U_HIDE_DRAFT_API + /** * Greater than operator. Performs only bitwise comparison. * @param text The UnicodeString to compare to this one. diff --git a/icu4c/source/test/intltest/ustrtest.cpp b/icu4c/source/test/intltest/ustrtest.cpp index 7a34b8a2f56..61bf20d1089 100644 --- a/icu4c/source/test/intltest/ustrtest.cpp +++ b/icu4c/source/test/intltest/ustrtest.cpp @@ -2453,6 +2453,14 @@ void UnicodeStringTest::TestU16StringView() { assertFalse("any == string-str16", any == str16); assertTrue("any == string-any", any == u"any"s); + // operator!= + assertTrue("any != pointer-p16", any != p16); + assertFalse("any != pointer-any", any != u"any"); + assertTrue("any != string_view-sv16", any != sv16); + assertFalse("any != string_view-any", any != u"any"sv); + assertTrue("any != string-str16", any != str16); + assertFalse("any != string-any", any != u"any"s); + // Assignment copies the string contents. UnicodeString x; x = p16; @@ -2541,6 +2549,14 @@ void UnicodeStringTest::TestWStringView() { assertFalse("any == string-str16", any == str16); assertTrue("any == string-any", any == L"any"s); + // operator!= + assertTrue("any != pointer-p16", any != p16); + assertFalse("any != pointer-any", any != L"any"); + assertTrue("any != string_view-sv16", any != sv16); + assertFalse("any != string_view-any", any != L"any"sv); + assertTrue("any != string-str16", any != str16); + assertFalse("any != string-any", any != L"any"s); + // Assignment copies the string contents. UnicodeString x; x = p16;