diff --git a/include/blt/std/format.h b/include/blt/std/format.h index cb28884..0fdd37a 100755 --- a/include/blt/std/format.h +++ b/include/blt/std/format.h @@ -11,6 +11,7 @@ #include #include #include +#include namespace blt::string { @@ -63,14 +64,13 @@ namespace blt::string { } struct utf8_string { - char* characters; - unsigned int size; + blt::scoped_buffer characters; }; // taken from java, adapted for c++. static inline utf8_string createUTFString(const std::string& str) { - const unsigned int strlen = (unsigned int) str.size(); + const auto strlen = (unsigned int) str.size(); unsigned int utflen = strlen; for (unsigned int i = 0; i < strlen; i++) { @@ -82,9 +82,7 @@ namespace blt::string { if (utflen > 65535 || /* overflow */ utflen < strlen) throw "UTF Error"; - utf8_string chars{}; - chars.size = utflen + 2; - chars.characters = new char[chars.size]; + utf8_string chars{scoped_buffer{utflen + 2}}; int count = 0; chars.characters[count++] = (char) ((utflen >> 0) & 0xFF); diff --git a/include/blt/std/memory.h b/include/blt/std/memory.h index 055b495..be494a1 100755 --- a/include/blt/std/memory.h +++ b/include/blt/std/memory.h @@ -73,13 +73,23 @@ namespace blt { _buffer = new T[size]; } - scoped_buffer(scoped_buffer& copy) = delete; + scoped_buffer(const scoped_buffer& copy) = delete; - scoped_buffer(scoped_buffer&& move) = delete; + scoped_buffer(scoped_buffer&& move) noexcept { + _buffer = move._buffer; + _size = move.size(); + move._buffer = nullptr; + } scoped_buffer operator=(scoped_buffer& copyAssignment) = delete; - scoped_buffer operator=(scoped_buffer&& moveAssignment) = delete; + scoped_buffer& operator=(scoped_buffer&& moveAssignment) noexcept { + _buffer = moveAssignment._buffer; + _size = moveAssignment.size(); + moveAssignment._buffer = nullptr; + + return *this; + } inline T& operator[](unsigned long index) { return _buffer[index];