summaryrefslogtreecommitdiff
path: root/src/uio.hh
blob: 091d0cef394c087a023a7dcdc97688daefb03534 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef UIO_HH
#define UIO_HH

#include "io.hh"  // IWYU pragma: export

#include <cstddef>
#include <expected>
#include <string>

namespace u {

enum class ReaderInputFormat : uint8_t {
  UTF8,
  UTF16_BE,
  UTF16_LE,
  DETECT,
};

struct ReaderConfig {
  // If false (default), invalid data is replaced with U+FFFD
  bool strict{false};
  // Input format
  ReaderInputFormat input{ReaderInputFormat::DETECT};
  // If true (default), any BOM found at start of stream will be skipped
  bool skip_bom{true};
};

}  // namespace u

namespace u8 {

class Reader : public io::Reader {
 public:
  using io::Reader::read;
  using io::Reader::repeat_read;

  [[nodiscard]] std::expected<size_t, io::ReadError> read(std::string& data,
                                                          size_t max);

  [[nodiscard]] std::expected<size_t, io::ReadError> repeat_read(
      std::string& data, size_t max);
};

[[nodiscard]] std::unique_ptr<Reader> open(std::unique_ptr<io::Reader> reader,
                                           u::ReaderConfig config = {});

}  // namespace u8

namespace u16 {

class Reader : public io::Reader {
 public:
  using io::Reader::read;
  using io::Reader::repeat_read;

  [[nodiscard]] std::expected<size_t, io::ReadError> read(std::u16string& data,
                                                          size_t max);

  [[nodiscard]] std::expected<size_t, io::ReadError> repeat_read(
      std::u16string& data, size_t max);
};

[[nodiscard]] std::unique_ptr<Reader> open(std::unique_ptr<io::Reader> reader,
                                           u::ReaderConfig config = {});

}  // namespace u16

#endif  // UIO_HH