Skip to content

Instantly share code, notes, and snippets.

@nilium
Created April 15, 2014 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilium/10787718 to your computer and use it in GitHub Desktop.
Save nilium/10787718 to your computer and use it in GitHub Desktop.
UTF8 bits stuff.. not entirely sure what the point of this was.
template <int OCTETS_>
struct utf8_bits_t
{
enum : int
{
OCTETS = OCTETS_,
OFFSET = 6 * (OCTETS - 1),
};
enum : uint32_t
{
MASK = (0xF8u << (4 - OCTETS)) & 0xFFu,
VALUE_MASK = (~MASK) & 0xFFu,
NAME = (MASK << 1) & 0xFFu,
};
uint8_t octet(uint32_t code, int nth)
{
if (nth == 0) {
return static_cast<uint8_t>(NAME | ((code >> OFFSET) & VALUE_MASK));
} else {
code >>= (OCTETS - 1) - nth;
code &= UTF8_VAL_MASK_INTERMEDIATE;
code |= UTF8_NAME_INTERMEDIATE;
return static_cast<uint8_t>(code);
}
}
constexpr utf8_bits_t() {}
constexpr uint32_t value_mask() const { return VALUE_MASK; }
constexpr uint32_t mask() const { return MASK; }
constexpr uint32_t name() const { return name; }
constexpr int octets() const { return octets; }
constexpr int offset() const { return offset; }
};
using utf8_bits_0 = utf8_bits_t<0>;
using utf8_bits_1 = utf8_bits_t<1>;
using utf8_bits_2 = utf8_bits_t<2>;
using utf8_bits_3 = utf8_bits_t<3>;
using utf8_bits_4 = utf8_bits_t<4>;
using utf8_bits_intermediate = utf8_bits_1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment