From fd7d36e4be144cf7941a2fb81e9a4c990e270ca8 Mon Sep 17 00:00:00 2001 From: NaifBanana <30419422+NaifBanana@users.noreply.github.com> Date: Thu, 9 Apr 2026 17:44:19 -0500 Subject: [PATCH] Added more Util tests and str-wstr related changes to util functions --- engine/NBCore/Utils.hpp | 66 +++++++++++++++++++++---- engine/NBCore/src/Utils.cpp | 31 +++++------- engine/NBCore/tests/testUtils.cpp | 81 +++++++++++++++++++++++++++++-- 3 files changed, 145 insertions(+), 33 deletions(-) diff --git a/engine/NBCore/Utils.hpp b/engine/NBCore/Utils.hpp index 3151183..e51d3f5 100644 --- a/engine/NBCore/Utils.hpp +++ b/engine/NBCore/Utils.hpp @@ -1,4 +1,6 @@ #pragma once +#include +#include #ifndef _NB_CORE_TYPES #define _NB_CORE_TYPES @@ -9,20 +11,65 @@ namespace nb { #ifdef _NB_TARGET_WINDOWS - const std::string NEWLINE("\r\n"); + const std::string NEWLINE; + const std::wstring WNEWLINE; #endif // _NB_TARGET_WINDOWS #ifdef _NB_TARGET_LINUX - const std::string NEWLINE("\n"); + const std::string NEWLINE; + const std::wstring WNEWLINE; #endif // _NB_TARGET_LINUX -template -struct ConstexprMap { - template - constexpr ConstexprMap(Input inp) : _data{inp} {} +template +std::enable_if_t, std::wstring> str_to_wstr(const T& str) { + std::mbstate_t state = std::mbstate_t(); + const char* cstr = std::string(str).c_str(); + std::size_t wlen = 1 + std::mbsrtowcs(nullptr, &cstr, 0, &state); + wchar_t* c_wstr= new wchar_t[wlen]; + std::mbsrtowcs(c_wstr, &cstr, wlen, &state); + std::wstring ret(c_wstr, wlen); + delete[] c_wstr; + return ret; +} -protected: - const std::array, N> _data; -}; +template +std::enable_if_t, std::string> wstr_to_str(const T& wstr) { + const wchar_t* c_wstr = std::wstring(wstr).c_str(); + std::size_t strlen = 1 + std::wcstombs(nullptr, c_wstr, 0); + char* c_str= new char[strlen]; + std::wcstombs(c_str, c_wstr, strlen); + std::string ret(c_str, strlen); + delete[] c_str; + return ret; +} + +template +T find_and_replace( + const A& original, + const B& find, + const C& replace +) { + const T& original_t = T(original); + const T& find_t = T(find); + const T& replace_t = T(replace); + + T ret = original_t; + + std::size_t find_len = find_t.length(); + std::size_t replace_len = replace_t.length(); + std::size_t currpos = 0; + std::size_t lastpos = 0; + while(true) { + lastpos = currpos; + currpos = original_t.find(find_t, lastpos); + if (currpos == T::npos) { + break; + } + ret = ret.erase(currpos, find_len); + ret = ret.insert(currpos, replace_t); + currpos += replace_len; + } + return ret; +} /* template T swap_endian(const T& val) { @@ -37,7 +84,6 @@ T swap_endian(const T& val) { return ret; } */ -std::string find_and_replace(const std::string& original, const std::string& find, const std::string& replace); // using ByteVector = std::vector; diff --git a/engine/NBCore/src/Utils.cpp b/engine/NBCore/src/Utils.cpp index f060189..f94f897 100644 --- a/engine/NBCore/src/Utils.cpp +++ b/engine/NBCore/src/Utils.cpp @@ -1,23 +1,14 @@ +#include +#include #include + #include "Utils.hpp" - -std::string nb::find_and_replace(const std::string& original, const std::string& find, const std::string& replace) { - std::string ret = original; - - std::size_t find_len = find.length(); - std::size_t replace_len = replace.length(); - std::size_t currpos = 0; - std::size_t lastpos = 0; - while(true) { - lastpos = currpos; - currpos = original.find(find, lastpos); - if (currpos == std::string::npos) { - break; - } - ret = ret.erase(currpos, find_len); - ret = ret.insert(currpos, replace); - currpos += replace_len; - } - return ret; -} \ No newline at end of file +#ifdef _NB_TARGET_WINDOWS + const std::string NEWLINE("\r\n"); + const std::wstring WNEWLINE(L"\r\n"); +#endif // _NB_TARGET_WINDOWS +#ifdef _NB_TARGET_LINUX + const std::string NEWLINE("\n"); + const std::wstring WNEWLINE(L"\n"); +#endif // _NB_TARGET_LINUX diff --git a/engine/NBCore/tests/testUtils.cpp b/engine/NBCore/tests/testUtils.cpp index 1ef1969..042e7a0 100644 --- a/engine/NBCore/tests/testUtils.cpp +++ b/engine/NBCore/tests/testUtils.cpp @@ -1,17 +1,20 @@ +#include #define CODE_ERROR_LOCATIONS -#include "Utils.hpp" #include +#include +#include "Utils.hpp" + namespace nb { -TEST(UtilsTest, Test) { +TEST(UtilsTest, TestFindAndReplace) { ASSERT_STREQ( find_and_replace("Jeff", "e", "efe").c_str(), "Jefeff" ); - auto tmp = find_and_replace("Naif", "a", "afa"); + std::string tmp = find_and_replace("Naif", "a", "afa"); ASSERT_STREQ( find_and_replace(tmp, "i", "ifi").c_str(), "Nafaifif" @@ -38,4 +41,76 @@ TEST(UtilsTest, Test) { ); } +TEST(UtilsTest, TestFindAndReplaceWstr) { + ASSERT_STREQ( + find_and_replace(L"Jeff", L"e", L"efe").c_str(), + L"Jefeff" + ); + + std::wstring tmp = find_and_replace(L"Naif", L"a", L"afa"); + ASSERT_STREQ( + find_and_replace(tmp, L"i", L"ifi").c_str(), + L"Nafaifif" + ); + + tmp = find_and_replace(L"aeiou", L"a", L"afa"); + tmp = find_and_replace(tmp, L"e", L"efe"); + tmp = find_and_replace(tmp, L"i", L"ifi"); + tmp = find_and_replace(tmp, L"o", L"ofo"); + tmp = find_and_replace(tmp, L"u", L"ufu"); + ASSERT_STREQ( + tmp.c_str(), + L"afaefeifiofoufu" + ); + + tmp = find_and_replace(tmp, L"afa", L"a"); + tmp = find_and_replace(tmp, L"efe", L"e"); + tmp = find_and_replace(tmp, L"ifi", L"i"); + tmp = find_and_replace(tmp, L"ofo", L"o"); + tmp = find_and_replace(tmp, L"ufu", L"u"); + ASSERT_STREQ( + tmp.c_str(), + L"aeiou" + ); +} + +TEST(UtilsTest, TestWstrToStr) { + ASSERT_STREQ( + nb::wstr_to_str(L"Hi!").c_str(), + "Hi!" + ); + ASSERT_STREQ( + nb::wstr_to_str(L"Naif").c_str(), + "Naif" + ); + ASSERT_STREQ( + nb::wstr_to_str(L"\r\r\r\n").c_str(), + "\r\r\r\n" + ); + ASSERT_STREQ( + nb::wstr_to_str(L"Naif\ttalks\r\ra\t\nlot").c_str(), + "Naif\ttalks\r\ra\t\nlot" + ); +} + +TEST(UtilsTest, TestStrToWstr) { + ASSERT_STREQ( + nb::str_to_wstr("Hi!").c_str(), + L"Hi!" + ); + ASSERT_STREQ( + nb::str_to_wstr("Naif").c_str(), + L"Naif" + ); + ASSERT_STREQ( + nb::str_to_wstr("\r\r\r\n").c_str(), + L"\r\r\r\n" + ); + ASSERT_STREQ( + nb::str_to_wstr("Naif\ttalks\r\ra\t\nlot").c_str(), + L"Naif\ttalks\r\ra\t\nlot" + ); +} + + } // namespace nb \ No newline at end of file