Skip to content

Commit 44f81b4

Browse files
RealityRippleroytam1
authored andcommitted
Issue #2538 - Part 3: Bring ZWJ Awareness to ClusterReverseIterator
Additional patch to resolve a bug exposed while working on the Cluster issue.
1 parent 68bcd4e commit 44f81b4

File tree

1 file changed

+46
-9
lines changed

1 file changed

+46
-9
lines changed

intl/unicharutil/util/nsUnicodeProperties.cpp

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ GetHangulSyllableType(uint32_t aCh)
180180
return HSType(u_getIntPropertyValue(aCh, UCHAR_HANGUL_SYLLABLE_TYPE));
181181
}
182182

183+
static const uint32_t kZWJ = 0x200d;
184+
183185
void
184186
ClusterIterator::Next()
185187
{
@@ -233,7 +235,6 @@ ClusterIterator::Next()
233235
}
234236
}
235237

236-
const uint32_t kZWJ = 0x200d;
237238
uint32_t aNextCh = 0;
238239
if (mPos + 1 < mLimit) {
239240
aNextCh = *mPos;
@@ -299,19 +300,55 @@ ClusterReverseIterator::Next()
299300
}
300301

301302
uint32_t ch;
302-
do {
303-
ch = *--mPos;
304303

305-
if (NS_IS_LOW_SURROGATE(ch) && mPos > mLimit &&
306-
NS_IS_HIGH_SURROGATE(*(mPos - 1))) {
307-
ch = SURROGATE_TO_UCS4(*--mPos, ch);
304+
bool nextWasComponent = false;
305+
size_t tRel = 0;
306+
size_t tPos = 0;
307+
size_t chLen = 0;
308+
309+
do {
310+
tRel++;
311+
ch = *(mPos - tRel);
312+
313+
if (NS_IS_LOW_SURROGATE(ch) && (mPos - tRel) > mLimit &&
314+
NS_IS_HIGH_SURROGATE(*(mPos - (tRel + 1)))) {
315+
tRel++;
316+
ch = SURROGATE_TO_UCS4(*(mPos - tRel), ch);
317+
if (chLen == 0) {
318+
chLen = 2;
319+
}
320+
} else if (chLen == 0) {
321+
chLen = 1;
308322
}
309323

310-
// TODO: Full extendCluster support.
311-
if (!(IsClusterExtender(ch) || IsEmojiClusterExtender(ch))) {
324+
bool prevWillBeZwj = false;
325+
bool validEmoji =
326+
(GetEmojiPresentation(ch) == EmojiDefault) ||
327+
(GetEmojiPresentation(ch) == EmojiComponent) ||
328+
((GetEmojiPresentation(ch) == TextDefault) && nextWasComponent);
329+
if (validEmoji) {
330+
tPos = tRel;
331+
332+
uint32_t aPrevCh = *(mPos - (tRel + 1));
333+
if (NS_IS_LOW_SURROGATE(aPrevCh) && (mPos - (tRel + 1)) > mLimit) {
334+
uint32_t aHighCh = *(mPos - (tRel + 2));
335+
if (NS_IS_HIGH_SURROGATE(aHighCh)) {
336+
aPrevCh = SURROGATE_TO_UCS4(aHighCh, aPrevCh);
337+
}
338+
}
339+
prevWillBeZwj = (aPrevCh == kZWJ);
340+
}
341+
if (!(IsClusterExtender(ch) ||
342+
IsEmojiClusterExtender(ch) ||
343+
prevWillBeZwj)) {
344+
if (tPos == 0) {
345+
tPos = chLen;
346+
}
312347
break;
313348
}
314-
} while (mPos > mLimit);
349+
nextWasComponent = (GetEmojiPresentation(ch) == EmojiComponent);
350+
} while ((mPos - tRel) > mLimit);
351+
mPos -= tPos;
315352

316353
// XXX May need to handle conjoining Jamo
317354

0 commit comments

Comments
 (0)