From 68f97980eb0c20b52b27697779f9b56ee8ba0a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ryszard=20Wi=C5=9Bniewski?= Date: Sat, 11 May 2013 17:53:32 +0200 Subject: [PATCH] ResXmlEncoders: replaced findNonPositionedSubstitutions() by more flexible findSubstitutions(). Also rewritten some bits. --- .../brut/androlib/res/xml/ResXmlEncoders.java | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlEncoders.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlEncoders.java index ea46364f..ec1d7afe 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlEncoders.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/xml/ResXmlEncoders.java @@ -16,6 +16,8 @@ package brut.androlib.res.xml; +import brut.util.Duo; + import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.List; @@ -135,11 +137,11 @@ public final class ResXmlEncoders { } public static boolean hasMultipleNonPositionalSubstitutions(String str) { - return findNonPositionalSubstitutions(str, 2).size() > 1; + return findSubstitutions(str, 2).m1.size() > 1; } public static String enumerateNonPositionalSubstitutions(String str) { - List subs = findNonPositionalSubstitutions(str, -1); + List subs = findSubstitutions(str, -1).m1; if (subs.size() < 2) { return str; } @@ -157,16 +159,21 @@ public final class ResXmlEncoders { } /** - * It searches for "%", but not "%%" nor "%(\d)+\$" + * It returns a tuple of: + * - a list of offsets of non positional substitutions. non-pos is defined as any "%" which isn't "%%" nor "%\d+\$" + * - a list of offsets of positional substitutions */ - private static List findNonPositionalSubstitutions(String str, - int max) { - int pos = 0; + private static Duo, List> findSubstitutions(String str, int nonPosMax) { + if (nonPosMax == -1) { + nonPosMax = Integer.MAX_VALUE; + } + int pos; int pos2 = 0; - int count = 0; int length = str.length(); - List ret = new ArrayList(); - while ((pos2 = (pos = str.indexOf('%', pos2)) + 1) != 0) { + List nonPositional = new ArrayList<>(); + List positional = new ArrayList<>(); + while ((pos = str.indexOf('%', pos2)) != -1) { + pos2 = pos + 1; if (pos2 == length) { break; } @@ -175,21 +182,20 @@ public final class ResXmlEncoders { continue; } if (c >= '0' && c <= '9' && pos2 < length) { - do { - c = str.charAt(pos2++); - } while (c >= '0' && c <= '9' && pos2 < length); - if (c == '$') { - continue; - } + while ((c = str.charAt(pos2++)) >= '0' && c <= '9' && pos2 < length); + if (c == '$') { + positional.add(pos); + continue; + } } - ret.add(pos); - if (max != -1 && ++count >= max) { + nonPositional.add(pos); + if (nonPositional.size() >= nonPosMax) { break; } } - return ret; + return new Duo<>(nonPositional, positional); } private static boolean isPrintableChar(char c) {