diff --git a/src/PluginManager.cpp b/src/PluginManager.cpp index b73e975..73988cf 100644 --- a/src/PluginManager.cpp +++ b/src/PluginManager.cpp @@ -199,7 +199,17 @@ std::optional Plugin::getStartupScript() void PluginManager::performForceInstallAndUpdateAsync(const std::string& source) { std::thread([source]() { - performForceInstallAndUpdateSync(source); + try { + performForceInstallAndUpdateSync(source); + } + catch (const std::exception& e) { + std::cerr << "[ BetterNCM ] [Plugin Remote Tasks] Update failed on " + << source << ": " << e.what() << std::endl; + } + catch (...) { + std::cerr << "[ BetterNCM ] [Plugin Remote Tasks] Unknown update error on " + << source << std::endl; + } }).detach(); } diff --git a/src/utils/BNString.hpp b/src/utils/BNString.hpp index 5173d47..822907b 100644 --- a/src/utils/BNString.hpp +++ b/src/utils/BNString.hpp @@ -201,12 +201,16 @@ class BNString : public std::wstring { return static_cast(index); } - [[nodiscard]] std::vector split(const std::wstring& delimiter) const { - std::vector result; + [[nodiscard]] std::vector split(const std::wstring& delimiter) const { + if (delimiter.empty()) { + throw std::invalid_argument("split delimiter must not be empty"); + } + + std::vector result; size_t start = 0; size_t end = 0; while ((end = this->find(delimiter, start)) != std::wstring::npos) { - result.push_back(this->substr(start, end - start)); + result.push_back(this->substr(start, end - start)); start = end + delimiter.length(); } result.push_back(this->substr(start)); diff --git a/tests/bnstring_split.cpp b/tests/bnstring_split.cpp new file mode 100644 index 0000000..c03f627 --- /dev/null +++ b/tests/bnstring_split.cpp @@ -0,0 +1,34 @@ +#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING +#include +#include +#include +#include +#include +#include "../src/utils/BNString.hpp" + +static void check(bool condition, const char* message) { + if (!condition) throw std::runtime_error(message); +} + +int main() { + const std::wstring url = L"https://gitcode.net/qq_21551787/bncm-plugin-packed/-/raw/master/"; + const BNString config(std::wstring(url + L"\n" + url)); + auto lines = config.split(L"\n"); + check(lines == std::vector{url, url}, "long URL contents"); + + // Results must own their data even after a temporary source is destroyed. + auto retained = BNString(std::wstring(url + L"\nshort")).split(L"\n"); + std::wstring reuse(4096, L'x'); + check(retained == std::vector{url, L"short"}, "source lifetime"); + check(BNString(std::wstring(L"a\n\nb\n")).split(L"\n") == + std::vector{L"a", L"", L"b", L""}, "empty fields"); + check(BNString(std::wstring(L"")).split(L"\n") == + std::vector{L""}, "empty source"); + check(BNString(std::wstring(L"a::b")).split(L"::") == + std::vector{L"a", L"b"}, "multi-character delimiter"); + bool rejected = false; + try { (void)config.split(L""); } + catch (const std::invalid_argument&) { rejected = true; } + check(rejected, "empty delimiter must be rejected"); + std::cout << "PASS: BNString split ownership and edge cases\n"; +}