2022-10-30 14:24:06 -04:00
|
|
|
#pragma once
|
|
|
|
|
2022-11-22 07:59:46 -05:00
|
|
|
#include <Geode/binding/GDString.hpp>
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <string>
|
2022-11-22 07:59:46 -05:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <utility>
|
|
|
|
#include <map>
|
2022-10-30 14:59:20 -04:00
|
|
|
#include <vector>
|
2022-10-30 14:24:06 -04:00
|
|
|
|
|
|
|
namespace gd {
|
2022-10-30 14:59:20 -04:00
|
|
|
struct InternalString {
|
|
|
|
union {
|
|
|
|
char m_storage[16];
|
|
|
|
char* m_pointer;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t m_length;
|
|
|
|
size_t m_capacity;
|
|
|
|
};
|
|
|
|
|
2022-11-22 07:59:46 -05:00
|
|
|
class string : GDString {
|
2022-10-30 14:59:20 -04:00
|
|
|
private:
|
|
|
|
InternalString m_data;
|
|
|
|
|
2022-11-22 07:59:46 -05:00
|
|
|
char* get_data() {
|
|
|
|
if (m_data.m_capacity < 16) return m_data.m_storage;
|
|
|
|
return m_data.m_pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* get_data() const {
|
|
|
|
if (m_data.m_capacity < 16) return m_data.m_storage;
|
|
|
|
return m_data.m_pointer;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
public:
|
|
|
|
template <class... Params>
|
|
|
|
string(Params&&... params) {
|
|
|
|
m_data.m_pointer = 0;
|
|
|
|
m_data.m_length = 0;
|
|
|
|
m_data.m_capacity = 15;
|
|
|
|
|
|
|
|
auto val = std::string(std::forward<Params>(params)...);
|
|
|
|
(void)this->winAssign(val.c_str(), val.size());
|
|
|
|
}
|
|
|
|
|
2022-11-22 08:46:03 -05:00
|
|
|
template <class Param>
|
|
|
|
string& operator=(Params&& param) {
|
|
|
|
std::string val;
|
|
|
|
val = std::forward<Params>(param);
|
|
|
|
(void)this->winAssign(val.c_str(), val.size());
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:59:20 -04:00
|
|
|
~string() {
|
|
|
|
(void)this->winDtor();
|
|
|
|
}
|
|
|
|
|
|
|
|
char& at(size_t pos) {
|
|
|
|
if (pos >= m_data.m_length) throw std::out_of_range("gd::string::at");
|
2022-11-22 07:59:46 -05:00
|
|
|
return (*this)[pos];
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char const& at(size_t pos) const {
|
|
|
|
if (pos >= m_data.m_length) throw std::out_of_range("gd::string::at");
|
2022-11-22 07:59:46 -05:00
|
|
|
return (*this)[pos];
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char& operator[](size_t pos) {
|
2022-11-22 07:59:46 -05:00
|
|
|
return this->get_data()[pos];
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char const& operator[](size_t pos) const {
|
2022-11-22 07:59:46 -05:00
|
|
|
return this->get_data()[pos];
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char* data() {
|
2022-11-22 07:59:46 -05:00
|
|
|
return this->get_data();
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char const* data() const {
|
2022-11-22 07:59:46 -05:00
|
|
|
return this->get_data();
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char const* c_str() const {
|
2022-11-22 07:59:46 -05:00
|
|
|
return this->get_data();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const {
|
|
|
|
return m_data.m_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator std::string() const {
|
|
|
|
return std::string(this->c_str(), this->size());
|
2022-10-30 14:59:20 -04:00
|
|
|
}
|
|
|
|
};
|
2022-10-30 14:24:06 -04:00
|
|
|
|
2022-11-22 07:59:46 -05:00
|
|
|
template <class T>
|
|
|
|
using vector = std::vector<T>;
|
|
|
|
|
|
|
|
template <class K, class V>
|
|
|
|
using map = std::map<K, V>;
|
|
|
|
}
|