diff --git a/UnitTests/UnitTests.vcxproj b/UnitTests/UnitTests.vcxproj index 5bbbf4303..6ad2de970 100644 --- a/UnitTests/UnitTests.vcxproj +++ b/UnitTests/UnitTests.vcxproj @@ -128,6 +128,7 @@ + diff --git a/UnitTests/UnitTests.vcxproj.filters b/UnitTests/UnitTests.vcxproj.filters index 48ea9a45d..5e390340c 100644 --- a/UnitTests/UnitTests.vcxproj.filters +++ b/UnitTests/UnitTests.vcxproj.filters @@ -45,6 +45,9 @@ Source Files + + Source Files + diff --git a/UnitTests/string.cpp b/UnitTests/string.cpp new file mode 100644 index 000000000..671a0913f --- /dev/null +++ b/UnitTests/string.cpp @@ -0,0 +1,37 @@ +/* + SPDX-License-Identifier: MIT + Copyright © 2023-2024 Amebis +*/ + +#include "pch.hpp" + +using namespace std; +#ifdef _WIN32 +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +#endif + +namespace UnitTests +{ + TEST_CLASS(string) + { + public: + TEST_METHOD(sprintf) + { + Assert::AreEqual(L"This is a test.", stdex::sprintf(L"This is %s.", stdex::locale_default, L"a test").c_str()); + Assert::AreEqual(15, stdex::sprintf(L"This is %s.", stdex::locale_default, L"a test").size()); + Assert::AreEqual("This is a test.", stdex::sprintf("This is %s.", stdex::locale_default, "a test").c_str()); + Assert::AreEqual(15, stdex::sprintf("This is %s.", stdex::locale_default, "a test").size()); + + wstring wstr; + std::string str; + for (size_t i = 0; i < 2000; i++) { + wstr += L"🐔Test🐮\r\n"; + str += "🐔Test🐮\r\n"; + } + Assert::AreEqual(wstr.c_str(), stdex::sprintf(L"%s", stdex::locale_default, wstr.data()).c_str()); + Assert::AreEqual(wstr.size(), stdex::sprintf(L"%s", stdex::locale_default, wstr.data()).size()); + Assert::AreEqual(str.c_str(), stdex::sprintf("%s", stdex::locale_utf8, str.data()).c_str()); + Assert::AreEqual(str.size(), stdex::sprintf("%s", stdex::locale_utf8, str.data()).size()); + } + }; +} diff --git a/include/stdex/string.hpp b/include/stdex/string.hpp index a1e46d95f..eaeadcf6b 100644 --- a/include/stdex/string.hpp +++ b/include/stdex/string.hpp @@ -2325,7 +2325,6 @@ namespace stdex inline int vsnprintf(_Out_z_cap_(capacity) char* str, _In_ size_t capacity, _In_z_ _Printf_format_string_params_(2) const char* format, _In_opt_ locale_t locale, _In_ va_list arg) { #ifdef _WIN32 - // Don't use _vsnprintf_s(). It terminates the string even if we want to print to the edge of the buffer. #pragma warning(suppress: 4996) return _vsnprintf_l(str, capacity, format, locale, arg); #else @@ -2336,7 +2335,6 @@ namespace stdex inline int vsnprintf(_Out_z_cap_(capacity) wchar_t* str, _In_ size_t capacity, _In_z_ _Printf_format_string_params_(2) const wchar_t* format, _In_opt_ locale_t locale, _In_ va_list arg) { #ifdef _WIN32 - // Don't use _vsnwprintf_s(). It terminates the string even if we want to print to the edge of the buffer. #pragma warning(suppress: 4996) return _vsnwprintf_l(str, capacity, format, locale, arg); #else