Merge branch 'main' into heightmaps

This commit is contained in:
MihailRis
2024-09-12 18:06:23 +03:00
12 changed files with 212 additions and 17 deletions
+56
View File
@@ -0,0 +1,56 @@
#pragma once
#include <memory>
#include <cstring>
namespace util {
template<typename T>
class Buffer {
std::unique_ptr<T[]> ptr;
size_t length;
public:
Buffer(size_t length)
: ptr(std::make_unique<T[]>(length)), length(length) {
}
Buffer(std::unique_ptr<T[]> ptr, size_t length)
: ptr(std::move(ptr)), length(length) {}
Buffer(const T* src, size_t length)
: ptr(std::make_unique<T[]>(length)), length(length) {
std::memcpy(ptr.get(), src, length);
}
T& operator[](long long index) {
return ptr[index];
}
const T& operator[](long long index) const {
return ptr[index];
}
T* data() {
return ptr.get();
}
const T* data() const {
return ptr.get();
}
size_t size() const {
return length;
}
std::unique_ptr<T[]> release() {
return std::move(ptr);
}
Buffer clone() const {
return Buffer(ptr.get(), length);
}
void resizeFast(size_t size) {
length = size;
}
};
}
+4 -4
View File
@@ -319,8 +319,8 @@ std::string util::mangleid(uint64_t value) {
return ss.str();
}
std::vector<ubyte> util::base64_decode(const char* str, size_t size) {
std::vector<ubyte> bytes((size / 4) * 3);
util::Buffer<ubyte> util::base64_decode(const char* str, size_t size) {
util::Buffer<ubyte> bytes((size / 4) * 3);
ubyte* dst = bytes.data();
for (size_t i = 0; i < size;) {
ubyte a = base64_decode_char(ubyte(str[i++]));
@@ -335,12 +335,12 @@ std::vector<ubyte> util::base64_decode(const char* str, size_t size) {
size_t outsize = bytes.size();
if (str[size - 1] == '=') outsize--;
if (str[size - 2] == '=') outsize--;
bytes.resize(outsize);
bytes.resizeFast(outsize);
}
return bytes;
}
std::vector<ubyte> util::base64_decode(const std::string& str) {
util::Buffer<ubyte> util::base64_decode(const std::string& str) {
return base64_decode(str.c_str(), str.size());
}
+3 -2
View File
@@ -4,6 +4,7 @@
#include <vector>
#include "typedefs.hpp"
#include "util/Buffer.hpp"
namespace util {
/// @brief Function used for string serialization in text formats
@@ -56,8 +57,8 @@ namespace util {
std::wstring to_wstring(double x, int precision);
std::string base64_encode(const ubyte* data, size_t size);
std::vector<ubyte> base64_decode(const char* str, size_t size);
std::vector<ubyte> base64_decode(const std::string& str);
util::Buffer<ubyte> base64_decode(const char* str, size_t size);
util::Buffer<ubyte> base64_decode(const std::string& str);
std::string mangleid(uint64_t value);