Skip to content

Commit 056ba13

Browse files
Merge pull request #10 from AbdurazaaqMohammed/patch-1
Fix replace with regex
2 parents 8c60b9c + a716468 commit 056ba13

2 files changed

Lines changed: 66 additions & 10 deletions

File tree

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Gradle files
2+
.gradle/
3+
build/
4+
5+
# Local configuration file (sdk path, etc)
6+
local.properties
7+
/local.properties
8+
9+
# Log/OS Files
10+
*.log
11+
12+
# Android Studio generated files and folders
13+
captures/
14+
.externalNativeBuild/
15+
.cxx/
16+
*.apk
17+
output.json
18+
19+
# IntelliJ
20+
*.iml
21+
.idea/
22+
misc.xml
23+
deploymentTargetDropDown.xml
24+
render.experimental.xml
25+
26+
# Keystore files
27+
*.jks
28+
*.keystore
29+
30+
# Google Services (e.g. APIs or Firebase)
31+
google-services.json
32+
33+
# Android Profiling
34+
*.hprof

app/src/main/java/modder/hub/dexeditor/fragment/SearchFragment.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -755,24 +755,46 @@ public void run() {
755755
int countInClass = 0;
756756

757757
if (type.equals("String")) {
758-
String findPattern = exactlyMatch ? "\"" + Pattern.quote(findQuery) + "\"" : Pattern.quote(findQuery);
759-
String replacePattern = exactlyMatch ? "\"" + java.util.regex.Matcher.quoteReplacement(replaceWith) + "\"" : java.util.regex.Matcher.quoteReplacement(replaceWith);
760-
Pattern p = Pattern.compile(findPattern, matchCase ? 0 : Pattern.CASE_INSENSITIVE);
761-
java.util.regex.Matcher m = p.matcher(originalText);
758+
Matcher lm = Pattern.compile("\"((?:\\\\.|[^\"\\\\])*)\"").matcher(originalText);
762759
StringBuffer sb = new StringBuffer();
763-
while (m.find()) {
764-
countInClass++;
765-
m.appendReplacement(sb, replacePattern);
760+
761+
int flag = matchCase ? 0 : Pattern.CASE_INSENSITIVE;
762+
String q1 = isRegex ? findQuery : Pattern.quote(findQuery);
763+
String q2 = exactlyMatch ? "^" + q1 + "$" : q1;
764+
Pattern innerFind = Pattern.compile(q2, flag);
765+
766+
while (lm.find()) {
767+
Matcher im = innerFind.matcher(lm.group(1));
768+
StringBuffer inner = new StringBuffer();
769+
while (im.find()) {
770+
countInClass++;
771+
try {
772+
im.appendReplacement(inner, isRegex ? replaceWith : Matcher.quoteReplacement(replaceWith));
773+
} catch (IndexOutOfBoundsException | IllegalArgumentException e) {
774+
errorClasses.add(className + ": invalid replacement pattern - " + e.getMessage());
775+
return;
776+
}
777+
}
778+
im.appendTail(inner);
779+
780+
String newLiteral = "\"" + inner + "\"";
781+
lm.appendReplacement(sb, Matcher.quoteReplacement(newLiteral));
766782
}
767-
m.appendTail(sb);
783+
lm.appendTail(sb);
768784
modifiedText = sb.toString();
769785
} else {
770786
if (isRegex) {
771787
Matcher m = compiledPattern.matcher(originalText);
772788
StringBuffer sb = new StringBuffer();
773789
while (m.find()) {
774790
countInClass++;
775-
m.appendReplacement(sb, Matcher.quoteReplacement(replaceWith));
791+
try {
792+
m.appendReplacement(sb, replaceWith);
793+
} catch (IndexOutOfBoundsException | IllegalArgumentException e) {
794+
// invalid group reference
795+
errorClasses.add(className + ": invalid replacement pattern - " + e.getMessage());
796+
return;
797+
}
776798
}
777799
m.appendTail(sb);
778800
modifiedText = sb.toString();
@@ -881,7 +903,7 @@ private String generateSmali(ClassDef classDef) throws Exception {
881903
@SuppressLint("NotifyDataSetChanged")
882904
private void onPostExecute(DexEditorActivity activity) {
883905
if (progressDialog.isShowing()) progressDialog.dismiss();
884-
Notify_MT.Notify(activity, "Info", "Total replaced " + replacedCount.get() + "times in " + affectedClasses.get() + "classes." , "Close");
906+
Notify_MT.Notify(activity, "Info", "Total replaced " + replacedCount.get() + " times in " + affectedClasses.get() + " classes." , "Close");
885907
if (!errorClasses.isEmpty()) {
886908
showErrorDialog(activity);
887909
}

0 commit comments

Comments
 (0)