+ char *strndup(const char *data, size_t size) const {
+ return strmemdup(data, strnlen(data, size));
+ }
+
+ char *strmemdup(const char *data, size_t size) const {
+ char *copy(new char[size + 1]);
+ memcpy(copy, data, size);
+ copy[size] = '\0';
+ return copy;
+ }
+
+ // XXX: this could be made much more efficient
+ __attribute__((__sentinel__))
+ char *strcat(const char *data, ...) {
+ size_t size(strlen(data)); {
+ va_list args;
+ va_start(args, data);
+
+ while (const char *arg = va_arg(args, const char *))
+ size += strlen(arg);
+
+ va_end(args);
+ }
+
+ char *copy(malloc<char>(size + 1)); {
+ va_list args;
+ va_start(args, data);
+
+ size_t offset(strlen(data));
+ memcpy(copy, data, offset);
+
+ while (const char *arg = va_arg(args, const char *)) {
+ size_t size(strlen(arg));
+ memcpy(copy + offset, arg, size);
+ offset += size;
+ }
+
+ va_end(args);
+ }
+
+ copy[size] = '\0';
+ return copy;
+ }
+
+ // XXX: most people using this might should use sprintf
+ char *itoa(long value) {
+ return sprintf(16, "%ld", value);
+ }
+
+ __attribute__((__format__(__printf__, 3, 4)))
+ char *sprintf(size_t size, const char *format, ...) {
+ va_list args;
+ va_start(args, format);
+ char *copy(vsprintf(size, format, args));
+ va_end(args);
+ return copy;
+ }
+
+ char *vsprintf(size_t size, const char *format, va_list args) {
+ va_list copy;
+ va_copy(copy, args);
+ char buffer[size];
+ int writ(vsnprintf(buffer, size, format, copy));
+ va_end(copy);
+ _assert(writ >= 0);
+
+ if (size_t(writ) >= size)
+ return vsprintf(writ + 1, format, args);
+ return strmemdup(buffer, writ);
+ }
+
+ void atexit(void (*code)(void *), void *data = NULL);
+
+ template <typename Type_>
+ Type_ &object() {
+ Type_ *value(new(*this) Type_());
+ atexit(&delete_<Type_>, value);
+ return *value;