diff --git a/icu4c/source/common/unicode/unistr.h b/icu4c/source/common/unicode/unistr.h index a2ce193c455..c5389d68adc 100644 --- a/icu4c/source/common/unicode/unistr.h +++ b/icu4c/source/common/unicode/unistr.h @@ -57,14 +57,35 @@ class StringSearch; * @stable */ #if U_SIZEOF_WCHAR_T==U_SIZEOF_UCHAR && U_CHARSET_FAMILY==U_ASCII_FAMILY -# define UNICODE_STRING(cs, length) UnicodeString(TRUE, (UChar *)L ## cs, length) +# define UNICODE_STRING(cs, length) UnicodeString(TRUE, (const UChar *)L ## cs, length) #elif U_SIZEOF_UCHAR==1 && U_CHARSET_FAMILY==U_ASCII_FAMILY -# define UNICODE_STRING(cs, length) UnicodeString(TRUE, (UChar *)cs, length) +# define UNICODE_STRING(cs, length) UnicodeString(TRUE, (const UChar *)cs, length) #else # define UNICODE_STRING(cs, length) UnicodeString(cs, length, "") #endif /** + * Unicode String literals in C++. + * Dependent on the platform properties, different UnicodeString + * constructors should be used to create a UnicodeString object from + * a string literal. + * The macros are defined for improved performance. + * They work only for strings that contain "invariant characters", i.e., + * only latin letters, digits, and some punctuation. + * See utypes.h for details. + * + * The string parameter must be a C string literal. + * @stable + */ +#if U_SIZEOF_WCHAR_T==U_SIZEOF_UCHAR && U_CHARSET_FAMILY==U_ASCII_FAMILY +# define UNICODE_STRING_SIMPLE(cs) UnicodeString((const UChar *)L ## cs) +#elif U_SIZEOF_UCHAR==1 && U_CHARSET_FAMILY==U_ASCII_FAMILY +# define UNICODE_STRING_SIMPLE(cs) UnicodeString((const UChar *)cs) +#else +# define UNICODE_STRING_SIMPLE(cs) UnicodeString(cs, "") +#endif + + /** * UnicodeString is a string class that stores Unicode characters directly and provides * similar functionality as the Java String class. * It is a concrete implementation of the abstract class Replaceable (for transliteration). diff --git a/icu4c/source/i18n/cpdtrans.cpp b/icu4c/source/i18n/cpdtrans.cpp index 3b247d73747..0958b8eb69f 100644 --- a/icu4c/source/i18n/cpdtrans.cpp +++ b/icu4c/source/i18n/cpdtrans.cpp @@ -97,7 +97,7 @@ CompoundTransliterator::CompoundTransliterator(const UnicodeString& newID, CompoundTransliterator::CompoundTransliterator(UTransDirection dir, UVector& list, UErrorCode& status) : - Transliterator(UnicodeString("", ""), 0), + Transliterator(UnicodeString(), 0), trans(0), compoundRBTIndex(-1) { init(list, dir, FALSE, status); diff --git a/icu4c/source/i18n/nortrans.cpp b/icu4c/source/i18n/nortrans.cpp index cd64b6412b1..dfd6fc7d227 100644 --- a/icu4c/source/i18n/nortrans.cpp +++ b/icu4c/source/i18n/nortrans.cpp @@ -129,18 +129,18 @@ void NormalizationTransliterator::registerIDs() { return; } - Transliterator::_registerFactory(UnicodeString("Any-NFC", ""), + Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-NFC"), _create, integerToken(UNORM_NFC)); - Transliterator::_registerFactory(UnicodeString("Any-NFKC", ""), + Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-NFKC"), _create, integerToken(UNORM_NFKC)); - Transliterator::_registerFactory(UnicodeString("Any-NFD", ""), + Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-NFD"), _create, integerToken(UNORM_NFD)); - Transliterator::_registerFactory(UnicodeString("Any-NFKD", ""), + Transliterator::_registerFactory(UNICODE_STRING_SIMPLE("Any-NFKD"), _create, integerToken(UNORM_NFKD)); - Transliterator::_registerSpecialInverse(UnicodeString("NFC", ""), - UnicodeString("NFD", ""), TRUE); - Transliterator::_registerSpecialInverse(UnicodeString("NFKC", ""), - UnicodeString("NFKD", ""), TRUE); + Transliterator::_registerSpecialInverse(UNICODE_STRING_SIMPLE("NFC"), + UNICODE_STRING_SIMPLE("NFD"), TRUE); + Transliterator::_registerSpecialInverse(UNICODE_STRING_SIMPLE("NFKC"), + UNICODE_STRING_SIMPLE("NFKD"), TRUE); } /** diff --git a/icu4c/source/i18n/rbt_rule.cpp b/icu4c/source/i18n/rbt_rule.cpp index bcce2fb5a66..4a9564da5e4 100644 --- a/icu4c/source/i18n/rbt_rule.cpp +++ b/icu4c/source/i18n/rbt_rule.cpp @@ -697,7 +697,7 @@ UnicodeString& TransliterationRule::toRule(UnicodeString& rule, rule.append((UChar)36/*$*/); } - appendToRule(rule, UnicodeString(" > ", ""), TRUE, escapeUnprintable, quoteBuf); + appendToRule(rule, UNICODE_STRING_SIMPLE(" > "), TRUE, escapeUnprintable, quoteBuf); // Emit the output pattern diff --git a/icu4c/source/i18n/remtrans.cpp b/icu4c/source/i18n/remtrans.cpp index 1dbd9c5b5bc..f864771cdda 100644 --- a/icu4c/source/i18n/remtrans.cpp +++ b/icu4c/source/i18n/remtrans.cpp @@ -20,8 +20,8 @@ void RemoveTransliterator::registerIDs() { Transliterator::_registerFactory(::ID, _create, integerToken(0)); - Transliterator::_registerSpecialInverse(UnicodeString("Remove", ""), - UnicodeString("Null", ""), FALSE); + Transliterator::_registerSpecialInverse(UNICODE_STRING_SIMPLE("Remove"), + UNICODE_STRING_SIMPLE("Null"), FALSE); } /** diff --git a/icu4c/source/i18n/titletrn.cpp b/icu4c/source/i18n/titletrn.cpp index 40f4c407700..62c3208fc92 100644 --- a/icu4c/source/i18n/titletrn.cpp +++ b/icu4c/source/i18n/titletrn.cpp @@ -96,8 +96,8 @@ void TitlecaseTransliterator::handleTransliterate( Mutex lock(&MUTEX); if (SKIP == NULL) { UErrorCode ec = U_ZERO_ERROR; - SKIP = new UnicodeSet(UnicodeString("[\\u00AD \\u2019 \\' [:Mn:] [:Me:] [:Cf:] [:Lm:] [:Sk:]]", ""), ec); - CASED = new UnicodeSet(UnicodeString("[[:Lu:] [:Ll:] [:Lt:]]", ""), ec); + SKIP = new UnicodeSet(UNICODE_STRING_SIMPLE("[\\u00AD \\u2019 \\' [:Mn:] [:Me:] [:Cf:] [:Lm:] [:Sk:]]"), ec); + CASED = new UnicodeSet(UNICODE_STRING_SIMPLE("[[:Lu:] [:Ll:] [:Lt:]]"), ec); ucln_i18n_registerCleanup(); } } diff --git a/icu4c/source/i18n/translit.cpp b/icu4c/source/i18n/translit.cpp index be9ccaaf396..11866ba63c0 100644 --- a/icu4c/source/i18n/translit.cpp +++ b/icu4c/source/i18n/translit.cpp @@ -1018,7 +1018,7 @@ UnicodeString& Transliterator::toRules(UnicodeString& rulesSource, rulesSource = getID(); } // KEEP in sync with rbt_pars - rulesSource.insert(0, UnicodeString("::", "")); + rulesSource.insert(0, UNICODE_STRING_SIMPLE("::")); rulesSource.append(ID_DELIM); return rulesSource; }