From 4a64ad9538ed3cfb1c60e2c6ede7368c6d691973 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 6 Feb 2014 14:39:10 +0900 Subject: [PATCH] Decimal number is treated as outputText Change-Id: I9d4a8ac5dd26a7c41fcfbe1842a40f347a2f2aab --- .../keyboard/internal/KeySpecParser.java | 20 +++++++++----- .../internal/KeySpecParserTestsBase.java | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java index 2925a4b76..8ce820cf3 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeySpecParser.java @@ -57,8 +57,15 @@ public final class KeySpecParser { } private static boolean hasCode(final String keySpec, final int labelEnd) { - if (labelEnd > 0 && labelEnd + 1 < keySpec.length() - && keySpec.startsWith(KeyboardCodesSet.PREFIX_CODE, labelEnd + 1)) { + if (labelEnd <= 0 || labelEnd + 1 >= keySpec.length()) { + return false; + } + if (keySpec.startsWith(KeyboardCodesSet.PREFIX_CODE, labelEnd + 1)) { + return true; + } + // This is a workaround to have a key that has a supplementary code point. We can't put a + // string in resource as a XML entity of a supplementary code point or a surrogate pair. + if (keySpec.startsWith(PREFIX_HEX, labelEnd + 1)) { return true; } return false; @@ -203,19 +210,20 @@ public final class KeySpecParser { return (StringUtils.codePointCount(label) == 1) ? label.codePointAt(0) : CODE_OUTPUT_TEXT; } - // TODO: Make this method private once Key.code attribute is removed. public static int parseCode(final String text, final KeyboardCodesSet codesSet, - final int defCode) { + final int defaultCode) { if (text == null) { - return defCode; + return defaultCode; } if (text.startsWith(KeyboardCodesSet.PREFIX_CODE)) { return codesSet.getCode(text.substring(KeyboardCodesSet.PREFIX_CODE.length())); } + // This is a workaround to have a key that has a supplementary code point. We can't put a + // string in resource as a XML entity of a supplementary code point or a surrogate pair. if (text.startsWith(PREFIX_HEX)) { return Integer.parseInt(text.substring(PREFIX_HEX.length()), 16); } - return Integer.parseInt(text); + return defaultCode; } public static int getIconId(final String keySpec) { diff --git a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTestsBase.java b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTestsBase.java index cb640b3f8..5d842950c 100644 --- a/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTestsBase.java +++ b/tests/src/com/android/inputmethod/keyboard/internal/KeySpecParserTestsBase.java @@ -219,6 +219,32 @@ abstract class KeySpecParserTestsBase extends AndroidTestCase { "a|c", null, ICON_UNDEFINED, mCodeSettings); } + public void testCodes() { + assertParser("Hexadecimal code", "a|0x1000", + "a", null, ICON_UNDEFINED, 0x1000); + assertParserError("Illegal hexadecimal code", "a|0x100X", + "a", null, ICON_UNDEFINED, CODE_UNSPECIFIED); + assertParser("Escaped hexadecimal code 1", "a|\\0x1000", + "a", "0x1000", ICON_UNDEFINED, CODE_OUTPUT_TEXT); + assertParser("Escaped hexadecimal code 2", "a|0\\x1000", + "a", "0x1000", ICON_UNDEFINED, CODE_OUTPUT_TEXT); + assertParser("Escaped hexadecimal code 2", "a|0\\x1000", + "a", "0x1000", ICON_UNDEFINED, CODE_OUTPUT_TEXT); + assertParserError("Illegally escaped hexadecimal code", "a|0x1\\000", + "a", null, ICON_UNDEFINED, CODE_UNSPECIFIED); + // This is a workaround to have a key that has a supplementary code point. We can't put a + // string in resource as a XML entity of a supplementary code point or a surrogate pair. + // TODO: Should pass this test. +// assertParser("Hexadecimal supplementary code", String.format("a|0x%06x", SURROGATE_CODE2), +// SURROGATE_PAIR2, null, ICON_UNDEFINED, SURROGATE_CODE2); + assertParser("Zero is treated as output text", "a|0", + "a", null, ICON_UNDEFINED, '0'); + assertParser("Digit is treated as output text", "a|3", + "a", null, ICON_UNDEFINED, '3'); + assertParser("Decimal number is treated as an output text", "a|2014", + "a", "2014", ICON_UNDEFINED, CODE_OUTPUT_TEXT); + } + public void testIcons() { assertParser("Icon with single letter", ICON_SETTINGS + "|a", null, null, mSettingsIconId, 'a');