summaryrefslogtreecommitdiff
path: root/utf/src/utf32.cc
blob: e33b0b489840dc98b3f06bf16dfa2d0c2dfa9b3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "utf32.hh"

#include "utf_error.hh"

namespace utf {

namespace {

inline bool valid_codepoint(uint32_t c) {
  return (c < 0xd800) || (c > 0xdfff && c <= 0x10ffff);
}

}  // namespace

uint32_t read32be(std::span<uint8_t const> data, std::size_t& offset) {
  if (offset > data.size() || data.size() - offset < 4)
    return NEED_MORE;
  uint32_t c = static_cast<uint32_t>(data[offset]) << 24
               | static_cast<uint32_t>(data[offset + 1] & 0xff) << 16
               | static_cast<uint32_t>(data[offset + 2] & 0xff) << 8
               | static_cast<uint32_t>(data[offset + 3] & 0xff);
  if (valid_codepoint(c)) {
    offset += 4;
    return c;
  }
  return INVALID;
}

uint32_t read32le(std::span<uint8_t const> data, std::size_t& offset) {
  if (offset > data.size() || data.size() - offset < 4)
    return NEED_MORE;
  uint32_t c = static_cast<uint32_t>(data[offset + 3]) << 24
               | static_cast<uint32_t>(data[offset + 2] & 0xff) << 16
               | static_cast<uint32_t>(data[offset + 1] & 0xff) << 8
               | static_cast<uint32_t>(data[offset] & 0xff);
  if (valid_codepoint(c)) {
    offset += 4;
    return c;
  }
  return INVALID;
}

}  // namespace utf