Fix implementation of processing escape character in INIReader

Optimize codes.
This commit is contained in:
Tindy X committed 2020-07-04 23:19:09 +08:00
1 parent 21d2900280
commit c2564a6fd9
6 files changed
+57 -66

No files matched your search

+1 -4
View File
@@ -236,10 +236,7 @@ public:
last_error_index++; last_error_index++;
string_size lineSize = strLine.size(); string_size lineSize = strLine.size();
if(lineSize && strLine[lineSize - 1] == '\r') //remove line break if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
{ strLine.erase(--lineSize);
strLine.erase(lineSize - 1);
lineSize--;
}
if((!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) && !inDirectSaveSection) //empty lines and comments are ignored if((!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) && !inDirectSaveSection) //empty lines and comments are ignored
continue; continue;
ProcessEscapeChar(strLine); ProcessEscapeChar(strLine);
+15 -24
View File
@@ -118,10 +118,7 @@ std::string convertRuleset(const std::string &content, int type)
strLine = trim(strLine); strLine = trim(strLine);
lineSize = strLine.size(); lineSize = strLine.size();
if(lineSize && strLine[lineSize - 1] == '\r') //remove line break if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
{ strLine.erase(--lineSize);
strLine.erase(lineSize - 1);
lineSize--;
}
if(!strLine.empty() && (strLine[0] != ';' && strLine[0] != '#' && !(lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/'))) if(!strLine.empty() && (strLine[0] != ';' && strLine[0] != '#' && !(lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')))
{ {
@@ -287,10 +284,7 @@ std::string getRuleset(RESPONSE_CALLBACK_ARGS)
lineSize = strLine.size(); lineSize = strLine.size();
if(lineSize && strLine[lineSize - 1] == '\r') //remove line break if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
{ strLine.erase(--lineSize);
strLine.erase(lineSize - 1);
lineSize--;
}
if(!strLine.empty() && (strLine[0] != ';' && strLine[0] != '#' && !(lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/'))) if(!strLine.empty() && (strLine[0] != ';' && strLine[0] != '#' && !(lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')))
{ {
@@ -299,7 +293,7 @@ std::string getRuleset(RESPONSE_CALLBACK_ARGS)
if(startsWith(strLine, "IP-CIDR6")) if(startsWith(strLine, "IP-CIDR6"))
strLine.replace(0, 8, "IP6-CIDR"); strLine.replace(0, 8, "IP6-CIDR");
strLine += "," + group; strLine += "," + group;
if(std::count(strLine.begin(), strLine.end(), ',') > 2 && regReplace(strLine, rule_match_regex, "$2") == ",no-resolve") if(count_least(strLine, ',', 3) && regReplace(strLine, rule_match_regex, "$2") == ",no-resolve")
strLine = regReplace(strLine, rule_match_regex, "$1$3$2"); strLine = regReplace(strLine, rule_match_regex, "$1$3$2");
else else
strLine = regReplace(strLine, rule_match_regex, "$1$3"); strLine = regReplace(strLine, rule_match_regex, "$1$3");
@@ -361,10 +355,7 @@ int importItems(string_array &target, bool scope_limit = true)
{ {
lineSize = strLine.size(); lineSize = strLine.size();
if(lineSize && strLine[lineSize - 1] == '\r') //remove line break if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
{ strLine.erase(--lineSize);
strLine = strLine.substr(0, lineSize - 1);
lineSize--;
}
if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored
continue; continue;
result.emplace_back(strLine); result.emplace_back(strLine);
@@ -411,11 +402,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; YAML::Node object;
std::string url, match, rep, strLine; std::string script, url, match, rep, strLine;
for(unsigned i = 0; i < node.size(); i++) for(unsigned i = 0; i < node.size(); i++)
{ {
object = node[i]; object = node[i];
object["script"] >>= script;
if(script.size())
{
dest.emplace_back("!!script:" + script);
continue;
}
object["import"] >>= url; object["import"] >>= url;
if(url.size()) if(url.size())
{ {
@@ -2032,17 +2029,14 @@ std::string surgeConfToClash(RESPONSE_CALLBACK_ARGS)
while(getline(ss, strLine, delimiter)) while(getline(ss, strLine, delimiter))
{ {
lineSize = strLine.size(); lineSize = strLine.size();
if(strLine[lineSize - 1] == '\r') //remove line break if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
{ strLine.erase(--lineSize);
strLine = strLine.substr(0, lineSize - 1);
lineSize--;
}
if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored
continue; continue;
else if(strLine.find("USER-AGENT") == 0 || strLine.find("URL-REGEX") == 0 || strLine.find("PROCESS-NAME") == 0 || strLine.find("AND") == 0 || strLine.find("OR") == 0) //remove unsupported types else if(strLine.find("USER-AGENT") == 0 || strLine.find("URL-REGEX") == 0 || strLine.find("PROCESS-NAME") == 0 || strLine.find("AND") == 0 || strLine.find("OR") == 0) //remove unsupported types
continue; continue;
strLine += strArray[2]; strLine += strArray[2];
if(std::count(strLine.begin(), strLine.end(), ',') > 2) if(count_least(strLine, ',', 3))
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2"); strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
rule.push_back(strLine); rule.push_back(strLine);
} }
@@ -2210,10 +2204,7 @@ std::string getRewriteRemote(RESPONSE_CALLBACK_ARGS)
{ {
lineSize = strLine.size(); lineSize = strLine.size();
if(lineSize && strLine[lineSize - 1] == '\r') //remove line break if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
{ strLine.erase(--lineSize);
strLine.erase(lineSize - 1);
lineSize--;
}
if(!strLine.empty() && regMatch(strLine, pattern)) if(!strLine.empty() && regMatch(strLine, pattern))
{ {
+4 -2
View File
@@ -1194,8 +1194,10 @@ void ProcessEscapeChar(std::string &str)
str.replace(pos, 2, "\t"); str.replace(pos, 2, "\t");
break; break;
default: default:
str.erase(pos, 1); /// ignore others for backward compatibility
//str.erase(pos, 1);
break;
} }
pos = str.find('\\', pos); pos = str.find('\\', pos + 1);
} }
} }
+14 -1
View File
@@ -131,9 +131,22 @@ template <typename T, typename U> static inline T to_number(const U &value, T de
int to_int(const std::string &str, int def_value = 0); int to_int(const std::string &str, int def_value = 0);
static inline bool count_least(const std::string &hay, const char needle, size_t cnt)
{
string_size pos = hay.find(needle);
while(pos != hay.npos)
{
cnt--;
if(!cnt)
return true;
pos = hay.find(needle, pos + 1);
}
return false;
}
static inline char getLineBreak(const std::string &str) static inline char getLineBreak(const std::string &str)
{ {
return count(str.begin(), str.end(), '\n') < 1 ? '\r' : '\n'; return count_least(str, '\n', 1) ? '\n' : '\r';
} }
class tribool class tribool
+20 -29
View File
@@ -486,13 +486,15 @@ std::string nodeRename(const nodeInfo &node, const string_array &rename_array)
string_size pos; string_size pos;
std::string match, rep; std::string match, rep;
std::string remark = node.remarks, real_rule; std::string remark = node.remarks, real_rule;
duk_context *ctx = duktape_init(); duk_context *ctx = NULL;
defer(duk_destroy_heap(ctx);) defer(duk_destroy_heap(ctx);)
for(const std::string &x : rename_array) for(const std::string &x : rename_array)
{ {
if(startsWith(x, "!!script:")) if(startsWith(x, "!!script:"))
{ {
if(!ctx)
ctx = duktape_init();
std::string script = x.substr(9); std::string script = x.substr(9);
if(startsWith(script, "path:")) if(startsWith(script, "path:"))
script = fileGet(script.substr(5), true); script = fileGet(script.substr(5), true);
@@ -547,13 +549,15 @@ std::string addEmoji(const nodeInfo &node, const string_array &emoji_array)
{ {
std::string real_rule; std::string real_rule;
string_size pos; string_size pos;
duk_context *ctx = duktape_init(); duk_context *ctx = NULL;
defer(duk_destroy_heap(ctx);) defer(duk_destroy_heap(ctx);)
for(const std::string &x : emoji_array) for(const std::string &x : emoji_array)
{ {
if(startsWith(x, "!!script:")) if(startsWith(x, "!!script:"))
{ {
if(!ctx)
ctx = duktape_init();
std::string script = x.substr(9); std::string script = x.substr(9);
if(startsWith(script, "path:")) if(startsWith(script, "path:"))
script = fileGet(script.substr(5), true); script = fileGet(script.substr(5), true);
@@ -636,7 +640,7 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset
if(strLine.find("FINAL") == 0) if(strLine.find("FINAL") == 0)
strLine.replace(0, 5, "MATCH"); strLine.replace(0, 5, "MATCH");
strLine += "," + rule_group; strLine += "," + rule_group;
if(std::count(strLine.begin(), strLine.end(), ',') > 2) if(count_least(strLine, ',', 3))
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2"); strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
allRules.emplace_back(strLine); allRules.emplace_back(strLine);
total_rules++; total_rules++;
@@ -660,11 +664,8 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset
lineSize--; lineSize--;
} }
*/ */
if(lineSize) if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
{ strLine.erase(--lineSize);
strLine = trim(trim_of(strLine, '\r'));
lineSize = strLine.size();
}
if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored
continue; continue;
/* /*
@@ -680,7 +681,7 @@ void rulesetToClash(YAML::Node &base_rule, std::vector<ruleset_content> &ruleset
strLine = replace_all_distinct(strLine, ",force-remote-dns", ""); strLine = replace_all_distinct(strLine, ",force-remote-dns", "");
*/ */
strLine += "," + rule_group; strLine += "," + rule_group;
if(std::count(strLine.begin(), strLine.end(), ',') > 2) if(count_least(strLine, ',', 3))
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2"); strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
allRules.emplace_back(strLine); allRules.emplace_back(strLine);
//Rules.push_back(strLine); //Rules.push_back(strLine);
@@ -727,7 +728,7 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
if(strLine.find("FINAL") == 0) if(strLine.find("FINAL") == 0)
strLine.replace(0, 5, "MATCH"); strLine.replace(0, 5, "MATCH");
strLine += "," + rule_group; strLine += "," + rule_group;
if(std::count(strLine.begin(), strLine.end(), ',') > 2) if(count_least(strLine, ',', 3))
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2"); strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
output_content += " - " + strLine + "\n"; output_content += " - " + strLine + "\n";
total_rules++; total_rules++;
@@ -744,17 +745,17 @@ std::string rulesetToClashStr(YAML::Node &base_rule, std::vector<ruleset_content
if(max_allowed_rules && total_rules > max_allowed_rules) if(max_allowed_rules && total_rules > max_allowed_rules)
break; break;
lineSize = strLine.size(); lineSize = strLine.size();
if(lineSize) if(lineSize && strLine[lineSize - 1] == '\r')
{ {
strLine = trim(trim_of(strLine, '\r')); strLine.erase(lineSize - 1);
lineSize = strLine.size(); lineSize = strLine.size();
} }
if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored
continue; continue;
if(!std::any_of(clash_rule_type.begin(), clash_rule_type.end(), [strLine](std::string type){return startsWith(strLine, type);})) if(std::none_of(clash_rule_type.begin(), clash_rule_type.end(), [strLine](std::string type){return startsWith(strLine, type);}))
continue; continue;
strLine += "," + rule_group; strLine += "," + rule_group;
if(std::count(strLine.begin(), strLine.end(), ',') > 2) if(count_least(strLine, ',', 3))
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2"); strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
output_content += " - " + strLine + "\n"; output_content += " - " + strLine + "\n";
total_rules++; total_rules++;
@@ -816,14 +817,14 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
strLine += "," + rule_group; strLine += "," + rule_group;
if(surge_ver == -1 || surge_ver == -2) if(surge_ver == -1 || surge_ver == -2)
{ {
if(std::count(strLine.begin(), strLine.end(), ',') > 2 && regReplace(strLine, rule_match_regex, "$2") == ",no-resolve") if(count_least(strLine, ',', 3) && regReplace(strLine, rule_match_regex, "$2") == ",no-resolve")
strLine = regReplace(strLine, rule_match_regex, "$1$3$2"); strLine = regReplace(strLine, rule_match_regex, "$1$3$2");
else else
strLine = regReplace(strLine, rule_match_regex, "$1$3"); strLine = regReplace(strLine, rule_match_regex, "$1$3");
} }
else else
{ {
if(std::count(strLine.begin(), strLine.end(), ',') > 2) if(count_least(strLine, ',', 3))
strLine = regReplace(strLine, rule_match_regex, "$1$3$2"); strLine = regReplace(strLine, rule_match_regex, "$1$3$2");
} }
strLine = replace_all_distinct(strLine, ",,", ","); strLine = replace_all_distinct(strLine, ",,", ",");
@@ -912,18 +913,8 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
if(max_allowed_rules && total_rules > max_allowed_rules) if(max_allowed_rules && total_rules > max_allowed_rules)
break; break;
lineSize = strLine.size(); lineSize = strLine.size();
/*
if(lineSize && strLine[lineSize - 1] == '\r') //remove line break if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
{ strLine.erase(--lineSize);
strLine.erase(lineSize - 1);
lineSize--;
}
*/
if(lineSize)
{
strLine = trim(trim_of(strLine, '\r'));
lineSize = strLine.size();
}
if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored
continue; continue;
@@ -960,14 +951,14 @@ void rulesetToSurge(INIReader &base_rule, std::vector<ruleset_content> &ruleset_
{ {
if(startsWith(strLine, "IP-CIDR6")) if(startsWith(strLine, "IP-CIDR6"))
strLine.replace(0, 8, "IP6-CIDR"); strLine.replace(0, 8, "IP6-CIDR");
if(std::count(strLine.begin(), strLine.end(), ',') > 2 && regReplace(strLine, rule_match_regex, "$2") == ",no-resolve") if(count_least(strLine, ',', 3) && regReplace(strLine, rule_match_regex, "$2") == ",no-resolve")
strLine = regReplace(strLine, rule_match_regex, "$1$3$2"); strLine = regReplace(strLine, rule_match_regex, "$1$3$2");
else else
strLine = regReplace(strLine, rule_match_regex, "$1$3"); strLine = regReplace(strLine, rule_match_regex, "$1$3");
} }
else else
{ {
if(std::count(strLine.begin(), strLine.end(), ',') > 2) if(count_least(strLine, ',', 3))
strLine = regReplace(strLine, rule_match_regex, "$1$3$2"); strLine = regReplace(strLine, rule_match_regex, "$1$3$2");
} }
allRules.emplace_back(strLine); allRules.emplace_back(strLine);
+3 -6
View File
@@ -261,7 +261,7 @@ int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &rules
if(strLine.find("FINAL") == 0) if(strLine.find("FINAL") == 0)
strLine.replace(0, 5, "MATCH"); strLine.replace(0, 5, "MATCH");
strLine += "," + rule_group; strLine += "," + rule_group;
if(std::count(strLine.begin(), strLine.end(), ',') > 2) if(count_least(strLine, ',', 3))
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2"); strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
rules.emplace_back(strLine); rules.emplace_back(strLine);
continue; continue;
@@ -329,10 +329,7 @@ int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &rules
{ {
lineSize = strLine.size(); lineSize = strLine.size();
if(lineSize && strLine[lineSize - 1] == '\r') //remove line break if(lineSize && strLine[lineSize - 1] == '\r') //remove line break
{ strLine.erase(--lineSize);
strLine.erase(lineSize - 1);
lineSize--;
}
if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored if(!lineSize || strLine[0] == ';' || strLine[0] == '#' || (lineSize >= 2 && strLine[0] == '/' && strLine[1] == '/')) //empty lines and comments are ignored
continue; continue;
@@ -351,7 +348,7 @@ int renderClashScript(YAML::Node &base_rule, std::vector<ruleset_content> &rules
else else
{ {
strLine += "," + rule_group; strLine += "," + rule_group;
if(std::count(strLine.begin(), strLine.end(), ',') > 2) if(count_least(strLine, ',', 3))
strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2"); strLine = regReplace(strLine, "^(.*?,.*?)(,.*)(,.*)$", "$1$3$2");
rules.emplace_back(strLine); rules.emplace_back(strLine);
} }