tudocomp
– The TU Dortmund Compression Framework
InputRestrictions.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <tudocomp/util.hpp>
4 
5 namespace tdc {namespace io {
11  std::vector<uint8_t> m_escape_bytes;
12  bool m_null_terminate;
13 
14  inline void sort_and_dedup() {
15  std::sort(m_escape_bytes.begin(), m_escape_bytes.end());
16  m_escape_bytes.erase(std::unique(m_escape_bytes.begin(), m_escape_bytes.end()), m_escape_bytes.end());
17  }
18 
19  friend inline InputRestrictions operator|(const InputRestrictions& a,
20  const InputRestrictions& b);
21 
22  public:
23  inline InputRestrictions(const std::vector<uint8_t>& escape_bytes = {},
24  bool null_terminate = false):
25  m_escape_bytes(escape_bytes),
26  m_null_terminate(null_terminate) {
27  sort_and_dedup();
28  }
29 
30  inline const std::vector<uint8_t>& escape_bytes() const {
31  return m_escape_bytes;
32  }
33 
34  inline bool null_terminate() const {
35  return m_null_terminate;
36  }
37 
38  inline bool has_no_escape_restrictions() const {
39  return m_escape_bytes.empty();
40  }
41 
42  inline bool has_no_restrictions() const {
43  return has_no_escape_restrictions() && (m_null_terminate == false);
44  }
45 
46  inline bool has_escape_restrictions() const {
48  }
49 
50  inline bool has_restrictions() const {
51  return !has_no_restrictions();
52  }
53  };
54 
55  inline std::ostream& operator<<(std::ostream& o,
56  const InputRestrictions& v) {
57  o << "{ escape_bytes: " << vec_to_debug_string(v.escape_bytes())
58  << ", null_termination: " << (v.null_terminate() ? "true" : "false")
59  << " }";
60  return o;
61  }
62 
65  // Yes, kind of overkill here...
66 
67  std::vector<uint8_t> merged;
68 
69  merged.insert(merged.end(), a.escape_bytes().begin(), a.escape_bytes().end());
70  merged.insert(merged.end(), b.escape_bytes().begin(), b.escape_bytes().end());
71 
72  auto r = InputRestrictions {
73  merged,
74  a.null_terminate() || b.null_terminate(),
75  };
76 
77  r.sort_and_dedup();
78 
79  return r;
80  }
81 
84  a = a | b;
85  return a;
86  }
87 
88  inline bool operator==(const InputRestrictions& lhs,
89  const InputRestrictions& rhs) {
90  return lhs.escape_bytes() == rhs.escape_bytes()
91  && lhs.null_terminate() == rhs.null_terminate();
92  }
93 
94  inline bool operator!=(const InputRestrictions& lhs,
95  const InputRestrictions& rhs) {
96  return !(lhs == rhs);
97  }
98 }}
bool operator==(const InputRestrictions &lhs, const InputRestrictions &rhs)
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
std::string vec_to_debug_string(const T &s, size_t indent=0)
Builds the string representation of a vector of byte values, sorrounded by square brackets ([ and ])...
InputRestrictions & operator|=(InputRestrictions &a, const InputRestrictions &b)
Merges two InpuTrestrictions to a combined set of restrictions.
Describes a set of restrictions placed on input data.
std::ostream & operator<<(std::ostream &o, const InputRestrictions &v)
bool operator!=(const InputRestrictions &lhs, const InputRestrictions &rhs)
const std::vector< uint8_t > & escape_bytes() const
InputRestrictions(const std::vector< uint8_t > &escape_bytes={}, bool null_terminate=false)
friend InputRestrictions operator|(const InputRestrictions &a, const InputRestrictions &b)
Merges two InpuTrestrictions to a combined set of restrictions.