From 031be519111a7bf86368ffd6cad96dd67ef9f720 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Wed, 1 Mar 2017 05:52:24 +0000 Subject: [PATCH] ICU-12992 move Char16Ptr to new char16ptr.h; change non-UnicodeString C++ functions from raw pointers to Char16Ptr where possible X-SVN-Rev: 39716 --- icu4c/source/common/Makefile.in | 2 +- icu4c/source/common/char16ptr.cpp | 55 +++ icu4c/source/common/common.vcxproj | 15 + icu4c/source/common/common.vcxproj.filters | 6 + icu4c/source/common/normlzr.cpp | 4 +- icu4c/source/common/ucharstrie.cpp | 3 +- icu4c/source/common/ucharstrieiterator.cpp | 2 +- icu4c/source/common/uchriter.cpp | 8 +- icu4c/source/common/unicode/casemap.h | 17 +- icu4c/source/common/unicode/char16ptr.h | 350 ++++++++++++++++++ icu4c/source/common/unicode/normlzr.h | 4 +- icu4c/source/common/unicode/ucharstrie.h | 6 +- icu4c/source/common/unicode/uchriter.h | 8 +- icu4c/source/common/unicode/unistr.h | 332 +---------------- icu4c/source/common/unistr.cpp | 42 --- .../source/common/ustr_titlecase_brkiter.cpp | 4 +- icu4c/source/common/ustrcase.cpp | 4 +- icu4c/source/common/ustrcase_locale.cpp | 8 +- icu4c/source/i18n/curramt.cpp | 4 +- icu4c/source/i18n/currunit.cpp | 4 +- icu4c/source/i18n/dtfmtsym.cpp | 2 +- icu4c/source/i18n/numfmt.cpp | 2 +- icu4c/source/i18n/smpdtfmt.cpp | 4 +- icu4c/source/i18n/unicode/curramt.h | 8 +- icu4c/source/i18n/unicode/currunit.h | 6 +- icu4c/source/i18n/unicode/dtfmtsym.h | 2 +- icu4c/source/i18n/unicode/numfmt.h | 2 +- icu4c/source/i18n/unum.cpp | 2 +- icu4c/source/test/intltest/dtfmttst.cpp | 2 +- icu4c/source/test/intltest/intltest.cpp | 2 +- icu4c/source/test/intltest/measfmttest.cpp | 2 +- icu4c/source/test/intltest/numfmtst.cpp | 8 +- icu4c/source/test/intltest/tsmthred.cpp | 4 +- 33 files changed, 490 insertions(+), 434 deletions(-) create mode 100644 icu4c/source/common/char16ptr.cpp create mode 100644 icu4c/source/common/unicode/char16ptr.h diff --git a/icu4c/source/common/Makefile.in b/icu4c/source/common/Makefile.in index 10fa8de38eb..5a82fbf7262 100644 --- a/icu4c/source/common/Makefile.in +++ b/icu4c/source/common/Makefile.in @@ -95,7 +95,7 @@ bytestrie.o bytestrieiterator.o \ ucharstrie.o ucharstriebuilder.o ucharstrieiterator.o \ dictionarydata.o \ edits.o \ -appendable.o ustr_cnv.o unistr_cnv.o unistr.o unistr_case.o unistr_props.o \ +char16ptr.o appendable.o ustr_cnv.o unistr_cnv.o unistr.o unistr_case.o unistr_props.o \ utf_impl.o ustring.o ustrcase.o ucasemap.o ucasemap_titlecase_brkiter.o cstring.o ustrfmt.o ustrtrns.o ustr_wcs.o utext.o \ unistr_case_locale.o ustrcase_locale.o unistr_titlecase_brkiter.o ustr_titlecase_brkiter.o \ normalizer2impl.o normalizer2.o filterednormalizer2.o normlzr.o unorm.o unormcmp.o loadednormalizer2impl.o \ diff --git a/icu4c/source/common/char16ptr.cpp b/icu4c/source/common/char16ptr.cpp new file mode 100644 index 00000000000..1da9cd8490c --- /dev/null +++ b/icu4c/source/common/char16ptr.cpp @@ -0,0 +1,55 @@ +// © 2017 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html + +// char16ptr.cpp +// created: 2017feb28 Markus W. Scherer + +#include "unicode/utypes.h" +#include "unicode/char16ptr.h" +#include "uassert.h" + +U_NAMESPACE_BEGIN + +#ifdef U_ALIASING_BARRIER + +Char16Ptr::Char16Ptr(int null) : p(nullptr) { + U_ASSERT(null == 0); + if (null != 0) { + // Try to provoke a crash. + p = reinterpret_cast(1); + } +} + +ConstChar16Ptr::ConstChar16Ptr(int null) : p(nullptr) { + U_ASSERT(null == 0); + if (null != 0) { + // Try to provoke a crash. + p = reinterpret_cast(1); + } +} + +#else + +Char16Ptr::Char16Ptr(int null) { + U_ASSERT(null == 0); + if (null == 0) { + u.cp = nullptr; + } else { + // Try to provoke a crash. + u.cp = reinterpret_cast(1); + } +} + +ConstChar16Ptr::ConstChar16Ptr(int null) { + U_ASSERT(null == 0); + if (null == 0) { + u.cp = nullptr; + } else { + // Try to provoke a crash. + u.cp = reinterpret_cast(1); + } +} + +#endif + +U_NAMESPACE_END diff --git a/icu4c/source/common/common.vcxproj b/icu4c/source/common/common.vcxproj index b735ee9398e..9e760180666 100644 --- a/icu4c/source/common/common.vcxproj +++ b/icu4c/source/common/common.vcxproj @@ -448,6 +448,7 @@ + @@ -1530,6 +1531,20 @@ ..\..\include\unicode\%(Filename)%(Extension);%(Outputs) copy "%(FullPath)" ..\..\include\unicode + + ..\..\include\unicode\%(Filename)%(Extension);%(Outputs) + + + copy "%(FullPath)" ..\..\include\unicode + + ..\..\include\unicode\%(Filename)%(Extension);%(Outputs) + copy "%(FullPath)" ..\..\include\unicode + + ..\..\include\unicode\%(Filename)%(Extension);%(Outputs) + copy "%(FullPath)" ..\..\include\unicode + + ..\..\include\unicode\%(Filename)%(Extension);%(Outputs) + copy "%(FullPath)" ..\..\include\unicode ..\..\include\unicode\%(Filename)%(Extension);%(Outputs) diff --git a/icu4c/source/common/common.vcxproj.filters b/icu4c/source/common/common.vcxproj.filters index 21387cd7508..b29fe2fc4f2 100644 --- a/icu4c/source/common/common.vcxproj.filters +++ b/icu4c/source/common/common.vcxproj.filters @@ -463,6 +463,9 @@ strings + + strings + strings @@ -1105,6 +1108,9 @@ strings + + strings + strings diff --git a/icu4c/source/common/normlzr.cpp b/icu4c/source/common/normlzr.cpp index 4569d742e2d..a1a4949d30a 100644 --- a/icu4c/source/common/normlzr.cpp +++ b/icu4c/source/common/normlzr.cpp @@ -40,7 +40,7 @@ Normalizer::Normalizer(const UnicodeString& str, UNormalizationMode mode) : init(); } -Normalizer::Normalizer(const UChar *str, int32_t length, UNormalizationMode mode) : +Normalizer::Normalizer(ConstChar16Ptr str, int32_t length, UNormalizationMode mode) : UObject(), fFilteredNorm2(NULL), fNorm2(NULL), fUMode(mode), fOptions(0), text(new UCharCharacterIterator(str, length)), currentIndex(0), nextIndex(0), @@ -435,7 +435,7 @@ Normalizer::setText(const CharacterIterator& newText, } void -Normalizer::setText(const UChar* newText, +Normalizer::setText(ConstChar16Ptr newText, int32_t length, UErrorCode &status) { diff --git a/icu4c/source/common/ucharstrie.cpp b/icu4c/source/common/ucharstrie.cpp index b83aec51b2c..e0b33af5194 100644 --- a/icu4c/source/common/ucharstrie.cpp +++ b/icu4c/source/common/ucharstrie.cpp @@ -175,7 +175,8 @@ UCharsTrie::next(int32_t uchar) { } UStringTrieResult -UCharsTrie::next(const UChar *s, int32_t sLength) { +UCharsTrie::next(ConstChar16Ptr ptr, int32_t sLength) { + const UChar *s=ptr; if(sLength<0 ? *s==0 : sLength==0) { // Empty input. return current(); diff --git a/icu4c/source/common/ucharstrieiterator.cpp b/icu4c/source/common/ucharstrieiterator.cpp index 56729951b40..b3132241fe2 100644 --- a/icu4c/source/common/ucharstrieiterator.cpp +++ b/icu4c/source/common/ucharstrieiterator.cpp @@ -21,7 +21,7 @@ U_NAMESPACE_BEGIN -UCharsTrie::Iterator::Iterator(const UChar *trieUChars, int32_t maxStringLength, +UCharsTrie::Iterator::Iterator(ConstChar16Ptr trieUChars, int32_t maxStringLength, UErrorCode &errorCode) : uchars_(trieUChars), pos_(uchars_), initialPos_(uchars_), diff --git a/icu4c/source/common/uchriter.cpp b/icu4c/source/common/uchriter.cpp index 0b3868e67d7..822168f5c8e 100644 --- a/icu4c/source/common/uchriter.cpp +++ b/icu4c/source/common/uchriter.cpp @@ -25,14 +25,14 @@ UCharCharacterIterator::UCharCharacterIterator() // never default construct! } -UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr, +UCharCharacterIterator::UCharCharacterIterator(ConstChar16Ptr textPtr, int32_t length) : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0), text(textPtr) { } -UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr, +UCharCharacterIterator::UCharCharacterIterator(ConstChar16Ptr textPtr, int32_t length, int32_t position) : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0, position), @@ -40,7 +40,7 @@ UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr, { } -UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr, +UCharCharacterIterator::UCharCharacterIterator(ConstChar16Ptr textPtr, int32_t length, int32_t textBegin, int32_t textEnd, @@ -349,7 +349,7 @@ UCharCharacterIterator::move32(int32_t delta, CharacterIterator::EOrigin origin) return pos; } -void UCharCharacterIterator::setText(const UChar* newText, +void UCharCharacterIterator::setText(ConstChar16Ptr newText, int32_t newTextLength) { text = newText; if(newText == 0 || newTextLength < 0) { diff --git a/icu4c/source/common/unicode/casemap.h b/icu4c/source/common/unicode/casemap.h index 2224172118c..1050f52d7e8 100644 --- a/icu4c/source/common/unicode/casemap.h +++ b/icu4c/source/common/unicode/casemap.h @@ -8,6 +8,7 @@ #define __CASEMAP_H__ #include "unicode/utypes.h" +#include "unicode/char16ptr.h" #include "unicode/uobject.h" /** @@ -59,8 +60,8 @@ public: */ static int32_t toLower( const char *locale, uint32_t options, - const UChar *src, int32_t srcLength, - UChar *dest, int32_t destCapacity, Edits *edits, + ConstChar16Ptr src, int32_t srcLength, + Char16Ptr dest, int32_t destCapacity, Edits *edits, UErrorCode &errorCode); /** @@ -93,8 +94,8 @@ public: */ static int32_t toUpper( const char *locale, uint32_t options, - const UChar *src, int32_t srcLength, - UChar *dest, int32_t destCapacity, Edits *edits, + ConstChar16Ptr src, int32_t srcLength, + Char16Ptr dest, int32_t destCapacity, Edits *edits, UErrorCode &errorCode); #if !UCONFIG_NO_BREAK_ITERATION @@ -140,8 +141,8 @@ public: */ static int32_t toTitle( const char *locale, uint32_t options, BreakIterator *iter, - const UChar *src, int32_t srcLength, - UChar *dest, int32_t destCapacity, Edits *edits, + ConstChar16Ptr src, int32_t srcLength, + Char16Ptr dest, int32_t destCapacity, Edits *edits, UErrorCode &errorCode); #endif // UCONFIG_NO_BREAK_ITERATION @@ -180,8 +181,8 @@ public: */ static int32_t fold( uint32_t options, - const UChar *src, int32_t srcLength, - UChar *dest, int32_t destCapacity, Edits *edits, + ConstChar16Ptr src, int32_t srcLength, + Char16Ptr dest, int32_t destCapacity, Edits *edits, UErrorCode &errorCode); private: diff --git a/icu4c/source/common/unicode/char16ptr.h b/icu4c/source/common/unicode/char16ptr.h new file mode 100644 index 00000000000..a949b7d4f54 --- /dev/null +++ b/icu4c/source/common/unicode/char16ptr.h @@ -0,0 +1,350 @@ +// © 2017 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html + +// char16ptr.h +// created: 2017feb28 Markus W. Scherer + +#ifndef __CHAR16PTR_H__ +#define __CHAR16PTR_H__ + +#include +#include "unicode/utypes.h" + +/** + * \file + * \brief C++ API: char16_t pointer wrappers with + * implicit conversion to/from bit-compatible raw pointer types. + */ + +U_NAMESPACE_BEGIN + +/** + * \def U_ALIASING_BARRIER + * Barrier for pointer anti-aliasing optimizations even across function boundaries. + * @internal + */ +#ifdef U_ALIASING_BARRIER + // Use the predefined value. +#elif defined(__clang__) || defined(__GNUC__) +# define U_ALIASING_BARRIER(ptr) asm volatile("" : "+rm"(ptr)) +#endif + +/** + * char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types, + * and from NULL. + * @draft ICU 59 + */ +class U_COMMON_API Char16Ptr final { +public: + /** + * Copies the pointer. + * TODO: @param p ... + * @draft ICU 59 + */ + inline Char16Ptr(char16_t *p); + /** + * Converts the pointer to char16_t *. + * @draft ICU 59 + */ + inline Char16Ptr(uint16_t *p); +#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) + /** + * Converts the pointer to char16_t *. + * (Only defined if U_SIZEOF_WCHAR_T==2.) + * @draft ICU 59 + */ + inline Char16Ptr(wchar_t *p); +#endif + /** + * nullptr constructor. + * @draft ICU 59 + */ + inline Char16Ptr(std::nullptr_t p); + /** + * NULL constructor. + * Must only be used for 0 which is usually the value of NULL. + * @draft ICU 59 + */ + Char16Ptr(int null); + /** + * Destructor. + * @draft ICU 59 + */ + inline ~Char16Ptr(); + + /** + * Pointer access. + * TODO @return ... + * @draft ICU 59 + */ + inline char16_t *get() const; + /** + * char16_t pointer access via type conversion (e.g., static_cast). + * @draft ICU 59 + */ + operator char16_t *() const { return get(); } + /** + * uint16_t pointer access via type conversion (e.g., static_cast). + * @draft ICU 59 + */ + inline operator uint16_t *() const; +#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) + /** + * wchar_t pointer access via type conversion (e.g., static_cast). + * @draft ICU 59 + */ + inline operator wchar_t *() const; +#endif + operator void *() const { return get(); } + + char16_t operator[](size_t offset) const { return get()[offset]; } + + UBool operator==(const Char16Ptr &other) const { return get() == other.get(); } + UBool operator!=(const Char16Ptr &other) const { return !operator==(other); } + UBool operator==(const char16_t *other) const { return get() == other; } + UBool operator!=(const char16_t *other) const { return !operator==(other); } + UBool operator==(const uint16_t *other) const { return static_cast(*this) == other; } + UBool operator!=(const uint16_t *other) const { return !operator==(other); } +#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) + UBool operator==(const wchar_t *other) const { return static_cast(*this) == other; } + UBool operator!=(const wchar_t *other) const { return !operator==(other); } +#endif + UBool operator==(const std::nullptr_t null) const { return get() == null; } + UBool operator!=(const std::nullptr_t null) const { return !operator==(null); } + /** + * Comparison with NULL. + * @return TRUE if the pointer is nullptr and null==0 + * @draft ICU 59 + */ + UBool operator==(int null) const { return get() == nullptr && null == 0; } + /** + * Comparison with NULL. + * @return TRUE if the pointer is not nullptr and null==0 + * @draft ICU 59 + */ + UBool operator!=(int null) const { return get() != nullptr && null == 0; } + + Char16Ptr operator+(size_t offset) const { return Char16Ptr(get() + offset); } + +private: + Char16Ptr() = delete; + +#ifdef U_ALIASING_BARRIER + template static char16_t *cast(T *t) { + U_ALIASING_BARRIER(t); + return reinterpret_cast(t); + } + + char16_t *p; +#else + union { + char16_t *cp; + uint16_t *up; + wchar_t *wp; + } u; +#endif +}; + +#ifdef U_ALIASING_BARRIER + +Char16Ptr::Char16Ptr(char16_t *p) : p(p) {} +Char16Ptr::Char16Ptr(uint16_t *p) : p(cast(p)) {} +#if U_SIZEOF_WCHAR_T==2 +Char16Ptr::Char16Ptr(wchar_t *p) : p(cast(p)) {} +#endif +Char16Ptr::Char16Ptr(std::nullptr_t p) : p(p) {} +Char16Ptr::~Char16Ptr() { + U_ALIASING_BARRIER(p); +} + +char16_t *Char16Ptr::get() const { return p; } + +Char16Ptr::operator uint16_t *() const { + U_ALIASING_BARRIER(p); + return reinterpret_cast(p); +} +#if U_SIZEOF_WCHAR_T==2 +Char16Ptr::operator wchar_t *() const { + U_ALIASING_BARRIER(p); + return reinterpret_cast(p); +} +#endif + +#else + +Char16Ptr::Char16Ptr(char16_t *p) { u.cp = p; } +Char16Ptr::Char16Ptr(uint16_t *p) { u.up = p; } +#if U_SIZEOF_WCHAR_T==2 +Char16Ptr::Char16Ptr(wchar_t *p) { u.wp = p; } +#endif +Char16Ptr::Char16Ptr(std::nullptr_t p) { u.cp = p; } +Char16Ptr::~Char16Ptr() {} + +char16_t *Char16Ptr::get() const { return u.cp; } + +Char16Ptr::operator uint16_t *() const { + return u.up; +} +#if U_SIZEOF_WCHAR_T==2 +Char16Ptr::operator wchar_t *() const { + return u.wp; +} +#endif + +#endif + +/** + * const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types, + * and from NULL. + * @draft ICU 59 + */ +class U_COMMON_API ConstChar16Ptr final { +public: + /** + * Copies the pointer. + * @draft ICU 59 + */ + inline ConstChar16Ptr(const char16_t *p); + /** + * Converts the pointer to char16_t *. + * @draft ICU 59 + */ + inline ConstChar16Ptr(const uint16_t *p); +#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) + /** + * Converts the pointer to char16_t *. + * (Only defined if U_SIZEOF_WCHAR_T==2.) + * @draft ICU 59 + */ + inline ConstChar16Ptr(const wchar_t *p); +#endif + /** + * nullptr constructor. + * @draft ICU 59 + */ + inline ConstChar16Ptr(const std::nullptr_t p); + /** + * NULL constructor. + * Must only be used for 0 which is usually the value of NULL. + * @draft ICU 59 + */ + ConstChar16Ptr(int null); + /** + * Destructor. + * @draft ICU 59 + */ + inline ~ConstChar16Ptr(); + + /** + * Pointer access. + * @draft ICU 59 + */ + inline const char16_t *get() const; + /** + * char16_t pointer access via type conversion (e.g., static_cast). + * @draft ICU 59 + */ + operator const char16_t *() const { return get(); } + /** + * uint16_t pointer access via type conversion (e.g., static_cast). + * @draft ICU 59 + */ + inline operator const uint16_t *() const; +#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) + /** + * wchar_t pointer access via type conversion (e.g., static_cast). + * @draft ICU 59 + */ + inline operator const wchar_t *() const; +#endif + operator const void *() const { return get(); } + + char16_t operator[](size_t offset) const { return get()[offset]; } + + UBool operator==(const ConstChar16Ptr &other) const { return get() == other.get(); } + UBool operator!=(const ConstChar16Ptr &other) const { return !operator==(other); } + UBool operator==(const char16_t *other) const { return get() == other; } + UBool operator!=(const char16_t *other) const { return !operator==(other); } + UBool operator==(const uint16_t *other) const { return static_cast(*this) == other; } + UBool operator!=(const uint16_t *other) const { return !operator==(other); } +#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) + UBool operator==(const wchar_t *other) const { return static_cast(*this) == other; } + UBool operator!=(const wchar_t *other) const { return !operator==(other); } +#endif + UBool operator==(const std::nullptr_t null) const { return get() == null; } + UBool operator!=(const std::nullptr_t null) const { return !operator==(null); } + UBool operator==(int null) const { return get() == nullptr && null == 0; } + UBool operator!=(int null) const { return get() != nullptr && null == 0; } + + ConstChar16Ptr operator+(size_t offset) { return ConstChar16Ptr(get() + offset); } + +private: + ConstChar16Ptr() = delete; + +#ifdef U_ALIASING_BARRIER + template static const char16_t *cast(const T *t) { + U_ALIASING_BARRIER(t); + return reinterpret_cast(t); + } + + const char16_t *p; +#else + union { + const char16_t *cp; + const uint16_t *up; + const wchar_t *wp; + } u; +#endif +}; + +#ifdef U_ALIASING_BARRIER + +ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p(p) {} +ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p(cast(p)) {} +#if U_SIZEOF_WCHAR_T==2 +ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p(cast(p)) {} +#endif +ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p(p) {} +ConstChar16Ptr::~ConstChar16Ptr() { + U_ALIASING_BARRIER(p); +} + +const char16_t *ConstChar16Ptr::get() const { return p; } + +ConstChar16Ptr::operator const uint16_t *() const { + U_ALIASING_BARRIER(p); + return reinterpret_cast(p); +} +#if U_SIZEOF_WCHAR_T==2 +ConstChar16Ptr::operator const wchar_t *() const { + U_ALIASING_BARRIER(p); + return reinterpret_cast(p); +} +#endif + +#else + +ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u.cp = p; } +ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u.up = p; } +#if U_SIZEOF_WCHAR_T==2 +ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u.wp = p; } +#endif +ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u.cp = p; } +ConstChar16Ptr::~ConstChar16Ptr() {} + +const char16_t *ConstChar16Ptr::get() const { return u.cp; } + +ConstChar16Ptr::operator const uint16_t *() const { + return u.up; +} +#if U_SIZEOF_WCHAR_T==2 +ConstChar16Ptr::operator const wchar_t *() const { + return u.wp; +} +#endif + +#endif + +U_NAMESPACE_END + +#endif // __CHAR16PTR_H__ diff --git a/icu4c/source/common/unicode/normlzr.h b/icu4c/source/common/unicode/normlzr.h index 53665e97842..31af07b7cc1 100644 --- a/icu4c/source/common/unicode/normlzr.h +++ b/icu4c/source/common/unicode/normlzr.h @@ -168,7 +168,7 @@ public: * @param mode The normalization mode. * @deprecated ICU 56 Use Normalizer2 instead. */ - Normalizer(const UChar* str, int32_t length, UNormalizationMode mode); + Normalizer(ConstChar16Ptr str, int32_t length, UNormalizationMode mode); /** * Creates a new Normalizer object for iterating over the @@ -704,7 +704,7 @@ public: * @param status a UErrorCode * @deprecated ICU 56 Use Normalizer2 instead. */ - void setText(const UChar* newText, + void setText(ConstChar16Ptr newText, int32_t length, UErrorCode &status); /** diff --git a/icu4c/source/common/unicode/ucharstrie.h b/icu4c/source/common/unicode/ucharstrie.h index 91c5ba1c2c2..fdff53f4d33 100644 --- a/icu4c/source/common/unicode/ucharstrie.h +++ b/icu4c/source/common/unicode/ucharstrie.h @@ -63,7 +63,7 @@ public: * @param trieUChars The UChar array that contains the serialized trie. * @stable ICU 4.8 */ - UCharsTrie(const UChar *trieUChars) + UCharsTrie(ConstChar16Ptr trieUChars) : ownedArray_(NULL), uchars_(trieUChars), pos_(uchars_), remainingMatchLength_(-1) {} @@ -208,7 +208,7 @@ public: * @return The match/value Result. * @stable ICU 4.8 */ - UStringTrieResult next(const UChar *s, int32_t length); + UStringTrieResult next(ConstChar16Ptr s, int32_t length); /** * Returns a matching string's value if called immediately after @@ -268,7 +268,7 @@ public: * function chaining. (See User Guide for details.) * @stable ICU 4.8 */ - Iterator(const UChar *trieUChars, int32_t maxStringLength, UErrorCode &errorCode); + Iterator(ConstChar16Ptr trieUChars, int32_t maxStringLength, UErrorCode &errorCode); /** * Iterates from the current state of the specified UCharsTrie. diff --git a/icu4c/source/common/unicode/uchriter.h b/icu4c/source/common/unicode/uchriter.h index 3408a555071..644fbd8a2a9 100644 --- a/icu4c/source/common/unicode/uchriter.h +++ b/icu4c/source/common/unicode/uchriter.h @@ -43,7 +43,7 @@ public: * @param length The length of the UChar array * @stable ICU 2.0 */ - UCharCharacterIterator(const UChar* textPtr, int32_t length); + UCharCharacterIterator(ConstChar16Ptr textPtr, int32_t length); /** * Create an iterator over the UChar array referred to by "textPtr". @@ -58,7 +58,7 @@ public: * @param position The starting position of the iteration * @stable ICU 2.0 */ - UCharCharacterIterator(const UChar* textPtr, int32_t length, + UCharCharacterIterator(ConstChar16Ptr textPtr, int32_t length, int32_t position); /** @@ -77,7 +77,7 @@ public: * @param position The starting position of the iteration * @stable ICU 2.0 */ - UCharCharacterIterator(const UChar* textPtr, int32_t length, + UCharCharacterIterator(ConstChar16Ptr textPtr, int32_t length, int32_t textBegin, int32_t textEnd, int32_t position); @@ -340,7 +340,7 @@ public: * Sets the iterator to iterate over a new range of text * @stable ICU 2.0 */ - void setText(const UChar* newText, int32_t newTextLength); + void setText(ConstChar16Ptr newText, int32_t newTextLength); /** * Copies the UChar array under iteration into the UnicodeString diff --git a/icu4c/source/common/unicode/unistr.h b/icu4c/source/common/unicode/unistr.h index 3ed6b7f41cc..331b01281ad 100644 --- a/icu4c/source/common/unicode/unistr.h +++ b/icu4c/source/common/unicode/unistr.h @@ -30,6 +30,7 @@ #include #include "unicode/utypes.h" +#include "unicode/char16ptr.h" #include "unicode/rep.h" #include "unicode/std_string.h" #include "unicode/stringpiece.h" @@ -57,337 +58,6 @@ u_strlen(const UChar *s); U_NAMESPACE_BEGIN -// TODO begin experiment --------------- - -/** - * \def U_ALIASING_BARRIER - * Barrier for pointer anti-aliasing optimizations even across function boundaries. - * @internal - */ -#ifdef U_ALIASING_BARRIER - // Use the predefined value. -#elif defined(__clang__) || defined(__GNUC__) -# define U_ALIASING_BARRIER(ptr) asm volatile("" : "+rm"(ptr)) -#endif - -/** - * char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types, - * and from NULL. - * @draft ICU 59 - */ -class U_COMMON_API Char16Ptr final { -public: - /** - * Copies the pointer. - * TODO: @param p ... - * @draft ICU 59 - */ - inline Char16Ptr(char16_t *p); - /** - * Converts the pointer to char16_t *. - * @draft ICU 59 - */ - inline Char16Ptr(uint16_t *p); -#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) - /** - * Converts the pointer to char16_t *. - * (Only defined if U_SIZEOF_WCHAR_T==2.) - * @draft ICU 59 - */ - inline Char16Ptr(wchar_t *p); -#endif - /** - * nullptr constructor. - * @draft ICU 59 - */ - inline Char16Ptr(std::nullptr_t p); - /** - * NULL constructor. - * Must only be used for 0 which is usually the value of NULL. - * @draft ICU 59 - */ - Char16Ptr(int null); - /** - * Destructor. - * @draft ICU 59 - */ - inline ~Char16Ptr(); - - /** - * Pointer access. - * TODO @return ... - * @draft ICU 59 - */ - inline char16_t *get() const; - /** - * char16_t pointer access via type conversion (e.g., static_cast). - * @draft ICU 59 - */ - operator char16_t *() const { return get(); } - /** - * uint16_t pointer access via type conversion (e.g., static_cast). - * @draft ICU 59 - */ - inline operator uint16_t *() const; -#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) - /** - * wchar_t pointer access via type conversion (e.g., static_cast). - * @draft ICU 59 - */ - inline operator wchar_t *() const; -#endif - operator void *() const { return get(); } - - char16_t operator[](size_t offset) const { return get()[offset]; } - - UBool operator==(const Char16Ptr &other) const { return get() == other.get(); } - UBool operator!=(const Char16Ptr &other) const { return !operator==(other); } - UBool operator==(const char16_t *other) const { return get() == other; } - UBool operator!=(const char16_t *other) const { return !operator==(other); } - UBool operator==(const uint16_t *other) const { return static_cast(*this) == other; } - UBool operator!=(const uint16_t *other) const { return !operator==(other); } -#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) - UBool operator==(const wchar_t *other) const { return static_cast(*this) == other; } - UBool operator!=(const wchar_t *other) const { return !operator==(other); } -#endif - UBool operator==(const std::nullptr_t null) const { return get() == null; } - UBool operator!=(const std::nullptr_t null) const { return !operator==(null); } - /** - * Comparison with NULL. - * @return TRUE if the pointer is nullptr and null==0 - * @draft ICU 59 - */ - UBool operator==(int null) const { return get() == nullptr && null == 0; } - /** - * Comparison with NULL. - * @return TRUE if the pointer is not nullptr and null==0 - * @draft ICU 59 - */ - UBool operator!=(int null) const { return get() != nullptr && null == 0; } - - Char16Ptr operator+(size_t offset) const { return Char16Ptr(get() + offset); } - -private: - Char16Ptr() = delete; - -#ifdef U_ALIASING_BARRIER - template static char16_t *cast(T *t) { - U_ALIASING_BARRIER(t); - return reinterpret_cast(t); - } - - char16_t *p; -#else - union { - char16_t *cp; - uint16_t *up; - wchar_t *wp; - } u; -#endif -}; - -#ifdef U_ALIASING_BARRIER - -Char16Ptr::Char16Ptr(char16_t *p) : p(p) {} -Char16Ptr::Char16Ptr(uint16_t *p) : p(cast(p)) {} -#if U_SIZEOF_WCHAR_T==2 -Char16Ptr::Char16Ptr(wchar_t *p) : p(cast(p)) {} -#endif -Char16Ptr::Char16Ptr(std::nullptr_t p) : p(p) {} -Char16Ptr::~Char16Ptr() { - U_ALIASING_BARRIER(p); -} - -char16_t *Char16Ptr::get() const { return p; } - -Char16Ptr::operator uint16_t *() const { - U_ALIASING_BARRIER(p); - return reinterpret_cast(p); -} -#if U_SIZEOF_WCHAR_T==2 -Char16Ptr::operator wchar_t *() const { - U_ALIASING_BARRIER(p); - return reinterpret_cast(p); -} -#endif - -#else - -Char16Ptr::Char16Ptr(char16_t *p) { u.cp = p; } -Char16Ptr::Char16Ptr(uint16_t *p) { u.up = p; } -#if U_SIZEOF_WCHAR_T==2 -Char16Ptr::Char16Ptr(wchar_t *p) { u.wp = p; } -#endif -Char16Ptr::Char16Ptr(std::nullptr_t p) { u.cp = p; } -Char16Ptr::~Char16Ptr() {} - -char16_t *Char16Ptr::get() const { return u.cp; } - -Char16Ptr::operator uint16_t *() const { - return u.up; -} -#if U_SIZEOF_WCHAR_T==2 -Char16Ptr::operator wchar_t *() const { - return u.wp; -} -#endif - -#endif - -/** - * const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types, - * and from NULL. - * @draft ICU 59 - */ -class U_COMMON_API ConstChar16Ptr final { -public: - /** - * Copies the pointer. - * @draft ICU 59 - */ - inline ConstChar16Ptr(const char16_t *p); - /** - * Converts the pointer to char16_t *. - * @draft ICU 59 - */ - inline ConstChar16Ptr(const uint16_t *p); -#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) - /** - * Converts the pointer to char16_t *. - * (Only defined if U_SIZEOF_WCHAR_T==2.) - * @draft ICU 59 - */ - inline ConstChar16Ptr(const wchar_t *p); -#endif - /** - * nullptr constructor. - * @draft ICU 59 - */ - inline ConstChar16Ptr(const std::nullptr_t p); - /** - * NULL constructor. - * Must only be used for 0 which is usually the value of NULL. - * @draft ICU 59 - */ - ConstChar16Ptr(int null); - /** - * Destructor. - * @draft ICU 59 - */ - inline ~ConstChar16Ptr(); - - /** - * Pointer access. - * @draft ICU 59 - */ - inline const char16_t *get() const; - /** - * char16_t pointer access via type conversion (e.g., static_cast). - * @draft ICU 59 - */ - operator const char16_t *() const { return get(); } - /** - * uint16_t pointer access via type conversion (e.g., static_cast). - * @draft ICU 59 - */ - inline operator const uint16_t *() const; -#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) - /** - * wchar_t pointer access via type conversion (e.g., static_cast). - * @draft ICU 59 - */ - inline operator const wchar_t *() const; -#endif - operator const void *() const { return get(); } - - char16_t operator[](size_t offset) const { return get()[offset]; } - - UBool operator==(const ConstChar16Ptr &other) const { return get() == other.get(); } - UBool operator!=(const ConstChar16Ptr &other) const { return !operator==(other); } - UBool operator==(const char16_t *other) const { return get() == other; } - UBool operator!=(const char16_t *other) const { return !operator==(other); } - UBool operator==(const uint16_t *other) const { return static_cast(*this) == other; } - UBool operator!=(const uint16_t *other) const { return !operator==(other); } -#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) - UBool operator==(const wchar_t *other) const { return static_cast(*this) == other; } - UBool operator!=(const wchar_t *other) const { return !operator==(other); } -#endif - UBool operator==(const std::nullptr_t null) const { return get() == null; } - UBool operator!=(const std::nullptr_t null) const { return !operator==(null); } - UBool operator==(int null) const { return get() == nullptr && null == 0; } - UBool operator!=(int null) const { return get() != nullptr && null == 0; } - - ConstChar16Ptr operator+(size_t offset) { return ConstChar16Ptr(get() + offset); } - -private: - ConstChar16Ptr() = delete; - -#ifdef U_ALIASING_BARRIER - template static const char16_t *cast(const T *t) { - U_ALIASING_BARRIER(t); - return reinterpret_cast(t); - } - - const char16_t *p; -#else - union { - const char16_t *cp; - const uint16_t *up; - const wchar_t *wp; - } u; -#endif -}; - -#ifdef U_ALIASING_BARRIER - -ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) : p(p) {} -ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) : p(cast(p)) {} -#if U_SIZEOF_WCHAR_T==2 -ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) : p(cast(p)) {} -#endif -ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) : p(p) {} -ConstChar16Ptr::~ConstChar16Ptr() { - U_ALIASING_BARRIER(p); -} - -const char16_t *ConstChar16Ptr::get() const { return p; } - -ConstChar16Ptr::operator const uint16_t *() const { - U_ALIASING_BARRIER(p); - return reinterpret_cast(p); -} -#if U_SIZEOF_WCHAR_T==2 -ConstChar16Ptr::operator const wchar_t *() const { - U_ALIASING_BARRIER(p); - return reinterpret_cast(p); -} -#endif - -#else - -ConstChar16Ptr::ConstChar16Ptr(const char16_t *p) { u.cp = p; } -ConstChar16Ptr::ConstChar16Ptr(const uint16_t *p) { u.up = p; } -#if U_SIZEOF_WCHAR_T==2 -ConstChar16Ptr::ConstChar16Ptr(const wchar_t *p) { u.wp = p; } -#endif -ConstChar16Ptr::ConstChar16Ptr(const std::nullptr_t p) { u.cp = p; } -ConstChar16Ptr::~ConstChar16Ptr() {} - -const char16_t *ConstChar16Ptr::get() const { return u.cp; } - -ConstChar16Ptr::operator const uint16_t *() const { - return u.up; -} -#if U_SIZEOF_WCHAR_T==2 -ConstChar16Ptr::operator const wchar_t *() const { - return u.wp; -} -#endif - -#endif - -// TODO end experiment ----------------- - #if !UCONFIG_NO_BREAK_ITERATION class BreakIterator; // unicode/brkiter.h #endif diff --git a/icu4c/source/common/unistr.cpp b/icu4c/source/common/unistr.cpp index a891dec799b..10c781746e1 100644 --- a/icu4c/source/common/unistr.cpp +++ b/icu4c/source/common/unistr.cpp @@ -98,48 +98,6 @@ U_CDECL_END U_NAMESPACE_BEGIN -#ifdef U_ALIASING_BARRIER - -Char16Ptr::Char16Ptr(int null) : p(nullptr) { - U_ASSERT(null == 0); - if (null != 0) { - // Try to provoke a crash. - p = reinterpret_cast(1); - } -} - -ConstChar16Ptr::ConstChar16Ptr(int null) : p(nullptr) { - U_ASSERT(null == 0); - if (null != 0) { - // Try to provoke a crash. - p = reinterpret_cast(1); - } -} - -#else - -Char16Ptr::Char16Ptr(int null) { - U_ASSERT(null == 0); - if (null == 0) { - u.cp = nullptr; - } else { - // Try to provoke a crash. - u.cp = reinterpret_cast(1); - } -} - -ConstChar16Ptr::ConstChar16Ptr(int null) { - U_ASSERT(null == 0); - if (null == 0) { - u.cp = nullptr; - } else { - // Try to provoke a crash. - u.cp = reinterpret_cast(1); - } -} - -#endif - /* The Replaceable virtual destructor can't be defined in the header due to how AIX works with multiple definitions of virtual functions. */ diff --git a/icu4c/source/common/ustr_titlecase_brkiter.cpp b/icu4c/source/common/ustr_titlecase_brkiter.cpp index 0b2ba02064b..ac46bc9359f 100644 --- a/icu4c/source/common/ustr_titlecase_brkiter.cpp +++ b/icu4c/source/common/ustr_titlecase_brkiter.cpp @@ -66,8 +66,8 @@ U_NAMESPACE_BEGIN int32_t CaseMap::toTitle( const char *locale, uint32_t options, BreakIterator *iter, - const UChar *src, int32_t srcLength, - UChar *dest, int32_t destCapacity, Edits *edits, + ConstChar16Ptr src, int32_t srcLength, + Char16Ptr dest, int32_t destCapacity, Edits *edits, UErrorCode &errorCode) { LocalPointer ownedIter; if(iter==NULL) { diff --git a/icu4c/source/common/ustrcase.cpp b/icu4c/source/common/ustrcase.cpp index 0e38a42e103..d0bdd1511ab 100644 --- a/icu4c/source/common/ustrcase.cpp +++ b/icu4c/source/common/ustrcase.cpp @@ -1198,8 +1198,8 @@ U_NAMESPACE_BEGIN int32_t CaseMap::fold( uint32_t options, - const UChar *src, int32_t srcLength, - UChar *dest, int32_t destCapacity, Edits *edits, + ConstChar16Ptr src, int32_t srcLength, + Char16Ptr dest, int32_t destCapacity, Edits *edits, UErrorCode &errorCode) { return ustrcase_map( UCASE_LOC_ROOT, options, UCASEMAP_BREAK_ITERATOR_NULL diff --git a/icu4c/source/common/ustrcase_locale.cpp b/icu4c/source/common/ustrcase_locale.cpp index 2ecd24f03ec..0c6d095d367 100644 --- a/icu4c/source/common/ustrcase_locale.cpp +++ b/icu4c/source/common/ustrcase_locale.cpp @@ -69,8 +69,8 @@ U_NAMESPACE_BEGIN int32_t CaseMap::toLower( const char *locale, uint32_t options, - const UChar *src, int32_t srcLength, - UChar *dest, int32_t destCapacity, Edits *edits, + ConstChar16Ptr src, int32_t srcLength, + Char16Ptr dest, int32_t destCapacity, Edits *edits, UErrorCode &errorCode) { return ustrcase_map( ustrcase_getCaseLocale(locale), options, UCASEMAP_BREAK_ITERATOR_NULL @@ -81,8 +81,8 @@ int32_t CaseMap::toLower( int32_t CaseMap::toUpper( const char *locale, uint32_t options, - const UChar *src, int32_t srcLength, - UChar *dest, int32_t destCapacity, Edits *edits, + ConstChar16Ptr src, int32_t srcLength, + Char16Ptr dest, int32_t destCapacity, Edits *edits, UErrorCode &errorCode) { return ustrcase_map( ustrcase_getCaseLocale(locale), options, UCASEMAP_BREAK_ITERATOR_NULL diff --git a/icu4c/source/i18n/curramt.cpp b/icu4c/source/i18n/curramt.cpp index 51f23b437ab..d2242707a1e 100644 --- a/icu4c/source/i18n/curramt.cpp +++ b/icu4c/source/i18n/curramt.cpp @@ -19,12 +19,12 @@ U_NAMESPACE_BEGIN -CurrencyAmount::CurrencyAmount(const Formattable& amount, const UChar* isoCode, +CurrencyAmount::CurrencyAmount(const Formattable& amount, ConstChar16Ptr isoCode, UErrorCode& ec) : Measure(amount, new CurrencyUnit(isoCode, ec), ec) { } -CurrencyAmount::CurrencyAmount(double amount, const UChar* isoCode, +CurrencyAmount::CurrencyAmount(double amount, ConstChar16Ptr isoCode, UErrorCode& ec) : Measure(Formattable(amount), new CurrencyUnit(isoCode, ec), ec) { } diff --git a/icu4c/source/i18n/currunit.cpp b/icu4c/source/i18n/currunit.cpp index 3c74c1c1d99..197885452f5 100644 --- a/icu4c/source/i18n/currunit.cpp +++ b/icu4c/source/i18n/currunit.cpp @@ -19,10 +19,10 @@ U_NAMESPACE_BEGIN -CurrencyUnit::CurrencyUnit(const UChar* _isoCode, UErrorCode& ec) { +CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) { *isoCode = 0; if (U_SUCCESS(ec)) { - if (_isoCode && u_strlen(_isoCode)==3) { + if (_isoCode != nullptr && u_strlen(_isoCode)==3) { u_strcpy(isoCode, _isoCode); char simpleIsoCode[4]; u_UCharsToChars(isoCode, simpleIsoCode, 4); diff --git a/icu4c/source/i18n/dtfmtsym.cpp b/icu4c/source/i18n/dtfmtsym.cpp index 333c85c8fe0..960971980bc 100644 --- a/icu4c/source/i18n/dtfmtsym.cpp +++ b/icu4c/source/i18n/dtfmtsym.cpp @@ -1368,7 +1368,7 @@ DateFormatSymbols::setZoneStrings(const UnicodeString* const *strings, int32_t r //------------------------------------------------------ -const UChar * U_EXPORT2 +ConstChar16Ptr U_EXPORT2 DateFormatSymbols::getPatternUChars(void) { return gPatternChars; diff --git a/icu4c/source/i18n/numfmt.cpp b/icu4c/source/i18n/numfmt.cpp index 951146725af..e84f9fdd8d2 100644 --- a/icu4c/source/i18n/numfmt.cpp +++ b/icu4c/source/i18n/numfmt.cpp @@ -1188,7 +1188,7 @@ void NumberFormat::setCurrency(const UChar* theCurrency, UErrorCode& ec) { } } -const UChar* NumberFormat::getCurrency() const { +ConstChar16Ptr NumberFormat::getCurrency() const { return fCurrency; } diff --git a/icu4c/source/i18n/smpdtfmt.cpp b/icu4c/source/i18n/smpdtfmt.cpp index 3c0670446b3..69a13450a68 100644 --- a/icu4c/source/i18n/smpdtfmt.cpp +++ b/icu4c/source/i18n/smpdtfmt.cpp @@ -3789,7 +3789,7 @@ SimpleDateFormat::toLocalizedPattern(UnicodeString& result, UErrorCode& status) const { translatePattern(fPattern, result, - UnicodeString(DateFormatSymbols::getPatternUChars()), + UnicodeString(DateFormatSymbols::getPatternUChars().get()), fSymbols->fLocalPatternChars, status); return result; } @@ -3811,7 +3811,7 @@ SimpleDateFormat::applyLocalizedPattern(const UnicodeString& pattern, { translatePattern(pattern, fPattern, fSymbols->fLocalPatternChars, - UnicodeString(DateFormatSymbols::getPatternUChars()), status); + UnicodeString(DateFormatSymbols::getPatternUChars().get()), status); } //---------------------------------------------------------------------- diff --git a/icu4c/source/i18n/unicode/curramt.h b/icu4c/source/i18n/unicode/curramt.h index 9071f11bd10..a645667d9d0 100644 --- a/icu4c/source/i18n/unicode/curramt.h +++ b/icu4c/source/i18n/unicode/curramt.h @@ -46,7 +46,7 @@ class U_I18N_API CurrencyAmount: public Measure { * is invalid, then this will be set to a failing value. * @stable ICU 3.0 */ - CurrencyAmount(const Formattable& amount, const UChar* isoCode, + CurrencyAmount(const Formattable& amount, ConstChar16Ptr isoCode, UErrorCode &ec); /** @@ -59,7 +59,7 @@ class U_I18N_API CurrencyAmount: public Measure { * then this will be set to a failing value. * @stable ICU 3.0 */ - CurrencyAmount(double amount, const UChar* isoCode, + CurrencyAmount(double amount, ConstChar16Ptr isoCode, UErrorCode &ec); /** @@ -115,14 +115,14 @@ class U_I18N_API CurrencyAmount: public Measure { * Return the ISO currency code of this object. * @stable ICU 3.0 */ - inline const UChar* getISOCurrency() const; + inline ConstChar16Ptr getISOCurrency() const; }; inline const CurrencyUnit& CurrencyAmount::getCurrency() const { return (const CurrencyUnit&) getUnit(); } -inline const UChar* CurrencyAmount::getISOCurrency() const { +inline ConstChar16Ptr CurrencyAmount::getISOCurrency() const { return getCurrency().getISOCurrency(); } diff --git a/icu4c/source/i18n/unicode/currunit.h b/icu4c/source/i18n/unicode/currunit.h index 61f9201ebb2..857d9ceafe0 100644 --- a/icu4c/source/i18n/unicode/currunit.h +++ b/icu4c/source/i18n/unicode/currunit.h @@ -44,7 +44,7 @@ class U_I18N_API CurrencyUnit: public MeasureUnit { * then this will be set to a failing value. * @stable ICU 3.0 */ - CurrencyUnit(const UChar* isoCode, UErrorCode &ec); + CurrencyUnit(ConstChar16Ptr isoCode, UErrorCode &ec); /** * Copy constructor @@ -93,7 +93,7 @@ class U_I18N_API CurrencyUnit: public MeasureUnit { * Return the ISO currency code of this object. * @stable ICU 3.0 */ - inline const UChar* getISOCurrency() const; + inline ConstChar16Ptr getISOCurrency() const; private: /** @@ -102,7 +102,7 @@ class U_I18N_API CurrencyUnit: public MeasureUnit { UChar isoCode[4]; }; -inline const UChar* CurrencyUnit::getISOCurrency() const { +inline ConstChar16Ptr CurrencyUnit::getISOCurrency() const { return isoCode; } diff --git a/icu4c/source/i18n/unicode/dtfmtsym.h b/icu4c/source/i18n/unicode/dtfmtsym.h index 512ebb8351f..8374de3c207 100644 --- a/icu4c/source/i18n/unicode/dtfmtsym.h +++ b/icu4c/source/i18n/unicode/dtfmtsym.h @@ -566,7 +566,7 @@ public: * @return the non-localized date-time pattern characters * @stable ICU 2.0 */ - static const UChar * U_EXPORT2 getPatternUChars(void); + static ConstChar16Ptr U_EXPORT2 getPatternUChars(void); /** * Gets localized date-time pattern characters. For example: 'u', 't', etc. diff --git a/icu4c/source/i18n/unicode/numfmt.h b/icu4c/source/i18n/unicode/numfmt.h index 1907476ff30..009d59f0916 100644 --- a/icu4c/source/i18n/unicode/numfmt.h +++ b/icu4c/source/i18n/unicode/numfmt.h @@ -940,7 +940,7 @@ public: * the currency in use, or a pointer to the empty string. * @stable ICU 2.6 */ - const UChar* getCurrency() const; + ConstChar16Ptr getCurrency() const; /** * Set a particular UDisplayContext value in the formatter, such as diff --git a/icu4c/source/i18n/unum.cpp b/icu4c/source/i18n/unum.cpp index b8d26612ff2..458a79c7fde 100644 --- a/icu4c/source/i18n/unum.cpp +++ b/icu4c/source/i18n/unum.cpp @@ -609,7 +609,7 @@ unum_getTextAttribute(const UNumberFormat* fmt, break; case UNUM_CURRENCY_CODE: - res = UnicodeString(df->getCurrency()); + res = UnicodeString(df->getCurrency().get()); break; default: diff --git a/icu4c/source/test/intltest/dtfmttst.cpp b/icu4c/source/test/intltest/dtfmttst.cpp index 63d127346da..f9461739257 100644 --- a/icu4c/source/test/intltest/dtfmttst.cpp +++ b/icu4c/source/test/intltest/dtfmttst.cpp @@ -514,7 +514,7 @@ void DateFormatTest::TestFieldPosition() { // local pattern chars data is not longer loaded // from icu locale bundle assertEquals("patternChars", PATTERN_CHARS, rootSyms.getLocalPatternChars(buf)); - assertEquals("patternChars", PATTERN_CHARS, DateFormatSymbols::getPatternUChars()); + assertEquals("patternChars", PATTERN_CHARS, DateFormatSymbols::getPatternUChars().get()); assertTrue("DATEFORMAT_FIELD_NAMES", DATEFORMAT_FIELD_NAMES_LENGTH == UDAT_FIELD_COUNT); #if UDAT_HAS_PATTERN_CHAR_FOR_TIME_SEPARATOR assertTrue("Data", UDAT_FIELD_COUNT == uprv_strlen(PATTERN_CHARS)); diff --git a/icu4c/source/test/intltest/intltest.cpp b/icu4c/source/test/intltest/intltest.cpp index ab5ce8c1272..bfefe3f8570 100644 --- a/icu4c/source/test/intltest/intltest.cpp +++ b/icu4c/source/test/intltest/intltest.cpp @@ -187,7 +187,7 @@ UnicodeString _toString(const Formattable& f) { case Formattable::kObject: { const CurrencyAmount* c = dynamic_cast(f.getObject()); if (c != NULL) { - s = _toString(c->getNumber()) + " " + UnicodeString(c->getISOCurrency()); + s = _toString(c->getNumber()) + " " + UnicodeString(c->getISOCurrency().get()); } else { s = UnicodeString("Unknown UObject"); } diff --git a/icu4c/source/test/intltest/measfmttest.cpp b/icu4c/source/test/intltest/measfmttest.cpp index f34d8397c01..10fa1c74950 100644 --- a/icu4c/source/test/intltest/measfmttest.cpp +++ b/icu4c/source/test/intltest/measfmttest.cpp @@ -1856,7 +1856,7 @@ void MeasureFormatTest::TestCurrencies() { u_uastrcpy(USD, "USD"); UErrorCode status = U_ZERO_ERROR; CurrencyAmount USD_1(1.0, USD, status); - assertEquals("Currency Code", USD, USD_1.getISOCurrency()); + assertEquals("Currency Code", USD, USD_1.getISOCurrency().get()); CurrencyAmount USD_2(2.0, USD, status); CurrencyAmount USD_NEG_1(-1.0, USD, status); if (!assertSuccess("Error creating currencies", status)) { diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index a8553f57802..ea67aa2d47c 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -440,7 +440,7 @@ UBool NumberFormatTestDataDriven::isParseCurrencyPass( } return TRUE; } - UnicodeString currStr(currAmt->getISOCurrency()); + UnicodeString currStr(currAmt->getISOCurrency().get()); Formattable resultFormattable(currAmt->getNumber()); UnicodeString resultStr(UnicodeString::fromUTF8(resultFormattable.getDecimalNumber(status))); if (tuple.output == "fail") { @@ -3168,7 +3168,7 @@ void NumberFormatTest::expectParseCurrency(const NumberFormat &fmt, const UChar* uprv_strcpy(theOperation, theInfo); uprv_strcat(theOperation, ", check currency:"); - assertEquals(theOperation, currency, currencyAmount->getISOCurrency()); + assertEquals(theOperation, currency, currencyAmount->getISOCurrency().get()); } @@ -3763,14 +3763,14 @@ NumberFormatTest::TestCurrencyFormatForMixParsing() { } else if (result.getType() != Formattable::kObject || (curramt = dynamic_cast(result.getObject())) == NULL || curramt->getNumber().getDouble() != 1234.56 || - UnicodeString(curramt->getISOCurrency()).compare(ISO_CURRENCY_USD) + UnicodeString(curramt->getISOCurrency().get()).compare(ISO_CURRENCY_USD) ) { errln("FAIL: getCurrencyFormat of default locale (en_US) failed roundtripping the number "); if (curramt->getNumber().getDouble() != 1234.56) { errln((UnicodeString)"wong number, expect: 1234.56" + ", got: " + curramt->getNumber().getDouble()); } if (curramt->getISOCurrency() != ISO_CURRENCY_USD) { - errln((UnicodeString)"wong currency, expect: USD" + ", got: " + curramt->getISOCurrency()); + errln((UnicodeString)"wong currency, expect: USD" + ", got: " + curramt->getISOCurrency().get()); } } } diff --git a/icu4c/source/test/intltest/tsmthred.cpp b/icu4c/source/test/intltest/tsmthred.cpp index 036d5e1d355..5cff8ee51d0 100644 --- a/icu4c/source/test/intltest/tsmthred.cpp +++ b/icu4c/source/test/intltest/tsmthred.cpp @@ -547,7 +547,7 @@ UBool ThreadSafeFormat::doStuff(int32_t offset, UnicodeString &appendErr, UError appendErr.append("fFormat currency != ") .append(kUSD) .append(", =") - .append(fFormat->getCurrency()) + .append(fFormat->getCurrency().get()) .append("! "); okay = FALSE; } @@ -556,7 +556,7 @@ UBool ThreadSafeFormat::doStuff(int32_t offset, UnicodeString &appendErr, UError appendErr.append("gFormat currency != ") .append(kUSD) .append(", =") - .append(gSharedData->fFormat->getCurrency()) + .append(gSharedData->fFormat->getCurrency().get()) .append("! "); okay = FALSE; }