mirror of
https://github.com/MetaCubeX/subconverter.git
synced 2025-11-04 18:19:42 +08:00
Optimize codes
This commit is contained in:
parent
0e07f44f07
commit
cb409ddf7e
@ -30,7 +30,7 @@ int importItems(string_array &target, bool scope_limit)
|
|||||||
unsigned int itemCount = 0;
|
unsigned int itemCount = 0;
|
||||||
for(std::string &x : target)
|
for(std::string &x : target)
|
||||||
{
|
{
|
||||||
if(x.find("!!import:") == x.npos)
|
if(x.find("!!import:") == std::string::npos)
|
||||||
{
|
{
|
||||||
result.emplace_back(x);
|
result.emplace_back(x);
|
||||||
continue;
|
continue;
|
||||||
@ -46,7 +46,7 @@ int importItems(string_array &target, bool scope_limit)
|
|||||||
content = webGet(path, proxy, global.cacheConfig);
|
content = webGet(path, proxy, global.cacheConfig);
|
||||||
else
|
else
|
||||||
writeLog(0, "File not found or not a valid URL: " + path, LOG_LEVEL_ERROR);
|
writeLog(0, "File not found or not a valid URL: " + path, LOG_LEVEL_ERROR);
|
||||||
if(!content.size())
|
if(content.empty())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
ss << content;
|
ss << content;
|
||||||
@ -98,7 +98,7 @@ void importItems(std::vector<toml::value> &root, const std::string &import_key,
|
|||||||
content = webGet(path, proxy, global.cacheConfig);
|
content = webGet(path, proxy, global.cacheConfig);
|
||||||
else
|
else
|
||||||
writeLog(0, "File not found or not a valid URL: " + path, LOG_LEVEL_ERROR);
|
writeLog(0, "File not found or not a valid URL: " + path, LOG_LEVEL_ERROR);
|
||||||
if(content.size())
|
if(!content.empty())
|
||||||
{
|
{
|
||||||
auto items = parseToml(content, path);
|
auto items = parseToml(content, path);
|
||||||
auto list = toml::find<std::vector<toml::value>>(items, import_key);
|
auto list = toml::find<std::vector<toml::value>>(items, import_key);
|
||||||
@ -110,32 +110,28 @@ void importItems(std::vector<toml::value> &root, const std::string &import_key,
|
|||||||
}
|
}
|
||||||
root.swap(newRoot);
|
root.swap(newRoot);
|
||||||
writeLog(0, "Imported " + std::to_string(count) + " item(s).");
|
writeLog(0, "Imported " + std::to_string(count) + " item(s).");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void readRegexMatch(YAML::Node node, const std::string &delimiter, string_array &dest, bool scope_limit = true)
|
void readRegexMatch(YAML::Node node, const std::string &delimiter, string_array &dest, bool scope_limit = true)
|
||||||
{
|
{
|
||||||
YAML::Node object;
|
for(auto && object : node)
|
||||||
std::string script, url, match, rep, strLine;
|
|
||||||
|
|
||||||
for(unsigned i = 0; i < node.size(); i++)
|
|
||||||
{
|
{
|
||||||
object = node[i];
|
std::string script, url, match, rep, strLine;
|
||||||
object["script"] >>= script;
|
object["script"] >>= script;
|
||||||
if(script.size())
|
if(!script.empty())
|
||||||
{
|
{
|
||||||
dest.emplace_back("!!script:" + script);
|
dest.emplace_back("!!script:" + script);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
object["import"] >>= url;
|
object["import"] >>= url;
|
||||||
if(url.size())
|
if(!url.empty())
|
||||||
{
|
{
|
||||||
dest.emplace_back("!!import:" + url);
|
dest.emplace_back("!!import:" + url);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
object["match"] >>= match;
|
object["match"] >>= match;
|
||||||
object["replace"] >>= rep;
|
object["replace"] >>= rep;
|
||||||
if(match.size() && rep.size())
|
if(!match.empty() && !rep.empty())
|
||||||
strLine = match + delimiter + rep;
|
strLine = match + delimiter + rep;
|
||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
@ -146,20 +142,17 @@ void readRegexMatch(YAML::Node node, const std::string &delimiter, string_array
|
|||||||
|
|
||||||
void readEmoji(YAML::Node node, string_array &dest, bool scope_limit = true)
|
void readEmoji(YAML::Node node, string_array &dest, bool scope_limit = true)
|
||||||
{
|
{
|
||||||
YAML::Node object;
|
for(auto && object : node)
|
||||||
std::string script, url, match, rep, strLine;
|
|
||||||
|
|
||||||
for(unsigned i = 0; i < node.size(); i++)
|
|
||||||
{
|
{
|
||||||
object = node[i];
|
std::string script, url, match, rep, strLine;
|
||||||
object["script"] >>= script;
|
object["script"] >>= script;
|
||||||
if(script.size())
|
if(!script.empty())
|
||||||
{
|
{
|
||||||
dest.emplace_back("!!script:" + script);
|
dest.emplace_back("!!script:" + script);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
object["import"] >>= url;
|
object["import"] >>= url;
|
||||||
if(url.size())
|
if(!url.empty())
|
||||||
{
|
{
|
||||||
url = "!!import:" + url;
|
url = "!!import:" + url;
|
||||||
dest.emplace_back(url);
|
dest.emplace_back(url);
|
||||||
@ -167,7 +160,7 @@ void readEmoji(YAML::Node node, string_array &dest, bool scope_limit = true)
|
|||||||
}
|
}
|
||||||
object["match"] >>= match;
|
object["match"] >>= match;
|
||||||
object["emoji"] >>= rep;
|
object["emoji"] >>= rep;
|
||||||
if(match.size() && rep.size())
|
if(!match.empty() && !rep.empty())
|
||||||
strLine = match + "," + rep;
|
strLine = match + "," + rep;
|
||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
@ -178,17 +171,12 @@ void readEmoji(YAML::Node node, string_array &dest, bool scope_limit = true)
|
|||||||
|
|
||||||
void readGroup(YAML::Node node, string_array &dest, bool scope_limit = true)
|
void readGroup(YAML::Node node, string_array &dest, bool scope_limit = true)
|
||||||
{
|
{
|
||||||
std::string strLine, name, type;
|
for(YAML::Node && object : node)
|
||||||
string_array tempArray;
|
|
||||||
YAML::Node object;
|
|
||||||
unsigned int i, j;
|
|
||||||
|
|
||||||
for(i = 0; i < node.size(); i++)
|
|
||||||
{
|
{
|
||||||
eraseElements(tempArray);
|
string_array tempArray;
|
||||||
object = node[i];
|
std::string name, type;
|
||||||
object["import"] >>= name;
|
object["import"] >>= name;
|
||||||
if(name.size())
|
if(!name.empty())
|
||||||
{
|
{
|
||||||
dest.emplace_back("!!import:" + name);
|
dest.emplace_back("!!import:" + name);
|
||||||
continue;
|
continue;
|
||||||
@ -202,7 +190,7 @@ void readGroup(YAML::Node node, string_array &dest, bool scope_limit = true)
|
|||||||
object["interval"] >>= interval;
|
object["interval"] >>= interval;
|
||||||
object["tolerance"] >>= tolerance;
|
object["tolerance"] >>= tolerance;
|
||||||
object["timeout"] >>= timeout;
|
object["timeout"] >>= timeout;
|
||||||
for(j = 0; j < object["rule"].size(); j++)
|
for(std::size_t j = 0; j < object["rule"].size(); j++)
|
||||||
tempArray.emplace_back(safe_as<std::string>(object["rule"][j]));
|
tempArray.emplace_back(safe_as<std::string>(object["rule"][j]));
|
||||||
switch(hash_(type))
|
switch(hash_(type))
|
||||||
{
|
{
|
||||||
@ -221,10 +209,7 @@ void readGroup(YAML::Node node, string_array &dest, bool scope_limit = true)
|
|||||||
tempArray.emplace_back(interval + "," + timeout + "," + tolerance);
|
tempArray.emplace_back(interval + "," + timeout + "," + tolerance);
|
||||||
}
|
}
|
||||||
|
|
||||||
strLine = std::accumulate(std::next(tempArray.begin()), tempArray.end(), tempArray[0], [](std::string a, std::string b) -> std::string
|
std::string strLine = join(tempArray, "`");
|
||||||
{
|
|
||||||
return std::move(a) + "`" + std::move(b);
|
|
||||||
});
|
|
||||||
dest.emplace_back(std::move(strLine));
|
dest.emplace_back(std::move(strLine));
|
||||||
}
|
}
|
||||||
importItems(dest, scope_limit);
|
importItems(dest, scope_limit);
|
||||||
@ -232,14 +217,11 @@ void readGroup(YAML::Node node, string_array &dest, bool scope_limit = true)
|
|||||||
|
|
||||||
void readRuleset(YAML::Node node, string_array &dest, bool scope_limit = true)
|
void readRuleset(YAML::Node node, string_array &dest, bool scope_limit = true)
|
||||||
{
|
{
|
||||||
std::string strLine, name, url, group, interval;
|
for(auto && object : node)
|
||||||
YAML::Node object;
|
|
||||||
|
|
||||||
for(unsigned int i = 0; i < node.size(); i++)
|
|
||||||
{
|
{
|
||||||
object = node[i];
|
std::string strLine, name, url, group, interval;
|
||||||
object["import"] >>= name;
|
object["import"] >>= name;
|
||||||
if(name.size())
|
if(!name.empty())
|
||||||
{
|
{
|
||||||
dest.emplace_back("!!import:" + name);
|
dest.emplace_back("!!import:" + name);
|
||||||
continue;
|
continue;
|
||||||
@ -248,13 +230,13 @@ void readRuleset(YAML::Node node, string_array &dest, bool scope_limit = true)
|
|||||||
object["group"] >>= group;
|
object["group"] >>= group;
|
||||||
object["rule"] >>= name;
|
object["rule"] >>= name;
|
||||||
object["interval"] >>= interval;
|
object["interval"] >>= interval;
|
||||||
if(url.size())
|
if(!url.empty())
|
||||||
{
|
{
|
||||||
strLine = group + "," + url;
|
strLine = group + "," + url;
|
||||||
if(interval.size())
|
if(!interval.empty())
|
||||||
strLine += "," + interval;
|
strLine += "," + interval;
|
||||||
}
|
}
|
||||||
else if(name.size())
|
else if(!name.empty())
|
||||||
strLine = group + ",[]" + name;
|
strLine = group + ",[]" + name;
|
||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@ -190,7 +190,7 @@ export function require (path) {
|
|||||||
class qjs_fetch_Headers
|
class qjs_fetch_Headers
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
qjs_fetch_Headers() {}
|
qjs_fetch_Headers() = default;
|
||||||
|
|
||||||
string_icase_map headers;
|
string_icase_map headers;
|
||||||
|
|
||||||
@ -218,7 +218,7 @@ public:
|
|||||||
class qjs_fetch_Request
|
class qjs_fetch_Request
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
qjs_fetch_Request() {}
|
qjs_fetch_Request() = default;
|
||||||
std::string method = "GET";
|
std::string method = "GET";
|
||||||
std::string url;
|
std::string url;
|
||||||
std::string proxy;
|
std::string proxy;
|
||||||
@ -231,7 +231,7 @@ public:
|
|||||||
class qjs_fetch_Response
|
class qjs_fetch_Response
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
qjs_fetch_Response() {}
|
qjs_fetch_Response() = default;
|
||||||
int status_code = 200;
|
int status_code = 200;
|
||||||
std::string content;
|
std::string content;
|
||||||
std::string cookies;
|
std::string cookies;
|
||||||
|
|||||||
@ -3,16 +3,17 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
template <typename T> void exception_thrower(T e)
|
template <typename T> void exception_thrower(T e, const std::string &cond, const std::string &file, int line)
|
||||||
{
|
{
|
||||||
if(!e)
|
if(!e)
|
||||||
throw std::runtime_error("rapidjson assertion failed");
|
throw std::runtime_error("rapidjson assertion failed: " + cond + " (" + file + ":" + std::to_string(line) + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef RAPIDJSON_ASSERT
|
#ifdef RAPIDJSON_ASSERT
|
||||||
#undef RAPIDJSON_ASSERT
|
#undef RAPIDJSON_ASSERT
|
||||||
#endif // RAPIDJSON_ASSERT
|
#endif // RAPIDJSON_ASSERT
|
||||||
#define RAPIDJSON_ASSERT(x) exception_thrower(x)
|
#define VALUE(x) #x
|
||||||
|
#define RAPIDJSON_ASSERT(x) exception_thrower(x, VALUE(x), __FILE__, __LINE__)
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
#include <rapidjson/writer.h>
|
#include <rapidjson/writer.h>
|
||||||
#include <rapidjson/error/en.h>
|
#include <rapidjson/error/en.h>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user