From ea8111fe256db67fd748e81f8e78286cc614a244 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Wed, 9 Sep 2015 19:29:17 +0000 Subject: [PATCH] ICU-11447 better SpoofChecker.CheckResult.toString() X-SVN-Rev: 37923 --- .../src/com/ibm/icu/text/SpoofChecker.java | 75 ++++++++++++------- .../icu/dev/test/text/SpoofCheckerTest.java | 12 +-- 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/SpoofChecker.java b/icu4j/main/classes/core/src/com/ibm/icu/text/SpoofChecker.java index bd749daed8c..edb392849b7 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/SpoofChecker.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/SpoofChecker.java @@ -293,10 +293,10 @@ public class SpoofChecker { /** * Mixed script confusable test. * - * When checking a single identifier, report a problem if the identifier contains multiple scripts, and is also + *

When checking a single identifier, report a problem if the identifier contains multiple scripts, and is also * confusable with some other identifier in a single script. * - * When testing whether two identifiers are confusable, report that they are if the two IDs are visually confusable, + *

When testing whether two identifiers are confusable, report that they are if the two IDs are visually confusable, * and and at least one contains characters from more than one script. * * @stable ICU 4.6 @@ -306,10 +306,10 @@ public class SpoofChecker { /** * Whole script confusable test. * - * When checking a single identifier, report a problem if The identifier is of a single script, and there exists a + *

When checking a single identifier, report a problem if The identifier is of a single script, and there exists a * confusable identifier in another script. * - * When testing whether two Identifiers are confusable, report that they are if each is of a single script, the + *

When testing whether two Identifiers are confusable, report that they are if each is of a single script, the * scripts of the two identifiers are different, and the identifiers are visually confusable. * * @stable ICU 4.6 @@ -319,7 +319,7 @@ public class SpoofChecker { /** * Any Case Modifier for confusable identifier tests. * - * When specified, consider all characters, of any case, when looking for confusables. If ANY_CASE is not specified, + *

When specified, consider all characters, of any case, when looking for confusables. If ANY_CASE is not specified, * identifiers being checked are assumed to have been case folded, and upper case conusable characters will not be * checked. * @@ -372,6 +372,8 @@ public class SpoofChecker { @Deprecated public static final int MIXED_NUMBERS = 128; + // Update CheckResult.toString() when a new check is added. + /** * Enable all spoof checks. * @@ -1569,33 +1571,50 @@ public class SpoofChecker { checks = 0; position = 0; } - - private static final String[] NAMES = { - "SINGLE_SCRIPT_CONFUSABLE", - "MIXED_SCRIPT_CONFUSABLE", - "WHOLE_SCRIPT_CONFUSABLE", - "ANY_CASE", - "RESTRICTION_LEVEL", - "INVISIBLE", - "CHAR_LIMIT", - "MIXED_NUMBERS" - }; - - /* (non-Javadoc) - * @see java.lang.Object#toString() + + /** + * {@inheritDoc} + * @stable ICU 4.6 */ @Override public String toString() { - return "checks: " + getCheckNames(checks) - + ", numerics: " + numerics.toPattern(false) - + ", position: " + position - + ", restrictionLevel: " + restrictionLevel - ; + StringBuilder sb = new StringBuilder(); + sb.append("checks:"); + if (checks == 0) { + sb.append(" none"); + } else if (checks == ALL_CHECKS) { + sb.append(" all"); + } else { + if ((checks & SINGLE_SCRIPT_CONFUSABLE) != 0) { + sb.append(" SINGLE_SCRIPT_CONFUSABLE"); + } + if ((checks & MIXED_SCRIPT_CONFUSABLE) != 0) { + sb.append(" MIXED_SCRIPT_CONFUSABLE"); + } + if ((checks & WHOLE_SCRIPT_CONFUSABLE) != 0) { + sb.append(" WHOLE_SCRIPT_CONFUSABLE"); + } + if ((checks & ANY_CASE) != 0) { + sb.append(" ANY_CASE"); + } + if ((checks & RESTRICTION_LEVEL) != 0) { + sb.append(" RESTRICTION_LEVEL"); + } + if ((checks & INVISIBLE) != 0) { + sb.append(" INVISIBLE"); + } + if ((checks & CHAR_LIMIT) != 0) { + sb.append(" CHAR_LIMIT"); + } + if ((checks & MIXED_NUMBERS) != 0) { + sb.append(" MIXED_NUMBERS"); + } + } + sb.append(", numerics: ").append(numerics.toPattern(false)); + sb.append(", position: ").append(position); + sb.append(", restrictionLevel: ").append(restrictionLevel); + return sb.toString(); } - - static String getCheckNames(int check) { - return check >= 1 && check < (1 << NAMES.length) ? NAMES[(int)Math.round(Math.log10(check)/Math.log10(2))] : null; - } // might be better way to do this, but just threw it together quickly. } /** diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/text/SpoofCheckerTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/text/SpoofCheckerTest.java index e35e7681c89..569fee7bb6a 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/text/SpoofCheckerTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/text/SpoofCheckerTest.java @@ -795,18 +795,12 @@ public class SpoofCheckerTest extends TestFmwk { errln(e.toString()); } } - + public void TestCheckResultToString11447() { - Object[][] tests = { - {"1", "[0]"}, - {"१", "[०]"}, - {"1१", "[0०]"}, - {"١۱", "[٠۰]"}, - }; CheckResult checkResult = new CheckResult(); SpoofChecker sc = new SpoofChecker.Builder() - .setChecks(-1) - .build(); + .setChecks(-1) + .build(); sc.failsChecks("1१", checkResult); assertTrue("CheckResult: ", checkResult.toString().contains("MIXED_NUMBERS")); } -- 2.40.0