Skip to content

Commit

Permalink
lvstring: optimize Utf8ToUnicode
Browse files Browse the repository at this point in the history
remove unnecessary assignment
  • Loading branch information
bbshelper authored and poire-z committed Jun 27, 2024
1 parent 8b1cb64 commit 64080df
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions crengine/src/lvstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3106,28 +3106,25 @@ void Utf8ToUnicode(const lUInt8 * src, int &srclen, lChar32 * dst, int &dstlen)
lChar32 * p = dst;
lChar32 * endp = p + dstlen;
lUInt32 ch;
bool matched;
while (p < endp && s < ends) {
ch = *s;
matched = false;
if ( (ch & 0x80) == 0 ) {
matched = true;
*p++ = (char)ch;
s++;
continue;
} else if ( (ch & 0xE0) == 0xC0 ) {
if (s + 2 > ends)
break;
if (IS_FOLLOWING(1)) {
matched = true;
*p++ = ((ch & 0x1F) << 6)
| CONT_BYTE(1,0);
s += 2;
continue;
}
} else if ( (ch & 0xF0) == 0xE0 ) {
if (s + 3 > ends)
break;
if (IS_FOLLOWING(1) && IS_FOLLOWING(2)) {
matched = true;
*p++ = ((ch & 0x0F) << 12)
| CONT_BYTE(1,6)
| CONT_BYTE(2,0);
Expand Down Expand Up @@ -3159,30 +3156,29 @@ void Utf8ToUnicode(const lUInt8 * src, int &srclen, lChar32 * dst, int &dstlen)
}
}
}
continue;
}
} else if ( (ch & 0xF8) == 0xF0 ) {
if (s + 4 > ends)
break;
if (IS_FOLLOWING(1) && IS_FOLLOWING(2) && IS_FOLLOWING(3)) {
matched = true;
*p++ = ((ch & 0x07) << 18)
| CONT_BYTE(1,12)
| CONT_BYTE(2,6)
| CONT_BYTE(3,0);
s += 4;
continue;
}
} else {
// Invalid first byte in UTF-8 sequence
// Pass with mask 0x7F, to resolve exception around env->NewStringUTF()
*p++ = (char) (ch & 0x7F);
s++;
matched = true; // just to avoid next if
continue;
}
// unexpected character
if (!matched) {
*p++ = '?';
s++;
}
*p++ = '?';
s++;
}
srclen = (int)(s - src);
dstlen = (int)(p - dst);
Expand Down

0 comments on commit 64080df

Please sign in to comment.