2023-10-09 17:24:40 +00:00
|
|
|
//
|
|
|
|
// Created by kyle on 2023-10-09.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef KGE_BUFFER_H
|
|
|
|
#define KGE_BUFFER_H
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
|
|
|
|
namespace klib {
|
|
|
|
|
|
|
|
|
|
|
|
class Buffer {
|
|
|
|
public:
|
|
|
|
Buffer();
|
2023-10-09 19:45:19 +00:00
|
|
|
explicit Buffer(size_t);
|
|
|
|
explicit Buffer(const char *);
|
|
|
|
~Buffer() { this->Reclaim(); }
|
2023-10-09 17:24:40 +00:00
|
|
|
|
|
|
|
uint8_t *Contents() { return this->contents; }
|
2023-10-09 19:45:19 +00:00
|
|
|
size_t Length() const { return this->length; };
|
|
|
|
size_t Capacity() const { return this->capacity; }
|
2023-10-09 17:24:40 +00:00
|
|
|
|
2023-10-09 19:45:19 +00:00
|
|
|
bool Append(const char *s);
|
2023-10-09 17:24:40 +00:00
|
|
|
bool Append(uint8_t *data, size_t datalen);
|
|
|
|
bool Append(uint8_t c);
|
2023-10-09 19:45:19 +00:00
|
|
|
|
|
|
|
bool Insert(size_t index, const char *s);
|
2023-10-09 17:24:40 +00:00
|
|
|
bool Insert(size_t index, uint8_t *data, size_t datalen);
|
|
|
|
bool Insert(size_t index, uint8_t c);
|
|
|
|
|
2023-10-09 19:45:19 +00:00
|
|
|
bool Remove(size_t index, size_t count);
|
|
|
|
bool Remove(size_t index); // remove single char
|
2023-10-09 17:24:40 +00:00
|
|
|
|
|
|
|
/* memory management */
|
|
|
|
void Resize(size_t newCapacity);
|
|
|
|
size_t Trim();
|
|
|
|
void Clear();
|
|
|
|
void Reclaim();
|
|
|
|
|
2023-10-09 19:45:19 +00:00
|
|
|
uint8_t &operator[](size_t index);
|
2023-10-09 17:24:40 +00:00
|
|
|
private:
|
2023-10-09 22:23:23 +00:00
|
|
|
size_t mustGrow(size_t delta) const;
|
2023-10-09 19:45:19 +00:00
|
|
|
bool shiftRight(size_t offset, size_t delta);
|
|
|
|
bool shiftLeft(size_t offset, size_t delta);
|
2023-10-09 17:24:40 +00:00
|
|
|
|
|
|
|
uint8_t *contents;
|
|
|
|
size_t length;
|
|
|
|
size_t capacity;
|
|
|
|
};
|
|
|
|
|
2023-10-09 19:45:19 +00:00
|
|
|
|
|
|
|
|
2023-10-09 17:24:40 +00:00
|
|
|
} // namespace klib
|
|
|
|
|
|
|
|
#endif //KGE_BUFFER_H
|