]> granicus.if.org Git - icu/commitdiff
ICU-12992 remove conversion from int; NULL is often nullptr or __null not simply 0
authorMarkus Scherer <markus.icu@gmail.com>
Wed, 1 Mar 2017 21:11:54 +0000 (21:11 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Wed, 1 Mar 2017 21:11:54 +0000 (21:11 +0000)
X-SVN-Rev: 39717

icu4c/source/common/char16ptr.cpp
icu4c/source/common/unicode/char16ptr.h
icu4c/source/common/unicode/unistr.h
icu4c/source/common/unistr.cpp
icu4c/source/test/intltest/ustrtest.cpp
icu4c/source/test/intltest/ustrtest.h

index 1da9cd8490ce4eae1cfa48a99e9e799af940fecb..5afec2af303a99153e6e8fb6df7c74f68f3f1f0d 100644 (file)
 
 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
index a949b7d4f5450b8ea8a94f197d03a7bdfefc7b4d..672fa46ed27447cad21f2a79272a554ad51b9ebb 100644 (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;
index 331b01281add64dad57929942e8ba9c56d5f4b91..3dd2fa6c2a57560e9ea81940a8c87213bd2e6cec 100644 (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 {
index 10c781746e1dfff13854ea898674f57d774e033e..3657d15e0f32508617a71c5c7219d3fdad29d010 100644 (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;
   }
 }
 
index 429ae325b0ce68b2578f28a48a21411c0ea0f677..ca031b1f423ef1b394405e0952b50450380c4cde 100644 (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);
-}
index a2e2fbd4b71b94604773a160a45437e2a00af76b..4ba348c431fd5ca7ef9fc7258b34e24ce514990a 100644 (file)
@@ -96,7 +96,6 @@ public:
     void TestUInt16Pointers();
     void TestWCharPointers();
     void TestNullPointers();
-    void TestZeroPointers();
 };
 
 #endif