ICU-12992 remove conversion from int; NULL is often nullptr or __null not simply 0

X-SVN-Rev: 39717
This commit is contained in:
Markus Scherer 2017-03-01 21:11:54 +00:00
parent 031be51911
commit 4c1c730f57
6 changed files with 17 additions and 90 deletions

View file

@ -10,46 +10,4 @@
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<char16_t *>(1);
}
}
ConstChar16Ptr::ConstChar16Ptr(int null) : p(nullptr) {
U_ASSERT(null == 0);
if (null != 0) {
// Try to provoke a crash.
p = reinterpret_cast<char16_t *>(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<char16_t *>(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<char16_t *>(1);
}
}
#endif
U_NAMESPACE_END

View file

@ -30,8 +30,7 @@ U_NAMESPACE_BEGIN
#endif
/**
* char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types,
* and from NULL.
* char16_t * wrapper with implicit conversion from/to distinct but bit-compatible pointer types.
* @draft ICU 59
*/
class U_COMMON_API Char16Ptr final {
@ -60,12 +59,6 @@ public:
* @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
@ -97,7 +90,7 @@ public:
#endif
operator void *() const { return get(); }
char16_t operator[](size_t offset) const { return get()[offset]; }
char16_t operator[](std::ptrdiff_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); }
@ -112,19 +105,19 @@ public:
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
* Comparison with 0.
* @return TRUE if the pointer is nullptr and zero==0
* @draft ICU 59
*/
UBool operator==(int null) const { return get() == nullptr && null == 0; }
UBool operator==(int zero) const { return get() == nullptr && zero == 0; }
/**
* Comparison with NULL.
* @return TRUE if the pointer is not nullptr and null==0
* Comparison with 0.
* @return TRUE if the pointer is not nullptr and zero==0
* @draft ICU 59
*/
UBool operator!=(int null) const { return get() != nullptr && null == 0; }
UBool operator!=(int zero) const { return get() != nullptr && zero == 0; }
Char16Ptr operator+(size_t offset) const { return Char16Ptr(get() + offset); }
Char16Ptr operator+(std::ptrdiff_t offset) const { return Char16Ptr(get() + offset); }
private:
Char16Ptr() = delete;
@ -194,8 +187,7 @@ Char16Ptr::operator wchar_t *() const {
#endif
/**
* const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types,
* and from NULL.
* const char16_t * wrapper with implicit conversion from/to distinct but bit-compatible pointer types.
* @draft ICU 59
*/
class U_COMMON_API ConstChar16Ptr final {
@ -223,12 +215,6 @@ public:
* @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
@ -259,7 +245,7 @@ public:
#endif
operator const void *() const { return get(); }
char16_t operator[](size_t offset) const { return get()[offset]; }
char16_t operator[](std::ptrdiff_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); }
@ -273,10 +259,10 @@ public:
#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; }
UBool operator==(int zero) const { return get() == nullptr && zero == 0; }
UBool operator!=(int zero) const { return get() != nullptr && zero == 0; }
ConstChar16Ptr operator+(size_t offset) { return ConstChar16Ptr(get() + offset); }
ConstChar16Ptr operator+(std::ptrdiff_t offset) { return ConstChar16Ptr(get() + offset); }
private:
ConstChar16Ptr() = delete;

View file

@ -3943,7 +3943,7 @@ UnicodeString::isBufferWritable() const
inline ConstChar16Ptr
UnicodeString::getBuffer() const {
if(fUnion.fFields.fLengthAndFlags&(kIsBogus|kOpenGetBuffer)) {
return 0;
return nullptr;
} else if(fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) {
return fUnion.fStackFields.fBuffer;
} else {

View file

@ -1221,7 +1221,7 @@ UnicodeString::unBogus() {
ConstChar16Ptr
UnicodeString::getTerminatedBuffer() {
if(!isWritable()) {
return 0;
return nullptr;
}
UChar *array = getArrayStart();
int32_t len = length();
@ -1723,7 +1723,7 @@ UnicodeString::getBuffer(int32_t minCapacity) {
setZeroLength();
return getArrayStart();
} else {
return 0;
return nullptr;
}
}

View file

@ -64,7 +64,6 @@ void UnicodeStringTest::runIndexedTest( int32_t index, UBool exec, const char* &
TESTCASE_AUTO(TestUInt16Pointers);
TESTCASE_AUTO(TestWCharPointers);
TESTCASE_AUTO(TestNullPointers);
TESTCASE_AUTO(TestZeroPointers);
TESTCASE_AUTO_END;
}
@ -2258,18 +2257,3 @@ UnicodeStringTest::TestNullPointers() {
UnicodeString(u"def").extract(nullptr, 0, errorCode);
assertEquals("buffer overflow extracting to nullptr", U_BUFFER_OVERFLOW_ERROR, errorCode);
}
void
UnicodeStringTest::TestZeroPointers() {
// There are constructor overloads with one and three integer parameters
// which match passing 0, so we cannot test using 0 for UnicodeString(pointer)
// or UnicodeString(read-only or writable alias).
// There are multiple two-parameter constructors that make using 0
// for the first parameter ambiguous already,
// so we cannot test using 0 for UnicodeString(pointer, length).
// extract() also has enough overloads to be ambiguous with 0.
// Test the pointer wrapper directly.
assertTrue("0 --> nullptr", Char16Ptr(0).get() == nullptr);
assertTrue("0 --> const nullptr", ConstChar16Ptr(0).get() == nullptr);
}

View file

@ -96,7 +96,6 @@ public:
void TestUInt16Pointers();
void TestWCharPointers();
void TestNullPointers();
void TestZeroPointers();
};
#endif