From 2079d5bd6e7369039c7b639625c234b7fe3ed3ca Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 31 Oct 2021 23:55:44 +0100 Subject: [PATCH] Added a few operators to String to simplify mixing it with std::strings. --- core/string.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ core/string.h | 13 +++++++++++ 2 files changed, 75 insertions(+) diff --git a/core/string.cpp b/core/string.cpp index 7c6d074..359c29e 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -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(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; diff --git a/core/string.h b/core/string.h index 386e96e..1bee7a6 100644 --- a/core/string.h +++ b/core/string.h @@ -3,6 +3,8 @@ #include +#include + 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: