mirror of
https://github.com/TrianguloY/UrlChecker.git
synced 2024-09-20 04:12:14 +02:00
slight optimization
avoids creating a matcher twice, looks nicer
This commit is contained in:
parent
7e2e755e6b
commit
dff73c1e63
@ -19,7 +19,6 @@ import com.trianguloy.urlchecker.utilities.JavaUtils;
|
|||||||
import com.trianguloy.urlchecker.utilities.RegexFix;
|
import com.trianguloy.urlchecker.utilities.RegexFix;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -111,14 +110,14 @@ class PatternDialog extends AModuleDialog {
|
|||||||
public void onModifyUrl(UrlData urlData, JavaUtils.Function<UrlData, Boolean> setNewUrl) {
|
public void onModifyUrl(UrlData urlData, JavaUtils.Function<UrlData, Boolean> setNewUrl) {
|
||||||
// init
|
// init
|
||||||
messages.clear();
|
messages.clear();
|
||||||
String url = urlData.url;
|
var url = urlData.url;
|
||||||
|
|
||||||
// check each pattern
|
// check each pattern
|
||||||
JSONObject patterns = catalog.getCatalog();
|
var patterns = catalog.getCatalog();
|
||||||
for (String pattern : JavaUtils.toList(patterns.keys())) {
|
for (var pattern : JavaUtils.toList(patterns.keys())) {
|
||||||
try {
|
try {
|
||||||
JSONObject data = patterns.optJSONObject(pattern);
|
var data = patterns.optJSONObject(pattern);
|
||||||
Message message = new Message(pattern);
|
var message = new Message(pattern);
|
||||||
|
|
||||||
// enabled?
|
// enabled?
|
||||||
if (data == null) continue;
|
if (data == null) continue;
|
||||||
@ -126,7 +125,7 @@ class PatternDialog extends AModuleDialog {
|
|||||||
|
|
||||||
// get regex (must exists)
|
// get regex (must exists)
|
||||||
if (!data.has("regex")) continue;
|
if (!data.has("regex")) continue;
|
||||||
Pattern regex = Pattern.compile(data.getString("regex"));
|
var regex_matcher = Pattern.compile(data.getString("regex")).matcher(url);
|
||||||
|
|
||||||
// applied?
|
// applied?
|
||||||
message.applied = urlData.getData(APPLIED + pattern) != null;
|
message.applied = urlData.getData(APPLIED + pattern) != null;
|
||||||
@ -134,7 +133,7 @@ class PatternDialog extends AModuleDialog {
|
|||||||
// check matches
|
// check matches
|
||||||
// if 'regexp' matches, the pattern can match
|
// if 'regexp' matches, the pattern can match
|
||||||
// if 'regexp' doesn't match, the patter doesn't match
|
// if 'regexp' doesn't match, the patter doesn't match
|
||||||
var matches = regex.matcher(url).find();
|
var matches = regex_matcher.find();
|
||||||
if (matches && data.has("excludeRegex")) {
|
if (matches && data.has("excludeRegex")) {
|
||||||
// if 'excludeRegex' doesn't exist, the pattern can match
|
// if 'excludeRegex' doesn't exist, the pattern can match
|
||||||
// if 'excludeRegex' matches, the pattern doesn't matches
|
// if 'excludeRegex' matches, the pattern doesn't matches
|
||||||
@ -147,12 +146,12 @@ class PatternDialog extends AModuleDialog {
|
|||||||
// check replacements
|
// check replacements
|
||||||
String replacement = null;
|
String replacement = null;
|
||||||
|
|
||||||
Object replacements = data.opt("replacement");
|
var replacements = data.opt("replacement");
|
||||||
if (replacements != null) {
|
if (replacements != null) {
|
||||||
// data exists
|
// data exists
|
||||||
if (replacements instanceof JSONArray) {
|
if (replacements instanceof JSONArray) {
|
||||||
// array, get random
|
// array, get random
|
||||||
JSONArray replacementsArray = (JSONArray) replacements;
|
var replacementsArray = (JSONArray) replacements;
|
||||||
replacement = replacementsArray.getString(new Random().nextInt(replacementsArray.length()));
|
replacement = replacementsArray.getString(new Random().nextInt(replacementsArray.length()));
|
||||||
} else {
|
} else {
|
||||||
// single data, get that one
|
// single data, get that one
|
||||||
@ -162,7 +161,7 @@ class PatternDialog extends AModuleDialog {
|
|||||||
|
|
||||||
if (replacement != null) {
|
if (replacement != null) {
|
||||||
// replace url
|
// replace url
|
||||||
message.newUrl = regexFix.replaceAll(url, regex, replacement);
|
message.newUrl = regexFix.replaceAll(url, regex_matcher, replacement);
|
||||||
|
|
||||||
// automatic? apply
|
// automatic? apply
|
||||||
if (data.optBoolean("automatic")) {
|
if (data.optBoolean("automatic")) {
|
||||||
|
@ -6,7 +6,6 @@ import android.view.View;
|
|||||||
import android.widget.Switch;
|
import android.widget.Switch;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On Android 10 and under, optional groups may yield a "null" in the replacement output instead of an empty string.
|
* On Android 10 and under, optional groups may yield a "null" in the replacement output instead of an empty string.
|
||||||
@ -42,18 +41,16 @@ public class RegexFix {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use this instead of: pattern.matcher(text).replaceAll(replacement)
|
* Use this instead of: matcher = pattern.matcher(text); result = matcher.replaceAll(replacement)
|
||||||
*/
|
*/
|
||||||
public String replaceAll(String text, Pattern pattern, String replacement) {
|
public String replaceAll(String text, Matcher matcher, String replacement) {
|
||||||
Matcher matcher = pattern.matcher(text);
|
|
||||||
|
|
||||||
if (IS_ANDROID_FIXED || !pttrn_regexfix.get()) {
|
if (IS_ANDROID_FIXED || !pttrn_regexfix.get()) {
|
||||||
// no fix required or explicitly disabled, just use native function
|
// no fix required or explicitly disabled, just use native function
|
||||||
return matcher.replaceAll(replacement);
|
return matcher.replaceAll(replacement);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copied from https://android.googlesource.com/platform/libcore/+/refs/heads/android13-release/ojluni/src/main/java/java/util/regex/Matcher.java#837
|
// Copied from https://android.googlesource.com/platform/libcore/+/refs/heads/android13-release/ojluni/src/main/java/java/util/regex/Matcher.java#837
|
||||||
boolean result = matcher.find();
|
boolean result = matcher.reset().find();
|
||||||
if (result) {
|
if (result) {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
int appendPos = 0;
|
int appendPos = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user