make unicode detector work on labels

This commit is contained in:
Daniel Gultsch 2018-03-09 21:39:10 +01:00
parent e2e5c04ef7
commit fe338a540a

View file

@ -86,16 +86,14 @@ public class IrregularUnicodeDetector {
builder.append('@'); builder.append('@');
} }
if (jid.getDomain() != null) { if (jid.getDomain() != null) {
int i = jid.getDomain().lastIndexOf('.'); String[] labels = jid.getDomain().split("\\.");
if (i != -1) { for (int i = 0; i < labels.length; ++i) {
String second = jid.getDomain().substring(0, i); SpannableString spannableString = new SpannableString(labels[i]);
String top = jid.getDomain().substring(i, jid.getDomain().length()); colorize(spannableString, patternTuple.domain.get(i), color);
SpannableString secondSpannableString = new SpannableString(second); if (i != 0) {
colorize(secondSpannableString, patternTuple.domain, color); builder.append('.');
builder.append(secondSpannableString); }
builder.append(top); builder.append(spannableString);
} else {
builder.append(jid.getDomain());
} }
} }
if (builder.length() != 0 && jid.getResource() != null) { if (builder.length() != 0 && jid.getResource() != null) {
@ -217,9 +215,9 @@ public class IrregularUnicodeDetector {
private static class PatternTuple { private static class PatternTuple {
private final Pattern local; private final Pattern local;
private final Pattern domain; private final List<Pattern> domain;
private PatternTuple(Pattern local, Pattern domain) { private PatternTuple(Pattern local, List<Pattern> domain) {
this.local = local; this.local = local;
this.domain = domain; this.domain = domain;
} }
@ -232,19 +230,13 @@ public class IrregularUnicodeDetector {
localPattern = null; localPattern = null;
} }
String domain = jid.getDomain(); String domain = jid.getDomain();
final Pattern domainPattern; final List<Pattern> domainPatterns = new ArrayList<>();
if (domain != null) { if (domain != null) {
int i = domain.lastIndexOf('.'); for (String label : domain.split("\\.")) {
if (i != -1) { domainPatterns.add(create(findIrregularCodePoints(label)));
String secondLevel = domain.substring(0, i);
domainPattern = create(findIrregularCodePoints(secondLevel));
} else {
domainPattern = null;
} }
} else {
domainPattern = null;
} }
return new PatternTuple(localPattern, domainPattern); return new PatternTuple(localPattern, domainPatterns);
} }
} }
} }