From b22717a642c2d416c56931efee61aa4ee386c027 Mon Sep 17 00:00:00 2001 From: Dragan Besevic Date: Thu, 2 Jun 2011 19:56:42 +0000 Subject: [PATCH] ICU-8484 fix the problem with infinite loop with surrogates for collation X-SVN-Rev: 30184 --- .../ibm/icu/text/CollationElementIterator.java | 17 +++++++++++++---- .../test/collator/CollationRegressionTest.java | 9 +++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/icu4j/main/classes/collate/src/com/ibm/icu/text/CollationElementIterator.java b/icu4j/main/classes/collate/src/com/ibm/icu/text/CollationElementIterator.java index 4cde4dc02ea..2ef8cb77fdb 100644 --- a/icu4j/main/classes/collate/src/com/ibm/icu/text/CollationElementIterator.java +++ b/icu4j/main/classes/collate/src/com/ibm/icu/text/CollationElementIterator.java @@ -1690,10 +1690,19 @@ public final class CollationElementIterator // Source string char was not in contraction table. // Unless it is a discontiguous contraction, we are done int miss = ch; - if(UTF16.isLeadSurrogate(ch)) { // in order to do the proper detection, we - // need to see if we're dealing with a supplementary - miss = UCharacterProperty.getRawSupplementary(ch, (char) nextChar()); - } + // ticket 8484 - porting changes from C for 6101 + // We test whether the next two char are surrogate pairs. + // This test is done if the iterator is not in the end. + // If there is no surrogate pair, the iterator + // goes back one if needed. + if(UTF16.isLeadSurrogate(ch) && !isEnd()) { + char surrNextChar = (char)nextChar(); + if (UTF16.isTrailSurrogate(surrNextChar)) { + miss = UCharacterProperty.getRawSupplementary(ch, surrNextChar); + } else { + previousChar(); + } + } int sCC; if (maxCC == 0 || (sCC = getCombiningClass(miss)) == 0 || sCC > maxCC || (allSame != 0 && sCC == maxCC) || diff --git a/icu4j/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationRegressionTest.java b/icu4j/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationRegressionTest.java index c9e32cb3ffd..f7c637516e0 100644 --- a/icu4j/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationRegressionTest.java +++ b/icu4j/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationRegressionTest.java @@ -1135,6 +1135,15 @@ public class CollationRegressionTest extends TestFmwk { } } + // Fixing the infinite loop for surrogates + public void Test8484() + { + String s = "\u9FE1\uCEF3\u2798\uAAB6\uDA7C"; + Collator coll = Collator.getInstance(); + CollationKey collKey = coll.getCollationKey(s); + logln("Pass: " + collKey.toString() + " generated OK."); + } + public void TestBengaliSortKey() throws Exception { char rules[] = { 0x26, 0x9fa, 0x3c, 0x98c, 0x3c, 0x9e1, 0x3c, 0x98f, 0x3c, 0x990, 0x3c, 0x993, 0x3c, 0x994, 0x3c, 0x9bc, 0x3c, 0x982, 0x3c, 0x983, 0x3c, 0x981, 0x3c, 0x9b0, 0x3c,