Skip to content

Commit

Permalink
Issue #2538 - Part 3: Bring ZWJ Awareness to ClusterReverseIterator
Browse files Browse the repository at this point in the history
Additional patch to resolve a bug exposed while working on the Cluster issue.
  • Loading branch information
RealityRipple committed Jun 28, 2024
1 parent 5d221d0 commit 4b1bf36
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions intl/unicharutil/util/nsUnicodeProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ GetHangulSyllableType(uint32_t aCh)
return HSType(u_getIntPropertyValue(aCh, UCHAR_HANGUL_SYLLABLE_TYPE));
}

static const uint32_t kZWJ = 0x200d;

void
ClusterIterator::Next()
{
Expand Down Expand Up @@ -233,7 +235,6 @@ ClusterIterator::Next()
}
}

const uint32_t kZWJ = 0x200d;
uint32_t aNextCh = 0;
if (mPos + 1 < mLimit) {
aNextCh = *mPos;
Expand Down Expand Up @@ -299,19 +300,55 @@ ClusterReverseIterator::Next()
}

uint32_t ch;
do {
ch = *--mPos;

if (NS_IS_LOW_SURROGATE(ch) && mPos > mLimit &&
NS_IS_HIGH_SURROGATE(*(mPos - 1))) {
ch = SURROGATE_TO_UCS4(*--mPos, ch);
bool nextWasComponent = false;
size_t tRel = 0;
size_t tPos = 0;
size_t chLen = 0;

do {
tRel++;
ch = *(mPos - tRel);

if (NS_IS_LOW_SURROGATE(ch) && (mPos - tRel) > mLimit &&
NS_IS_HIGH_SURROGATE(*(mPos - (tRel + 1)))) {
tRel++;
ch = SURROGATE_TO_UCS4(*(mPos - tRel), ch);
if (chLen == 0) {
chLen = 2;
}
} else if (chLen == 0) {
chLen = 1;
}

// TODO: Full extendCluster support.
if (!(IsClusterExtender(ch) || IsEmojiClusterExtender(ch))) {
bool prevWillBeZwj = false;
bool validEmoji =
(GetEmojiPresentation(ch) == EmojiDefault) ||
(GetEmojiPresentation(ch) == EmojiComponent) ||
((GetEmojiPresentation(ch) == TextDefault) && nextWasComponent);
if (validEmoji) {
tPos = tRel;

uint32_t aPrevCh = *(mPos - (tRel + 1));
if (NS_IS_LOW_SURROGATE(aPrevCh) && (mPos - (tRel + 1)) > mLimit) {
uint32_t aHighCh = *(mPos - (tRel + 2));
if (NS_IS_HIGH_SURROGATE(aHighCh)) {
aPrevCh = SURROGATE_TO_UCS4(aHighCh, aPrevCh);
}
}
prevWillBeZwj = (aPrevCh == kZWJ);
}
if (!(IsClusterExtender(ch) ||
IsEmojiClusterExtender(ch) ||
prevWillBeZwj)) {
if (tPos == 0) {
tPos = chLen;
}
break;
}
} while (mPos > mLimit);
nextWasComponent = (GetEmojiPresentation(ch) == EmojiComponent);
} while ((mPos - tRel) > mLimit);
mPos -= tPos;

// XXX May need to handle conjoining Jamo

Expand Down

0 comments on commit 4b1bf36

Please sign in to comment.