Write a branchless version of this function: ~~~ #include <cstdint> namespace…
- User
Write a branchless version of this function:
#include <cstdint> namespace utf8_constants { inline constexpr std::uint32_t max_unicode = 0x10FFFF; inline constexpr std::uint32_t surrogate_start = 0xD800; inline constexpr std::uint32_t surrogate_end = 0xDFFF; } // Semantic check for overlong sequences, surrogates, and out-of-range values. // Called only when a utf-8 sequence length is > 1 constexpr bool is_valid_utf8_code_point(std::uint32_t code_point, int sequence_len) noexcept { assert(sequence_len >= 2 && sequence_len <= 4); if (code_point < 0x80) { return false; // overlong 2,3,4 byte ASCII } else if (code_point < 0x800) { if (sequence_len > 2) return false; // overlong 3,4 byte } else if (code_point < 0x10000) { if (sequence_len == 4) return false;// overlong 4 byte if (code_point >= utf8_constants::surrogate_start && code_point <= utf8_constants::surrogate_end) return false; // surrogate range } else if (code_point > utf8_constants::max_unicode) { return false; // exceeding max allowed } return true; } Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.