From a3e1c3469767f8c4101d4383e78efc9467347e61 Mon Sep 17 00:00:00 2001 From: Doug Felt Date: Thu, 11 Mar 2004 21:42:41 +0000 Subject: [PATCH] ICU-3630 cover jsr 204 APIs where possible, also jb 3523 after a fashion X-SVN-Rev: 14689 --- .../com/ibm/icu/dev/test/lang/UTF16Test.java | 1044 +++++++++-------- icu4j/src/com/ibm/icu/text/UTF16.java | 677 +++++------ 2 files changed, 874 insertions(+), 847 deletions(-) diff --git a/icu4j/src/com/ibm/icu/dev/test/lang/UTF16Test.java b/icu4j/src/com/ibm/icu/dev/test/lang/UTF16Test.java index f642bdd8343..19ce0cf1a11 100755 --- a/icu4j/src/com/ibm/icu/dev/test/lang/UTF16Test.java +++ b/icu4j/src/com/ibm/icu/dev/test/lang/UTF16Test.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/lang/UTF16Test.java,v $ -* $Date: 2004/03/11 16:51:50 $ -* $Revision: 1.21 $ +* $Date: 2004/03/11 21:42:41 $ +* $Revision: 1.22 $ * ******************************************************************************* */ @@ -26,212 +26,218 @@ import com.ibm.icu.impl.Utility; */ public final class UTF16Test extends TestFmwk { - // constructor =================================================== + // constructor =================================================== - /** - * Constructor - */ - public UTF16Test() - { - } + /** + * Constructor + */ + public UTF16Test() + { + } - // public methods ================================================ + // public methods ================================================ - /** - * Testing UTF16 class methods append - */ - public void TestAppend() - { + /** + * Testing UTF16 class methods append + */ + public void TestAppend() + { StringBuffer strbuff = new StringBuffer("this is a string "); char array[] = new char[UCharacter.MAX_VALUE >> 2]; int strsize = strbuff.length(); int arraysize = strsize; Utility.getChars(strbuff, 0, strsize, array, 0); - for (int i = 1; i < UCharacter.MAX_VALUE; i += 100) - { - UTF16.append(strbuff, i); - arraysize = UTF16.append(array, arraysize, i); + for (int i = 1; i < UCharacter.MAX_VALUE; i += 100) { + UTF16.append(strbuff, i); + arraysize = UTF16.append(array, arraysize, i); - String arraystr = new String(array, 0, arraysize); - if (!arraystr.equals(strbuff.toString())) { - errln("FAIL Comparing char array append and string append " + - "with 0x" + Integer.toHexString(i)); - } + String arraystr = new String(array, 0, arraysize); + if (!arraystr.equals(strbuff.toString())) { + errln("FAIL Comparing char array append and string append " + + "with 0x" + Integer.toHexString(i)); + } - // this is to cater for the combination of 0xDBXX 0xDC50 which - // forms a supplementary character - if (i == 0xDC51) { - strsize --; - } + // this is to cater for the combination of 0xDBXX 0xDC50 which + // forms a supplementary character + if (i == 0xDC51) { + strsize --; + } - if (UTF16.countCodePoint(strbuff) != strsize + (i / 100) + 1) { - errln("FAIL Counting code points in string appended with " + - " 0x" + Integer.toHexString(i)); - break; - } - } - } + if (UTF16.countCodePoint(strbuff) != strsize + (i / 100) + 1) { + errln("FAIL Counting code points in string appended with " + + " 0x" + Integer.toHexString(i)); + break; + } + } + + // coverage for new 1.5 - cover only so no real test + strbuff = new StringBuffer(); + UTF16.appendCodePoint(strbuff, 0x10000); + if (strbuff.length() != 2) { + errln("fail appendCodePoint"); + } + } - /** - * Testing UTF16 class methods bounds - */ - public void TestBounds() - { + /** + * Testing UTF16 class methods bounds + */ + public void TestBounds() + { StringBuffer strbuff = - //0 12345 6 7 8 9 - new StringBuffer("\udc000123\ud800\udc00\ud801\udc01\ud802"); + //0 12345 6 7 8 9 + new StringBuffer("\udc000123\ud800\udc00\ud801\udc01\ud802"); String str = strbuff.toString(); char array[] = str.toCharArray(); int boundtype[] = {UTF16.SINGLE_CHAR_BOUNDARY, - UTF16.SINGLE_CHAR_BOUNDARY, - UTF16.SINGLE_CHAR_BOUNDARY, - UTF16.SINGLE_CHAR_BOUNDARY, - UTF16.SINGLE_CHAR_BOUNDARY, - UTF16.LEAD_SURROGATE_BOUNDARY, - UTF16.TRAIL_SURROGATE_BOUNDARY, - UTF16.LEAD_SURROGATE_BOUNDARY, - UTF16.TRAIL_SURROGATE_BOUNDARY, - UTF16.SINGLE_CHAR_BOUNDARY}; + UTF16.SINGLE_CHAR_BOUNDARY, + UTF16.SINGLE_CHAR_BOUNDARY, + UTF16.SINGLE_CHAR_BOUNDARY, + UTF16.SINGLE_CHAR_BOUNDARY, + UTF16.LEAD_SURROGATE_BOUNDARY, + UTF16.TRAIL_SURROGATE_BOUNDARY, + UTF16.LEAD_SURROGATE_BOUNDARY, + UTF16.TRAIL_SURROGATE_BOUNDARY, + UTF16.SINGLE_CHAR_BOUNDARY}; int length = str.length(); for (int i = 0; i < length; i ++) { - if (UTF16.bounds(str, i) != boundtype[i]) { + if (UTF16.bounds(str, i) != boundtype[i]) { errln("FAIL checking bound type at index " + i); - } - if (UTF16.bounds(strbuff, i) != boundtype[i]) { + } + if (UTF16.bounds(strbuff, i) != boundtype[i]) { errln("FAIL checking bound type at index " + i); - } - if (UTF16.bounds(array, 0, length, i) != boundtype[i]) { + } + if (UTF16.bounds(array, 0, length, i) != boundtype[i]) { errln("FAIL checking bound type at index " + i); - } + } } // does not straddle between supplementary character int start = 4; int limit = 9; int subboundtype1[] = {UTF16.SINGLE_CHAR_BOUNDARY, - UTF16.LEAD_SURROGATE_BOUNDARY, - UTF16.TRAIL_SURROGATE_BOUNDARY, - UTF16.LEAD_SURROGATE_BOUNDARY, - UTF16.TRAIL_SURROGATE_BOUNDARY}; + UTF16.LEAD_SURROGATE_BOUNDARY, + UTF16.TRAIL_SURROGATE_BOUNDARY, + UTF16.LEAD_SURROGATE_BOUNDARY, + UTF16.TRAIL_SURROGATE_BOUNDARY}; try { - UTF16.bounds(array, start, limit, -1); - errln("FAIL Out of bounds index in bounds should fail"); + UTF16.bounds(array, start, limit, -1); + errln("FAIL Out of bounds index in bounds should fail"); } catch (Exception e) { - // getting rid of warnings - System.out.print(""); + // getting rid of warnings + System.out.print(""); } for (int i = 0; i < limit - start; i ++) { - if (UTF16.bounds(array, start, limit, i) != subboundtype1[i]) { + if (UTF16.bounds(array, start, limit, i) != subboundtype1[i]) { errln("FAILED Subarray bounds in [" + start + ", " + limit + - "] expected " + subboundtype1[i] + " at offset " + i); - } + "] expected " + subboundtype1[i] + " at offset " + i); + } } // starts from the mid of a supplementary character int subboundtype2[] = {UTF16.SINGLE_CHAR_BOUNDARY, - UTF16.LEAD_SURROGATE_BOUNDARY, - UTF16.TRAIL_SURROGATE_BOUNDARY}; + UTF16.LEAD_SURROGATE_BOUNDARY, + UTF16.TRAIL_SURROGATE_BOUNDARY}; start = 6; limit = 9; for (int i = 0; i < limit - start; i ++) { - if (UTF16.bounds(array, start, limit, i) != subboundtype2[i]) { + if (UTF16.bounds(array, start, limit, i) != subboundtype2[i]) { errln("FAILED Subarray bounds in [" + start + ", " + limit + - "] expected " + subboundtype2[i] + " at offset " + i); - } + "] expected " + subboundtype2[i] + " at offset " + i); + } } // ends in the mid of a supplementary character int subboundtype3[] = {UTF16.LEAD_SURROGATE_BOUNDARY, - UTF16.TRAIL_SURROGATE_BOUNDARY, - UTF16.SINGLE_CHAR_BOUNDARY}; + UTF16.TRAIL_SURROGATE_BOUNDARY, + UTF16.SINGLE_CHAR_BOUNDARY}; start = 5; limit = 8; for (int i = 0; i < limit - start; i ++) { - if (UTF16.bounds(array, start, limit, i) != subboundtype3[i]) { + if (UTF16.bounds(array, start, limit, i) != subboundtype3[i]) { errln("FAILED Subarray bounds in [" + start + ", " + limit + - "] expected " + subboundtype3[i] + " at offset " + i); - } + "] expected " + subboundtype3[i] + " at offset " + i); + } } - } + } - /** - * Testing UTF16 class methods charAt and charAtCodePoint - */ - public void TestCharAt() - { + /** + * Testing UTF16 class methods charAt and charAtCodePoint + */ + public void TestCharAt() + { StringBuffer strbuff = - new StringBuffer("12345\ud800\udc0167890\ud800\udc02"); + new StringBuffer("12345\ud800\udc0167890\ud800\udc02"); if (UTF16.charAt(strbuff, 0) != '1' || UTF16.charAt(strbuff, 2) != '3' || UTF16.charAt(strbuff, 5) != 0x10001 || - UTF16.charAt(strbuff, 6) != 0x10001 || - UTF16.charAt(strbuff, 12) != 0x10002 || - UTF16.charAt(strbuff, 13) != 0x10002) { - errln("FAIL Getting character from string buffer error" ); + UTF16.charAt(strbuff, 6) != 0x10001 || + UTF16.charAt(strbuff, 12) != 0x10002 || + UTF16.charAt(strbuff, 13) != 0x10002) { + errln("FAIL Getting character from string buffer error" ); } String str = strbuff.toString(); if (UTF16.charAt(str, 0) != '1' || UTF16.charAt(str, 2) != '3' || - UTF16.charAt(str, 5) != 0x10001 || UTF16.charAt(str, 6) != 0x10001 - || UTF16.charAt(str, 12) != 0x10002 || - UTF16.charAt(str, 13) != 0x10002) - { + UTF16.charAt(str, 5) != 0x10001 || UTF16.charAt(str, 6) != 0x10001 + || UTF16.charAt(str, 12) != 0x10002 || + UTF16.charAt(str, 13) != 0x10002) + { errln("FAIL Getting character from string error" ); - } + } char array[] = str.toCharArray(); int start = 0; int limit = str.length(); if (UTF16.charAt(array, start, limit, 0) != '1' || - UTF16.charAt(array, start, limit, 2) != '3' || - UTF16.charAt(array, start, limit, 5) != 0x10001 || - UTF16.charAt(array, start, limit, 6) != 0x10001 || - UTF16.charAt(array, start, limit, 12) != 0x10002 || - UTF16.charAt(array, start, limit, 13) != 0x10002) { - errln("FAIL Getting character from array error" ); + UTF16.charAt(array, start, limit, 2) != '3' || + UTF16.charAt(array, start, limit, 5) != 0x10001 || + UTF16.charAt(array, start, limit, 6) != 0x10001 || + UTF16.charAt(array, start, limit, 12) != 0x10002 || + UTF16.charAt(array, start, limit, 13) != 0x10002) { + errln("FAIL Getting character from array error" ); } // check the sub array here. start = 6; limit = 13; try { - UTF16.charAt(array, start, limit, -1); - errln("FAIL out of bounds error expected"); + UTF16.charAt(array, start, limit, -1); + errln("FAIL out of bounds error expected"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.charAt(array, start, limit, 8); - errln("FAIL out of bounds error expected"); + UTF16.charAt(array, start, limit, 8); + errln("FAIL out of bounds error expected"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } if (UTF16.charAt(array, start, limit, 0) != 0xdc01) { - errln("FAIL Expected result in subarray 0xdc01"); + errln("FAIL Expected result in subarray 0xdc01"); } if (UTF16.charAt(array, start, limit, 6) != 0xd800) { - errln("FAIL Expected result in subarray 0xd800"); + errln("FAIL Expected result in subarray 0xd800"); } ReplaceableString replaceable = new ReplaceableString(str); if (UTF16.charAt(replaceable, 0) != '1' || UTF16.charAt(replaceable, 2) != '3' || - UTF16.charAt(replaceable, 5) != 0x10001 || - UTF16.charAt(replaceable, 6) != 0x10001 || - UTF16.charAt(replaceable, 12) != 0x10002 || - UTF16.charAt(replaceable, 13) != 0x10002) { - errln("FAIL Getting character from replaceable error" ); + UTF16.charAt(replaceable, 5) != 0x10001 || + UTF16.charAt(replaceable, 6) != 0x10001 || + UTF16.charAt(replaceable, 12) != 0x10002 || + UTF16.charAt(replaceable, 13) != 0x10002) { + errln("FAIL Getting character from replaceable error" ); } - } + } - /** - * Testing UTF16 class methods countCodePoint - */ - public void TestCountCodePoint() - { + /** + * Testing UTF16 class methods countCodePoint + */ + public void TestCountCodePoint() + { StringBuffer strbuff = new StringBuffer(""); char array[] = null; if (UTF16.countCodePoint(strbuff) != 0 || - UTF16.countCodePoint("") != 0 || - UTF16.countCodePoint(array,0 ,0) != 0) { - errln("FAIL Counting code points for empty strings"); + UTF16.countCodePoint("") != 0 || + UTF16.countCodePoint(array,0 ,0) != 0) { + errln("FAIL Counting code points for empty strings"); } strbuff = new StringBuffer("this is a string "); @@ -240,41 +246,41 @@ public final class UTF16Test extends TestFmwk int size = str.length(); if (UTF16.countCodePoint(array, 0, 0) != 0) { - errln("FAIL Counting code points for 0 offset array"); + errln("FAIL Counting code points for 0 offset array"); } if (UTF16.countCodePoint(str) != size || - UTF16.countCodePoint(strbuff) != size || - UTF16.countCodePoint(array, 0, size) != size) { - errln("FAIL Counting code points"); + UTF16.countCodePoint(strbuff) != size || + UTF16.countCodePoint(array, 0, size) != size) { + errln("FAIL Counting code points"); } UTF16.append(strbuff, 0x10000); str = strbuff.toString(); array = str.toCharArray(); if (UTF16.countCodePoint(str) != size + 1 || - UTF16.countCodePoint(strbuff) != size + 1 || - UTF16.countCodePoint(array, 0, size + 1) != size + 1 || - UTF16.countCodePoint(array, 0, size + 2) != size + 1) { - errln("FAIL Counting code points"); + UTF16.countCodePoint(strbuff) != size + 1 || + UTF16.countCodePoint(array, 0, size + 1) != size + 1 || + UTF16.countCodePoint(array, 0, size + 2) != size + 1) { + errln("FAIL Counting code points"); } UTF16.append(strbuff, 0x61); str = strbuff.toString(); array = str.toCharArray(); if (UTF16.countCodePoint(str) != size + 2 || - UTF16.countCodePoint(strbuff) != size + 2 || - UTF16.countCodePoint(array, 0, size + 1) != size + 1 || - UTF16.countCodePoint(array, 0, size + 2) != size + 1 || - UTF16.countCodePoint(array, 0, size + 3) != size + 2) { - errln("FAIL Counting code points"); + UTF16.countCodePoint(strbuff) != size + 2 || + UTF16.countCodePoint(array, 0, size + 1) != size + 1 || + UTF16.countCodePoint(array, 0, size + 2) != size + 1 || + UTF16.countCodePoint(array, 0, size + 3) != size + 2) { + errln("FAIL Counting code points"); } - } + } - /** - * Testing UTF16 class methods delete - */ - public void TestDelete() - { //01234567890123456 + /** + * Testing UTF16 class methods delete + */ + public void TestDelete() + { //01234567890123456 StringBuffer strbuff = new StringBuffer("these are strings"); int size = strbuff.length(); char array[] = strbuff.toString().toCharArray(); @@ -286,15 +292,15 @@ public final class UTF16Test extends TestFmwk UTF16.delete(strbuff, 3); UTF16.delete(strbuff, 3); try { - UTF16.delete(strbuff, strbuff.length()); - errln("FAIL deleting out of bounds character should fail"); + UTF16.delete(strbuff, strbuff.length()); + errln("FAIL deleting out of bounds character should fail"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } UTF16.delete(strbuff, strbuff.length() - 1); if (!strbuff.toString().equals("the string")) { - errln("FAIL expected result after deleting characters is " + - "\"the string\""); + errln("FAIL expected result after deleting characters is " + + "\"the string\""); } size = UTF16.delete(array, size, 3); @@ -304,18 +310,18 @@ public final class UTF16Test extends TestFmwk size = UTF16.delete(array, size, 3); size = UTF16.delete(array, size, 3); try { - UTF16.delete(array, size, size); - errln("FAIL deleting out of bounds character should fail"); + UTF16.delete(array, size, size); + errln("FAIL deleting out of bounds character should fail"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } size = UTF16.delete(array, size, size - 1); String str = new String(array, 0, size); if (!str.equals("the string")) { - errln("FAIL expected result after deleting characters is " + - "\"the string\""); + errln("FAIL expected result after deleting characters is " + + "\"the string\""); } - //012345678 9 01 2 3 4 + //012345678 9 01 2 3 4 strbuff = new StringBuffer("string: \ud800\udc00 \ud801\udc01 \ud801\udc01"); size = strbuff.length(); array = strbuff.toString().toCharArray(); @@ -328,7 +334,7 @@ public final class UTF16Test extends TestFmwk UTF16.delete(strbuff, 6); UTF16.delete(strbuff, 6); if (!strbuff.toString().equals("string")) { - errln("FAIL expected result after deleting characters is \"string\""); + errln("FAIL expected result after deleting characters is \"string\""); } size = UTF16.delete(array, size, 8); @@ -340,153 +346,153 @@ public final class UTF16Test extends TestFmwk size = UTF16.delete(array, size, 6); str = new String(array, 0, size); if (!str.equals("string")) { - errln("FAIL expected result after deleting characters is \"string\""); + errln("FAIL expected result after deleting characters is \"string\""); } - } + } - /** - * Testing findOffsetFromCodePoint and findCodePointOffset - */ - public void TestfindOffset() - { + /** + * Testing findOffsetFromCodePoint and findCodePointOffset + */ + public void TestfindOffset() + { // jitterbug 47 String str = "a\uD800\uDC00b"; StringBuffer strbuff = new StringBuffer(str); char array[] = str.toCharArray(); int limit = str.length(); if (UTF16.findCodePointOffset(str, 0) != 0 || - UTF16.findOffsetFromCodePoint(str, 0) != 0 || - UTF16.findCodePointOffset(strbuff, 0) != 0 || - UTF16.findOffsetFromCodePoint(strbuff, 0) != 0 || - UTF16.findCodePointOffset(array, 0, limit, 0) != 0 || - UTF16.findOffsetFromCodePoint(array, 0, limit, 0) != 0) { - errln("FAIL Getting the first codepoint offset to a string with " + - "supplementary characters"); + UTF16.findOffsetFromCodePoint(str, 0) != 0 || + UTF16.findCodePointOffset(strbuff, 0) != 0 || + UTF16.findOffsetFromCodePoint(strbuff, 0) != 0 || + UTF16.findCodePointOffset(array, 0, limit, 0) != 0 || + UTF16.findOffsetFromCodePoint(array, 0, limit, 0) != 0) { + errln("FAIL Getting the first codepoint offset to a string with " + + "supplementary characters"); } if (UTF16.findCodePointOffset(str, 1) != 1 || - UTF16.findOffsetFromCodePoint(str, 1) != 1 || - UTF16.findCodePointOffset(strbuff, 1) != 1 || - UTF16.findOffsetFromCodePoint(strbuff, 1) != 1 || - UTF16.findCodePointOffset(array, 0, limit, 1) != 1 || - UTF16.findOffsetFromCodePoint(array, 0, limit, 1) != 1) { - errln("FAIL Getting the second codepoint offset to a string with " + - "supplementary characters"); + UTF16.findOffsetFromCodePoint(str, 1) != 1 || + UTF16.findCodePointOffset(strbuff, 1) != 1 || + UTF16.findOffsetFromCodePoint(strbuff, 1) != 1 || + UTF16.findCodePointOffset(array, 0, limit, 1) != 1 || + UTF16.findOffsetFromCodePoint(array, 0, limit, 1) != 1) { + errln("FAIL Getting the second codepoint offset to a string with " + + "supplementary characters"); } if (UTF16.findCodePointOffset(str, 2) != 1 || - UTF16.findOffsetFromCodePoint(str, 2) != 3 || - UTF16.findCodePointOffset(strbuff, 2) != 1 || - UTF16.findOffsetFromCodePoint(strbuff, 2) != 3 || - UTF16.findCodePointOffset(array, 0, limit, 2) != 1 || - UTF16.findOffsetFromCodePoint(array, 0, limit, 2) != 3) { - errln("FAIL Getting the third codepoint offset to a string with " + - "supplementary characters"); + UTF16.findOffsetFromCodePoint(str, 2) != 3 || + UTF16.findCodePointOffset(strbuff, 2) != 1 || + UTF16.findOffsetFromCodePoint(strbuff, 2) != 3 || + UTF16.findCodePointOffset(array, 0, limit, 2) != 1 || + UTF16.findOffsetFromCodePoint(array, 0, limit, 2) != 3) { + errln("FAIL Getting the third codepoint offset to a string with " + + "supplementary characters"); } if (UTF16.findCodePointOffset(str, 3) != 2 || - UTF16.findOffsetFromCodePoint(str, 3) != 4 || - UTF16.findCodePointOffset(strbuff, 3) != 2 || - UTF16.findOffsetFromCodePoint(strbuff, 3) != 4 || - UTF16.findCodePointOffset(array, 0, limit, 3) != 2 || - UTF16.findOffsetFromCodePoint(array, 0, limit, 3) != 4) { - errln("FAIL Getting the last codepoint offset to a string with " + - "supplementary characters"); + UTF16.findOffsetFromCodePoint(str, 3) != 4 || + UTF16.findCodePointOffset(strbuff, 3) != 2 || + UTF16.findOffsetFromCodePoint(strbuff, 3) != 4 || + UTF16.findCodePointOffset(array, 0, limit, 3) != 2 || + UTF16.findOffsetFromCodePoint(array, 0, limit, 3) != 4) { + errln("FAIL Getting the last codepoint offset to a string with " + + "supplementary characters"); } if (UTF16.findCodePointOffset(str, 4) != 3 || - UTF16.findCodePointOffset(strbuff, 4) != 3 || - UTF16.findCodePointOffset(array, 0, limit, 4) != 3) { - errln("FAIL Getting the length offset to a string with " + - "supplementary characters"); + UTF16.findCodePointOffset(strbuff, 4) != 3 || + UTF16.findCodePointOffset(array, 0, limit, 4) != 3) { + errln("FAIL Getting the length offset to a string with " + + "supplementary characters"); } try { - UTF16.findCodePointOffset(str, 5); - errln("FAIL Getting the a non-existence codepoint to a string " + - "with supplementary characters"); + UTF16.findCodePointOffset(str, 5); + errln("FAIL Getting the a non-existence codepoint to a string " + + "with supplementary characters"); } catch (Exception e) { - // this is a success - logln("Passed out of bounds codepoint offset"); + // this is a success + logln("Passed out of bounds codepoint offset"); } try { - UTF16.findOffsetFromCodePoint(str, 4); - errln("FAIL Getting the a non-existence codepoint to a string " + - "with supplementary characters"); + UTF16.findOffsetFromCodePoint(str, 4); + errln("FAIL Getting the a non-existence codepoint to a string " + + "with supplementary characters"); } catch (Exception e) { - // this is a success - logln("Passed out of bounds codepoint offset"); + // this is a success + logln("Passed out of bounds codepoint offset"); } try { - UTF16.findCodePointOffset(strbuff, 5); - errln("FAIL Getting the a non-existence codepoint to a string " + - "with supplementary characters"); + UTF16.findCodePointOffset(strbuff, 5); + errln("FAIL Getting the a non-existence codepoint to a string " + + "with supplementary characters"); } catch (Exception e) { - // this is a success - logln("Passed out of bounds codepoint offset"); + // this is a success + logln("Passed out of bounds codepoint offset"); } try { - UTF16.findOffsetFromCodePoint(strbuff, 4); - errln("FAIL Getting the a non-existence codepoint to a string " + - "with supplementary characters"); + UTF16.findOffsetFromCodePoint(strbuff, 4); + errln("FAIL Getting the a non-existence codepoint to a string " + + "with supplementary characters"); } catch (Exception e) { - // this is a success - logln("Passed out of bounds codepoint offset"); + // this is a success + logln("Passed out of bounds codepoint offset"); } try { - UTF16.findCodePointOffset(array, 0, limit, 5); - errln("FAIL Getting the a non-existence codepoint to a string " + - "with supplementary characters"); + UTF16.findCodePointOffset(array, 0, limit, 5); + errln("FAIL Getting the a non-existence codepoint to a string " + + "with supplementary characters"); } catch (Exception e) { - // this is a success - logln("Passed out of bounds codepoint offset"); + // this is a success + logln("Passed out of bounds codepoint offset"); } try { - UTF16.findOffsetFromCodePoint(array, 0, limit, 4); - errln("FAIL Getting the a non-existence codepoint to a string " + - "with supplementary characters"); + UTF16.findOffsetFromCodePoint(array, 0, limit, 4); + errln("FAIL Getting the a non-existence codepoint to a string " + + "with supplementary characters"); } catch (Exception e) { - // this is a success - logln("Passed out of bounds codepoint offset"); + // this is a success + logln("Passed out of bounds codepoint offset"); } if (UTF16.findCodePointOffset(array, 1, 3, 0) != 0 || - UTF16.findOffsetFromCodePoint(array, 1, 3, 0) != 0 || - UTF16.findCodePointOffset(array, 1, 3, 1) != 0 || - UTF16.findCodePointOffset(array, 1, 3, 2) != 1 || - UTF16.findOffsetFromCodePoint(array, 1, 3, 1) != 2) { - errln("FAIL Getting valid codepoint offset in sub array"); + UTF16.findOffsetFromCodePoint(array, 1, 3, 0) != 0 || + UTF16.findCodePointOffset(array, 1, 3, 1) != 0 || + UTF16.findCodePointOffset(array, 1, 3, 2) != 1 || + UTF16.findOffsetFromCodePoint(array, 1, 3, 1) != 2) { + errln("FAIL Getting valid codepoint offset in sub array"); } - } + } - /** - * Testing UTF16 class methods getCharCount, *Surrogate - */ - public void TestGetCharCountSurrogate() - { + /** + * Testing UTF16 class methods getCharCount, *Surrogate + */ + public void TestGetCharCountSurrogate() + { if (UTF16.getCharCount(0x61) != 1 || - UTF16.getCharCount(0x10000) != 2) { - errln("FAIL getCharCount result failure"); + UTF16.getCharCount(0x10000) != 2) { + errln("FAIL getCharCount result failure"); } if (UTF16.getLeadSurrogate(0x61) != 0 || - UTF16.getTrailSurrogate(0x61) != 0x61 || - UTF16.isLeadSurrogate((char)0x61) || - UTF16.isTrailSurrogate((char)0x61) || - UTF16.getLeadSurrogate(0x10000) != 0xd800 || - UTF16.getTrailSurrogate(0x10000) != 0xdc00 || - UTF16.isLeadSurrogate((char)0xd800) != true || - UTF16.isTrailSurrogate((char)0xd800) || - UTF16.isLeadSurrogate((char)0xdc00) || - UTF16.isTrailSurrogate((char)0xdc00) != true) { - errln("FAIL *Surrogate result failure"); + UTF16.getTrailSurrogate(0x61) != 0x61 || + UTF16.isLeadSurrogate((char)0x61) || + UTF16.isTrailSurrogate((char)0x61) || + UTF16.getLeadSurrogate(0x10000) != 0xd800 || + UTF16.getTrailSurrogate(0x10000) != 0xdc00 || + UTF16.isLeadSurrogate((char)0xd800) != true || + UTF16.isTrailSurrogate((char)0xd800) || + UTF16.isLeadSurrogate((char)0xdc00) || + UTF16.isTrailSurrogate((char)0xdc00) != true) { + errln("FAIL *Surrogate result failure"); } if (UTF16.isSurrogate((char)0x61) || !UTF16.isSurrogate((char)0xd800) || !UTF16.isSurrogate((char)0xdc00)) { - errln("FAIL isSurrogate result failure"); + errln("FAIL isSurrogate result failure"); } - } + } - /** - * Testing UTF16 class method insert - */ - public void TestInsert() - { + /** + * Testing UTF16 class method insert + */ + public void TestInsert() + { StringBuffer strbuff = new StringBuffer("0123456789"); char array[] = new char[128]; Utility.getChars(strbuff, 0, strbuff.length(), array, 0); @@ -496,7 +502,7 @@ public final class UTF16Test extends TestFmwk UTF16.insert(strbuff, 5, 'e'); UTF16.insert(strbuff, 5, 't'); if (!(strbuff.toString().equals("01234test56789"))) { - errln("FAIL inserting \"test\""); + errln("FAIL inserting \"test\""); } length = UTF16.insert(array, length, 5, 't'); length = UTF16.insert(array, length, 5, 's'); @@ -504,205 +510,205 @@ public final class UTF16Test extends TestFmwk length = UTF16.insert(array, length, 5, 't'); String str = new String(array, 0, length); if (!(str.equals("01234test56789"))) { - errln("FAIL inserting \"test\""); + errln("FAIL inserting \"test\""); } UTF16.insert(strbuff, 0, 0x10000); UTF16.insert(strbuff, 11, 0x10000); UTF16.insert(strbuff, strbuff.length(), 0x10000); if (!(strbuff.toString().equals( - "\ud800\udc0001234test\ud800\udc0056789\ud800\udc00"))) { - errln("FAIL inserting supplementary characters"); + "\ud800\udc0001234test\ud800\udc0056789\ud800\udc00"))) { + errln("FAIL inserting supplementary characters"); } length = UTF16.insert(array, length, 0, 0x10000); length = UTF16.insert(array, length, 11, 0x10000); length = UTF16.insert(array, length, length, 0x10000); str = new String(array, 0, length); if (!(str.equals( - "\ud800\udc0001234test\ud800\udc0056789\ud800\udc00"))) { - errln("FAIL inserting supplementary characters"); + "\ud800\udc0001234test\ud800\udc0056789\ud800\udc00"))) { + errln("FAIL inserting supplementary characters"); } try { - UTF16.insert(strbuff, -1, 0); - errln("FAIL invalid insertion offset"); + UTF16.insert(strbuff, -1, 0); + errln("FAIL invalid insertion offset"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.insert(strbuff, 64, 0); - errln("FAIL invalid insertion offset"); + UTF16.insert(strbuff, 64, 0); + errln("FAIL invalid insertion offset"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.insert(array, length, -1, 0); - errln("FAIL invalid insertion offset"); + UTF16.insert(array, length, -1, 0); + errln("FAIL invalid insertion offset"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.insert(array, length, 64, 0); - errln("FAIL invalid insertion offset"); + UTF16.insert(array, length, 64, 0); + errln("FAIL invalid insertion offset"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - // exceeded array size - UTF16.insert(array, array.length, 64, 0); - errln("FAIL invalid insertion offset"); + // exceeded array size + UTF16.insert(array, array.length, 64, 0); + errln("FAIL invalid insertion offset"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } - } + } - /* - * Testing moveCodePointOffset APIs - */ - public void TestMoveCodePointOffset() - { + /* + * Testing moveCodePointOffset APIs + */ + public void TestMoveCodePointOffset() + { //01234567890 1 2 3 45678901234 String str = new String("0123456789\ud800\udc00\ud801\udc010123456789"); int move1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 12, 14, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}; int move2[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 14, 15, 15, 16, 17, - 18, 19, 20, 21, 22, 23, 24}; + 18, 19, 20, 21, 22, 23, 24}; int move3[] = {3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 15, 16, 16, 17, 18, - 19, 20, 21, 22, 23, 24}; + 19, 20, 21, 22, 23, 24}; int size = str.length(); for (int i = 0; i < size; i ++) { - if (UTF16.moveCodePointOffset(str, i, 1) != move1[i]) { + if (UTF16.moveCodePointOffset(str, i, 1) != move1[i]) { errln("FAIL: Moving offset " + i + - " by 1 codepoint expected result " + move1[i]); - } - try { + " by 1 codepoint expected result " + move1[i]); + } + try { if (UTF16.moveCodePointOffset(str, i, 2) != move2[i]) { - errln("FAIL: Moving offset " + i + + errln("FAIL: Moving offset " + i + " by 2 codepoint expected result " + move2[i]); } - } catch (IndexOutOfBoundsException e) { + } catch (IndexOutOfBoundsException e) { if (i <= 22) { - throw e; + throw e; } - } - try + } + try { - if (UTF16.moveCodePointOffset(str, i, 3) != move3[i]) { + if (UTF16.moveCodePointOffset(str, i, 3) != move3[i]) { errln("FAIL: Moving offset " + i + - " by 3 codepoint expected result " + move3[i]); - } + " by 3 codepoint expected result " + move3[i]); + } } catch (IndexOutOfBoundsException e) { - if (i <= 21) { + if (i <= 21) { throw e; - } + } } } StringBuffer strbuff = new StringBuffer(str); for (int i = 0; i < size; i ++) { - if (UTF16.moveCodePointOffset(strbuff, i, 1) != move1[i]) { + if (UTF16.moveCodePointOffset(strbuff, i, 1) != move1[i]) { errln("FAIL: Moving offset " + i + - " by 1 codepoint expected result " + move1[i]); - } - try { + " by 1 codepoint expected result " + move1[i]); + } + try { if (UTF16.moveCodePointOffset(strbuff, i, 2) != move2[i]) { - errln("FAIL: Moving offset " + i + + errln("FAIL: Moving offset " + i + " by 2 codepoint expected result " + move2[i]); } - } catch (IndexOutOfBoundsException e) { + } catch (IndexOutOfBoundsException e) { if (i <= 22) { - throw e; + throw e; } - } - try + } + try { - if (UTF16.moveCodePointOffset(strbuff, i, 3) != move3[i]) { + if (UTF16.moveCodePointOffset(strbuff, i, 3) != move3[i]) { errln("FAIL: Moving offset " + i + - " by 3 codepoint expected result " + move3[i]); - } + " by 3 codepoint expected result " + move3[i]); + } } catch (IndexOutOfBoundsException e) { - if (i <= 21) { + if (i <= 21) { throw e; - } + } } } char strarray[] = str.toCharArray(); for (int i = 0; i < size; i ++) { - if (UTF16.moveCodePointOffset(strarray, 0, size, i, 1) != move1[i]) + if (UTF16.moveCodePointOffset(strarray, 0, size, i, 1) != move1[i]) { - errln("FAIL: Moving offset " + i + - " by 1 codepoint expected result " + move1[i]); + errln("FAIL: Moving offset " + i + + " by 1 codepoint expected result " + move1[i]); } - try { + try { if (UTF16.moveCodePointOffset(strarray, 0, size, i, 2) != - move2[i]) { - errln("FAIL: Moving offset " + i + + move2[i]) { + errln("FAIL: Moving offset " + i + " by 2 codepoint expected result " + move2[i]); } - } catch (IndexOutOfBoundsException e) { + } catch (IndexOutOfBoundsException e) { if (i <= 22) { - throw e; + throw e; } - } - try + } + try { - if (UTF16.moveCodePointOffset(strarray, 0, size, i, 3) != - move3[i]) { + if (UTF16.moveCodePointOffset(strarray, 0, size, i, 3) != + move3[i]) { errln("FAIL: Moving offset " + i + - " by 3 codepoint expected result " + move3[i]); - } + " by 3 codepoint expected result " + move3[i]); + } } catch (IndexOutOfBoundsException e) { - if (i <= 21) { + if (i <= 21) { throw e; - } + } } } if (UTF16.moveCodePointOffset(strarray, 9, 13, 0, 2) != 3) { - errln("FAIL: Moving offset 0 by 2 codepoint in subarray [9, 13] " + - "expected result 3"); + errln("FAIL: Moving offset 0 by 2 codepoint in subarray [9, 13] " + + "expected result 3"); } if (UTF16.moveCodePointOffset(strarray, 9, 13, 1, 2) != 4) { - errln("FAIL: Moving offset 1 by 2 codepoint in subarray [9, 13] " + - "expected result 4"); + errln("FAIL: Moving offset 1 by 2 codepoint in subarray [9, 13] " + + "expected result 4"); } if (UTF16.moveCodePointOffset(strarray, 11, 14, 0, 2) != 3) { - errln("FAIL: Moving offset 0 by 2 codepoint in subarray [11, 14] " - + "expected result 3"); + errln("FAIL: Moving offset 0 by 2 codepoint in subarray [11, 14] " + + "expected result 3"); } - } + } - /** - * Testing UTF16 class methods setCharAt - */ - public void TestSetCharAt() - { + /** + * Testing UTF16 class methods setCharAt + */ + public void TestSetCharAt() + { StringBuffer strbuff = new StringBuffer("012345"); char array[] = new char[128]; Utility.getChars(strbuff, 0, strbuff.length(), array, 0); int length = 6; for (int i = 0; i < length; i ++) { - UTF16.setCharAt(strbuff, i, '0'); - UTF16.setCharAt(array, length, i, '0'); + UTF16.setCharAt(strbuff, i, '0'); + UTF16.setCharAt(array, length, i, '0'); } String str = new String(array, 0, length); if (!(strbuff.toString().equals("000000")) || - !(str.equals("000000"))) { - errln("FAIL: setChar to '0' failed"); + !(str.equals("000000"))) { + errln("FAIL: setChar to '0' failed"); } UTF16.setCharAt(strbuff, 0, 0x10000); UTF16.setCharAt(strbuff, 4, 0x10000); UTF16.setCharAt(strbuff, 7, 0x10000); if (!(strbuff.toString().equals( - "\ud800\udc0000\ud800\udc000\ud800\udc00"))) { - errln("FAIL: setChar to 0x10000 failed"); + "\ud800\udc0000\ud800\udc000\ud800\udc00"))) { + errln("FAIL: setChar to 0x10000 failed"); } length = UTF16.setCharAt(array, length, 0, 0x10000); length = UTF16.setCharAt(array, length, 4, 0x10000); length = UTF16.setCharAt(array, length, 7, 0x10000); str = new String(array, 0, length); if (!(str.equals("\ud800\udc0000\ud800\udc000\ud800\udc00"))) { - errln("FAIL: setChar to 0x10000 failed"); + errln("FAIL: setChar to 0x10000 failed"); } UTF16.setCharAt(strbuff, 0, '0'); UTF16.setCharAt(strbuff, 1, '1'); @@ -711,8 +717,8 @@ public final class UTF16Test extends TestFmwk UTF16.setCharAt(strbuff, 4, '4'); UTF16.setCharAt(strbuff, 5, '5'); if (!strbuff.toString().equals("012345")) { - errln("Fail converting supplementaries in StringBuffer to BMP " + - "characters"); + errln("Fail converting supplementaries in StringBuffer to BMP " + + "characters"); } length = UTF16.setCharAt(array, length, 0, '0'); length = UTF16.setCharAt(array, length, 1, '1'); @@ -722,43 +728,43 @@ public final class UTF16Test extends TestFmwk length = UTF16.setCharAt(array, length, 5, '5'); str = new String(array, 0, length); if (!str.equals("012345")) { - errln("Fail converting supplementaries in array to BMP " + - "characters"); + errln("Fail converting supplementaries in array to BMP " + + "characters"); } try { - UTF16.setCharAt(strbuff, -1, 0); - errln("FAIL: setting character at invalid offset"); + UTF16.setCharAt(strbuff, -1, 0); + errln("FAIL: setting character at invalid offset"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.setCharAt(array, length, -1, 0); - errln("FAIL: setting character at invalid offset"); + UTF16.setCharAt(array, length, -1, 0); + errln("FAIL: setting character at invalid offset"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.setCharAt(strbuff, length, 0); - errln("FAIL: setting character at invalid offset"); + UTF16.setCharAt(strbuff, length, 0); + errln("FAIL: setting character at invalid offset"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.setCharAt(array, length, length, 0); - errln("FAIL: setting character at invalid offset"); + UTF16.setCharAt(array, length, length, 0); + errln("FAIL: setting character at invalid offset"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } - } + } - /** - * Testing UTF16 valueof APIs - */ - public void TestValueOf() - { + /** + * Testing UTF16 valueof APIs + */ + public void TestValueOf() + { if (!UTF16.valueOf(0x61).equals("a") || - !UTF16.valueOf(0x10000).equals("\ud800\udc00")) { - errln("FAIL: valueof(char32)"); + !UTF16.valueOf(0x10000).equals("\ud800\udc00")) { + errln("FAIL: valueof(char32)"); } String str = new String("01234\ud800\udc0056789"); StringBuffer strbuff = new StringBuffer(str); @@ -766,71 +772,71 @@ public final class UTF16Test extends TestFmwk int length = str.length(); String expected[] = {"0", "1", "2", "3", "4", "\ud800\udc00", - "\ud800\udc00", "5", "6", "7", "8", "9"}; + "\ud800\udc00", "5", "6", "7", "8", "9"}; for (int i = 0; i < length; i ++) { - if (!UTF16.valueOf(str, i).equals(expected[i]) || + if (!UTF16.valueOf(str, i).equals(expected[i]) || !UTF16.valueOf(strbuff, i).equals(expected[i]) || !UTF16.valueOf(array, 0, length, i).equals(expected[i])) { errln("FAIL: valueOf() expected " + expected[i]); - } + } } try { - UTF16.valueOf(str, -1); - errln("FAIL: out of bounds error expected"); + UTF16.valueOf(str, -1); + errln("FAIL: out of bounds error expected"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.valueOf(strbuff, -1); - errln("FAIL: out of bounds error expected"); + UTF16.valueOf(strbuff, -1); + errln("FAIL: out of bounds error expected"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.valueOf(array, 0, length, -1); - errln("FAIL: out of bounds error expected"); + UTF16.valueOf(array, 0, length, -1); + errln("FAIL: out of bounds error expected"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.valueOf(str, length); - errln("FAIL: out of bounds error expected"); + UTF16.valueOf(str, length); + errln("FAIL: out of bounds error expected"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.valueOf(strbuff, length); - errln("FAIL: out of bounds error expected"); + UTF16.valueOf(strbuff, length); + errln("FAIL: out of bounds error expected"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.valueOf(array, 0, length, length); - errln("FAIL: out of bounds error expected"); + UTF16.valueOf(array, 0, length, length); + errln("FAIL: out of bounds error expected"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } if (!UTF16.valueOf(array, 6, length, 0).equals("\udc00") || - !UTF16.valueOf(array, 0, 6, 5).equals("\ud800")) { - errln("FAIL: error getting partial supplementary character"); + !UTF16.valueOf(array, 0, 6, 5).equals("\ud800")) { + errln("FAIL: error getting partial supplementary character"); } try { - UTF16.valueOf(array, 3, 5, -1); - errln("FAIL: out of bounds error expected"); + UTF16.valueOf(array, 3, 5, -1); + errln("FAIL: out of bounds error expected"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } try { - UTF16.valueOf(array, 3, 5, 3); - errln("FAIL: out of bounds error expected"); + UTF16.valueOf(array, 3, 5, 3); + errln("FAIL: out of bounds error expected"); } catch (Exception e) { - System.out.print(""); + System.out.print(""); } - } + } public void TestIndexOf() { - //012345678901234567890123456789012345 + //012345678901234567890123456789012345 String test1 = "test test ttest tetest testesteststt"; String test2 = "test"; int testChar1 = 0x74; @@ -883,13 +889,13 @@ public final class UTF16Test extends TestFmwk } int occurrences = 0; for (int startPos = 0; startPos != -1 && startPos < test1.length();) - { - startPos = UTF16.indexOf(test1, test2, startPos); - if (startPos >= 0) { - ++ occurrences; - startPos += 4; - } - } + { + startPos = UTF16.indexOf(test1, test2, startPos); + if (startPos >= 0) { + ++ occurrences; + startPos += 4; + } + } if (occurrences != 6) { errln("indexOf failed: expected to find 6 occurrences, found " + occurrences); @@ -897,13 +903,13 @@ public final class UTF16Test extends TestFmwk occurrences = 0; for (int startPos = 10; startPos != -1 && startPos < test1.length();) - { - startPos = UTF16.indexOf(test1, test2, startPos); - if (startPos >= 0) { - ++ occurrences; - startPos += 4; - } - } + { + startPos = UTF16.indexOf(test1, test2, startPos); + if (startPos >= 0) { + ++ occurrences; + startPos += 4; + } + } if (occurrences != 4) { errln("indexOf with starting offset failed: expected to find 4 occurrences, found " + occurrences); @@ -911,7 +917,7 @@ public final class UTF16Test extends TestFmwk occurrences = 0; for (int startPos = 0; - startPos != -1 && startPos < test3.length();) { + startPos != -1 && startPos < test3.length();) { startPos = UTF16.indexOf(test3, test4, startPos); if (startPos != -1) { ++ occurrences; @@ -920,7 +926,7 @@ public final class UTF16Test extends TestFmwk } if (occurrences != 4) { errln("indexOf failed: expected to find 4 occurrences, found " - + occurrences); + + occurrences); } occurrences = 0; @@ -939,7 +945,7 @@ public final class UTF16Test extends TestFmwk occurrences = 0; for (int startPos = 0; - startPos != -1 && startPos < test1.length();) { + startPos != -1 && startPos < test1.length();) { startPos = UTF16.indexOf(test1, testChar1, startPos); if (startPos != -1) { ++ occurrences; @@ -953,7 +959,7 @@ public final class UTF16Test extends TestFmwk occurrences = 0; for (int startPos = 10; - startPos != -1 && startPos < test1.length();) { + startPos != -1 && startPos < test1.length();) { startPos = UTF16.indexOf(test1, testChar1, startPos); if (startPos != -1) { ++ occurrences; @@ -962,12 +968,12 @@ public final class UTF16Test extends TestFmwk } if (occurrences != 12) { errln("indexOf with character & start offset failed: expected to find 12 occurrences, found " - + occurrences); + + occurrences); } occurrences = 0; for (int startPos = 0; - startPos != -1 && startPos < test3.length();) { + startPos != -1 && startPos < test3.length();) { startPos = UTF16.indexOf(test3, testChar2, startPos); if (startPos != -1) { ++ occurrences; @@ -989,7 +995,7 @@ public final class UTF16Test extends TestFmwk } if (occurrences != 3) { errln("indexOf with character & start & end offsets failed: expected to find 2 occurrences, found " - + occurrences); + + occurrences); } occurrences = 0; for (int startPos = 32; startPos != -1;) { @@ -1013,7 +1019,7 @@ public final class UTF16Test extends TestFmwk } if (occurrences != 7) { errln("lastIndexOf with character & start & end offsets failed: expected to find 11 occurrences, found " - + occurrences); + + occurrences); } //testing UChar32 @@ -1026,63 +1032,63 @@ public final class UTF16Test extends TestFmwk } if (occurrences != 3) { errln("lastIndexOf with character & start & end offsets failed: expected to find 3 occurrences, found " - + occurrences); + + occurrences); } // testing supplementary for (int i = 0; i < INDEXOF_SUPPLEMENTARY_CHAR_.length; i ++) { - int ch = INDEXOF_SUPPLEMENTARY_CHAR_[i]; - for (int j = 0; j < INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i].length; - j ++) { - int index = 0; - int expected = INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i][j]; - if (j > 0) { - index = INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i][j - 1] + 1; - } - if (UTF16.indexOf(INDEXOF_SUPPLEMENTARY_STRING_, ch, index) != - expected || - UTF16.indexOf(INDEXOF_SUPPLEMENTARY_STRING_, - UCharacter.toString(ch), index) != - expected) { - errln("Failed finding index for supplementary 0x" + - Integer.toHexString(ch)); - } - index = INDEXOF_SUPPLEMENTARY_STRING_.length(); - if (j < INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i].length - 1) { - index = INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i][j + 1] - 1; - } - if (UTF16.lastIndexOf(INDEXOF_SUPPLEMENTARY_STRING_, ch, - index) != expected || - UTF16.lastIndexOf(INDEXOF_SUPPLEMENTARY_STRING_, - UCharacter.toString(ch), index) - != expected) - { - errln("Failed finding last index for supplementary 0x" + - Integer.toHexString(ch)); - } - } + int ch = INDEXOF_SUPPLEMENTARY_CHAR_[i]; + for (int j = 0; j < INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i].length; + j ++) { + int index = 0; + int expected = INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i][j]; + if (j > 0) { + index = INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i][j - 1] + 1; + } + if (UTF16.indexOf(INDEXOF_SUPPLEMENTARY_STRING_, ch, index) != + expected || + UTF16.indexOf(INDEXOF_SUPPLEMENTARY_STRING_, + UCharacter.toString(ch), index) != + expected) { + errln("Failed finding index for supplementary 0x" + + Integer.toHexString(ch)); + } + index = INDEXOF_SUPPLEMENTARY_STRING_.length(); + if (j < INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i].length - 1) { + index = INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[i][j + 1] - 1; + } + if (UTF16.lastIndexOf(INDEXOF_SUPPLEMENTARY_STRING_, ch, + index) != expected || + UTF16.lastIndexOf(INDEXOF_SUPPLEMENTARY_STRING_, + UCharacter.toString(ch), index) + != expected) + { + errln("Failed finding last index for supplementary 0x" + + Integer.toHexString(ch)); + } + } } for (int i = 0; i < INDEXOF_SUPPLEMENTARY_STR_INDEX_.length; i ++) { - int index = 0; - int expected = INDEXOF_SUPPLEMENTARY_STR_INDEX_[i]; - if (i > 0) { - index = INDEXOF_SUPPLEMENTARY_STR_INDEX_[i - 1] + 1; - } - if (UTF16.indexOf(INDEXOF_SUPPLEMENTARY_STRING_, + int index = 0; + int expected = INDEXOF_SUPPLEMENTARY_STR_INDEX_[i]; + if (i > 0) { + index = INDEXOF_SUPPLEMENTARY_STR_INDEX_[i - 1] + 1; + } + if (UTF16.indexOf(INDEXOF_SUPPLEMENTARY_STRING_, + INDEXOF_SUPPLEMENTARY_STR_, index) != expected) { + errln("Failed finding index for supplementary string " + + hex(INDEXOF_SUPPLEMENTARY_STRING_)); + } + index = INDEXOF_SUPPLEMENTARY_STRING_.length(); + if (i < INDEXOF_SUPPLEMENTARY_STR_INDEX_.length - 1) { + index = INDEXOF_SUPPLEMENTARY_STR_INDEX_[i + 1] - 1; + } + if (UTF16.lastIndexOf(INDEXOF_SUPPLEMENTARY_STRING_, INDEXOF_SUPPLEMENTARY_STR_, index) != expected) { - errln("Failed finding index for supplementary string " + - hex(INDEXOF_SUPPLEMENTARY_STRING_)); - } - index = INDEXOF_SUPPLEMENTARY_STRING_.length(); - if (i < INDEXOF_SUPPLEMENTARY_STR_INDEX_.length - 1) { - index = INDEXOF_SUPPLEMENTARY_STR_INDEX_[i + 1] - 1; - } - if (UTF16.lastIndexOf(INDEXOF_SUPPLEMENTARY_STRING_, - INDEXOF_SUPPLEMENTARY_STR_, index) != expected) { - errln("Failed finding last index for supplementary string " + - hex(INDEXOF_SUPPLEMENTARY_STRING_)); - } + errln("Failed finding last index for supplementary string " + + hex(INDEXOF_SUPPLEMENTARY_STRING_)); + } } } @@ -1153,12 +1159,12 @@ public final class UTF16Test extends TestFmwk public void TestReverse() { StringBuffer test = new StringBuffer( - "backwards words say to used I"); + "backwards words say to used I"); StringBuffer result = UTF16.reverse(test); if (!result.toString().equals("I desu ot yas sdrow sdrawkcab")) { errln("reverse() failed: Expected \"I desu ot yas sdrow sdrawkcab\",\n got \"" - + result + "\""); + + result + "\""); } StringBuffer testbuffer = new StringBuffer(); UTF16.append(testbuffer, 0x2f999); @@ -1201,25 +1207,25 @@ public final class UTF16Test extends TestFmwk compare.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_DEFAULT); if (compare.getIgnoreCase() != true || compare.getIgnoreCaseOption() - != UTF16.StringComparator.FOLD_CASE_DEFAULT) { + != UTF16.StringComparator.FOLD_CASE_DEFAULT) { errln("Error setting ignore case and options"); } compare.setIgnoreCase(false, UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I); if (compare.getIgnoreCase() != false || compare.getIgnoreCaseOption() - != UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I) { + != UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I) { errln("Error setting ignore case and options"); } compare.setIgnoreCase(true, UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I); if (compare.getIgnoreCase() != true || compare.getIgnoreCaseOption() - != UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I) { + != UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I) { errln("Error setting ignore case and options"); } compare.setIgnoreCase(false, UTF16.StringComparator.FOLD_CASE_DEFAULT); if (compare.getIgnoreCase() != false || compare.getIgnoreCaseOption() - != UTF16.StringComparator.FOLD_CASE_DEFAULT) { + != UTF16.StringComparator.FOLD_CASE_DEFAULT) { errln("Error setting ignore case and options"); } } @@ -1233,7 +1239,7 @@ public final class UTF16Test extends TestFmwk "\ud84d\udc56"}; UTF16.StringComparator cpcompare = new UTF16.StringComparator(true, false, - UTF16.StringComparator.FOLD_CASE_DEFAULT); + UTF16.StringComparator.FOLD_CASE_DEFAULT); UTF16.StringComparator cucompare = new UTF16.StringComparator(); for (int i = 0; i < str.length - 1; ++ i) { @@ -1270,7 +1276,7 @@ public final class UTF16Test extends TestFmwk // test u_strcasecmp() - exclude special i compare.setIgnoreCase(true, - UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I); + UTF16.StringComparator.FOLD_CASE_EXCLUDE_SPECIAL_I); result = compare.compare(mixed, otherExcludeSpecialI); if (result != 0) { errln("error: exclude_i compare(mixed, other) = " + result @@ -1293,7 +1299,7 @@ public final class UTF16Test extends TestFmwk different.substring(0, 4)); if (result != 0) { errln("error: default compare(mixed substring, different substring) = " - + result + " instead of 0"); + + result + " instead of 0"); } // test substrings - stop in the middle of the sharp s (U+00df) compare.setIgnoreCase(true, @@ -1302,14 +1308,14 @@ public final class UTF16Test extends TestFmwk different.substring(0, 5)); if (result <= 0) { errln("error: default compare(mixed substring, different substring) = " - + result + " instead of positive"); + + result + " instead of positive"); } } public void TestHasMoreCodePointsThan() { String str = "\u0061\u0062\ud800\udc00\ud801\udc01\u0063\ud802\u0064" - + "\udc03\u0065\u0066\ud804\udc04\ud805\udc05\u0067"; + + "\udc03\u0065\u0066\ud804\udc04\ud805\udc05\u0067"; int length = str.length(); while (length >= 0) { for (int i = 0; i <= length; ++ i) { @@ -1333,7 +1339,7 @@ public final class UTF16Test extends TestFmwk number); if (flag != (UTF16.countCodePoint((String)null) > number)) { errln("hasMoreCodePointsThan(null, " + number + ") = " - + flag + " is wrong"); + + flag + " is wrong"); } } } @@ -1359,13 +1365,13 @@ public final class UTF16Test extends TestFmwk for (int i = 0; i <= length; ++ i) { for (int number = -2; number <= 2; ++ number) { boolean flag = UTF16.hasMoreCodePointsThan( - (StringBuffer)null, number); + (StringBuffer)null, number); if (flag != (UTF16.countCodePoint((StringBuffer)null) > number)) - { - errln("hasMoreCodePointsThan(null, " + number + ") = " - + flag + " is wrong"); - } + { + errln("hasMoreCodePointsThan(null, " + number + ") = " + + flag + " is wrong"); + } } } } @@ -1377,7 +1383,7 @@ public final class UTF16Test extends TestFmwk for (int number = -1; number <= ((limit - start) + 2); ++ number) { boolean flag = UTF16.hasMoreCodePointsThan(strarray, - start, limit, number); + start, limit, number); if (flag != (UTF16.countCodePoint(strarray, start, limit) > number)) { errln("hasMoreCodePointsThan(" @@ -1396,13 +1402,13 @@ public final class UTF16Test extends TestFmwk for (int i = 0; i <= length; ++ i) { for (int number = -2; number <= 2; ++ number) { boolean flag = UTF16.hasMoreCodePointsThan( - (StringBuffer)null, number); + (StringBuffer)null, number); if (flag != (UTF16.countCodePoint((StringBuffer)null) > number)) - { - errln("hasMoreCodePointsThan(null, " + number + ") = " - + flag + " is wrong"); - } + { + errln("hasMoreCodePointsThan(null, " + number + ") = " + + flag + " is wrong"); + } } } } @@ -1426,20 +1432,6 @@ public final class UTF16Test extends TestFmwk } } - public static void main(String[] arg) - { - try - { - UTF16Test test = new UTF16Test(); - test.run(arg); - // test.TestCaseCompare(); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - public void TestNewString() { final int[] codePoints = { UCharacter.toCodePoint(UCharacter.MIN_HIGH_SURROGATE, UCharacter.MAX_LOW_SURROGATE), @@ -1496,6 +1488,20 @@ public final class UTF16Test extends TestFmwk } } + public static void main(String[] arg) + { + try + { + UTF16Test test = new UTF16Test(); + test.run(arg); + // test.TestCaseCompare(); + } + catch (Exception e) + { + e.printStackTrace(); + } + } + // private data members ---------------------------------------------- @@ -1503,18 +1509,18 @@ public final class UTF16Test extends TestFmwk "\ud841\udc02\u0071\udc02\ud841\u0071\ud841\udc02\u0071\u0072" + "\ud841\udc02\u0071\ud841\udc02\u0071\udc02\ud841\u0073"; private final static int INDEXOF_SUPPLEMENTARY_CHAR_[] = - {0x71, 0xd841, 0xdc02, - UCharacter.getCodePoint((char)0xd841, - (char)0xdc02)}; + {0x71, 0xd841, 0xdc02, + UCharacter.getCodePoint((char)0xd841, + (char)0xdc02)}; private final static int INDEXOF_SUPPLEMENTARY_CHAR_INDEX_[][] = - {{2, 5, 8, 12, 15}, - {4, 17}, - {3, 16}, - {0, 6, 10, 13} - }; - private final static String INDEXOF_SUPPLEMENTARY_STR_ = "\udc02\ud841"; + {{2, 5, 8, 12, 15}, + {4, 17}, + {3, 16}, + {0, 6, 10, 13} + }; + private final static String INDEXOF_SUPPLEMENTARY_STR_ = "\udc02\ud841"; private final static int INDEXOF_SUPPLEMENTARY_STR_INDEX_[] = - {3, 16}; + {3, 16}; // private methods --------------------------------------------------- } diff --git a/icu4j/src/com/ibm/icu/text/UTF16.java b/icu4j/src/com/ibm/icu/text/UTF16.java index d53c30fbac7..a236362fbc0 100755 --- a/icu4j/src/com/ibm/icu/text/UTF16.java +++ b/icu4j/src/com/ibm/icu/text/UTF16.java @@ -5,8 +5,8 @@ ******************************************************************************* * * $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/text/UTF16.java,v $ -* $Date: 2004/03/11 16:51:51 $ -* $Revision: 1.36 $ +* $Date: 2004/03/11 21:42:41 $ +* $Revision: 1.37 $ * ******************************************************************************* */ @@ -15,6 +15,7 @@ package com.ibm.icu.text; import com.ibm.icu.impl.UCharacterProperty; import com.ibm.icu.impl.NormalizerImpl; + /** *

Standalone utility class providing UTF16 character conversions and * indexing conversions.

@@ -108,8 +109,8 @@ public final class UTF16 * @stable ICU 2.1 */ public static final int SINGLE_CHAR_BOUNDARY = 1, - LEAD_SURROGATE_BOUNDARY = 2, - TRAIL_SURROGATE_BOUNDARY = 5; + LEAD_SURROGATE_BOUNDARY = 2, + TRAIL_SURROGATE_BOUNDARY = 5; /** * The lowest Unicode code point value. * @stable ICU 2.1 @@ -131,7 +132,7 @@ public final class UTF16 * @stable ICU 2.1 */ public static final int LEAD_SURROGATE_MIN_VALUE = 0xD800; - /** + /** * Trail surrogate minimum value * @stable ICU 2.1 */ @@ -141,7 +142,7 @@ public final class UTF16 * @stable ICU 2.1 */ public static final int LEAD_SURROGATE_MAX_VALUE = 0xDBFF; - /** + /** * Trail surrogate maximum value * @stable ICU 2.1 */ @@ -204,31 +205,31 @@ public final class UTF16 // For simplicity in usage, and because the frequency of pairs is // low, look both directions. - if (single <= LEAD_SURROGATE_MAX_VALUE) { - ++ offset16; - if (source.length() != offset16) { + if (single <= LEAD_SURROGATE_MAX_VALUE) { + ++ offset16; + if (source.length() != offset16) { char trail = source.charAt(offset16); - if (trail >= TRAIL_SURROGATE_MIN_VALUE && - trail <= TRAIL_SURROGATE_MAX_VALUE) { - return UCharacterProperty.getRawSupplementary(single, - trail); - } + if (trail >= TRAIL_SURROGATE_MIN_VALUE && + trail <= TRAIL_SURROGATE_MAX_VALUE) { + return UCharacterProperty.getRawSupplementary(single, + trail); } - } - else + } + } + else { -- offset16; if (offset16 >= 0) { - // single is a trail surrogate so - char lead = source.charAt(offset16); - if (lead >= LEAD_SURROGATE_MIN_VALUE && - lead <= LEAD_SURROGATE_MAX_VALUE) { - return UCharacterProperty.getRawSupplementary(lead, - single); - } + // single is a trail surrogate so + char lead = source.charAt(offset16); + if (lead >= LEAD_SURROGATE_MIN_VALUE && + lead <= LEAD_SURROGATE_MAX_VALUE) { + return UCharacterProperty.getRawSupplementary(lead, + single); + } } } - return single; // return unmatched surrogate + return single; // return unmatched surrogate } /** @@ -265,29 +266,29 @@ public final class UTF16 // For simplicity in usage, and because the frequency of pairs is // low, look both directions. - if (single <= LEAD_SURROGATE_MAX_VALUE) + if (single <= LEAD_SURROGATE_MAX_VALUE) { ++ offset16; if (source.length() != offset16) - { - char trail = source.charAt(offset16); - if (isTrailSurrogate(trail)) - return UCharacterProperty.getRawSupplementary(single, trail); - } + { + char trail = source.charAt(offset16); + if (isTrailSurrogate(trail)) + return UCharacterProperty.getRawSupplementary(single, trail); + } } - else + else { -- offset16; if (offset16 >= 0) - { - // single is a trail surrogate so - char lead = source.charAt(offset16); - if (isLeadSurrogate(lead)) { - return UCharacterProperty.getRawSupplementary(lead, single); - } - } + { + // single is a trail surrogate so + char lead = source.charAt(offset16); + if (isLeadSurrogate(lead)) { + return UCharacterProperty.getRawSupplementary(lead, single); + } + } } - return single; // return unmatched surrogate + return single; // return unmatched surrogate } /** @@ -327,24 +328,24 @@ public final class UTF16 // Convert the UTF-16 surrogate pair if necessary. // For simplicity in usage, and because the frequency of pairs is // low, look both directions. - if (single <= LEAD_SURROGATE_MAX_VALUE) { - offset16 ++; - if (offset16 >= limit) { - return single; - } - char trail = source[offset16]; - if (isTrailSurrogate(trail)) { - return UCharacterProperty.getRawSupplementary(single, trail); - } + if (single <= LEAD_SURROGATE_MAX_VALUE) { + offset16 ++; + if (offset16 >= limit) { + return single; + } + char trail = source[offset16]; + if (isTrailSurrogate(trail)) { + return UCharacterProperty.getRawSupplementary(single, trail); + } } else { // isTrailSurrogate(single), so if (offset16 == start) { return single; } offset16 --; - char lead = source[offset16]; - if (isLeadSurrogate(lead)) - return UCharacterProperty.getRawSupplementary(lead, single); + char lead = source[offset16]; + if (isLeadSurrogate(lead)) + return UCharacterProperty.getRawSupplementary(lead, single); } return single; // return unmatched surrogate } @@ -383,29 +384,29 @@ public final class UTF16 // For simplicity in usage, and because the frequency of pairs is // low, look both directions. - if (single <= LEAD_SURROGATE_MAX_VALUE) + if (single <= LEAD_SURROGATE_MAX_VALUE) { ++ offset16; if (source.length() != offset16) - { - char trail = source.charAt(offset16); - if (isTrailSurrogate(trail)) - return UCharacterProperty.getRawSupplementary(single, trail); - } + { + char trail = source.charAt(offset16); + if (isTrailSurrogate(trail)) + return UCharacterProperty.getRawSupplementary(single, trail); + } } - else + else { -- offset16; if (offset16 >= 0) - { - // single is a trail surrogate so - char lead = source.charAt(offset16); - if (isLeadSurrogate(lead)) { - return UCharacterProperty.getRawSupplementary(lead, single); - } - } + { + // single is a trail surrogate so + char lead = source.charAt(offset16); + if (isLeadSurrogate(lead)) { + return UCharacterProperty.getRawSupplementary(lead, single); + } + } } - return single; // return unmatched surrogate + return single; // return unmatched surrogate } /** @@ -420,7 +421,7 @@ public final class UTF16 public static int getCharCount(int char32) { if (char32 < SUPPLEMENTARY_MIN_VALUE) { - return 1; + return 1; } return 2; } @@ -452,12 +453,12 @@ public final class UTF16 char ch = source.charAt(offset16); if (isSurrogate(ch)) { if (isLeadSurrogate(ch)) - { - if (++ offset16 < source.length() && - isTrailSurrogate(source.charAt(offset16))) { - return LEAD_SURROGATE_BOUNDARY; - } - } + { + if (++ offset16 < source.length() && + isTrailSurrogate(source.charAt(offset16))) { + return LEAD_SURROGATE_BOUNDARY; + } + } else { // isTrailSurrogate(ch), so -- offset16; @@ -496,12 +497,12 @@ public final class UTF16 char ch = source.charAt(offset16); if (isSurrogate(ch)) { if (isLeadSurrogate(ch)) - { - if (++ offset16 < source.length() && - isTrailSurrogate(source.charAt(offset16))) { - return LEAD_SURROGATE_BOUNDARY; - } - } + { + if (++ offset16 < source.length() && + isTrailSurrogate(source.charAt(offset16))) { + return LEAD_SURROGATE_BOUNDARY; + } + } else { // isTrailSurrogate(ch), so -- offset16; @@ -612,8 +613,8 @@ public final class UTF16 public static char getLeadSurrogate(int char32) { if (char32 >= SUPPLEMENTARY_MIN_VALUE) { - return (char)(LEAD_SURROGATE_OFFSET_ + - (char32 >> LEAD_SURROGATE_SHIFT_)); + return (char)(LEAD_SURROGATE_OFFSET_ + + (char32 >> LEAD_SURROGATE_SHIFT_)); } return 0; @@ -632,8 +633,8 @@ public final class UTF16 public static char getTrailSurrogate(int char32) { if (char32 >= SUPPLEMENTARY_MIN_VALUE) { - return (char)(TRAIL_SURROGATE_MIN_VALUE + - (char32 & TRAIL_SURROGATE_MASK_)); + return (char)(TRAIL_SURROGATE_MIN_VALUE + + (char32 & TRAIL_SURROGATE_MASK_)); } return (char)char32; @@ -654,7 +655,7 @@ public final class UTF16 public static String valueOf(int char32) { if (char32 < CODEPOINT_MIN_VALUE || char32 > CODEPOINT_MAX_VALUE) { - throw new IllegalArgumentException("Illegal codepoint"); + throw new IllegalArgumentException("Illegal codepoint"); } return toString(char32); } @@ -678,11 +679,11 @@ public final class UTF16 public static String valueOf(String source, int offset16) { switch (bounds(source, offset16)) { - case LEAD_SURROGATE_BOUNDARY: - return source.substring(offset16, offset16 + 2); - case TRAIL_SURROGATE_BOUNDARY: - return source.substring(offset16 - 1, offset16 + 1); - default: return source.substring(offset16, offset16 + 1); + case LEAD_SURROGATE_BOUNDARY: + return source.substring(offset16, offset16 + 2); + case TRAIL_SURROGATE_BOUNDARY: + return source.substring(offset16 - 1, offset16 + 1); + default: return source.substring(offset16, offset16 + 1); } } @@ -705,11 +706,11 @@ public final class UTF16 public static String valueOf(StringBuffer source, int offset16) { switch (bounds(source, offset16)) { - case LEAD_SURROGATE_BOUNDARY: - return source.substring(offset16, offset16 + 2); - case TRAIL_SURROGATE_BOUNDARY: - return source.substring(offset16 - 1, offset16 + 1); - default: return source.substring(offset16, offset16 + 1); + case LEAD_SURROGATE_BOUNDARY: + return source.substring(offset16, offset16 + 2); + case TRAIL_SURROGATE_BOUNDARY: + return source.substring(offset16 - 1, offset16 + 1); + default: return source.substring(offset16, offset16 + 1); } } @@ -738,10 +739,10 @@ public final class UTF16 int offset16) { switch (bounds(source, start, limit, offset16)) { - case LEAD_SURROGATE_BOUNDARY: - return new String(source, start + offset16, 2); - case TRAIL_SURROGATE_BOUNDARY: - return new String(source, start + offset16 - 1, 2); + case LEAD_SURROGATE_BOUNDARY: + return new String(source, start + offset16, 2); + case TRAIL_SURROGATE_BOUNDARY: + return new String(source, start + offset16 - 1, 2); } return new String(source, start + offset16, 1); } @@ -763,21 +764,21 @@ public final class UTF16 result = 0, count = offset32; if (offset32 < 0 || offset32 > size) { - throw new StringIndexOutOfBoundsException(offset32); + throw new StringIndexOutOfBoundsException(offset32); } while (result < size && count > 0) - { - ch = source.charAt(result); - if (isLeadSurrogate(ch) && ((result + 1) < size) && - isTrailSurrogate(source.charAt(result + 1))) { - result ++; - } + { + ch = source.charAt(result); + if (isLeadSurrogate(ch) && ((result + 1) < size) && + isTrailSurrogate(source.charAt(result + 1))) { + result ++; + } - count --; - result ++; - } + count --; + result ++; + } if (count != 0) { - throw new StringIndexOutOfBoundsException(offset32); + throw new StringIndexOutOfBoundsException(offset32); } return result; } @@ -800,21 +801,21 @@ public final class UTF16 result = 0, count = offset32; if (offset32 < 0 || offset32 > size) { - throw new StringIndexOutOfBoundsException(offset32); + throw new StringIndexOutOfBoundsException(offset32); } while (result < size && count > 0) - { - ch = source.charAt(result); - if (isLeadSurrogate(ch) && ((result + 1) < size) && - isTrailSurrogate(source.charAt(result + 1))) { - result ++; - } + { + ch = source.charAt(result); + if (isLeadSurrogate(ch) && ((result + 1) < size) && + isTrailSurrogate(source.charAt(result + 1))) { + result ++; + } - count --; - result ++; - } + count --; + result ++; + } if (count != 0) { - throw new StringIndexOutOfBoundsException(offset32); + throw new StringIndexOutOfBoundsException(offset32); } return result; } @@ -838,21 +839,21 @@ public final class UTF16 int result = start, count = offset32; if (offset32 > limit - start) { - throw new ArrayIndexOutOfBoundsException(offset32); + throw new ArrayIndexOutOfBoundsException(offset32); } while (result < limit && count > 0) - { - ch = source[result]; - if (isLeadSurrogate(ch) && ((result + 1) < limit) && - isTrailSurrogate(source[result + 1])) { - result ++; - } + { + ch = source[result]; + if (isLeadSurrogate(ch) && ((result + 1) < limit) && + isTrailSurrogate(source[result + 1])) { + result ++; + } - count --; - result ++; - } + count --; + result ++; + } if (count != 0) { - throw new ArrayIndexOutOfBoundsException(offset32); + throw new ArrayIndexOutOfBoundsException(offset32); } return result - start; } @@ -881,7 +882,7 @@ public final class UTF16 public static int findCodePointOffset(String source, int offset16) { if (offset16 < 0 || offset16 > source.length()) { - throw new StringIndexOutOfBoundsException(offset16); + throw new StringIndexOutOfBoundsException(offset16); } int result = 0; @@ -889,17 +890,17 @@ public final class UTF16 boolean hadLeadSurrogate = false; for (int i = 0; i < offset16; ++ i) - { - ch = source.charAt(i); - if (hadLeadSurrogate && isTrailSurrogate(ch)) { - hadLeadSurrogate = false; // count valid trail as zero - } - else - { - hadLeadSurrogate = isLeadSurrogate(ch); - ++ result; // count others as 1 - } - } + { + ch = source.charAt(i); + if (hadLeadSurrogate && isTrailSurrogate(ch)) { + hadLeadSurrogate = false; // count valid trail as zero + } + else + { + hadLeadSurrogate = isLeadSurrogate(ch); + ++ result; // count others as 1 + } + } if (offset16 == source.length()) { return result; @@ -908,7 +909,7 @@ public final class UTF16 // end of source being the less significant surrogate character // shift result back to the start of the supplementary character if (hadLeadSurrogate && (isTrailSurrogate(source.charAt(offset16)))) { - result --; + result --; } return result; @@ -938,7 +939,7 @@ public final class UTF16 public static int findCodePointOffset(StringBuffer source, int offset16) { if (offset16 < 0 || offset16 > source.length()) { - throw new StringIndexOutOfBoundsException(offset16); + throw new StringIndexOutOfBoundsException(offset16); } int result = 0; @@ -946,17 +947,17 @@ public final class UTF16 boolean hadLeadSurrogate = false; for (int i = 0; i < offset16; ++ i) - { - ch = source.charAt(i); - if (hadLeadSurrogate && isTrailSurrogate(ch)) { - hadLeadSurrogate = false; // count valid trail as zero - } - else - { - hadLeadSurrogate = isLeadSurrogate(ch); - ++ result; // count others as 1 - } - } + { + ch = source.charAt(i); + if (hadLeadSurrogate && isTrailSurrogate(ch)) { + hadLeadSurrogate = false; // count valid trail as zero + } + else + { + hadLeadSurrogate = isLeadSurrogate(ch); + ++ result; // count others as 1 + } + } if (offset16 == source.length()) { return result; @@ -965,9 +966,9 @@ public final class UTF16 // end of source being the less significant surrogate character // shift result back to the start of the supplementary character if (hadLeadSurrogate && (isTrailSurrogate(source.charAt(offset16)))) - { - result --; - } + { + result --; + } return result; } @@ -990,7 +991,7 @@ public final class UTF16 * @param source text to analyse * @param start offset of the substring * @param limit offset of the substring - * @param offset16 UTF-16 relative to start + * @param offset16 UTF-16 relative to start * @return UTF-32 offset relative to start * @exception IndexOutOfBoundsException if offset16 is not within the * range of start and limit. @@ -1001,7 +1002,7 @@ public final class UTF16 { offset16 += start; if (offset16 > limit) { - throw new StringIndexOutOfBoundsException(offset16); + throw new StringIndexOutOfBoundsException(offset16); } int result = 0; @@ -1009,17 +1010,17 @@ public final class UTF16 boolean hadLeadSurrogate = false; for (int i = start; i < offset16; ++ i) - { + { ch = source[i]; if (hadLeadSurrogate && isTrailSurrogate(ch)) { - hadLeadSurrogate = false; // count valid trail as zero + hadLeadSurrogate = false; // count valid trail as zero } else - { - hadLeadSurrogate = isLeadSurrogate(ch); - ++ result; // count others as 1 - } - } + { + hadLeadSurrogate = isLeadSurrogate(ch); + ++ result; // count others as 1 + } + } if (offset16 == limit) { return result; @@ -1028,7 +1029,7 @@ public final class UTF16 // end of source being the less significant surrogate character // shift result back to the start of the supplementary character if (hadLeadSurrogate && (isTrailSurrogate(source[offset16]))) { - result --; + result --; } return result; @@ -1039,6 +1040,7 @@ public final class UTF16 * If a validity check is required, use * isLegal() on * char32 before calling. + * @param target the buffer to append to * @param char32 value to append. * @return the updated StringBuffer * @exception IllegalArgumentException thrown when char32 does not lie @@ -1049,21 +1051,35 @@ public final class UTF16 { // Check for irregular values if (char32 < CODEPOINT_MIN_VALUE || char32 > CODEPOINT_MAX_VALUE) { - throw new IllegalArgumentException("Illegal codepoint"); + throw new IllegalArgumentException("Illegal codepoint: " + Integer.toHexString(char32)); } // Write the UTF-16 values if (char32 >= SUPPLEMENTARY_MIN_VALUE) - { - target.append(getLeadSurrogate(char32)); + { + target.append(getLeadSurrogate(char32)); target.append(getTrailSurrogate(char32)); - } - else { - target.append((char)char32); - } - return target; + } + else { + target.append((char)char32); + } + return target; } - + + /** + * Cover JDK 1.5 APIs. Append the code point to the buffer and return the buffer + * as a convenience. + * + * @param target the buffer to append to + * @param cp the code point to append + * @return the updated StringBuffer + * @throw IllegalArgumentException if cp is not a valid code point + * @draft ICU 3.0 + */ + public static StringBuffer appendCodePoint(StringBuffer target, int cp) { + return append(target, cp); + } + /** * Adds a codepoint to offset16 position of the argument char array. * @param target char array to be append with the new code point @@ -1083,10 +1099,10 @@ public final class UTF16 } // Write the UTF-16 values if (char32 >= SUPPLEMENTARY_MIN_VALUE) - { - target[limit ++] = getLeadSurrogate(char32); - target[limit ++] = getTrailSurrogate(char32); - } + { + target[limit ++] = getLeadSurrogate(char32); + target[limit ++] = getTrailSurrogate(char32); + } else { target[limit ++] = (char)char32; } @@ -1154,10 +1170,10 @@ public final class UTF16 char single = target.charAt(offset16); if (isSurrogate(single)) - { - // pairs of the surrogate with offset16 at the lead char found - if (isLeadSurrogate(single) && (target.length() > offset16 + 1) - && isTrailSurrogate(target.charAt(offset16 + 1))) { + { + // pairs of the surrogate with offset16 at the lead char found + if (isLeadSurrogate(single) && (target.length() > offset16 + 1) + && isTrailSurrogate(target.charAt(offset16 + 1))) { count ++; } else { @@ -1165,13 +1181,13 @@ public final class UTF16 // found if (isTrailSurrogate(single) && (offset16 > 0) && isLeadSurrogate(target.charAt(offset16 -1))) - { - offset16 --; - count ++; - } + { + offset16 --; + count ++; + } } } - target.replace(offset16, offset16 + count, valueOf(char32)); + target.replace(offset16, offset16 + count, valueOf(char32)); } /** @@ -1198,10 +1214,10 @@ public final class UTF16 char single = target[offset16]; if (isSurrogate(single)) - { - // pairs of the surrogate with offset16 at the lead char found - if (isLeadSurrogate(single) && (target.length > offset16 + 1) && - isTrailSurrogate(target[offset16 + 1])) { + { + // pairs of the surrogate with offset16 at the lead char found + if (isLeadSurrogate(single) && (target.length > offset16 + 1) && + isTrailSurrogate(target[offset16 + 1])) { count ++; } else { @@ -1209,44 +1225,44 @@ public final class UTF16 // found if (isTrailSurrogate(single) && (offset16 > 0) && isLeadSurrogate(target[offset16 -1])) - { - offset16 --; - count ++; - } + { + offset16 --; + count ++; + } } } - String str = valueOf(char32); - int result = limit; - int strlength = str.length(); - target[offset16] = str.charAt(0); - if (count == strlength) { - if (count == 2) { - target[offset16 + 1] = str.charAt(1); - } + String str = valueOf(char32); + int result = limit; + int strlength = str.length(); + target[offset16] = str.charAt(0); + if (count == strlength) { + if (count == 2) { + target[offset16 + 1] = str.charAt(1); } - else { - // this is not exact match in space, we'll have to do some - // shifting - System.arraycopy(target, offset16 + count, target, - offset16 + strlength, limit - (offset16 + count)); - if (count < strlength) { - // char32 is a supplementary character trying to squeeze into - // a non-supplementary space - target[offset16 + 1] = str.charAt(1); - result ++; - if (result < target.length) { - target[result] = 0; - } - } - else { - // char32 is a non-supplementary character trying to fill - // into a supplementary space - result --; + } + else { + // this is not exact match in space, we'll have to do some + // shifting + System.arraycopy(target, offset16 + count, target, + offset16 + strlength, limit - (offset16 + count)); + if (count < strlength) { + // char32 is a supplementary character trying to squeeze into + // a non-supplementary space + target[offset16 + 1] = str.charAt(1); + result ++; + if (result < target.length) { target[result] = 0; } } - return result; + else { + // char32 is a non-supplementary character trying to fill + // into a supplementary space + result --; + target[result] = 0; + } + } + return result; } /** @@ -1260,7 +1276,7 @@ public final class UTF16 * @stable ICU 2.1 */ public static int moveCodePointOffset(String source, int offset16, - int shift32) + int shift32) { int size = source.length(); if (offset16 < 0 || shift32 + offset16 > size) { @@ -1270,16 +1286,16 @@ public final class UTF16 int result = offset16; int count = shift32; while (result < size && count > 0) - { - ch = source.charAt(result); - if (isLeadSurrogate(ch) && ((result + 1) < size) && - isTrailSurrogate(source.charAt(result + 1))) { + { + ch = source.charAt(result); + if (isLeadSurrogate(ch) && ((result + 1) < size) && + isTrailSurrogate(source.charAt(result + 1))) { + result ++; + } + + count --; result ++; } - - count --; - result ++; - } if (count != 0) { throw new StringIndexOutOfBoundsException(shift32); } @@ -1297,7 +1313,7 @@ public final class UTF16 * @stable ICU 2.1 */ public static int moveCodePointOffset(StringBuffer source, int offset16, - int shift32) + int shift32) { int size = source.length(); if (offset16 < 0 || shift32 + offset16 > size) { @@ -1307,16 +1323,16 @@ public final class UTF16 int result = offset16; int count = shift32; while (result < size && count > 0) - { - ch = source.charAt(result); - if (isLeadSurrogate(ch) && ((result + 1) < size) && - isTrailSurrogate(source.charAt(result + 1))) { + { + ch = source.charAt(result); + if (isLeadSurrogate(ch) && ((result + 1) < size) && + isTrailSurrogate(source.charAt(result + 1))) { + result ++; + } + + count --; result ++; } - - count --; - result ++; - } if (count != 0) { throw new StringIndexOutOfBoundsException(shift32); } @@ -1346,16 +1362,16 @@ public final class UTF16 int result = offset16; int count = shift32; while (result < limit && count > 0) - { - ch = source[result]; - if (isLeadSurrogate(ch) && ((result + 1) < limit) && - isTrailSurrogate(source[result + 1])) { + { + ch = source[result]; + if (isLeadSurrogate(ch) && ((result + 1) < limit) && + isTrailSurrogate(source[result + 1])) { + result ++; + } + + count --; result ++; } - - count --; - result ++; - } if (count != 0) { throw new StringIndexOutOfBoundsException(shift32); } @@ -1384,7 +1400,7 @@ public final class UTF16 * @stable ICU 2.1 */ public static StringBuffer insert(StringBuffer target, int offset16, - int char32) + int char32) { String str = valueOf(char32); if (offset16 != target.length() && @@ -1429,7 +1445,7 @@ public final class UTF16 throw new ArrayIndexOutOfBoundsException(offset16 + size); } System.arraycopy(target, offset16, target, offset16 + size, - limit - offset16); + limit - offset16); target[offset16] = str.charAt(0); if (size == 2) { target[offset16 + 1] = str.charAt(1); @@ -1451,13 +1467,13 @@ public final class UTF16 { int count = 1; switch (bounds(target, offset16)) { - case LEAD_SURROGATE_BOUNDARY: - count ++; - break; - case TRAIL_SURROGATE_BOUNDARY: - count ++; - offset16 --; - break; + case LEAD_SURROGATE_BOUNDARY: + count ++; + break; + case TRAIL_SURROGATE_BOUNDARY: + count ++; + offset16 --; + break; } target.delete(offset16, offset16 + count); return target; @@ -1478,16 +1494,16 @@ public final class UTF16 { int count = 1; switch (bounds(target, 0, limit, offset16)) { - case LEAD_SURROGATE_BOUNDARY: - count ++; - break; - case TRAIL_SURROGATE_BOUNDARY: - count ++; - offset16 --; - break; + case LEAD_SURROGATE_BOUNDARY: + count ++; + break; + case TRAIL_SURROGATE_BOUNDARY: + count ++; + offset16 --; + break; } System.arraycopy(target, offset16 + count, target, offset16, - limit - (offset16 + count)); + limit - (offset16 + count)); target[limit - count] = 0; return limit - count; } @@ -1517,7 +1533,7 @@ public final class UTF16 if (char32 < CODEPOINT_MIN_VALUE || char32 > CODEPOINT_MAX_VALUE) { throw new IllegalArgumentException( - "Argument char32 is not a valid codepoint"); + "Argument char32 is not a valid codepoint"); } // non-surrogate bmp if (char32 < LEAD_SURROGATE_MIN_VALUE || @@ -1625,7 +1641,7 @@ public final class UTF16 { if (char32 < CODEPOINT_MIN_VALUE || char32 > CODEPOINT_MAX_VALUE) { throw new IllegalArgumentException( - "Argument char32 is not a valid codepoint"); + "Argument char32 is not a valid codepoint"); } // non-surrogate bmp if (char32 < LEAD_SURROGATE_MIN_VALUE || @@ -1733,7 +1749,7 @@ public final class UTF16 { if (char32 < CODEPOINT_MIN_VALUE || char32 > CODEPOINT_MAX_VALUE) { throw new IllegalArgumentException( - "Argument char32 is not a valid codepoint"); + "Argument char32 is not a valid codepoint"); } // non-surrogate bmp if (char32 < LEAD_SURROGATE_MIN_VALUE || @@ -1849,7 +1865,7 @@ public final class UTF16 { if (char32 < CODEPOINT_MIN_VALUE || char32 > CODEPOINT_MAX_VALUE) { throw new IllegalArgumentException( - "Argument char32 is not a valid codepoint"); + "Argument char32 is not a valid codepoint"); } // non-surrogate bmp if (char32 < LEAD_SURROGATE_MIN_VALUE || @@ -1977,11 +1993,11 @@ public final class UTF16 { if (oldChar32 <= 0 || oldChar32 > CODEPOINT_MAX_VALUE) { throw new IllegalArgumentException( - "Argument oldChar32 is not a valid codepoint"); + "Argument oldChar32 is not a valid codepoint"); } if (newChar32 <= 0 || newChar32 > CODEPOINT_MAX_VALUE) { throw new IllegalArgumentException( - "Argument newChar32 is not a valid codepoint"); + "Argument newChar32 is not a valid codepoint"); } int index = indexOf(source, oldChar32); @@ -2188,7 +2204,7 @@ public final class UTF16 int length = limit - start; if (length < 0 || start < 0 || limit < 0) { throw new IndexOutOfBoundsException( - "Start and limit indexes should be non-negative and start <= limit"); + "Start and limit indexes should be non-negative and start <= limit"); } if (number < 0) { return true; @@ -2309,9 +2325,9 @@ public final class UTF16 * @draft ICU 3.0 */ public static String newString(int[] codePoints, int offset, int count) { - if (count < 0) { - throw new IllegalArgumentException(); - } + if (count < 0) { + throw new IllegalArgumentException(); + } char[] chars = new char[count]; int w = 0; for (int r = offset, e = offset + count; r < e; ++r) { @@ -2345,23 +2361,23 @@ public final class UTF16 } /** - *

UTF16 string comparator class. - * Allows UTF16 string comparison to be done with the various modes

- * - *

The code unit or code point comparison differ only when comparing - * supplementary code points (\u10000..\u10ffff) to BMP code points - * near the end of the BMP (i.e., \ue000..\uffff). In code unit - * comparison, high BMP code points sort after supplementary code points - * because they are stored as pairs of surrogates which are at - * \ud800..\udfff.

- * @see #FOLD_CASE_DEFAULT - * @see #FOLD_CASE_EXCLUDE_SPECIAL_I - * @stable ICU 2.1 - */ + *

UTF16 string comparator class. + * Allows UTF16 string comparison to be done with the various modes

+ * + *

The code unit or code point comparison differ only when comparing + * supplementary code points (\u10000..\u10ffff) to BMP code points + * near the end of the BMP (i.e., \ue000..\uffff). In code unit + * comparison, high BMP code points sort after supplementary code points + * because they are stored as pairs of surrogates which are at + * \ud800..\udfff.

+ * @see #FOLD_CASE_DEFAULT + * @see #FOLD_CASE_EXCLUDE_SPECIAL_I + * @stable ICU 2.1 + */ public static final class StringComparator implements java.util.Comparator { // public constructor ------------------------------------------------ @@ -2531,15 +2547,15 @@ public final class UTF16 String str1 = (String)a; String str2 = (String)b; - if (str1 == str2) { - return 0; - } - if (str1 == null) { - return -1; - } - if (str2 == null) { - return 1; - } + if (str1 == str2) { + return 0; + } + if (str1 == null) { + return -1; + } + if (str2 == null) { + return 1; + } if (m_ignoreCase_) { return compareCaseInsensitive(str1, str2); @@ -2554,14 +2570,17 @@ public final class UTF16 * False if code point comparison is required. */ private int m_codePointCompare_; + /** * Fold case comparison option. */ private int m_foldCase_; + /** * Flag indicator if ignore case is to be used during comparison */ private boolean m_ignoreCase_; + /** * Code point order offset for surrogate characters */ @@ -2579,8 +2598,8 @@ public final class UTF16 private int compareCaseInsensitive(String s1, String s2) { return NormalizerImpl.cmpEquivFold(s1, s2, - m_foldCase_ | m_codePointCompare_ - | Normalizer.COMPARE_IGNORE_CASE); + m_foldCase_ | m_codePointCompare_ + | Normalizer.COMPARE_IGNORE_CASE); } /** @@ -2630,7 +2649,7 @@ public final class UTF16 // subtract 0x2800 from BMP code points to make them smaller // than supplementary ones if ((c1 <= LEAD_SURROGATE_MAX_VALUE && (index + 1) != length1 - && isTrailSurrogate(s1.charAt(index + 1))) + && isTrailSurrogate(s1.charAt(index + 1))) || (isTrailSurrogate(c1) && index != 0 && isLeadSurrogate(s1.charAt(index - 1)))) { // part of a surrogate pair, leave >=d800 @@ -2662,34 +2681,36 @@ public final class UTF16 // private data members ------------------------------------------------- /** - * Shift value for lead surrogate to form a supplementary character. - */ - private static final int LEAD_SURROGATE_SHIFT_ = 10; - /** - * Mask to retrieve the significant value from a trail surrogate. - */ - private static final int TRAIL_SURROGATE_MASK_ = 0x3FF; + * Shift value for lead surrogate to form a supplementary character. + */ + private static final int LEAD_SURROGATE_SHIFT_ = 10; + + /** + * Mask to retrieve the significant value from a trail surrogate. + */ + private static final int TRAIL_SURROGATE_MASK_ = 0x3FF; + /** * Value that all lead surrogate starts with */ private static final int LEAD_SURROGATE_OFFSET_ = - LEAD_SURROGATE_MIN_VALUE - - (SUPPLEMENTARY_MIN_VALUE - >> LEAD_SURROGATE_SHIFT_); + LEAD_SURROGATE_MIN_VALUE - + (SUPPLEMENTARY_MIN_VALUE + >> LEAD_SURROGATE_SHIFT_); // private methods ------------------------------------------------------ /** - *

Converts argument code point and returns a String object representing - * the code point's value in UTF16 format.

- *

This method does not check for the validity of the codepoint, the - * results are not guaranteed if a invalid codepoint is passed as - * argument.

- *

The result is a string whose length is 1 for non-supplementary code - * points, 2 otherwise.

- * @param ch code point - * @return string representation of the code point - */ + *

Converts argument code point and returns a String object representing + * the code point's value in UTF16 format.

+ *

This method does not check for the validity of the codepoint, the + * results are not guaranteed if a invalid codepoint is passed as + * argument.

+ *

The result is a string whose length is 1 for non-supplementary code + * points, 2 otherwise.

+ * @param ch code point + * @return string representation of the code point + */ private static String toString(int ch) { if (ch < SUPPLEMENTARY_MIN_VALUE) {