Skip to content

Commit

Permalink
Optimize codes
Browse files Browse the repository at this point in the history
  • Loading branch information
tindy2013 committed Oct 9, 2023
1 parent 0e07f44 commit cb409dd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 49 deletions.
68 changes: 25 additions & 43 deletions src/handler/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -146,28 +142,25 @@ 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);
continue;
}
object["match"] >>= match;
object["emoji"] >>= rep;
if(match.size() && rep.size())
if(!match.empty() && !rep.empty())
strLine = match + "," + rep;
else
continue;
Expand All @@ -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;
Expand All @@ -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))
{
Expand All @@ -221,25 +209,19 @@ 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);
}

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;
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/script/script_quickjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export function require (path) {
class qjs_fetch_Headers
{
public:
qjs_fetch_Headers() {}
qjs_fetch_Headers() = default;

string_icase_map headers;

Expand Down Expand Up @@ -218,7 +218,7 @@ class qjs_fetch_Headers
class qjs_fetch_Request
{
public:
qjs_fetch_Request() {}
qjs_fetch_Request() = default;
std::string method = "GET";
std::string url;
std::string proxy;
Expand All @@ -231,7 +231,7 @@ class qjs_fetch_Request
class qjs_fetch_Response
{
public:
qjs_fetch_Response() {}
qjs_fetch_Response() = default;
int status_code = 200;
std::string content;
std::string cookies;
Expand Down
7 changes: 4 additions & 3 deletions src/utils/rapidjson_extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit cb409dd

Please sign in to comment.