Added a few operators to String to simplify mixing it with std::strings.

This commit is contained in:
Relintai 2021-10-31 23:55:44 +01:00
parent 7ef1c76e4a
commit 2079d5bd6e
2 changed files with 75 additions and 0 deletions

View File

@ -407,6 +407,16 @@ void String::append_str(const String &other) {
_data[_size] = '\0';
}
void String::append_str(const std::string &str) {
ensure_capacity(_size + str.size() + 1); // +1 for the null terminator
for (int i = 0; i < str.size(); ++i) {
_data[_size++] = str[i];
}
_data[_size] = '\0';
}
float String::to_float() {
return atof(c_str());
}
@ -423,6 +433,10 @@ uint32_t String::to_uint() {
return static_cast<uint32_t>(atoll(c_str()));
}
std::string String::to_string() {
return std::string(c_str());
}
char *String::c_str() {
return _data;
}
@ -473,6 +487,11 @@ String &String::operator+=(const char *p_c_str) {
return *this;
}
String &String::operator+=(const std::string &b) {
append_str(b);
return *this;
}
String operator+(String lhs, const String &rhs) {
lhs += rhs;
@ -491,6 +510,14 @@ String operator+(String lhs, const char rhs) {
return lhs;
}
String operator+(String lhs, const std::string &rhs) {
lhs += rhs;
return lhs;
}
bool operator==(const String &a, const String &b) {
if (a._size != b._size) {
return false;
@ -551,6 +578,32 @@ bool operator!=(const char *b, const String &a) {
return !(a == b);
}
bool operator==(const String &a, std::string &b) {
if (a._size != b.size()) {
return false;
}
char *bp = &b[0];
for (int i = 0; i < a._size; ++i) {
if (a[i] != bp[i]) {
return false;
}
}
return true;
}
bool operator!=(const String &a, std::string &b) {
return !(a == b);
}
bool operator==(std::string &b, const String &a) {
return (a == b);
}
bool operator!=(std::string &b, const String &a) {
return !(a == b);
}
String::String() {
_data = nullptr;
_actual_size = 0;
@ -630,6 +683,15 @@ String::String(int prealloc, int grow_by) {
ensure_capacity(prealloc);
}
String::String(const std::string &str) {
_data = nullptr;
_actual_size = 0;
_size = 0;
_grow_by = 100;
append_str(str);
}
String::~String() {
if (_data) {
delete[] _data;

View File

@ -3,6 +3,8 @@
#include <inttypes.h>
#include <string>
class String {
public:
void push_back(const char element);
@ -50,11 +52,13 @@ public:
void append_str(const char* str);
void append_str(const String &other);
void append_str(const std::string &str);
float to_float();
double to_double();
int to_int();
uint32_t to_uint();
std::string to_string();
char *c_str();
const char *c_str() const;
@ -68,10 +72,12 @@ public:
String &operator+=(const String &b);
String &operator+=(const char chr);
String &operator+=(const char *p_c_str);
String &operator+=(const std::string &b);
friend String operator+(String lhs, const String &rhs);
friend String operator+(String lhs, const char *rhs);
friend String operator+(String lhs, const char rhs);
friend String operator+(String lhs, const std::string &rhs);
friend bool operator==(const String &a, const String &b);
friend bool operator!=(const String &a, const String &b);
@ -82,6 +88,12 @@ public:
friend bool operator==(const char *b, const String &a);
friend bool operator!=(const char *b, const String &a);
friend bool operator==(const String &a, std::string &b);
friend bool operator!=(const String &a, std::string &b);
friend bool operator==(std::string &b, const String &a);
friend bool operator!=(std::string &b, const String &a);
String();
String(const String &other);
String(const String &other, const int grow_by);
@ -89,6 +101,7 @@ public:
String(const char* p_c_str, const int grow_by);
String(const int prealloc);
String(const int prealloc, const int grow_by);
String(const std::string &str);
~String();
private: