diff -r 5ba63022681b javafx-ui-common/src/com/sun/javafx/Utils.java --- a/javafx-ui-common/src/com/sun/javafx/Utils.java Tue Aug 21 15:48:30 2012 -0400 +++ b/javafx-ui-common/src/com/sun/javafx/Utils.java Tue Aug 21 16:00:36 2012 -0400 @@ -1083,91 +1083,75 @@ * Unicode-related utilities * * * **************************************************************************/ - - private static UnicodeProcessor unicodeProcessor = new UnicodeProcessor(); + public static String convertUnicode(String src) { - return unicodeProcessor.process(src); - } - - private static final class UnicodeProcessor { /** The input buffer, index of next character to be read, * index of one past last character in buffer. */ - private char[] buf; - private int bp; - private int buflen; + char[] buf; + int bp; + int buflen; /** The current character. */ - private char ch; + char ch; /** The buffer index of the last converted unicode character */ - private int unicodeConversionBp = -1; + int unicodeConversionBp = -1; + + buf = src.toCharArray(); + buflen = buf.length; + bp = -1; - public String process(String src) { - buf = src.toCharArray(); - buflen = buf.length; - bp = -1; + char[] dst = new char[buflen]; + int dstIndex = 0; - char[] dst = new char[buflen]; - int dstIndex = 0; - - while (bp < buflen - 1) { - ch = buf[++bp]; - if (ch == '\\') { - convertUnicode(); - } - dst[dstIndex++] = ch; - } - - return new String(dst, 0, dstIndex); - } - - /** Convert unicode escape; bp points to initial '\' character - * (Spec 3.3). - */ - private void convertUnicode() { - if (ch == '\\' && unicodeConversionBp != bp) { - bp++; ch = buf[bp]; - if (ch == 'u') { - do { - bp++; ch = buf[bp]; - } while (ch == 'u'); - int limit = bp + 3; - if (limit < buflen) { - int d = digit(16); - int code = d; - while (bp < limit && d >= 0) { + while (bp < buflen - 1) { + ch = buf[++bp]; + if (ch == '\\') { + if (unicodeConversionBp != bp) { + bp++; ch = buf[bp]; + if (ch == 'u') { + do { bp++; ch = buf[bp]; - d = digit(16); - code = (code << 4) + d; + } while (ch == 'u'); + int limit = bp + 3; + if (limit < buflen) { + char c = ch; + int result = Character.digit(c, 16); + if (result >= 0 && c > 0x7f) { + //lexError(pos+1, "illegal.nonascii.digit"); + ch = "0123456789abcdef".charAt(result); + } + int d = result; + int code = d; + while (bp < limit && d >= 0) { + bp++; ch = buf[bp]; + char c1 = ch; + int result1 = Character.digit(c1, 16); + if (result1 >= 0 && c1 > 0x7f) { + //lexError(pos+1, "illegal.nonascii.digit"); + ch = "0123456789abcdef".charAt(result1); + } + d = result1; + code = (code << 4) + d; + } + if (d >= 0) { + ch = (char)code; + unicodeConversionBp = bp; + } } - if (d >= 0) { - ch = (char)code; - unicodeConversionBp = bp; - return; - } + //lexError(bp, "illegal.unicode.esc"); + } else { + bp--; + ch = '\\'; } - //lexError(bp, "illegal.unicode.esc"); - } else { - bp--; - ch = '\\'; } } + dst[dstIndex++] = ch; } - - /** Convert an ASCII digit from its base (8, 10, or 16) - * to its value. - */ - private int digit(int base) { - char c = ch; - int result = Character.digit(c, base); - if (result >= 0 && c > 0x7f) { - //lexError(pos+1, "illegal.nonascii.digit"); - ch = "0123456789abcdef".charAt(result); - } - return result; - } + + return new String(dst, 0, dstIndex); } } diff -r 5ba63022681b javafx-ui-common/test/unit/com/sun/javafx/UtilsTest.java --- a/javafx-ui-common/test/unit/com/sun/javafx/UtilsTest.java Tue Aug 21 15:48:30 2012 -0400 +++ b/javafx-ui-common/test/unit/com/sun/javafx/UtilsTest.java Tue Aug 21 16:00:36 2012 -0400 @@ -58,4 +58,78 @@ split = Utils.split(s, "VK_LEFT_ARROW_EXT"); assertEquals("Array content: " + Arrays.toString(split),0, split.length); } + + @Test + public void testConvertUnicode() { + String s = ""; + String r = Utils.convertUnicode(s); + assertEquals("", r); + + /*String*/ s = "test"; + /*String*/ r = Utils.convertUnicode(s); + assertEquals("test", r); + + /*String*/ s = "hi\\u1234"; + /*String*/ r = Utils.convertUnicode(s); + assertEquals("hi\u1234", r); + + /*String*/ s = "\\u5678"; + /*String*/ r = Utils.convertUnicode(s); + assertEquals("\u5678", r); + + /*String*/ s = "hi\\u1234there\\u432112"; + /*String*/ r = Utils.convertUnicode(s); + assertEquals("hi\u1234there\u432112", r); + + /*String*/ s = "Hello\u5678There"; + /*String*/ r = Utils.convertUnicode(s); + assertEquals("Hello\u5678There", r); + + /*String*/ s = "\\this\\is\\a\\windows\\path"; + /*String*/ r = Utils.convertUnicode(s); + assertEquals("\\this\\is\\a\\windows\\path", r); + + /*String*/ s = "\\this\\is\\a\\12\\windows\\path"; + /*String*/ r = Utils.convertUnicode(s); + assertEquals("\\this\\is\\a\\12\\windows\\path", r); + + /*String*/ s = "u12u12"; + /*String*/ r = Utils.convertUnicode(s); + assertEquals("u12u12", r); + + /*String*/ s = "hello\nu1234\n"; + /*String*/ r = Utils.convertUnicode(s); + assertEquals("hello\nu1234\n", r); + } + + @Test + public void testConvertUnicodeFail2_2() { + + //Error case - null + //String s = null; + //String r = Utils.convertUnicode(s); + //assertEquals("", r); + + //String s = "\\"; + //String r = Utils.convertUnicode(s); + //assertEquals("\\", r); + + //Error case - no length + ///*String*/ s = "hi\\u"; + ///*String*/ r = Utils.convertUnicode(s); + //assertEquals("hi\\u", r); + } + + @Test + public void testConvertUnicodeWrong2_2() { + + //Error case - short length + String s = "hi\\u12"; + String r = Utils.convertUnicode(s); + //assertEquals("hi\\u12", r); + + /*String*/ s = "\\this\\is\\a\\umm\\windows\\path"; + /*String*/ r = Utils.convertUnicode(s); + //assertEquals("\\this\\is\\a\\umm\\windows\\path", r); + } }