#ifndef INCLUDED_BUFFER_H
#define INCLUDED_BUFFER_H
+#include <cstdarg>
+#include <cstdio>
+#include <cstring>
+
namespace u {
using byte = char;
return this->data();
}
- operator byte *() {
+ operator cstring() const {
+ return c_str();
+ }
+
+ std::size_t strlen() const {
+ return ::strlen(c_str());
+ }
+
+ byte *mut() {
return this->data();
}
+
+// operator byte *() {
+// return mut();
+// }
+
+ void terminate(long offset = -1) {
+ if (offset < 0) {
+ offset += this->size();
+ }
+ mut()[offset] = '\0';
+ }
+
+ void copy(cstring str, unsigned int offset = 0, unsigned int limit = 0) {
+ if (!limit) {
+ limit = this->size() - offset;
+ }
+ strncpy(mut() + offset, str, limit);
+ }
+
+ __attribute__((format(printf, 2, 3)))
+ void sprintf(const char *format, ...) {
+ // todo: check for overflow
+ va_list argptr;
+ va_start(argptr, format);
+ vsprintf(this->data(), format, argptr);
+ va_end(argptr);
+ }
};
template<int sz>