Repository: xbmc/xbmc
File: xbmc/filesystem/bluray/BitReader.h (C/C++)

Code before:
  if (bytes.size() < offset + 2)
    throw std::out_of_range("Not enough bytes to extract a WORD");
  return static_cast<uint16_t>(std::to_integer<uint16_t>(bytes[offset + 1]) |
                               std::to_integer<uint16_t>(bytes[offset]) << 8);
}

constexpr uint8_t GetByte(const std::span<std::byte> bytes, unsigned int offset)
{
  if (bytes.size() < offset + 1)
    throw std::out_of_range("Not enough bytes to extract a BYTE");
  return std::to_integer<uint8_t>(bytes[offset]);
}

inline std::string GetString(const std::span<std::byte> bytes,
                             unsigned int offset,
                             unsigned int length)
{
  if (bytes.size() < offset + length)
    throw std::out_of_range("Not enough bytes to extract a STRING");
  return std::string{reinterpret_cast<const char*>(bytes.data() + offset), length};
}

class BitReader
{
public:
  explicit BitReader(const std::span<std::byte>& buffer)
    : m_data(buffer.data()),
      m_size(static_cast<uint32_t>(buffer.size()))
  {
  }

  uint32_t ReadBits(uint32_t n)
  {
    if (n > 32)
      throw std::out_of_range("Can only read up to 32 bits at a time");

    uint32_t value{0};

    while (n > 0 && m_offset < m_size)
    {
      uint32_t bitsAvailable{8 - m_bitPosition};
      uint32_t bitsToRead{(n < bitsAvailable) ? n : bitsAvailable};

      value = (value << bitsToRead) |
              ((std::to_integer<uint32_t>(m_data[m_offset]) >> (bitsAvailable - bitsToRead)) &
               ((1u << bitsToRead) - 1));

      m_bitPosition += bitsToRead;
      n -= bitsToRead;

      if (m_bitPosition == 8)

Review comment (suggestion):
While you are on it, maybe change this to `std::span<std::byte> buffer`. It's supposed to be a value type like `std::string_view`.

Code after:
  if (bytes.size() < offset + 2)
    throw std::out_of_range("Not enough bytes to extract a WORD");
  return static_cast<uint16_t>(std::to_integer<uint16_t>(bytes[offset + 1]) |
                               std::to_integer<uint16_t>(bytes[offset]) << 8);
}

constexpr uint8_t GetByte(const std::span<std::byte> bytes, unsigned int offset)
{
  if (bytes.size() < offset + 1)
    throw std::out_of_range("Not enough bytes to extract a BYTE");
  return std::to_integer<uint8_t>(bytes[offset]);
}

inline std::string GetString(const std::span<std::byte> bytes,
                             unsigned int offset,
                             unsigned int length)
{
  if (bytes.size() < offset + length)
    throw std::out_of_range("Not enough bytes to extract a STRING");
  return std::string{reinterpret_cast<const char*>(bytes.data() + offset), length};
}

class BitReader
{
public:
  explicit BitReader(std::span<std::byte> buffer)
    : m_data(buffer.data()),
      m_size(static_cast<uint32_t>(buffer.size()))
  {
  }

  uint32_t ReadBits(uint32_t n)
  {
    if (n > 32)
      throw std::out_of_range("Can only read up to 32 bits at a time");

    uint32_t value{0};

    while (n > 0 && m_offset < m_size)
    {
      uint32_t bitsAvailable{8 - m_bitPosition};
      uint32_t bitsToRead{(n < bitsAvailable) ? n : bitsAvailable};

      value = (value << bitsToRead) |
              ((std::to_integer<uint32_t>(m_data[m_offset]) >> (bitsAvailable - bitsToRead)) &
               ((1u << bitsToRead) - 1));

      m_bitPosition += bitsToRead;
      n -= bitsToRead;

      if (m_bitPosition == 8)