Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarByteArray.hpp
blob: 9b3e410c1f06d763d5fe1ba0328382e8d5f1c832 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#pragma once

#include "StarHash.hpp"
#include "StarException.hpp"
#include "StarFormat.hpp"

namespace Star {

STAR_CLASS(ByteArray);

// Class to hold an array of bytes.  Contains an internal buffer that may be
// larger than what is reported by size(), to avoid repeated allocations when a
// ByteArray grows.
class ByteArray {
public:
  typedef char value_type;
  typedef char* iterator;
  typedef char const* const_iterator;

  // Constructs a byte array from a given c string WITHOUT including the
  // trailing '\0'
  static ByteArray fromCString(char const* str);
  // Same, but includes the trailing '\0'
  static ByteArray fromCStringWithNull(char const* str);
  static ByteArray withReserve(size_t capacity);

  ByteArray();
  ByteArray(size_t dataSize, char c);
  ByteArray(char const* data, size_t dataSize);
  ByteArray(ByteArray const& b);
  ByteArray(ByteArray&& b) noexcept;
  ~ByteArray();

  ByteArray& operator=(ByteArray const& b);
  ByteArray& operator=(ByteArray&& b) noexcept;

  char const* ptr() const;
  char* ptr();

  size_t size() const;
  // Maximum size before realloc
  size_t capacity() const;
  // Is zero size
  bool empty() const;

  // Sets size to 0.
  void clear();
  // Clears and resets buffer to empty.
  void reset();

  void reserve(size_t capacity);

  void resize(size_t size);
  // resize, filling new space with given byte if it exists.
  void resize(size_t size, char f);

  // fill array with byte.
  void fill(char c);
  // fill array and resize to new size.
  void fill(size_t size, char c);

  void append(ByteArray const& b);
  void append(char const* data, size_t len);
  void appendByte(char b);

  void copyTo(char* data, size_t len) const;
  void copyTo(char* data) const;

  // Copy from ByteArray starting at pos, to data, with size len.
  void copyTo(char* data, size_t pos, size_t len) const;
  // Copy from data pointer to ByteArray at pos with size len.
  // Resizes if needed.
  void writeFrom(char const* data, size_t pos, size_t len);

  ByteArray sub(size_t b, size_t s) const;
  ByteArray left(size_t s) const;
  ByteArray right(size_t s) const;

  void trimLeft(size_t s);
  void trimRight(size_t s);

  // returns location of first character that is different than the given
  // ByteArray.
  size_t diffChar(ByteArray const& b) const;
  // returns -1 if this < b, 0 if this == b, 1 if this > b
  int compare(ByteArray const& b) const;

  template <typename Combiner>
  ByteArray combineWith(Combiner&& combine, ByteArray const& rhs, bool extend = false);

  ByteArray andWith(ByteArray const& rhs, bool extend = false);
  ByteArray orWith(ByteArray const& rhs, bool extend = false);
  ByteArray xorWith(ByteArray const& rhs, bool extend = false);

  iterator begin();
  iterator end();

  const_iterator begin() const;
  const_iterator end() const;

  void insert(size_t pos, char byte);
  iterator insert(const_iterator pos, char byte);
  void push_back(char byte);

  char& operator[](size_t i);
  char operator[](size_t i) const;
  char at(size_t i) const;

  bool operator<(ByteArray const& b) const;
  bool operator==(ByteArray const& b) const;
  bool operator!=(ByteArray const& b) const;

private:
  char* m_data;
  size_t m_capacity;
  size_t m_size;
};

template <>
struct hash<ByteArray> {
  size_t operator()(ByteArray const& b) const;
};

std::ostream& operator<<(std::ostream& os, ByteArray const& b);

inline void ByteArray::clear() {
  resize(0);
}

inline void ByteArray::resize(size_t size) {
  reserve(size);
  m_size = size;
}

inline void ByteArray::append(ByteArray const& b) {
  append(b.ptr(), b.size());
}

inline void ByteArray::append(const char* data, size_t len) {
  resize(m_size + len);
  std::memcpy(m_data + m_size - len, data, len);
}

inline void ByteArray::appendByte(char b) {
  resize(m_size + 1);
  m_data[m_size - 1] = b;
}

inline bool ByteArray::empty() const {
  return m_size == 0;
}

inline char const* ByteArray::ptr() const {
  return m_data;
}

inline char* ByteArray::ptr() {
  return m_data;
}

inline size_t ByteArray::size() const {
  return m_size;
}

inline size_t ByteArray::capacity() const {
  return m_capacity;
}

inline void ByteArray::copyTo(char* data, size_t len) const {
  len = min(m_size, len);
  std::memcpy(data, m_data, len);
}

inline void ByteArray::copyTo(char* data) const {
  copyTo(data, m_size);
}

inline void ByteArray::copyTo(char* data, size_t pos, size_t len) const {
  if (len == 0 || pos >= m_size)
    return;

  len = min(m_size - pos, len);
  std::memcpy(data, m_data + pos, len);
}

inline void ByteArray::writeFrom(const char* data, size_t pos, size_t len) {
  if (pos + len > m_size)
    resize(pos + len);

  std::memcpy(m_data + pos, data, len);
}

template <typename Combiner>
ByteArray ByteArray::combineWith(Combiner&& combine, ByteArray const& rhs, bool extend) {
  ByteArray const* smallerArray = &rhs;
  ByteArray const* largerArray = this;

  if (m_size < rhs.size())
    swap(smallerArray, largerArray);

  ByteArray res;
  res.resize(smallerArray->size());

  for (size_t i = 0; i < smallerArray->size(); ++i)
    res[i] = combine((*smallerArray)[i], (*largerArray)[i]);

  if (extend) {
    res.resize(largerArray->size());
    for (size_t i = smallerArray->size(); i < largerArray->size(); ++i)
      res[i] = (*largerArray)[i];
  }

  return res;
}

inline ByteArray::iterator ByteArray::begin() {
  return m_data;
}

inline ByteArray::iterator ByteArray::end() {
  return m_data + m_size;
}

inline ByteArray::const_iterator ByteArray::begin() const {
  return m_data;
}

inline ByteArray::const_iterator ByteArray::end() const {
  return m_data + m_size;
}

inline char& ByteArray::operator[](size_t i) {
  starAssert(i < m_size);
  return m_data[i];
}

inline char ByteArray::operator[](size_t i) const {
  starAssert(i < m_size);
  return m_data[i];
}

inline char ByteArray::at(size_t i) const {
  if (i >= m_size)
    throw OutOfRangeException(strf("Out of range in ByteArray::at({})", i));

  return m_data[i];
}

inline size_t hash<ByteArray>::operator()(ByteArray const& b) const {
  PLHasher hash;
  for (size_t i = 0; i < b.size(); ++i)
    hash.put(b[i]);
  return hash.hash();
}

}

template <> struct fmt::formatter<Star::ByteArray> : ostream_formatter {};