Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make utf8_length_from_utf32 branchless. #489

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions src/ada_idna.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,10 @@ size_t utf8_length_from_utf32(const char32_t* buf, size_t len) {
const uint32_t* p = reinterpret_cast<const uint32_t*>(buf);
size_t counter{0};
for (size_t i = 0; i < len; i++) {
/** ASCII **/
if (p[i] <= 0x7F) {
counter++;
}
/** two-byte **/
else if (p[i] <= 0x7FF) {
counter += 2;
}
/** three-byte **/
else if (p[i] <= 0xFFFF) {
counter += 3;
}
/** four-bytes **/
else {
counter += 4;
}
++counter; // ASCII
counter += static_cast<size_t>(p[i] > 0x7F); // two-byte
counter += static_cast<size_t>(p[i] > 0x7FF); // three-byte
counter += static_cast<size_t>(p[i] > 0xFFFF); // four-bytes
}
return counter;
}
Expand Down
Loading