ResXmlEncoders +hasMultipleNonPositionalSubstitutions() +enumerateNonPositionalSubstitutions() .

This commit is contained in:
Ryszard Wiśniewski 2011-05-14 03:47:41 +02:00
parent 19b5e2726a
commit 265ee7ceac
2 changed files with 34 additions and 9 deletions

View File

@ -53,15 +53,8 @@ public class ResStringValue extends ResScalarValue {
@Override @Override
protected void serializeExtraXmlAttrs(XmlSerializer serializer, protected void serializeExtraXmlAttrs(XmlSerializer serializer,
ResResource res) throws IOException { ResResource res) throws IOException {
int pos = 0; if (ResXmlEncoders.hasMultipleNonPositionalSubstitutions(mRawValue)) {
int count = 0; serializer.attribute(null, "formatted", "false");
while((pos = mRawValue.indexOf('%', pos)) != -1) {
if (mRawValue.charAt(pos + 1) != '%') {
if (++count >= 2) {
serializer.attribute(null, "formatted", "false");
return;
}
}
} }
} }
} }

View File

@ -133,6 +133,38 @@ public final class ResXmlEncoders {
return out.toString(); return out.toString();
} }
public static boolean hasMultipleNonPositionalSubstitutions(String str) {
int pos = 0;
int count = 0;
while((pos = str.indexOf('%', pos)) != -1) {
if (str.charAt(pos + 1) != '%') {
if (++count >= 2) {
return true;
}
}
pos += 2;
}
return false;
}
public static String enumerateNonPositionalSubstitutions(String str) {
StringBuilder out = new StringBuilder(str);
int pos = 0;
int count = 0;
int offset = 0;
while((pos = str.indexOf('%', pos)) != -1) {
if (str.charAt(pos + 1) != '%') {
count++;
out.insert(pos + offset + 1, String.valueOf(count) + "$");
offset += 2;
}
pos += 2;
}
return out.toString();
}
private static boolean isPrintableChar(char c) { private static boolean isPrintableChar(char c) {
Character.UnicodeBlock block = Character.UnicodeBlock.of(c); Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
return !Character.isISOControl(c) return !Character.isISOControl(c)