From 44558a6059e75be02e73d70cb28fa031b6085c4d Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Sat, 1 Jun 2019 13:59:37 -0400 Subject: [PATCH] change to format strings --- extensions/lua.cpp | 11 +++++------ extensions/regexfilter.cpp | 2 +- extensions/util.h | 4 +--- include/common.h | 23 +++++++++++++++++++++-- text.cpp | 12 ++++++------ 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/extensions/lua.cpp b/extensions/lua.cpp index 0b930d5..c9346bd 100644 --- a/extensions/lua.cpp +++ b/extensions/lua.cpp @@ -63,7 +63,7 @@ struct : QMainWindow }; connect(loadButton, &QPushButton::clicked, [=](bool) { - ++revCount; + revCount += 1; script->assign(scriptEditor->toPlainText().toUtf8()); save(); }); @@ -103,8 +103,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) { - thread_local static std::unique_ptr> L_(luaL_newstate()); - thread_local static lua_State* L = L_.get(); + thread_local static struct { std::unique_ptr> L{ luaL_newstate() }; operator lua_State*() { return L.get(); } } L; thread_local static auto _ = (luaL_openlibs(L), luaL_dostring(L, "function ProcessSentence() end")); thread_local static int revCount = 0; @@ -114,7 +113,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) luaL_dostring(L, "ProcessSentence = nil"); if (luaL_dostring(L, script->c_str()) != LUA_OK) { - sentence += NEWLINE + LUA_ERROR + StringToWideString(lua_tolstring(L, 1, nullptr)); + sentence += L"\n" + FormatWideString(LUA_ERROR, StringToWideString(lua_tolstring(L, 1, nullptr)).c_str()); lua_settop(L, 0); return logErrors; } @@ -122,7 +121,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) if (lua_getglobal(L, "ProcessSentence") != LUA_TFUNCTION) { - sentence += NEWLINE + LUA_ERROR + L"ProcessSentence is not a function"; + sentence += L"\n" + FormatWideString(LUA_ERROR, L"ProcessSentence is not a function"); lua_settop(L, 0); return logErrors; } @@ -136,7 +135,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) } if (lua_pcallk(L, 2, 1, 0, NULL, NULL) != LUA_OK) { - sentence += NEWLINE + LUA_ERROR + StringToWideString(lua_tolstring(L, 1, nullptr)); + sentence += L"\n" + FormatWideString(LUA_ERROR, StringToWideString(lua_tolstring(L, 1, nullptr)).c_str()); lua_settop(L, 0); return logErrors; } diff --git a/extensions/regexfilter.cpp b/extensions/regexfilter.cpp index f2b782e..cff1c53 100644 --- a/extensions/regexfilter.cpp +++ b/extensions/regexfilter.cpp @@ -28,7 +28,7 @@ struct : QMainWindow std::lock_guard l(m); try { regex = newRegex.toStdWString(); } catch (...) { return output->setText(INVALID_REGEX); } - output->setText(CURRENT_FILTER + newRegex); + output->setText(QString(CURRENT_FILTER).arg(newRegex)); }); resize(350, 60); setCentralWidget(centralWidget); diff --git a/extensions/util.h b/extensions/util.h index 280d43a..fe9ced7 100644 --- a/extensions/util.h +++ b/extensions/util.h @@ -6,7 +6,7 @@ class RateLimiter { public: RateLimiter(int requests, int delay) : requestsLeft(requests), delay(delay) {} - bool Request() { CreateTimerQueueTimer(&DUMMY, timerQueue, [](void* This, BOOLEAN) { ++((RateLimiter*)This)->requestsLeft; }, this, delay, 0, 0); return --requestsLeft > 0; } + bool Request() { CreateTimerQueueTimer(&DUMMY, timerQueue, [](void* This, BOOLEAN) { ((RateLimiter*)This)->requestsLeft += 1; }, this, delay, 0, 0); return --requestsLeft > 0; } int delay; private: @@ -28,5 +28,3 @@ inline std::string WideStringToString(const std::wstring& text) WideCharToMultiByte(CP_UTF8, 0, text.c_str(), -1, buffer.data(), buffer.size(), nullptr, nullptr); return buffer.data(); } - -inline const std::wstring NEWLINE = L"\n"; diff --git a/include/common.h b/include/common.h index cae90f6..3d497c4 100644 --- a/include/common.h +++ b/include/common.h @@ -72,14 +72,33 @@ private: std::unique_ptr h; }; +#pragma warning(push) +#pragma warning(disable: 4996) +template +inline std::string FormatString(const char* format, Args... args) +{ + std::string buffer(snprintf(nullptr, 0, format, args...), '\0'); + sprintf(buffer.data(), format, args...); + return buffer; +} + +template +inline std::wstring FormatWideString(const wchar_t* format, Args... args) +{ + std::wstring buffer(_snwprintf(nullptr, 0, format, args...), L'\0'); + _swprintf(buffer.data(), format, args...); + return buffer; +} +#pragma warning(pop) + #ifdef _DEBUG -#define TEST(...) inline auto TEST__RUNNER__DUMMY = CreateThread(nullptr, 0, [](auto) { __VA_ARGS__; return 0UL; }, NULL, 0, nullptr); +#define TEST(...) inline auto TEST_RUNNER_DUMMY = CreateThread(nullptr, 0, [](auto) { __VA_ARGS__; return 0UL; }, NULL, 0, nullptr); #else #define TEST(...) #endif #ifdef _DEBUG -#define TEST_SYNC(...) inline auto TEST__RUNNER__DUMMY = std::invoke([] { __VA_ARGS__; return 0UL; }); +#define TEST_SYNC(...) inline auto TEST_RUNNER_DUMMY = [] { __VA_ARGS__; return 0UL; }(); #else #define TEST_SYNC(...) #endif diff --git a/text.cpp b/text.cpp index 0acc8f3..866fc90 100644 --- a/text.cpp +++ b/text.cpp @@ -111,10 +111,10 @@ function ProcessSentence(sentence, sentenceInfo) --Your code here... end)"; const char* LOAD_LUA_SCRIPT = u8"Load Script"; -const wchar_t* LUA_ERROR = L"Lua error: "; +const wchar_t* LUA_ERROR = L"Lua error: %s"; const char* REGEX_FILTER = u8"Regex Filter"; const char* INVALID_REGEX = u8"Invalid regex"; -const char* CURRENT_FILTER = u8"Currently filtering: "; +const char* CURRENT_FILTER = u8"Currently filtering: %1"; const wchar_t* REPLACER_INSTRUCTIONS = LR"(This file only does anything when the "Replacer" extension is used. Replacement commands must be formatted like this: |ORIG|original_text|BECOMES|replacement_text|END| @@ -224,7 +224,7 @@ Clic y arrastra los bordes de la ventana para moverla, o en la esquina inferior TOPMOST = u8"Siempre visible"; REGEX_FILTER = u8"Filtro Regex"; INVALID_REGEX = u8"Regex inválido"; - CURRENT_FILTER = u8"Actualmente filtrando: "; + CURRENT_FILTER = u8"Actualmente filtrando: %1"; #endif // SPANISH #ifdef SIMPLIFIED_CHINESE @@ -291,7 +291,7 @@ Clic y arrastra los bordes de la ventana para moverla, o en la esquina inferior TOPMOST = u8"总是位于最上层"; REGEX_FILTER = u8"正则表达式过滤器"; INVALID_REGEX = u8"无效的正则表达式"; - CURRENT_FILTER = u8"当前过滤中: "; + CURRENT_FILTER = u8"当前过滤中: %1"; #endif // SIMPLIFIED_CHINESE #ifdef RUSSIAN @@ -381,10 +381,10 @@ function ProcessSentence(sentence, sentenceInfo) --Ваш код здесь... end)"; LOAD_LUA_SCRIPT = u8"Загрузить скрипт"; - LUA_ERROR = L"Ошибка Lua: "; + LUA_ERROR = L"Ошибка Lua: %s"; REGEX_FILTER = u8"Фильтр Regex"; INVALID_REGEX = u8"Неверный regex"; - CURRENT_FILTER = u8"Сейчас фильтруется: "; + CURRENT_FILTER = u8"Сейчас фильтруется: %1"; REPLACER_INSTRUCTIONS = LR"(Этот файл делает что-то только когда используется расширение "Replacer". Команды для замены должны выглядеть так: |ORIG|текст_оригинала|BECOMES|текст_замены|END|