Optimize codes

This commit is contained in:
Tindy X 2023-10-09 16:06:06 +08:00
parent 0e07f44f07
commit cb409ddf7e
No known key found for this signature in database
GPG Key ID: CD068E6DB6C55A1C
3 changed files with 32 additions and 49 deletions

View File

@ -30,7 +30,7 @@ int importItems(string_array &target, bool scope_limit)
unsigned int itemCount = 0;
for(std::string &x : target)
{
if(x.find("!!import:") == x.npos)
if(x.find("!!import:") == std::string::npos)
{
result.emplace_back(x);
continue;
@ -46,7 +46,7 @@ int importItems(string_array &target, bool scope_limit)
content = webGet(path, proxy, global.cacheConfig);
else
writeLog(0, "File not found or not a valid URL: " + path, LOG_LEVEL_ERROR);
if(!content.size())
if(content.empty())
return -1;
ss << content;
@ -98,7 +98,7 @@ void importItems(std::vector<toml::value> &root, const std::string &import_key,
content = webGet(path, proxy, global.cacheConfig);
else
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 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);
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)
{
YAML::Node object;
std::string script, url, match, rep, strLine;
for(unsigned i = 0; i < node.size(); i++)
for(auto && object : node)
{
object = node[i];
std::string script, url, match, rep, strLine;
object["script"] >>= script;
if(script.size())
if(!script.empty())
{
dest.emplace_back("!!script:" + script);
continue;
}
object["import"] >>= url;
if(url.size())
if(!url.empty())
{
dest.emplace_back("!!import:" + url);
continue;
}
object["match"] >>= match;
object["replace"] >>= rep;
if(match.size() && rep.size())
if(!match.empty() && !rep.empty())
strLine = match + delimiter + rep;
else
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)
{
YAML::Node object;
std::string script, url, match, rep, strLine;
for(unsigned i = 0; i < node.size(); i++)
for(auto && object : node)
{
object = node[i];
std::string script, url, match, rep, strLine;
object["script"] >>= script;
if(script.size())
if(!script.empty())
{
dest.emplace_back("!!script:" + script);
continue;
}
object["import"] >>= url;
if(url.size())
if(!url.empty())
{
url = "!!import:" + 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["emoji"] >>= rep;
if(match.size() && rep.size())
if(!match.empty() && !rep.empty())
strLine = match + "," + rep;
else
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)
{
std::string strLine, name, type;
string_array tempArray;
YAML::Node object;
unsigned int i, j;
for(i = 0; i < node.size(); i++)
for(YAML::Node && object : node)
{
eraseElements(tempArray);
object = node[i];
string_array tempArray;
std::string name, type;
object["import"] >>= name;
if(name.size())
if(!name.empty())
{
dest.emplace_back("!!import:" + name);
continue;
@ -202,7 +190,7 @@ void readGroup(YAML::Node node, string_array &dest, bool scope_limit = true)
object["interval"] >>= interval;
object["tolerance"] >>= tolerance;
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]));
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);
}
strLine = std::accumulate(std::next(tempArray.begin()), tempArray.end(), tempArray[0], [](std::string a, std::string b) -> std::string
{
return std::move(a) + "`" + std::move(b);
});
std::string strLine = join(tempArray, "`");
dest.emplace_back(std::move(strLine));
}
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)
{
std::string strLine, name, url, group, interval;
YAML::Node object;
for(unsigned int i = 0; i < node.size(); i++)
for(auto && object : node)
{
object = node[i];
std::string strLine, name, url, group, interval;
object["import"] >>= name;
if(name.size())
if(!name.empty())
{
dest.emplace_back("!!import:" + name);
continue;
@ -248,13 +230,13 @@ void readRuleset(YAML::Node node, string_array &dest, bool scope_limit = true)
object["group"] >>= group;
object["rule"] >>= name;
object["interval"] >>= interval;
if(url.size())
if(!url.empty())
{
strLine = group + "," + url;
if(interval.size())
if(!interval.empty())
strLine += "," + interval;
}
else if(name.size())
else if(!name.empty())
strLine = group + ",[]" + name;
else
continue;

View File

@ -190,7 +190,7 @@ export function require (path) {
class qjs_fetch_Headers
{
public:
qjs_fetch_Headers() {}
qjs_fetch_Headers() = default;
string_icase_map headers;
@ -218,7 +218,7 @@ public:
class qjs_fetch_Request
{
public:
qjs_fetch_Request() {}
qjs_fetch_Request() = default;
std::string method = "GET";
std::string url;
std::string proxy;
@ -231,7 +231,7 @@ public:
class qjs_fetch_Response
{
public:
qjs_fetch_Response() {}
qjs_fetch_Response() = default;
int status_code = 200;
std::string content;
std::string cookies;

View File

@ -3,16 +3,17 @@
#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)
throw std::runtime_error("rapidjson assertion failed");
throw std::runtime_error("rapidjson assertion failed: " + cond + " (" + file + ":" + std::to_string(line) + ")");
}
#ifdef RAPIDJSON_ASSERT
#undef 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/writer.h>
#include <rapidjson/error/en.h>