ResXmlEncoders: replaced findNonPositionedSubstitutions() by more flexible findSubstitutions().

Also rewritten some bits.
This commit is contained in:
Ryszard Wiśniewski 2013-05-11 17:53:32 +02:00
parent 997df5cfc5
commit 68f97980eb

View File

@ -16,6 +16,8 @@
package brut.androlib.res.xml; package brut.androlib.res.xml;
import brut.util.Duo;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -135,11 +137,11 @@ public final class ResXmlEncoders {
} }
public static boolean hasMultipleNonPositionalSubstitutions(String str) { 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) { public static String enumerateNonPositionalSubstitutions(String str) {
List<Integer> subs = findNonPositionalSubstitutions(str, -1); List<Integer> subs = findSubstitutions(str, -1).m1;
if (subs.size() < 2) { if (subs.size() < 2) {
return str; 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<Integer> findNonPositionalSubstitutions(String str, private static Duo<List<Integer>, List<Integer>> findSubstitutions(String str, int nonPosMax) {
int max) { if (nonPosMax == -1) {
int pos = 0; nonPosMax = Integer.MAX_VALUE;
}
int pos;
int pos2 = 0; int pos2 = 0;
int count = 0;
int length = str.length(); int length = str.length();
List<Integer> ret = new ArrayList<Integer>(); List<Integer> nonPositional = new ArrayList<>();
while ((pos2 = (pos = str.indexOf('%', pos2)) + 1) != 0) { List<Integer> positional = new ArrayList<>();
while ((pos = str.indexOf('%', pos2)) != -1) {
pos2 = pos + 1;
if (pos2 == length) { if (pos2 == length) {
break; break;
} }
@ -175,21 +182,20 @@ public final class ResXmlEncoders {
continue; continue;
} }
if (c >= '0' && c <= '9' && pos2 < length) { if (c >= '0' && c <= '9' && pos2 < length) {
do { while ((c = str.charAt(pos2++)) >= '0' && c <= '9' && pos2 < length);
c = str.charAt(pos2++); if (c == '$') {
} while (c >= '0' && c <= '9' && pos2 < length); positional.add(pos);
if (c == '$') { continue;
continue; }
}
} }
ret.add(pos); nonPositional.add(pos);
if (max != -1 && ++count >= max) { if (nonPositional.size() >= nonPosMax) {
break; break;
} }
} }
return ret; return new Duo<>(nonPositional, positional);
} }
private static boolean isPrintableChar(char c) { private static boolean isPrintableChar(char c) {