util/Utf8Digest.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <QByteArray> | ||
| 4 | #include <QCryptographicHash> | ||
| 5 | #include <QStringView> | ||
| 6 | #include <optional> | ||
| 7 | |||
| 8 | namespace MailJD { | ||
| 9 | |||
| 10 | // Hash the exact UTF-8 representation without retaining a whole encoded | ||
| 11 | // copy. A chunk boundary never splits a valid surrogate pair. | ||
| 12 | 59 | inline std::optional<QByteArray> sha256Utf8HexUpTo(QStringView view, qsizetype maxBytes) { | |
| 13 |
1/1✓ Branch 2 → 3 taken 59 times.
|
59 | QCryptographicHash hash(QCryptographicHash::Sha256); |
| 14 | 59 | qsizetype bytes = 0; | |
| 15 |
2/2✓ Branch 42 → 4 taken 59 times.
✓ Branch 42 → 43 taken 59 times.
|
118 | for (qsizetype offset = 0; offset < view.size();) { |
| 16 | 59 | qsizetype count = qMin<qsizetype>(4096, view.size() - offset); | |
| 17 | 59 | if (offset + count < view.size() && | |
| 18 | ✗ | view.at(offset + count - 1).unicode() >= 0xd800 && | |
| 19 | ✗ | view.at(offset + count - 1).unicode() <= 0xdbff && | |
| 20 |
1/4✗ Branch 7 → 8 not taken.
✓ Branch 7 → 21 taken 59 times.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 21 not taken.
|
59 | view.at(offset + count).unicode() >= 0xdc00 && |
| 21 |
1/4✗ Branch 19 → 20 not taken.
✗ Branch 19 → 21 not taken.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 59 times.
|
59 | view.at(offset + count).unicode() <= 0xdfff) --count; |
| 22 |
2/2✓ Branch 25 → 26 taken 59 times.
✓ Branch 26 → 27 taken 59 times.
|
59 | const QByteArray chunk = view.mid(offset, count).toString().toUtf8(); |
| 23 |
1/2✗ Branch 29 → 30 not taken.
✓ Branch 29 → 32 taken 59 times.
|
59 | if (chunk.size() > maxBytes - bytes) return std::nullopt; |
| 24 | 59 | hash.addData(chunk); | |
| 25 | 59 | bytes += chunk.size(); | |
| 26 | 59 | offset += count; | |
| 27 | 59 | } | |
| 28 |
2/2✓ Branch 43 → 44 taken 59 times.
✓ Branch 44 → 45 taken 59 times.
|
59 | return hash.result().toHex(); |
| 29 | 59 | } | |
| 30 | |||
| 31 | } // namespace MailJD | ||
| 32 |