@@ -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