util/Utf8ByteCount.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QStringView> | ||
| 4 | |||
| 5 | namespace MailJD { | ||
| 6 | |||
| 7 | // Exact UTF-8 length without allocating an encoded copy. Return limit + 1 as | ||
| 8 | // soon as the budget is exceeded. Rare malformed UTF-16 falls back to Qt's | ||
| 9 | // encoder so the admission count retains its exact existing semantics. | ||
| 10 | 905 | inline qsizetype utf8ByteCountUpTo(QStringView text, qsizetype limit) { | |
| 11 | 905 | qsizetype bytes = 0; | |
| 12 |
2/2✓ Branch 38 → 3 taken 113540140 times.
✓ Branch 38 → 39 taken 905 times.
|
113541045 | for (qsizetype index = 0; index < text.size(); ++index) { |
| 13 | 113540140 | const ushort unit = text.at(index).unicode(); | |
| 14 | 113540140 | qsizetype width = 3; | |
| 15 |
2/2✓ Branch 5 → 6 taken 113540090 times.
✓ Branch 5 → 7 taken 50 times.
|
113540140 | if (unit < 0x80) width = 1; |
| 16 |
2/2✓ Branch 7 → 8 taken 30 times.
✓ Branch 7 → 9 taken 20 times.
|
50 | else if (unit < 0x800) width = 2; |
| 17 |
1/4✗ Branch 10 → 11 not taken.
✓ Branch 10 → 20 taken 10 times.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 20 not taken.
|
10 | else if (unit >= 0xd800 && unit <= 0xdbff && index + 1 < text.size() && |
| 18 |
2/4✓ Branch 9 → 10 taken 10 times.
✓ Branch 9 → 20 taken 10 times.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 20 not taken.
|
30 | text.at(index + 1).unicode() >= 0xdc00 && |
| 19 |
1/4✗ Branch 18 → 19 not taken.
✗ Branch 18 → 20 not taken.
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 20 times.
|
20 | text.at(index + 1).unicode() <= 0xdfff) { |
| 20 | ✗ | width = 4; | |
| 21 | ✗ | ++index; | |
| 22 |
3/4✓ Branch 23 → 24 taken 10 times.
✓ Branch 23 → 34 taken 10 times.
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 34 taken 10 times.
|
20 | } else if (unit >= 0xd800 && unit <= 0xdfff) { |
| 23 | ✗ | const qsizetype encoded = text.toString().toUtf8().size(); | |
| 24 | ✗ | return encoded > limit ? limit + 1 : encoded; | |
| 25 | } | ||
| 26 |
1/2✗ Branch 34 → 35 not taken.
✓ Branch 34 → 36 taken 113540140 times.
|
113540140 | if (width > limit - bytes) return limit + 1; |
| 27 | 113540140 | bytes += width; | |
| 28 | } | ||
| 29 | 905 | return bytes; | |
| 30 | } | ||
| 31 | |||
| 32 | } // namespace MailJD | ||
| 33 |