32 String() noexcept : data_(
nullptr), size_(0), capacity_(0) {}
36 String(
const char* str,
size_t len) : size_(len), capacity_(len + 1) {
37 data_ =
new char[capacity_];
38 memcpy(data_, str, len);
42 String(
const String& other) : size_(other.size_), capacity_(other.capacity_) {
43 data_ =
new char[capacity_];
44 memcpy(data_, other.data_, size_ + 1);
47 String(
String&& other) noexcept : data_(other.data_), size_(other.size_), capacity_(other.capacity_) {
48 other.data_ =
nullptr;
61 if (capacity_ < other.size_ + 1) {
63 capacity_ = other.capacity_;
64 data_ =
new char[capacity_];
67 memcpy(data_, other.data_, size_ + 1);
77 capacity_ = other.capacity_;
78 other.data_ =
nullptr;
101 size_t size()
const {
return size_; }
104 bool empty()
const {
return size_ == 0; }
112 void resize(
size_t newSize,
char ch =
'\0') {
113 if (newSize > capacity_) {
116 if (newSize > size_) {
117 memset(data_ + size_, ch, newSize - size_);
124 if (newCapacity > capacity_) {
125 char* newData =
new char[newCapacity];
126 memcpy(newData, data_, size_ + 1);
129 capacity_ = newCapacity;
134 return append(other.data_, other.size_);
138 return append(str, strlen(str));
142 if (size_ + len > capacity_) {
145 memcpy(data_ + size_, str, len);
152 const char*
c_str()
const {
return data_; }
155 return strcmp(data_, other.data_);
159 const char* result = strstr(data_ + pos, str.data_);
160 return result ? result - data_ : npos;
167 len = std::min(len, size_ - pos);
168 return String(data_ + pos, len);
236 static const size_t npos =
static_cast<size_t>(-1);
int compare(const String &other) const
size_t find(const String &str, size_t pos=0) const
const char & operator[](size_t index) const
const char * c_str() const
friend bool operator>(const String &lhs, const String &rhs)
String & operator=(const String &other)
char & operator[](size_t index)
String & append(const char *str, size_t len)
String(const char *str, size_t len)
String(String &&other) noexcept
~String()
Frees the backing buffer.
friend String operator+(const String &lhs, const String &rhs)
String & append(const char *str)
friend bool operator<=(const String &lhs, const String &rhs)
void reserve(size_t newCapacity)
friend bool operator==(const String &lhs, const String &rhs)
friend bool operator>=(const String &lhs, const String &rhs)
String & operator+=(const char *str)
friend String operator+(const String &lhs, const char *rhs)
String(const String &other)
String substr(size_t pos=0, size_t len=npos) const
friend bool operator!=(const String &lhs, const String &rhs)
friend std::ostream & operator<<(std::ostream &os, const String &str)
String & operator+=(const String &other)
String & append(const String &other)
friend bool operator<(const String &lhs, const String &rhs)
void resize(size_t newSize, char ch='\0')
friend String operator+(const char *lhs, const String &rhs)
String & operator+=(char ch)
String & operator=(String &&other) noexcept
Root namespace for everything the engine exposes.