Skip to content

Commit

Permalink
Improve regex replace, fix backslash escapes for format parsing (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
qsniyg committed Jan 22, 2025
1 parent 05f1b9d commit 8279af1
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 108 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Improvements:

* Various improvements/fixes to existing rules (including Flickr, Twitch)

Bugfixes:

* Fix backslash escapes breaking parsing for format strings

Special thanks to arlenestrigger, fyhtma, BlindWanda, chr1sx, James Joint, musicianjam888, Velocidensity, Egor Popov, Froktime for their contributions and reports for this release

---
Expand Down
109 changes: 51 additions & 58 deletions src/userscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129725,22 +129725,15 @@ var $$IMU_EXPORT$$;

var current = "";
var in_bracket = false;
var escape = false;
for (var i = 0; i < formatstr.length; i++) {
var c = formatstr[i];
let nextc = null;
if (i+1 < formatstr.length)
nextc = formatstr[i + 1];

if (c === '\\') {
if (escape) {
current += '\\';
} else {
escape = true;
}

continue;
}

if (escape) {
current += c;
if (c === '\\' && (nextc === '{' || nextc === '}')) {
current += nextc;
i++;
continue;
}

Expand Down Expand Up @@ -129787,46 +129780,10 @@ var $$IMU_EXPORT$$;
var op = null;
var compare_flags = [];

var match = varname.match(/^([^?]+)[?](.*)$/);
if (match) {
varname = match[1];
default_value = match[2];
}

match = varname.match(/^([^!/:=]+)([!=]=)(.*)$/);
if (match) {
varname = match[1];
compare_value = match[3];
compare_not = match[2] === "!=";
op = "eq";
}

match = varname.match(/^([^!/:=]+)(!)?\/([rc]{0,2})=(.*)$/);
if (match) {
varname = match[1];
compare_value = match[4];
compare_not = match[2] === "!";
op = "contains";
var flags = match[3];
if (string_indexof(flags, "r") >= 0)
compare_flags.push("regex");
if (string_indexof(flags, "c") >= 0)
compare_flags.push("nicase");
}

match = varname.match(/^([^!/:=]+):=(.*)$/);
if (match) {
varname = match[1];
compare_value = match[2];
op = "set";
}

var varvalue = vars[varname];

let process_varvalue = [];

// thanks to musicianjam888 on github for the idea: https://github.com/qsniyg/maxurl/issues/1398
match = varname.match(/^(.*?)\/(.*?\/.*?\/c?)(:[0-9]+\.?)?$/);
let match = varname.match(/^([^!/:=]+?)\/(.*?\/.*?\/c?)(:[0-9]+\.?)?$/);
if (match) {
varname = match[1];
if (match[3]) // :[0-9]+\.?
Expand All @@ -129835,7 +129792,6 @@ var $$IMU_EXPORT$$;
let regexval = match[2]; // don't pass match to closure because it gets overridden
process_varvalue.push(function(value) {
let regexmatch = "", regexreplace = "", regexflags = "";
let in_escape = false;
let stage = 0;

let commit = function(c) {
Expand All @@ -129852,15 +129808,18 @@ var $$IMU_EXPORT$$;

for (let i = 0; i < regexval.length; i++) {
let c = regexval[i];
let nextc = null;
if (i+1 < regexval.length)
nextc = regexval[i + 1];

if (in_escape || stage === 2) {
if (stage === 2) {
commit(c);
in_escape = false;
continue;
}

if (c === "\\") {
in_escape = true;
if (c === "\\" && nextc === "/") {
commit("/");
i++;
continue;
}

Expand Down Expand Up @@ -129893,14 +129852,48 @@ var $$IMU_EXPORT$$;
});
}

var match = varname.match(/^(.*?):([0-9]+)(\.)?$/);
match = varname.match(/^([^?]+)[?](.*)$/);
if (match) {
varname = match[1];
default_value = match[2];
}

match = varname.match(/^([^!/:=]+)([!=]=)(.*)$/);
if (match) {
varname = match[1];
compare_value = match[3];
compare_not = match[2] === "!=";
op = "eq";
}

match = varname.match(/^([^!/:=]+)(!)?\/([rc]{0,2})=(.*)$/);
if (match) {
varname = match[1];
compare_value = match[4];
compare_not = match[2] === "!";
op = "contains";
var flags = match[3];
if (string_indexof(flags, "r") >= 0)
compare_flags.push("regex");
if (string_indexof(flags, "c") >= 0)
compare_flags.push("nicase");
}

match = varname.match(/^([^!/:=]+):=(.*)$/);
if (match) {
varname = match[1];
compare_value = match[2];
op = "set";
}

match = varname.match(/^(.*?):([0-9]+)(\.)?$/);
if (match) {
varname = match[1];

let trimamt = parse_int(match[2]);
let add_ellipsis = match[3];

process_varvalue.push(function(value) {
process_varvalue.push(function(varvalue) {
var newvalue = varvalue.substr(0, trimamt);
if (add_ellipsis) {
if (newvalue !== varvalue) {
Expand All @@ -129913,7 +129906,7 @@ var $$IMU_EXPORT$$;
});
}

varvalue = vars[varname];
var varvalue = vars[varname];
for (let proc of process_varvalue) {
if (!varvalue)
break;
Expand Down
96 changes: 46 additions & 50 deletions userscript.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -115738,19 +115738,14 @@ var $$IMU_EXPORT$$;
};
var current = "";
var in_bracket = false;
var escape = false;
for (var i = 0; i < formatstr.length; i++) {
var c = formatstr[i];
if (c === '\\') {
if (escape) {
current += '\\';
} else {
escape = true;
}
continue;
}
if (escape) {
current += c;
var nextc = null;
if (i + 1 < formatstr.length)
nextc = formatstr[i + 1];
if (c === '\\' && (nextc === '{' || nextc === '}')) {
current += nextc;
i++;
continue;
}
if (c === '{') {
Expand Down Expand Up @@ -115789,48 +115784,16 @@ var $$IMU_EXPORT$$;
compare_not = false;
op = null;
compare_flags = [];
match = varname.match(/^([^?]+)[?](.*)$/);
if (match) {
varname = match[1];
default_value = match[2];
}
match = varname.match(/^([^!/:=]+)([!=]=)(.*)$/);
if (match) {
varname = match[1];
compare_value = match[3];
compare_not = match[2] === "!=";
op = "eq";
}
match = varname.match(/^([^!/:=]+)(!)?\/([rc]{0,2})=(.*)$/);
if (match) {
varname = match[1];
compare_value = match[4];
compare_not = match[2] === "!";
op = "contains";
flags = match[3];
if (string_indexof(flags, "r") >= 0)
compare_flags.push("regex");
if (string_indexof(flags, "c") >= 0)
compare_flags.push("nicase");
}
match = varname.match(/^([^!/:=]+):=(.*)$/);
if (match) {
varname = match[1];
compare_value = match[2];
op = "set";
}
varvalue = vars[varname];
var process_varvalue = [];
// thanks to musicianjam888 on github for the idea: https://github.com/qsniyg/maxurl/issues/1398
match = varname.match(/^(.*?)\/(.*?\/.*?\/c?)(:[0-9]+\.?)?$/);
var match = varname.match(/^([^!/:=]+?)\/(.*?\/.*?\/c?)(:[0-9]+\.?)?$/);
if (match) {
varname = match[1];
if (match[3]) // :[0-9]+\.?
varname += match[3];
var regexval_1 = match[2]; // don't pass match to closure because it gets overridden
process_varvalue.push(function(value) {
var regexmatch = "", regexreplace = "", regexflags = "";
var in_escape = false;
var stage = 0;
var commit = function(c) {
if (stage === 0) {
Expand All @@ -115845,13 +115808,16 @@ var $$IMU_EXPORT$$;
};
for (var i_18 = 0; i_18 < regexval_1.length; i_18++) {
var c = regexval_1[i_18];
if (in_escape || stage === 2) {
var nextc = null;
if (i_18 + 1 < regexval_1.length)
nextc = regexval_1[i_18 + 1];
if (stage === 2) {
commit(c);
in_escape = false;
continue;
}
if (c === "\\") {
in_escape = true;
if (c === "\\" && nextc === "/") {
commit("/");
i_18++;
continue;
}
if (c === "/") {
Expand All @@ -115877,12 +115843,42 @@ var $$IMU_EXPORT$$;
return value.replace(regex, regexreplace);
});
}
match = varname.match(/^([^?]+)[?](.*)$/);
if (match) {
varname = match[1];
default_value = match[2];
}
match = varname.match(/^([^!/:=]+)([!=]=)(.*)$/);
if (match) {
varname = match[1];
compare_value = match[3];
compare_not = match[2] === "!=";
op = "eq";
}
match = varname.match(/^([^!/:=]+)(!)?\/([rc]{0,2})=(.*)$/);
if (match) {
varname = match[1];
compare_value = match[4];
compare_not = match[2] === "!";
op = "contains";
flags = match[3];
if (string_indexof(flags, "r") >= 0)
compare_flags.push("regex");
if (string_indexof(flags, "c") >= 0)
compare_flags.push("nicase");
}
match = varname.match(/^([^!/:=]+):=(.*)$/);
if (match) {
varname = match[1];
compare_value = match[2];
op = "set";
}
match = varname.match(/^(.*?):([0-9]+)(\.)?$/);
if (match) {
varname = match[1];
var trimamt_1 = parse_int(match[2]);
var add_ellipsis_1 = match[3];
process_varvalue.push(function(value) {
process_varvalue.push(function(varvalue) {
var newvalue = varvalue.substr(0, trimamt_1);
if (add_ellipsis_1) {
if (newvalue !== varvalue) {
Expand Down Expand Up @@ -115946,7 +115942,7 @@ var $$IMU_EXPORT$$;
str += varvalue;
}
};
var varname, default_value, compare_value, compare_not, op, compare_flags, match, flags, varvalue, match, usevar, varobj;
var varname, default_value, compare_value, compare_not, op, compare_flags, flags, varvalue, usevar, varobj;
for (var i = 0; i < parsed.length; i++) {
var state_2 = _loop_3();
if (typeof state_2 === "object")
Expand Down

0 comments on commit 8279af1

Please sign in to comment.