mirror of
https://github.com/Xaymar/obs-StreamFX
synced 2024-11-27 22:03:01 +00:00
utility: Add recursive text lookup function
Similar to the one used in enc-amf, allows looking up string containing other strings, thus drastically simplifying the necessary time to build translated strings, while also caching them for future use.
This commit is contained in:
parent
3e62416af2
commit
45728702ff
2 changed files with 66 additions and 0 deletions
|
@ -18,3 +18,65 @@
|
|||
*/
|
||||
|
||||
#include "utility.hpp"
|
||||
#include <sstream>
|
||||
#include "plugin.hpp"
|
||||
|
||||
// OBS
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4201)
|
||||
#endif
|
||||
#include <obs.h>
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
const char* obs_module_recursive_text(const char* to_translate, size_t depth)
|
||||
{
|
||||
static std::unordered_map<std::string, std::string> translate_map;
|
||||
|
||||
if (depth == 0) {
|
||||
return obs_module_text(to_translate);
|
||||
}
|
||||
|
||||
std::string key = to_translate;
|
||||
auto value = translate_map.find(std::string(key));
|
||||
if (value != translate_map.end()) {
|
||||
return value->second.c_str();
|
||||
} else {
|
||||
std::string orig = obs_module_text(to_translate);
|
||||
std::stringstream out;
|
||||
|
||||
{
|
||||
size_t seq_start, seq_end = 0;
|
||||
bool seq_got = false;
|
||||
|
||||
for (size_t pos = 0; pos <= orig.length(); pos++) {
|
||||
std::string chr = orig.substr(pos, 2);
|
||||
if (chr == "\\@") {
|
||||
if (seq_got) {
|
||||
out << obs_module_recursive_text(orig.substr(seq_start, pos - seq_start).c_str(), (depth - 1));
|
||||
seq_end = pos + 2;
|
||||
} else {
|
||||
out << orig.substr(seq_end, pos - seq_end);
|
||||
seq_start = pos + 2;
|
||||
}
|
||||
seq_got = !seq_got;
|
||||
pos += 1;
|
||||
}
|
||||
}
|
||||
if (seq_end != orig.length()) {
|
||||
out << orig.substr(seq_end, orig.length() - seq_end);
|
||||
}
|
||||
|
||||
translate_map.insert({key, out.str()});
|
||||
}
|
||||
|
||||
auto value = translate_map.find(key);
|
||||
if (value != translate_map.end()) {
|
||||
return value->second.c_str();
|
||||
} else {
|
||||
throw std::runtime_error("Insert into map failed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,10 @@
|
|||
|
||||
#pragma once
|
||||
#include <type_traits>
|
||||
#include <cinttypes>
|
||||
#include <limits>
|
||||
|
||||
const char* obs_module_recursive_text(const char* to_translate, size_t depth = std::numeric_limits<size_t>::max());
|
||||
|
||||
template<typename Enum>
|
||||
struct enable_bitmask_operators {
|
||||
|
|
Loading…
Reference in a new issue