scsl  1.1.1
Shimmering Clarity Standard Library
Buffer.h
Go to the documentation of this file.
1 
27 #ifndef KGE_BUFFER_H
28 #define KGE_BUFFER_H
29 
30 #include <cstdint>
31 #include <iostream>
32 
33 
34 namespace scsl {
35 
48 class Buffer {
49 public:
51  Buffer();
52 
57  explicit Buffer(size_t initialCapacity);
58 
60  explicit Buffer(const char *s);
61 
63  explicit Buffer(const std::string& s);
64 
66 
68  uint8_t *Contents() const;
69 
70  std::string ToString() const;
71 
75  size_t Length() const;
76 
79  size_t Capacity() const;
80 
85  bool Append(const char *s);
86 
91  bool Append(const std::string &s);
92 
98  bool Append(const uint8_t *data, const size_t datalen);
99 
104  bool Append(const uint8_t c);
105 
115  bool Insert(const size_t index, const char *s);
116 
126  bool Insert(const size_t index, const std::string &s);
127 
138  bool
139  Insert(const size_t index, const uint8_t *data, const size_t datalen);
140 
150  bool Insert(const size_t index, const uint8_t c);
151 
157  bool Remove(const size_t index, const size_t count);
158 
163  bool Remove(size_t index); // remove single char
164 
165  /* memory management */
166 
173  void Resize(size_t newCapacity);
174 
178  size_t Trim();
179 
183 
187 
192 
195  void Clear();
196 
199  void Reclaim();
200 
205  void HexDump(std::ostream &os);
206 
215  uint8_t &operator[](size_t index);
216 
220  friend bool operator==(const Buffer &lhs, const Buffer &rhs);
221 
222 private:
223  size_t mustGrow(size_t delta) const;
224 
225  bool shiftRight(size_t offset, size_t delta);
226 
227  bool shiftLeft(size_t offset, size_t delta);
228 
229  uint8_t *contents;
230  size_t length;
231  size_t capacity;
232  bool autoTrim;
233 };
234 
236 std::ostream &operator<<(std::ostream &os, const Buffer &buf);
237 
240 inline bool operator!=(const Buffer &lhs, const Buffer &rhs) { return !(lhs == rhs); };
241 
242 } // namespace scsl
243 
244 
245 #endif // KGE_BUFFER_H
Basic line buffer.
Definition: Buffer.h:48
size_t Capacity() const
Return the amount of memory allocated for the Buffer.
void HexDump(std::ostream &os)
Buffer(const char *s)
Construct with a C-style string.
std::string ToString() const
Buffer()
Construct an empty buffer with no memory allocated.
void EnableAutoTrim()
Buffer(const std::string &s)
\buffer Construct with an initial string.
size_t Length() const
The length of data stored in the buffer.
friend bool operator==(const Buffer &lhs, const Buffer &rhs)
bool Append(const uint8_t *data, const size_t datalen)
Append a byte buffer to the end of the buffer.
bool Append(const uint8_t c)
Append a single character to the end of the buffer.
uint8_t & operator[](size_t index)
bool Append(const std::string &s)
bool Insert(const size_t index, const std::string &s)
Insert a string into the buffer at index.
size_t Trim()
Resize the Buffer capacity based on its length.
void Resize(size_t newCapacity)
Changes the capacity of the buffer to newCapacity.
uint8_t * Contents() const
Retrieve the buffer's contents.
void DisableAutoTrim()
Buffer(size_t initialCapacity)
bool Append(const char *s)
Append a C-style string to the end of the buffer.
bool Remove(const size_t index, const size_t count)
Remove count bytes from the buffer at index.
bool AutoTrimIsEnabled()
bool Remove(size_t index)
Remove removes a single byte from the buffer.
bool Insert(const size_t index, const uint8_t *data, const size_t datalen)
Insert a uint8_t buffer into the buffer at index.
bool Insert(const size_t index, const char *s)
Insert a C-style string into the buffer at index.
void Reclaim()
bool Insert(const size_t index, const uint8_t c)
Insert a character into the buffer at index.
scsl is the top-level namespace containing all the code in this library.
Definition: scsl.h:43
std::ostream & operator<<(std::ostream &os, Arena &arena)
bool operator!=(const Buffer &lhs, const Buffer &rhs)
Definition: Buffer.h:240