From 1d9d521e6da7ae8178c829f0c008a5fc468800b3 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 27 Mar 2021 18:15:12 +0100 Subject: [PATCH] Finished string. --- 01_alapok/main.cpp | 19 ++++ 01_alapok/string.cpp | 200 ++++++++++++++++++++++++++++++++++++++++--- 01_alapok/string.h | 56 +++++++++--- 3 files changed, 249 insertions(+), 26 deletions(-) diff --git a/01_alapok/main.cpp b/01_alapok/main.cpp index 560bbee..6aafb3f 100755 --- a/01_alapok/main.cpp +++ b/01_alapok/main.cpp @@ -3,6 +3,7 @@ #include "int_vector.h" #include "vector.h" +#include "string.h" int main() { @@ -30,6 +31,24 @@ int main() { std::cout << std::endl; + //s + String s; + + s.push_back('1'); + s.push_back('1'); + s += "daddaad"; + + for (int i = 0; i < s.size(); ++i) { + std::cout << s[i] << std::endl; + } + + std::cout << s.c_str() << std::endl; + + std::cout << std::endl; + + std::cout << String("12").to_int() << std::endl; + std::cout << String("12.43").to_float() << std::endl; + std::cout << "asd" << std::endl; return 0; } \ No newline at end of file diff --git a/01_alapok/string.cpp b/01_alapok/string.cpp index c703df6..d180fae 100644 --- a/01_alapok/string.cpp +++ b/01_alapok/string.cpp @@ -1,6 +1,8 @@ #include "string.h" -void String::push_back(const int element) { +#include + +void String::push_back(const char element) { ensure_capacity(_size + 1); _data[_size++] = element; @@ -20,7 +22,7 @@ void String::remove(const int index) { --_size; } -void String::erase(const int element) { +void String::erase(const char element) { int index = find(element); if (index != -1) { @@ -36,15 +38,15 @@ bool String::empty() const { return _size == 0; } -int String::get(const int index) { +char String::get(const int index) { return _data[index]; } -const int &String::get(const int index) const { +const char String::get(const int index) const { return _data[index]; } -void String::set(const int index, const int value) { +void String::set(const int index, const char value) { _data[index] = value; } @@ -63,7 +65,7 @@ void String::ensure_capacity(const int capacity) { int tsize = capacity + _grow_by; - int *nd = new int[tsize]; + char *nd = new char[tsize]; for (int i = 0; i < _size; ++i) { nd[i] = _data[i]; @@ -75,20 +77,20 @@ void String::ensure_capacity(const int capacity) { } void String::resize(const int s) { - ensure_capacity(s); + ensure_capacity(s + 1); // +1 for the null terminator _size = s; } void String::append_array(const String &other) { - ensure_capacity(_size + other._size); + ensure_capacity(_size + other._size + 1); // +1 for the null terminator for (int i = 0; i < other._size; ++i) { _data[_size++] = other._data[i]; } } -int String::find(const int val) const { +int String::find(const char val) const { for (int i = 0; i < _size; ++i) { if (_data[i] == val) { return i; @@ -98,22 +100,148 @@ int String::find(const int val) const { return -1; } -int *String::dataw() { +float String::to_float() { + return atof(c_str()); +} + +double String::to_double() { + return atof(c_str()); +} + +int String::to_int() { + return atoi(c_str()); +} + +uint32_t String::to_uint() { + return static_cast(atoll(c_str())); +} + +char *String::c_str() { + _data[_size] = '\0'; + return _data; } -const int *String::data() const { +char *String::dataw() { return _data; } -const int &String::operator[](const int index) const { +const char *String::data() const { + return _data; +} + +const char String::operator[](const int index) const { return _data[index]; } -int &String::operator[](const int index) { +char String::operator[](const int index) { return _data[index]; } +String &String::operator+=(const String &b) { + ensure_capacity(_size + b._size + 1); // +1 for the null terminator + + for (int i = 0; i < b._size; ++i) { + _data[_size++] = b._data[i]; + } + + return *this; +} + +String &String::operator+=(const char chr) { + push_back(chr); + + return *this; +} + +String &String::operator+=(const char *p_c_str) { + int i = 0; + while (p_c_str[i] != '\0') { + push_back(p_c_str[i]); + ++i; + } + + return *this; +} + +String operator+(String lhs, const String &rhs) { + lhs += rhs; + + return lhs; +} + +String operator+(String lhs, const char *rhs) { + lhs += rhs; + + return lhs; +} + +String operator+(String lhs, const char rhs) { + lhs += rhs; + + return lhs; +} + +bool operator==(const String &a, const String &b) { + if (a._size != b._size) { + return false; + } + + for (int i = 0; i < a._size; ++i) { + if (a[i] != b[i]) { + return false; + } + } + + return true; +} + +bool operator!=(const String &a, const String &b) { + return !(a == b); +} + +bool operator==(const String &a, const char *b) { + int i = 0; + while (b[i] != '\0' && i < a._size) { + if (a[i] != b[i]) { + return false; + } + + ++i; + } + + if (i != a._size) { + return false; + } + + return true; +} + +bool operator!=(const String &a, const char *b) { + return !(a == b); +} + +bool operator==(const char *b, const String &a) { + int i = 0; + while (b[i] != '\0' && i < a._size) { + if (a[i] != b[i]) { + return false; + } + + ++i; + } + + if (i != a._size) { + return false; + } + + return true; +} + +bool operator!=(const char *b, const String &a) { + return !(a == b); +} + String::String() { _data = nullptr; _actual_size = 0; @@ -121,6 +249,52 @@ String::String() { _grow_by = 100; } +String::String(const String &other) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; + + //+1 for the null terminator in case its needed + ensure_capacity(other.size() + 1); + + for (int i = 0; i < other._size; ++i) { + _data[i] = other._data[i]; + } +} + +String::String(const String &other, int grow_by) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = grow_by; + + //+1 for the null terminator in case its needed + ensure_capacity(other.size() + 1); + + for (int i = 0; i < other._size; ++i) { + _data[i] = other._data[i]; + } +} + +String::String(const char* p_c_str) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = 100; + + operator+=(p_c_str); +} + +String::String(const char* p_c_str, const int grow_by) { + _data = nullptr; + _actual_size = 0; + _size = 0; + _grow_by = grow_by; + + operator+=(p_c_str); +} + String::String(int prealloc) { _data = nullptr; _actual_size = 0; diff --git a/01_alapok/string.h b/01_alapok/string.h index b05fd37..6332d86 100644 --- a/01_alapok/string.h +++ b/01_alapok/string.h @@ -1,38 +1,68 @@ #ifndef STRING_H #define STRING_H +#include + class String { public: - void push_back(const int element); + void push_back(const char element); void pop_back(); void remove(const int index); - void erase(const int element); + void erase(const char element); void clear(); bool empty() const; - int get(const int index); - const int &get(const int index) const; - void set(const int index, const int value); + char get(const int index); + const char get(const int index) const; + void set(const int index, const char value); int size() const; int capacity() const; void ensure_capacity(const int capacity); void resize(const int s); void append_array(const String &other); - int find(const int val) const; + int find(const char val) const; - int *dataw(); - const int *data() const; + float to_float(); + double to_double(); + int to_int(); + uint32_t to_uint(); - const int &operator[](const int index) const; - int &operator[](const int index); + char *c_str(); + + char *dataw(); + const char *data() const; + + const char operator[](const int index) const; + char operator[](const int index); + + String &operator+=(const String &b); + String &operator+=(const char chr); + String &operator+=(const char *p_c_str); + + 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 bool operator==(const String &a, const String &b); + friend bool operator!=(const String &a, const String &b); + + friend bool operator==(const String &a, const char *b); + friend bool operator!=(const String &a, const char *b); + + friend bool operator==(const char *b, const String &a); + friend bool operator!=(const char *b, const String &a); String(); - String(int prealloc); - String(int prealloc, int grow_by); + String(const String &other); + String(const String &other, const int grow_by); + String(const char* p_c_str); + String(const char* p_c_str, const int grow_by); + String(const int prealloc); + String(const int prealloc, const int grow_by); private: - int *_data; + char *_data; int _actual_size; int _size; int _grow_by;