From c0bee5678c2acade239de44fbd5e8787c42fbd7a Mon Sep 17 00:00:00 2001 From: berryplus Date: Sat, 23 Nov 2019 14:58:45 +0900 Subject: [PATCH] =?UTF-8?q?NULL=E4=BB=A3=E5=85=A5=E3=81=A8NULL=E5=8A=A0?= =?UTF-8?q?=E7=AE=97=E3=81=AE=E4=BB=95=E6=A7=98=E3=82=92=E8=A6=8B=E7=9B=B4?= =?UTF-8?q?=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/mem/CNativeW.h | 17 +++++++++++++---- tests/unittests/test-cnative.cpp | 24 ++++++++---------------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/sakura_core/mem/CNativeW.h b/sakura_core/mem/CNativeW.h index 2ff1225afd..e8ef846a5a 100644 --- a/sakura_core/mem/CNativeW.h +++ b/sakura_core/mem/CNativeW.h @@ -79,10 +79,19 @@ class CNativeW final : public CNative{ //演算子 CNativeW& operator = (const CNativeW& rhs) { CNative::operator=(rhs); return *this; } CNativeW& operator = (CNativeW&& rhs) noexcept { CNative::operator=(std::forward(rhs)); return *this; } - const CNativeW& operator+=(wchar_t wch) { AppendString(&wch,1); return *this; } - const CNativeW& operator=(wchar_t wch) { SetString(&wch,1); return *this; } - const CNativeW& operator+=(const CNativeW& rhs) { AppendNativeData(rhs); return *this; } - CNativeW operator+(const CNativeW& rhs) const { CNativeW tmp=*this; return tmp+=rhs; } + CNativeW operator + (const CNativeW& rhs) const { return std::move(CNativeW(*this) += rhs); } + CNativeW& operator += (const CNativeW& rhs) { AppendNativeData(rhs); return *this; } + + CNativeW& operator = (wchar_t ch) + { + if (!ch) return (*this = nullptr); + return (*this = CNativeW(&ch, 1)); + } + CNativeW& operator += (wchar_t ch) + { + const wchar_t sz[]{ ch, 0 }; + return (*this += sz); + } //ネイティブ取得インターフェース wchar_t operator[](int nIndex) const; //!< 任意位置の文字取得。nIndexは文字単位。 diff --git a/tests/unittests/test-cnative.cpp b/tests/unittests/test-cnative.cpp index 7aeeb505be..152b1759fa 100644 --- a/tests/unittests/test-cnative.cpp +++ b/tests/unittests/test-cnative.cpp @@ -253,23 +253,19 @@ TEST(CNativeW, AssignStringNullPointer) /*! * @brief 代入演算子(NULL指定)の仕様 - * @remark バッファが確保される - * @remark 文字列長は1になる - * @remark バッファサイズは1+1以上になる - * @note バグですね(^^; + * @remark バッファを確保している場合は解放される + * @remark 文字列長はゼロになる */ TEST(CNativeW, AssignStringNullLiteral) { - CNativeW value; + CNativeW value(L"test"); #ifdef _MSC_VER value = NULL; // operator = (wchar_t) と解釈される #else value = static_cast(NULL); #endif - ASSERT_STREQ(L"", value.GetStringPtr()); - EXPECT_EQ(1, value.GetStringLength()); - EXPECT_LT(1 + 1, value.capacity()); - EXPECT_EQ(0, value[0]); // 長さ=1なので1文字目を参照できるが、NULが返ってくる + ASSERT_EQ(NULL, value.GetStringPtr()); + EXPECT_EQ(0, value.GetStringLength()); } /*! @@ -321,10 +317,7 @@ TEST(CNativeW, AppendStringNullPointer) /*! * @brief 加算代入演算子(NULL指定)の仕様 - * @remark バッファが確保される - * @remark 文字列長は1になる - * @remark バッファサイズは1+1以上になる - * @note バグですね(^^; + * @remark 加算代入しても内容に変化無し */ TEST(CNativeW, AppendStringNullLiteral) { @@ -334,9 +327,8 @@ TEST(CNativeW, AppendStringNullLiteral) #else value += static_cast(NULL); #endif - ASSERT_STREQ(L"", value.GetStringPtr()); - EXPECT_EQ(1, value.GetStringLength()); - EXPECT_LT(1 + 1, value.capacity()); + ASSERT_EQ(NULL, value.GetStringPtr()); + EXPECT_EQ(0, value.GetStringLength()); } /*!