change to format strings

This commit is contained in:
Akash Mozumdar 2019-06-01 13:59:37 -04:00
parent 096a1b49a7
commit 44558a6059
5 changed files with 34 additions and 18 deletions

View File

@ -63,7 +63,7 @@ struct : QMainWindow
}; };
connect(loadButton, &QPushButton::clicked, [=](bool) connect(loadButton, &QPushButton::clicked, [=](bool)
{ {
++revCount; revCount += 1;
script->assign(scriptEditor->toPlainText().toUtf8()); script->assign(scriptEditor->toPlainText().toUtf8());
save(); save();
}); });
@ -103,8 +103,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved
bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
{ {
thread_local static std::unique_ptr<lua_State, Functor<lua_close>> L_(luaL_newstate()); thread_local static struct { std::unique_ptr<lua_State, Functor<lua_close>> L{ luaL_newstate() }; operator lua_State*() { return L.get(); } } L;
thread_local static lua_State* L = L_.get();
thread_local static auto _ = (luaL_openlibs(L), luaL_dostring(L, "function ProcessSentence() end")); thread_local static auto _ = (luaL_openlibs(L), luaL_dostring(L, "function ProcessSentence() end"));
thread_local static int revCount = 0; thread_local static int revCount = 0;
@ -114,7 +113,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
luaL_dostring(L, "ProcessSentence = nil"); luaL_dostring(L, "ProcessSentence = nil");
if (luaL_dostring(L, script->c_str()) != LUA_OK) 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); lua_settop(L, 0);
return logErrors; return logErrors;
} }
@ -122,7 +121,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
if (lua_getglobal(L, "ProcessSentence") != LUA_TFUNCTION) 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); lua_settop(L, 0);
return logErrors; return logErrors;
} }
@ -136,7 +135,7 @@ bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo)
} }
if (lua_pcallk(L, 2, 1, 0, NULL, NULL) != LUA_OK) 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); lua_settop(L, 0);
return logErrors; return logErrors;
} }

View File

@ -28,7 +28,7 @@ struct : QMainWindow
std::lock_guard l(m); std::lock_guard l(m);
try { regex = newRegex.toStdWString(); } try { regex = newRegex.toStdWString(); }
catch (...) { return output->setText(INVALID_REGEX); } catch (...) { return output->setText(INVALID_REGEX); }
output->setText(CURRENT_FILTER + newRegex); output->setText(QString(CURRENT_FILTER).arg(newRegex));
}); });
resize(350, 60); resize(350, 60);
setCentralWidget(centralWidget); setCentralWidget(centralWidget);

View File

@ -6,7 +6,7 @@ class RateLimiter
{ {
public: public:
RateLimiter(int requests, int delay) : requestsLeft(requests), delay(delay) {} 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; int delay;
private: 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); WideCharToMultiByte(CP_UTF8, 0, text.c_str(), -1, buffer.data(), buffer.size(), nullptr, nullptr);
return buffer.data(); return buffer.data();
} }
inline const std::wstring NEWLINE = L"\n";

View File

@ -72,14 +72,33 @@ private:
std::unique_ptr<void, HandleCleaner> h; std::unique_ptr<void, HandleCleaner> h;
}; };
#pragma warning(push)
#pragma warning(disable: 4996)
template <typename... Args>
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 <typename... Args>
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 #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 #else
#define TEST(...) #define TEST(...)
#endif #endif
#ifdef _DEBUG #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 #else
#define TEST_SYNC(...) #define TEST_SYNC(...)
#endif #endif

View File

@ -111,10 +111,10 @@ function ProcessSentence(sentence, sentenceInfo)
--Your code here... --Your code here...
end)"; end)";
const char* LOAD_LUA_SCRIPT = u8"Load Script"; 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* REGEX_FILTER = u8"Regex Filter";
const char* INVALID_REGEX = u8"Invalid regex"; 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. const wchar_t* REPLACER_INSTRUCTIONS = LR"(This file only does anything when the "Replacer" extension is used.
Replacement commands must be formatted like this: Replacement commands must be formatted like this:
|ORIG|original_text|BECOMES|replacement_text|END| |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"; TOPMOST = u8"Siempre visible";
REGEX_FILTER = u8"Filtro Regex"; REGEX_FILTER = u8"Filtro Regex";
INVALID_REGEX = u8"Regex inválido"; INVALID_REGEX = u8"Regex inválido";
CURRENT_FILTER = u8"Actualmente filtrando: "; CURRENT_FILTER = u8"Actualmente filtrando: %1";
#endif // SPANISH #endif // SPANISH
#ifdef SIMPLIFIED_CHINESE #ifdef SIMPLIFIED_CHINESE
@ -291,7 +291,7 @@ Clic y arrastra los bordes de la ventana para moverla, o en la esquina inferior
TOPMOST = u8"总是位于最上层"; TOPMOST = u8"总是位于最上层";
REGEX_FILTER = u8"正则表达式过滤器"; REGEX_FILTER = u8"正则表达式过滤器";
INVALID_REGEX = u8"无效的正则表达式"; INVALID_REGEX = u8"无效的正则表达式";
CURRENT_FILTER = u8"当前过滤中: "; CURRENT_FILTER = u8"当前过滤中: %1";
#endif // SIMPLIFIED_CHINESE #endif // SIMPLIFIED_CHINESE
#ifdef RUSSIAN #ifdef RUSSIAN
@ -381,10 +381,10 @@ function ProcessSentence(sentence, sentenceInfo)
--Ваш код здесь... --Ваш код здесь...
end)"; end)";
LOAD_LUA_SCRIPT = u8"Загрузить скрипт"; LOAD_LUA_SCRIPT = u8"Загрузить скрипт";
LUA_ERROR = L"Ошибка Lua: "; LUA_ERROR = L"Ошибка Lua: %s";
REGEX_FILTER = u8"Фильтр Regex"; REGEX_FILTER = u8"Фильтр Regex";
INVALID_REGEX = u8"Неверный regex"; INVALID_REGEX = u8"Неверный regex";
CURRENT_FILTER = u8"Сейчас фильтруется: "; CURRENT_FILTER = u8"Сейчас фильтруется: %1";
REPLACER_INSTRUCTIONS = LR"(Этот файл делает что-то только когда используется расширение "Replacer". REPLACER_INSTRUCTIONS = LR"(Этот файл делает что-то только когда используется расширение "Replacer".
Команды для замены должны выглядеть так: Команды для замены должны выглядеть так:
|ORIG|текст_оригинала|BECOMES|текстамены|END| |ORIG|текст_оригинала|BECOMES|текстамены|END|