From 9934db76d04da6b3b6e8fe750cb5488708b64a70 Mon Sep 17 00:00:00 2001 From: Mari Sano Date: Mon, 26 Oct 2020 12:21:40 +0900 Subject: [PATCH] =?UTF-8?q?strprintf/vstrprintf=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sakura_core/debug/Debug1.cpp | 9 +- sakura_core/util/string_ex.h | 47 ++++ tests/unittests/test-int2dec.cpp | 99 +++------ tests/unittests/test-string_ex.cpp | 79 +++++++ tests/unittests/tests1.vcxproj | 291 +++++++++++++------------ tests/unittests/tests1.vcxproj.filters | 173 +++++++-------- 6 files changed, 394 insertions(+), 304 deletions(-) create mode 100644 tests/unittests/test-string_ex.cpp diff --git a/sakura_core/debug/Debug1.cpp b/sakura_core/debug/Debug1.cpp index f53b5b7b2c..b745d18012 100644 --- a/sakura_core/debug/Debug1.cpp +++ b/sakura_core/debug/Debug1.cpp @@ -21,6 +21,8 @@ #include #include +#include "util/string_ex.h" + #if defined(_DEBUG) || defined(USE_RELPRINT) // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- // @@ -54,13 +56,8 @@ void DebugOutW( LPCWSTR lpFmt, ...) ::DebugBreak(); - const int count = _vscwprintf( lpFmt, argList ); - std::wstring strTooLongMessage; - strTooLongMessage.reserve( count ); - - ::_vsnwprintf_s( strTooLongMessage.data(), count + 1, _TRUNCATE, lpFmt, argList ); - strTooLongMessage.assign( strTooLongMessage.data(), count ); + vstrprintf( strTooLongMessage, lpFmt, argList ); ::OutputDebugStringW( strTooLongMessage.c_str() ); } diff --git a/sakura_core/util/string_ex.h b/sakura_core/util/string_ex.h index 263ea4d460..d0c41273b5 100644 --- a/sakura_core/util/string_ex.h +++ b/sakura_core/util/string_ex.h @@ -26,6 +26,8 @@ #define SAKURA_STRING_EX_87282FEB_4B23_4112_9C5A_419F43618705_H_ #pragma once +#include + // 2007.10.19 kobake // string.h で定義されている関数を拡張したようなモノ達 @@ -200,6 +202,51 @@ inline int auto_vsprintf(WCHAR* buf, const WCHAR* format, va_list& v){ return tc inline int auto_vsprintf_s(ACHAR* buf, size_t nBufCount, const ACHAR* format, va_list& v){ return tchar_vsprintf_s(buf, nBufCount, format, v); } inline int auto_vsprintf_s(WCHAR* buf, size_t nBufCount, const WCHAR* format, va_list& v){ return tchar_vsprintf_s(buf, nBufCount, format, v); } +template +inline int vstrprintf( std::basic_string& strOut, const ChType* pszFormat, va_list& argList ) +{ + static_assert( 0, "not implemented" ); + return -1; +} + +template +inline int strprintf( std::basic_string& strOut, const ChType* pszFormat, ... ) +{ + static_assert( 0, "not implemented" ); + return -1; +} + +template<> +inline int vstrprintf( std::wstring& strOut, const WCHAR* pszFormat, va_list& argList ) +{ + strOut.clear(); + + const int cchOut = _vscwprintf( pszFormat, argList ); + if( cchOut > 0 ){ + + strOut.reserve( cchOut ); + + ::vswprintf_s( strOut.data(), strOut.capacity(), pszFormat, argList ); + + strOut.assign( strOut.data(), cchOut ); + } + + return cchOut; +} + +template<> +inline int strprintf( std::wstring& strOut, const WCHAR* pszFormat, ... ) +{ + va_list argList; + va_start( argList, pszFormat ); + + const int nRet = vstrprintf( strOut, pszFormat, argList ); + + va_end( argList ); + + return nRet; +} + // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- // // 文字コード変換 // // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- // diff --git a/tests/unittests/test-int2dec.cpp b/tests/unittests/test-int2dec.cpp index 96d532c5df..148737648d 100644 --- a/tests/unittests/test-int2dec.cpp +++ b/tests/unittests/test-int2dec.cpp @@ -1,79 +1,42 @@ -#include +/*! @file */ +/* + Copyright (C) 2018-2020 Sakura Editor Organization -#include + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. -#ifndef NOMINMAX -#define NOMINMAX -#endif /* #ifndef NOMINMAX */ - -#include -#include -#include "basis/primitive.h" -#include "util/string_ex2.h" + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: -template -void test_int2dec(T value, ptrdiff_t lenExpected, const wchar_t* strExpected) -{ - wchar_t buff[int2dec_destBufferSufficientLength()]; - ptrdiff_t len = int2dec(value, buff); - EXPECT_EQ(len, lenExpected); - EXPECT_STREQ(buff, strExpected); -} + 1. The origin of this software must not be misrepresented; + you must not claim that you wrote the original software. + If you use this software in a product, an acknowledgment + in the product documentation would be appreciated but is + not required. -template -void test_plusminus(T plusValue, ptrdiff_t lenExpected, const wchar_t* strExpected) -{ - test_int2dec(plusValue, lenExpected, strExpected); - test_int2dec(-plusValue, 1+lenExpected, (std::wstring(L"-")+strExpected).c_str()); -} - -static -void test_32_64_plus_minus(int value, ptrdiff_t lenExpected, const wchar_t* strExpected) -{ - test_plusminus(value, lenExpected, strExpected); - test_plusminus(value, lenExpected, strExpected); -} + 2. Altered source versions must be plainly marked as such, + and must not be misrepresented as being the original software. -TEST(int2dec_test, zero) -{ - test_int2dec(0, 1, L"0"); - test_int2dec(0, 1, L"0"); -} + 3. This notice may not be removed or altered from any source + distribution. +*/ +#include -TEST(int2dec_test, digits) -{ - test_32_64_plus_minus(2, 1, L"2"); - test_32_64_plus_minus(3, 1, L"3"); - test_32_64_plus_minus(4, 1, L"4"); - test_32_64_plus_minus(5, 1, L"5"); - test_32_64_plus_minus(6, 1, L"6"); - test_32_64_plus_minus(7, 1, L"7"); - test_32_64_plus_minus(8, 1, L"8"); - test_32_64_plus_minus(9, 1, L"9"); -} +#ifndef NOMINMAX +#define NOMINMAX +#endif /* #ifndef NOMINMAX */ -TEST(int2dec_test, max) -{ - test_int2dec(std::numeric_limits::max(), 10, L"2147483647"); - test_int2dec(std::numeric_limits::max(), 19, L"9223372036854775807"); -} +#include +#include -TEST(int2dec_test, min) -{ - test_int2dec(std::numeric_limits::min(), 11, L"-2147483648"); - test_int2dec(std::numeric_limits::min(), 20, L"-9223372036854775808"); -} +#include "basis/primitive.h" +#include "util/string_ex.h" -TEST(int2dec_test, group_sequence) +TEST(string_ex, strprintf) { - test_32_64_plus_minus(1, 1, L"1"); - test_32_64_plus_minus(12, 2, L"12"); - test_32_64_plus_minus(123, 3, L"123"); - test_32_64_plus_minus(1234, 4, L"1234"); - test_32_64_plus_minus(12345, 5, L"12345"); - test_32_64_plus_minus(123456, 6, L"123456"); - test_32_64_plus_minus(1234567, 7, L"1234567"); - test_32_64_plus_minus(12345678, 8, L"12345678"); - test_32_64_plus_minus(123456789, 9, L"123456789"); - test_32_64_plus_minus(1234567890, 10, L"1234567890"); + std::wstring text; + strprintf( text, L"%s-%d", L"test", 1 ); + ASSERT_STREQ(L"test-1", text.c_str()); } diff --git a/tests/unittests/test-string_ex.cpp b/tests/unittests/test-string_ex.cpp new file mode 100644 index 0000000000..96d532c5df --- /dev/null +++ b/tests/unittests/test-string_ex.cpp @@ -0,0 +1,79 @@ +#include + +#include + +#ifndef NOMINMAX +#define NOMINMAX +#endif /* #ifndef NOMINMAX */ + +#include +#include +#include "basis/primitive.h" +#include "util/string_ex2.h" + +template +void test_int2dec(T value, ptrdiff_t lenExpected, const wchar_t* strExpected) +{ + wchar_t buff[int2dec_destBufferSufficientLength()]; + ptrdiff_t len = int2dec(value, buff); + EXPECT_EQ(len, lenExpected); + EXPECT_STREQ(buff, strExpected); +} + +template +void test_plusminus(T plusValue, ptrdiff_t lenExpected, const wchar_t* strExpected) +{ + test_int2dec(plusValue, lenExpected, strExpected); + test_int2dec(-plusValue, 1+lenExpected, (std::wstring(L"-")+strExpected).c_str()); +} + +static +void test_32_64_plus_minus(int value, ptrdiff_t lenExpected, const wchar_t* strExpected) +{ + test_plusminus(value, lenExpected, strExpected); + test_plusminus(value, lenExpected, strExpected); +} + +TEST(int2dec_test, zero) +{ + test_int2dec(0, 1, L"0"); + test_int2dec(0, 1, L"0"); +} + +TEST(int2dec_test, digits) +{ + test_32_64_plus_minus(2, 1, L"2"); + test_32_64_plus_minus(3, 1, L"3"); + test_32_64_plus_minus(4, 1, L"4"); + test_32_64_plus_minus(5, 1, L"5"); + test_32_64_plus_minus(6, 1, L"6"); + test_32_64_plus_minus(7, 1, L"7"); + test_32_64_plus_minus(8, 1, L"8"); + test_32_64_plus_minus(9, 1, L"9"); +} + +TEST(int2dec_test, max) +{ + test_int2dec(std::numeric_limits::max(), 10, L"2147483647"); + test_int2dec(std::numeric_limits::max(), 19, L"9223372036854775807"); +} + +TEST(int2dec_test, min) +{ + test_int2dec(std::numeric_limits::min(), 11, L"-2147483648"); + test_int2dec(std::numeric_limits::min(), 20, L"-9223372036854775808"); +} + +TEST(int2dec_test, group_sequence) +{ + test_32_64_plus_minus(1, 1, L"1"); + test_32_64_plus_minus(12, 2, L"12"); + test_32_64_plus_minus(123, 3, L"123"); + test_32_64_plus_minus(1234, 4, L"1234"); + test_32_64_plus_minus(12345, 5, L"12345"); + test_32_64_plus_minus(123456, 6, L"123456"); + test_32_64_plus_minus(1234567, 7, L"1234567"); + test_32_64_plus_minus(12345678, 8, L"12345678"); + test_32_64_plus_minus(123456789, 9, L"123456789"); + test_32_64_plus_minus(1234567890, 10, L"1234567890"); +} diff --git a/tests/unittests/tests1.vcxproj b/tests/unittests/tests1.vcxproj index 2da2e36862..7106bff7db 100644 --- a/tests/unittests/tests1.vcxproj +++ b/tests/unittests/tests1.vcxproj @@ -1,146 +1,147 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - {701e3407-ec27-49f7-adc7-520cf2b4b438} - Win32Proj - - - - - Application - false - Unicode - - - - - - - - - (ProjectDir)..\..\..\build\$(Platform)\$(Configuration)\unittests\ - $(Platform)\$(Configuration)\ - - - - - - $(SolutionDir)sakura_core;%(AdditionalIncludeDirectories) - _CONSOLE;%(PreprocessorDefinitions) - NotUsing - true - stdcpp17 - /source-charset:utf-8 /execution-charset:shift_jis %(AdditionalOptions) - - - Console - comctl32.lib;Imm32.lib;mpr.lib;imagehlp.lib;Shlwapi.lib;%(AdditionalDependencies) - true - - - - - _DEBUG;%(PreprocessorDefinitions) - MultiThreadedDebug - EnableFastChecks - Disabled - - - false - - - - - NDEBUG;%(PreprocessorDefinitions) - MultiThreaded - ProgramDatabase - - - UseFastLinkTimeCodeGeneration - true - true - - - - - WIN32;%(PreprocessorDefinitions) - - - true - - - - - X64;%(PreprocessorDefinitions) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {af03508c-515e-4a0e-87be-67ed1e254bd0} - - - {7a6d0f29-e560-4985-835b-5f92a08eb242} - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {701e3407-ec27-49f7-adc7-520cf2b4b438} + Win32Proj + + + + + Application + false + Unicode + + + + + + + + + (ProjectDir)..\..\..\build\$(Platform)\$(Configuration)\unittests\ + $(Platform)\$(Configuration)\ + + + + + + $(SolutionDir)sakura_core;%(AdditionalIncludeDirectories) + _CONSOLE;%(PreprocessorDefinitions) + NotUsing + true + stdcpp17 + /source-charset:utf-8 /execution-charset:shift_jis %(AdditionalOptions) + + + Console + comctl32.lib;Imm32.lib;mpr.lib;imagehlp.lib;Shlwapi.lib;%(AdditionalDependencies) + true + + + + + _DEBUG;%(PreprocessorDefinitions) + MultiThreadedDebug + EnableFastChecks + Disabled + + + false + + + + + NDEBUG;%(PreprocessorDefinitions) + MultiThreaded + ProgramDatabase + + + UseFastLinkTimeCodeGeneration + true + true + + + + + WIN32;%(PreprocessorDefinitions) + + + true + + + + + X64;%(PreprocessorDefinitions) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {af03508c-515e-4a0e-87be-67ed1e254bd0} + + + {7a6d0f29-e560-4985-835b-5f92a08eb242} + + + + + + \ No newline at end of file diff --git a/tests/unittests/tests1.vcxproj.filters b/tests/unittests/tests1.vcxproj.filters index 0ffb1ec742..87f26be228 100644 --- a/tests/unittests/tests1.vcxproj.filters +++ b/tests/unittests/tests1.vcxproj.filters @@ -1,86 +1,89 @@ - - - - - {690c7184-b796-460a-8fed-595dfa1930f4} - - - {594146a8-ae7c-401b-a064-af37dac4216e} - - - {0eefa0df-ca7f-489c-9844-9a182e1dba18} - - - - - Other Files - - - - - Test Files - - - Test Files - - - Test Files - - - Test Files - - - Test Files - - - Test Files - - - Test Files - - - Test Files - - - Test Files - - - Test Files - - - Test Files - - - Test Files - - - Test Files - - - Other Files - - - Test Files - - - Test Files - - - Test Files - - - Other Files - - - Test Files - - - Test Files - - - - - Other Files - - + + + + + {690c7184-b796-460a-8fed-595dfa1930f4} + + + {594146a8-ae7c-401b-a064-af37dac4216e} + + + {0eefa0df-ca7f-489c-9844-9a182e1dba18} + + + + + Other Files + + + + + Test Files + + + Test Files + + + Test Files + + + Test Files + + + Test Files + + + Test Files + + + Test Files + + + Test Files + + + Test Files + + + Test Files + + + Test Files + + + Test Files + + + Test Files + + + Other Files + + + Test Files + + + Test Files + + + Test Files + + + Other Files + + + Test Files + + + Test Files + + + Test Files + + + + + Other Files + + \ No newline at end of file