From a6621a6ff60ed098c603f749dfaf8286634892f8 Mon Sep 17 00:00:00 2001 From: sikalovaliza <116655998+sikalovaliza@users.noreply.github.com> Date: Sun, 13 Nov 2022 16:50:35 +0300 Subject: [PATCH 1/5] fix_string --- .DS_Store | Bin 6148 -> 6148 bytes string.cpp | 220 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 string.cpp diff --git a/.DS_Store b/.DS_Store index 8242b2cb1c5ac6a2073d49c49b9a79bf5358a476..8b74c12407bcd62a9dff60ef0bb99d8c957efafe 100644 GIT binary patch delta 78 zcmZoMXfc@J&&a(oU^g=(_hcTH%Z6MG#SA43MGTn?c?{_cdJM@71q=nAIr+&+Ir&Kp a3=9H5yb6e)|A&IjcUZnNZD!~A%MSo_9UAcf delta 31 ncmZoMXfc@J&&ahgU^g=(*JK`+%bSH+zc5W~Fxkw`@s}R}qF@SV diff --git a/string.cpp b/string.cpp new file mode 100644 index 0000000..6fc8fc8 --- /dev/null +++ b/string.cpp @@ -0,0 +1,220 @@ +#include +#include + +class String { +private: + char* str = nullptr; + size_t size_of_string = 0; + size_t capacity = 0; + +public: + explicit String(size_t size_of_string, char c = '\0') { + this->size_of_string = size_of_string; + capacity = 2 * size_of_string; + str = (char*)malloc(capacity); + memset(str, c, size_of_string); + } + + String() = default; + + String(const String& s): String(s.size_of_string, '\0') { + memcpy(str, s.str, size_of_string); + } + + String(const char c) : String(1, c) {} + + String(std::initializer_list lst) { + size_of_string = lst.size(); + str = (char*)malloc(2 * size_of_string); + capacity = 2 * size_of_string; + std::copy(lst.begin(), lst.end(), str); + } + + // malloc, realloc, calloc == C, free + String& operator+=(const String& rhs) { + while (size_of_string + rhs.size_of_string >= capacity) { + str = (char*)realloc(str, 2 * size_of_string); + capacity *= 2; + } + memcpy(str + size_of_string, rhs.str, rhs.size_of_string); + size_of_string += rhs.size_of_string; + return *this; + } + + String operator+(const String& rhs) const { + String new_string = *this; + return new_string += rhs; + } + + char& operator[](size_t index) { + return str[index]; // == *(str + index) + } + + const char& operator[](size_t index) const { + return str[index]; + } + + bool operator==(const String& rhs) const { + return strcmp(str, rhs.str) == 0; + } + + bool operator!=(const String& rhs) const { + return strcmp(str, rhs.str) != 0; + } + + bool operator<(const String& rhs) const { + return strcmp(str, rhs.str) < 0; + } + + bool operator>(const String& rhs) const { + return strcmp(str, rhs.str) > 0; + } + + bool operator<=(const String& rhs) const { + return strcmp(str, rhs.str) <= 0; + } + + bool operator>=(const String& rhs) const { + return strcmp(str, rhs.str) >= 0; + } + + friend std::ostream& operator<<(std::ostream &out, const String &String); + friend std::istream& operator>>(std::istream &in, String &String); + + String Substr(size_t start, size_t count) { + String new_string; + new_string.str = (char*)malloc(2 * count); + new_string.size_of_string = count; + new_string.capacity = 2 * count; + std::copy(str + start, str + count - 1, new_string.str); + return new_string; + } + + size_t Find(String substring) { + size_t count = 0; + size_t index_substring = 0; + for (size_t index = 0; index < size_of_string; ++index) { + if (str[index] == substring[index_substring]) { + ++count; + ++index_substring; + if (count == substring.Size()) { + return index - count + 1; + } + } else { + count = 0; + index_substring = 0; + if (str[index] == substring[0]) { + ++count; + ++index_substring; + } + } + } + return size_of_string; + } + + size_t Rfind(String substring) { + size_t count = 0; + size_t index_substring = substring.size_of_string - 1; + for (size_t index = size_of_string - 1; index < size_of_string; --index) { + if (str[index] == substring[index_substring]) { + ++count; + --index_substring; + if (count == substring.Size()) { + return index; + } + } else { + count = 0; + index_substring = 0; + if (str[index] == substring[substring.size_of_string - 1]) { + ++count; + --index_substring; + } + + } + } + return size_of_string; + } + + void Push_back(char add) { + if (size_of_string + 1 == capacity) { + str = (char*)realloc(str, size_of_string * 2); + capacity *= 2; + } + str[size_of_string] = add; + size_of_string += 1; + } + + void Pop_back() { + size_of_string -= 1; + str[size_of_string] = '\0'; + } + void clear() { + size_of_string = 0; + str = nullptr; + } + + void Shrink_to_fit() { + capacity = size_of_string + 1; + str = (char*)realloc(str, size_of_string); + } + + size_t Length() const { + return size_of_string; + } + + size_t Size() const { + return size_of_string; + } + + size_t Capacity() const { + return this->capacity; + } + + const char& Front() const { + return str[0]; + } + + char& Front() { + return str[0]; + } + + const char& Back() const { + return str[size_of_string - 1]; + } + + char& Back() { + return str[size_of_string - 1]; + } + + bool Empty() const{ + return size_of_string == 0; + } + + char* Data() { + return str; + } + + const char* Data() const { + return str; + } + +~String() { + if (capacity > 0) { + free(str); + } + } +}; + +std::ostream& operator<<(std::ostream &out, const String &new_string) { + out << new_string.Data(); + return out; +} + +std::istream& operator>>(std::istream &in, String &new_string) { + new_string.str = (char*)realloc(new_string.str, new_string.size_of_string * 2); + new_string[new_string.size_of_string] = '\0'; + for (size_t i = 0; i <= new_string.size_of_string; i++) { + in >> new_string.str[i]; + } + return in; +} \ No newline at end of file From 0023f7c79fc7ab38049c325dbfa7a39e3ffc9711 Mon Sep 17 00:00:00 2001 From: sikalovaliza <116655998+sikalovaliza@users.noreply.github.com> Date: Sun, 13 Nov 2022 17:54:43 +0300 Subject: [PATCH 2/5] task#3 --- .DS_Store | Bin 6148 -> 6148 bytes string.cpp => string.h | 82 ++++++++++++++++++++--------------------- 2 files changed, 39 insertions(+), 43 deletions(-) rename string.cpp => string.h (73%) diff --git a/.DS_Store b/.DS_Store index 8b74c12407bcd62a9dff60ef0bb99d8c957efafe..091ac6145cfc4e287f07de55f2faad9180307c18 100644 GIT binary patch delta 41 xcmZoMXfc@J&&ahgU^g=(*JK`+%aes!6<7-x7#JBhTdsize_of_string = size_of_string; + explicit String(size_t size, char c = '\0') { + size_of_string = size; capacity = 2 * size_of_string; str = (char*)malloc(capacity); memset(str, c, size_of_string); } - String() = default; + String(); String(const String& s): String(s.size_of_string, '\0') { memcpy(str, s.str, size_of_string); @@ -25,16 +25,15 @@ class String { String(std::initializer_list lst) { size_of_string = lst.size(); - str = (char*)malloc(2 * size_of_string); capacity = 2 * size_of_string; + str = (char*)malloc(capacity); std::copy(lst.begin(), lst.end(), str); } - // malloc, realloc, calloc == C, free String& operator+=(const String& rhs) { while (size_of_string + rhs.size_of_string >= capacity) { str = (char*)realloc(str, 2 * size_of_string); - capacity *= 2; + capacity <<= 1; } memcpy(str + size_of_string, rhs.str, rhs.size_of_string); size_of_string += rhs.size_of_string; @@ -47,7 +46,7 @@ class String { } char& operator[](size_t index) { - return str[index]; // == *(str + index) + return str[index]; } const char& operator[](size_t index) const { @@ -59,7 +58,7 @@ class String { } bool operator!=(const String& rhs) const { - return strcmp(str, rhs.str) != 0; + return !(*this == rhs); } bool operator<(const String& rhs) const { @@ -81,70 +80,67 @@ class String { friend std::ostream& operator<<(std::ostream &out, const String &String); friend std::istream& operator>>(std::istream &in, String &String); - String Substr(size_t start, size_t count) { + String substr(size_t start, size_t count) { String new_string; - new_string.str = (char*)malloc(2 * count); - new_string.size_of_string = count; new_string.capacity = 2 * count; - std::copy(str + start, str + count - 1, new_string.str); + new_string.str = (char*)malloc(new_string.capacity); + new_string.size_of_string = count; + memcpy(new_string.str, str + start, count - 1); return new_string; } - size_t Find(String substring) { + size_t find(String substring) { size_t count = 0; size_t index_substring = 0; - for (size_t index = 0; index < size_of_string; ++index) { + size_t index = 0; + while (index < size_of_string) { if (str[index] == substring[index_substring]) { ++count; ++index_substring; - if (count == substring.Size()) { + if (count == substring.size()) { return index - count + 1; } } else { + index -= count; count = 0; index_substring = 0; - if (str[index] == substring[0]) { - ++count; - ++index_substring; - } } + ++index; } return size_of_string; } - size_t Rfind(String substring) { + size_t rfind(String substring) { size_t count = 0; size_t index_substring = substring.size_of_string - 1; - for (size_t index = size_of_string - 1; index < size_of_string; --index) { + size_t index = size_of_string - 1; + while (index < size_of_string) { if (str[index] == substring[index_substring]) { ++count; --index_substring; - if (count == substring.Size()) { + if (count == substring.size()) { return index; } } else { + index += count; count = 0; - index_substring = 0; - if (str[index] == substring[substring.size_of_string - 1]) { - ++count; - --index_substring; - } - + index_substring = substring.size_of_string - 1; } + --index; } return size_of_string; } - void Push_back(char add) { + void push_back(char add) { if (size_of_string + 1 == capacity) { str = (char*)realloc(str, size_of_string * 2); capacity *= 2; } str[size_of_string] = add; - size_of_string += 1; + ++size_of_string; } - void Pop_back() { + void pop_back() { size_of_string -= 1; str[size_of_string] = '\0'; } @@ -158,43 +154,43 @@ class String { str = (char*)realloc(str, size_of_string); } - size_t Length() const { + size_t length() const { return size_of_string; } - size_t Size() const { + size_t size() const { return size_of_string; } size_t Capacity() const { - return this->capacity; + return capacity; } - const char& Front() const { + const char& front() const { return str[0]; } - char& Front() { + char& front() { return str[0]; } - const char& Back() const { + const char& back() const { return str[size_of_string - 1]; } - char& Back() { + char& back() { return str[size_of_string - 1]; } - bool Empty() const{ + bool empty() const{ return size_of_string == 0; } - char* Data() { + char* data() { return str; } - const char* Data() const { + const char* data() const { return str; } @@ -206,7 +202,7 @@ class String { }; std::ostream& operator<<(std::ostream &out, const String &new_string) { - out << new_string.Data(); + out << new_string.data(); return out; } @@ -217,4 +213,4 @@ std::istream& operator>>(std::istream &in, String &new_string) { in >> new_string.str[i]; } return in; -} \ No newline at end of file +} From 45fc99248e59bf5ce26c42fab890eef67071f22f Mon Sep 17 00:00:00 2001 From: sikalovaliza <116655998+sikalovaliza@users.noreply.github.com> Date: Mon, 14 Nov 2022 00:07:58 +0300 Subject: [PATCH 3/5] task#4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit исправила операторы, изменила везде "*2" --- .DS_Store | Bin 6148 -> 6148 bytes string.cpp | 288 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 288 insertions(+) create mode 100644 string.cpp diff --git a/.DS_Store b/.DS_Store index 091ac6145cfc4e287f07de55f2faad9180307c18..54828bcb9ce894d1fc4db1d4878bccbe7fa260e2 100644 GIT binary patch delta 78 zcmZoMXfc@J&&a(oU^g=(_hcTH%Z6MG#SA43MGTn?c?{_cdJM@71q=nAIr+&+Ir&Kp a3=9H5d>x42{fC0hcUZnNZD!~A%MSo}ts5l( delta 31 ncmZoMXfc@J&&ahgU^g=(*JK`+%bSH+zc5W~Fxkw`@s}R}qF@SV diff --git a/string.cpp b/string.cpp new file mode 100644 index 0000000..4404f94 --- /dev/null +++ b/string.cpp @@ -0,0 +1,288 @@ +#include +#include + +class String { +private: + char* str = nullptr; + size_t size_of_string = 0; + size_t capacity_ = 0; + +public: + explicit String(size_t size, char c = '\0') { + size_of_string = size; + capacity_ = 2 * size_of_string; + str = (char*)malloc(capacity_); + memset(str, c, size_of_string); + str[size_of_string] = '\0'; + } + + String(const char* snew) { + size_t count = strlen(snew); + capacity_ = count << 1; + str = (char*)malloc(capacity_); + memcpy(str, snew, count); + size_of_string = count; + str[count] = '\0'; + } + + + String() = default; + + String(const String& s): String(s.size_of_string, '\0') { + memcpy(str, s.str, size_of_string); + } + + String(const char c) : String(1, c) {} + + String(std::initializer_list lst) { + size_of_string = lst.size(); + capacity_ = size_of_string << 1; + str = (char*)malloc(capacity_); + std::copy(lst.begin(), lst.end(), str); + str[size_of_string] = '\0'; + } + + String& operator+=(const String& rhs) { + while (size_of_string + rhs.size_of_string >= capacity_) { + str = (char*)realloc(str, 2 * size_of_string); + capacity_ <<= 1; + } + memcpy(str + size_of_string, rhs.str, rhs.size_of_string); + size_of_string += rhs.size_of_string; + str[size_of_string] = '\0'; + return *this; + } + + String& operator=(const String& string_new) { + String copy = string_new; + swap(copy); + return *this; + } + + void swap(String& string_new) { + std::swap(str, string_new.str); + std::swap(size_of_string, string_new.size_of_string); + } + + + char& operator[](size_t index) { + return str[index]; + } + + const char& operator[](size_t index) const { + return str[index]; + } + + friend std::ostream& operator<<(std::ostream &out, const String &String); + friend std::istream& operator>>(std::istream &in, String &String); + + String& substr(size_t start, size_t count) { + String new_string; + new_string.capacity_ = count << 1; + new_string.str = (char*)malloc(new_string.capacity_); + new_string.size_of_string = count; + memcpy(new_string.str, str + start, count - 1); + new_string[size_of_string] = '\0'; + return new_string; + } + + size_t find(String& substring) { + size_t count = 0; + size_t index_substring = 0; + size_t index = 0; + while (index < size_of_string) { + if (str[index] == substring[index_substring]) { + ++count; + ++index_substring; + if (count == substring.size()) { + return index - count + 1; + } + } else { + index -= count; + count = 0; + index_substring = 0; + } + ++index; + } + return size_of_string; + } + + size_t rfind(String& substring) { + size_t count = 0; + size_t index_substring = substring.size_of_string - 1; + size_t index = size_of_string - 1; + while (index < size_of_string) { + if (str[index] == substring[index_substring]) { + ++count; + --index_substring; + if (count == substring.size()) { + return index; + } + } else { + index += count; + count = 0; + index_substring = substring.size_of_string - 1; + } + --index; + } + return size_of_string; + } + + void push_back(char add) { + if (size_of_string + 1 == capacity_) { + str = (char*)realloc(str, size_of_string * 2); + capacity_ <<= 1; + } + str[size_of_string] = add; + str[size_of_string + 1] = '\0'; + ++size_of_string; + } + + void pop_back() { + size_of_string -= 1; + str[size_of_string] = '\0'; + } + + void clear() { + size_of_string = 0; + str = nullptr; + } + + void shrink_to_fit() { + capacity_ = size_of_string + 1; + str = (char*)realloc(str, size_of_string); + } + + size_t length() const { + return size_of_string; + } + + size_t size() const { + return size_of_string; + } + + size_t capacity() const { + return capacity_; + } + + const char& front() const { + return str[0]; + } + + char& front() { + return str[0]; + } + + const char& back() const { + return str[size_of_string - 1]; + } + + char& back() { + return str[size_of_string - 1]; + } + + bool empty() const{ + return size_of_string == 0; + } + + char* data() { + return str; + } + + const char* data() const { + return str; + } + +~String() { + if (capacity_ > 0) { + free(str); + } + } +}; + +String operator+(const String& string_old, const String& rhs) { + String new_string = string_old; + return new_string += rhs; +} + + + +bool operator<(const String& string_old, const String& rhs) { + for (size_t index = 0; index < std::max(string_old.size(), rhs.size()); ++index) { + if (string_old[index] > rhs[index]) { + return 0; + } + if (string_old[index] < rhs[index]) { + return 1; + } + } + return 0; +} + +bool operator>(const String& string_old, const String& rhs) { + for (size_t index = 0; index < std::max(string_old.size(), rhs.size()); ++index) { + if (string_old[index] > rhs[index]) { + return 1; + } + if (string_old[index] < rhs[index]) { + return 0; + } + } + return 0; +} + + +bool operator<=(const String& string_old, const String& rhs) { + for (size_t index = 0; index < std::max(string_old.size(), rhs.size()); ++index) { + if (string_old[index] > rhs[index]) { + return 0; + } + } + return 1; +} + +bool operator>=(const String& string_old, const String& rhs) { + for (size_t index = 0; index < std::max(string_old.size(), rhs.size()); ++index) { + if (string_old[index] < rhs[index]) { + return 0; + } + } + return 1; +} + +bool operator==(const String& string_old, const String& rhs) { + if (string_old.size() != rhs.size()) { + return 0; + } + for(size_t index = 0; index < string_old.size(); ++index) { + if (string_old[index] != rhs[index]) { + return 0; + } + } + return 1; +} + +bool operator!=(const String& string_old, const String& rhs) { + if (string_old.size() != rhs.size()) { + return 1; + } + for(size_t index = 0; index < string_old.size(); ++index) { + if (string_old[index] != rhs[index]) { + return 1; + } + } + return 0; +} + +std::ostream& operator<<(std::ostream &out, const String &new_string) { + out << new_string.data(); + return out; +} + +std::istream& operator>>(std::istream &in, String &new_string) { + for (size_t i = 0; i <= new_string.size_of_string; i++) { + in >> new_string[i]; + } + new_string[new_string.size_of_string] = '\0'; + return in; +} From fe7db9b33fccaef124935b4461ab85394cb5c49b Mon Sep 17 00:00:00 2001 From: sikalovaliza <116655998+sikalovaliza@users.noreply.github.com> Date: Tue, 13 Dec 2022 13:33:27 +0300 Subject: [PATCH 4/5] =?UTF-8?q?#3=20=D0=BF=D1=80=D0=BE=D1=85=D0=BE=D0=B4?= =?UTF-8?q?=D0=B8=D1=82=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 6148 -> 6148 bytes string.h | 415 ++++++++++++++++++++++++++++++++---------------------- 2 files changed, 244 insertions(+), 171 deletions(-) diff --git a/.DS_Store b/.DS_Store index 54828bcb9ce894d1fc4db1d4878bccbe7fa260e2..4058e68d9d26029ec495b9a53102459772540c2a 100644 GIT binary patch delta 19 acmZoMXffDe#LC84!oa{Nve|<5j1T}Z!UV1W delta 19 acmZoMXffDe#LC7{z`(%BxY>gBj1T}Z9R!vD diff --git a/string.h b/string.h index 9be4cf7..3d9c3b3 100644 --- a/string.h +++ b/string.h @@ -1,103 +1,105 @@ -#include -#include +#include +#include +#include class String { -private: - char* str = nullptr; - size_t size_of_string = 0; - size_t capacity = 0; - -public: - explicit String(size_t size, char c = '\0') { - size_of_string = size; - capacity = 2 * size_of_string; - str = (char*)malloc(capacity); - memset(str, c, size_of_string); - } - - String(); - - String(const String& s): String(s.size_of_string, '\0') { - memcpy(str, s.str, size_of_string); - } - - String(const char c) : String(1, c) {} - - String(std::initializer_list lst) { - size_of_string = lst.size(); - capacity = 2 * size_of_string; - str = (char*)malloc(capacity); - std::copy(lst.begin(), lst.end(), str); - } - - String& operator+=(const String& rhs) { - while (size_of_string + rhs.size_of_string >= capacity) { - str = (char*)realloc(str, 2 * size_of_string); - capacity <<= 1; - } - memcpy(str + size_of_string, rhs.str, rhs.size_of_string); - size_of_string += rhs.size_of_string; + public: + String(size_t size, char element = '\0') { + size_of_string_ = size; + capacity_ = 2 * size_of_string_; + std::free(array_of_string_); + array_of_string_ = + static_cast(std::malloc(capacity_ * sizeof(char))); + memset(array_of_string_, element, size_of_string_); + array_of_string_[size_of_string_] = '\0'; + } + + String(const char* new_string) { + if (new_string != nullptr) { + size_t count = strlen(new_string); + capacity_ = 1 + int(count << 1); + std::free(array_of_string_); + array_of_string_ = + static_cast(std::malloc(capacity_ * sizeof(char))); + memcpy(array_of_string_, new_string, count); + size_of_string_ = count; + array_of_string_[count] = '\0'; + } + } + + String() = default; + + String(const String& new_string) : String(new_string.data()) {} + + String(const char kElement) : String(1, kElement) {} + String& operator+=(const String& right_string) { + while (size_of_string_ + right_string.size_of_string_ >= capacity_) { + capacity_ = 2 * capacity_ + 1; + array_of_string_ = static_cast( + std::realloc(array_of_string_, capacity_ * sizeof(char))); + } + memcpy(array_of_string_ + size_of_string_, right_string.array_of_string_, + right_string.size_of_string_); + size_of_string_ += right_string.size_of_string_; + array_of_string_[size_of_string_] = '\0'; return *this; } - - String operator+(const String& rhs) const { - String new_string = *this; - return new_string += rhs; + + String& operator=(const String& string_new) { + if (string_new.size_of_string_ == 0) { + std::free(array_of_string_); + array_of_string_ = nullptr; + return *this; + } + if (capacity_ < string_new.capacity_) { + capacity_ = string_new.capacity_ + 1; + array_of_string_ = static_cast( + std::realloc(array_of_string_, capacity_ * sizeof(char))); + } + size_of_string_ = string_new.size_of_string_; + memcpy(array_of_string_, string_new.array_of_string_, size_of_string_); + return *this; } - + char& operator[](size_t index) { - return str[index]; + if (index < size_of_string_) { + return array_of_string_[index]; + } + static char value = '\0'; + return value; } - + const char& operator[](size_t index) const { - return str[index]; - } - - bool operator==(const String& rhs) const { - return strcmp(str, rhs.str) == 0; - } - - bool operator!=(const String& rhs) const { - return !(*this == rhs); - } - - bool operator<(const String& rhs) const { - return strcmp(str, rhs.str) < 0; - } - - bool operator>(const String& rhs) const { - return strcmp(str, rhs.str) > 0; - } - - bool operator<=(const String& rhs) const { - return strcmp(str, rhs.str) <= 0; - } - - bool operator>=(const String& rhs) const { - return strcmp(str, rhs.str) >= 0; - } - - friend std::ostream& operator<<(std::ostream &out, const String &String); - friend std::istream& operator>>(std::istream &in, String &String); - - String substr(size_t start, size_t count) { - String new_string; - new_string.capacity = 2 * count; - new_string.str = (char*)malloc(new_string.capacity); - new_string.size_of_string = count; - memcpy(new_string.str, str + start, count - 1); - return new_string; - } - - size_t find(String substring) { + if (index < size_of_string_) { + return array_of_string_[index]; + } + static char value = '\0'; + return value; + } + + String substr(size_t start, size_t count) const { + if (start + count < size_of_string_ + 1) { + String new_string; + new_string.capacity_ = 2 * count + 1; + new_string.array_of_string_ = + static_cast(std::malloc(new_string.capacity_ * sizeof(char))); + new_string.size_of_string_ = count; + memcpy(new_string.array_of_string_, array_of_string_ + start, count); + new_string[size_of_string_] = '\0'; + return new_string; + } + return '\0'; + } + + size_t find(const String kSubstring) const { size_t count = 0; size_t index_substring = 0; size_t index = 0; - while (index < size_of_string) { - if (str[index] == substring[index_substring]) { + while (index < size_of_string_) { + if (array_of_string_[index] == kSubstring[index_substring]) { ++count; ++index_substring; - if (count == substring.size()) { + if (count == kSubstring.size()) { return index - count + 1; } } else { @@ -107,110 +109,181 @@ class String { } ++index; } - return size_of_string; + return size_of_string_; } - - size_t rfind(String substring) { + + size_t rfind(const String kSubstring) const { size_t count = 0; - size_t index_substring = substring.size_of_string - 1; - size_t index = size_of_string - 1; - while (index < size_of_string) { - if (str[index] == substring[index_substring]) { + size_t index_substring = kSubstring.size_of_string_ - 1; + size_t index = size_of_string_ - 1; + while (index < size_of_string_) { + if (array_of_string_[index] == kSubstring[index_substring]) { ++count; --index_substring; - if (count == substring.size()) { + if (count == kSubstring.size()) { return index; } } else { index += count; count = 0; - index_substring = substring.size_of_string - 1; + index_substring = kSubstring.size_of_string_ - 1; } --index; } - return size_of_string; + return size_of_string_; } - - void push_back(char add) { - if (size_of_string + 1 == capacity) { - str = (char*)realloc(str, size_of_string * 2); - capacity *= 2; + + void push_back(char element) { + if (capacity_ == 0) { + capacity_ = 2; + array_of_string_ = static_cast( + realloc(array_of_string_, capacity_ * sizeof(char))); + } + if (size_of_string_ + 1 >= capacity_) { + capacity_ <<= 1; + array_of_string_ = static_cast( + realloc(array_of_string_, capacity_ * sizeof(char))); } - str[size_of_string] = add; - ++size_of_string; - } - + array_of_string_[size_of_string_] = element; + array_of_string_[size_of_string_ + 1] = '\0'; + ++size_of_string_; + } + void pop_back() { - size_of_string -= 1; - str[size_of_string] = '\0'; + size_of_string_ -= 1; + array_of_string_[size_of_string_] = '\0'; } + void clear() { - size_of_string = 0; - str = nullptr; - } - - void Shrink_to_fit() { - capacity = size_of_string + 1; - str = (char*)realloc(str, size_of_string); - } - - size_t length() const { - return size_of_string; - } - - size_t size() const { - return size_of_string; - } - - size_t Capacity() const { - return capacity; - } - - const char& front() const { - return str[0]; - } - - char& front() { - return str[0]; - } - - const char& back() const { - return str[size_of_string - 1]; - } - - char& back() { - return str[size_of_string - 1]; - } - - bool empty() const{ - return size_of_string == 0; - } - - char* data() { - return str; - } - - const char* data() const { - return str; - } - -~String() { - if (capacity > 0) { - free(str); - } - } + size_of_string_ = 0; + array_of_string_[0] = '\0'; + } + + void shrink_to_fit() { + capacity_ = size_of_string_ + 1; + array_of_string_ = static_cast( + realloc(array_of_string_, size_of_string_ * sizeof(char))); + } + + size_t length() const { return size_of_string_; } + + size_t size() const { return size_of_string_; } + + size_t capacity() const { return capacity_ - 1; } + + const char& front() const { return array_of_string_[0]; } + + char& front() { return array_of_string_[0]; } + + const char& back() const { return array_of_string_[size_of_string_ - 1]; } + + char& back() { return array_of_string_[size_of_string_ - 1]; } + + bool empty() const { return size_of_string_ == 0; } + + char* data() { return array_of_string_; } + + const char* data() const { return array_of_string_; } + + ~String() { + if (capacity_ > 0) { + std::free(array_of_string_); + } + } + + private: + size_t size_of_string_ = 0; + size_t capacity_ = 0; + char* array_of_string_ = nullptr; }; -std::ostream& operator<<(std::ostream &out, const String &new_string) { - out << new_string.data(); - return out; +String operator+(const String& left_string, const String& right_string) { + String new_string = left_string; + return new_string += right_string; +} + +bool operator<(const String& left_string, const String& right_string) { + for (size_t index = 0; + index < std::max(left_string.size(), right_string.size()); ++index) { + if (right_string[index] < left_string[index]) { + return false; + } + if (left_string[index] < right_string[index]) { + return true; + } + } + return false; +} + +bool operator>(const String& left_string, const String& right_string) { + for (size_t index = 0; + index < std::max(left_string.size(), right_string.size()); ++index) { + if (right_string[index] < left_string[index]) { + return true; + } + if (left_string[index] < right_string[index]) { + return false; + } + } + return false; +} + +bool operator<=(const String& left_string, const String& right_string) { + for (size_t index = 0; + index < std::max(left_string.size(), right_string.size()); ++index) { + if (right_string[index] < left_string[index]) { + return false; + } + } + return true; +} +bool operator>=(const String& left_string, const String& right_string) { + for (size_t index = 0; + index < std::max(left_string.size(), right_string.size()); ++index) { + if (left_string[index] < right_string[index]) { + return false; + } + } + return true; +} + +bool operator==(const String& left_string, const String& right_string) { + if (left_string.size() != right_string.size()) { + return false; + } + for (size_t index = 0; index < left_string.size(); ++index) { + if (left_string[index] != right_string[index]) { + return false; + } + } + return true; +} + +bool operator!=(const String& left_string, const String& right_string) { + if (left_string.size() != right_string.size()) { + return true; + } + for (size_t index = 0; index < left_string.size(); ++index) { + if (left_string[index] != right_string[index]) { + return true; + } + } + return false; +} + +std::ostream& operator<<(std::ostream& output, const String& new_string) { + if (new_string.data() == nullptr) { + return output; + } + return output << new_string.data(); } -std::istream& operator>>(std::istream &in, String &new_string) { - new_string.str = (char*)realloc(new_string.str, new_string.size_of_string * 2); - new_string[new_string.size_of_string] = '\0'; - for (size_t i = 0; i <= new_string.size_of_string; i++) { - in >> new_string.str[i]; +std::istream& operator>>(std::istream& input, String& new_string) { + new_string = "\0"; + char element = input.get(); + while (!input.eof() && !static_cast(std::isspace(element))) { + new_string.push_back(element); + element = input.get(); } - return in; + return input; } From 0a4f47d4504d36fb62f67d005d4256c662eba705 Mon Sep 17 00:00:00 2001 From: sikalovaliza <116655998+sikalovaliza@users.noreply.github.com> Date: Tue, 13 Dec 2022 13:37:40 +0300 Subject: [PATCH 5/5] =?UTF-8?q?#4=20=D1=82=D0=B5=D1=81=D1=82=D1=8B=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D0=BF=D1=80=D0=BE=D1=85=D0=BE=D0=B4=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 6148 -> 6148 bytes string.h | 204 ++++++++++++++++++++---------------------------------- 2 files changed, 75 insertions(+), 129 deletions(-) diff --git a/.DS_Store b/.DS_Store index 4058e68d9d26029ec495b9a53102459772540c2a..86a03eba9afb8fe23ba35dda522f01a2aea0ef88 100644 GIT binary patch delta 32 ocmZoMXfc@J&&ahgU^g=(*Jd6TQO3=ItlO9-HW+MX=lIJH0H1>i#sB~S delta 53 zcmZoMXfc@J&&a(oU^g=(_hudzQATk+h78Y~{N$vZ{3Hej1_1^J#u6YF+04Ouo@p~X H$6tN`X=V(> diff --git a/string.h b/string.h index 3d9c3b3..8e46117 100644 --- a/string.h +++ b/string.h @@ -4,21 +4,18 @@ class String { public: - String(size_t size, char element = '\0') { - size_of_string_ = size; - capacity_ = 2 * size_of_string_; - std::free(array_of_string_); + explicit String(size_t size, char element = '\0') + : size_of_string_{size}, capacity_{size + 1} { array_of_string_ = static_cast(std::malloc(capacity_ * sizeof(char))); memset(array_of_string_, element, size_of_string_); array_of_string_[size_of_string_] = '\0'; } - String(const char* new_string) { + String(const char* new_string) : size_of_string_(0), capacity_(0) { if (new_string != nullptr) { size_t count = strlen(new_string); - capacity_ = 1 + int(count << 1); - std::free(array_of_string_); + capacity_ = count + 1; array_of_string_ = static_cast(std::malloc(capacity_ * sizeof(char))); memcpy(array_of_string_, new_string, count); @@ -27,17 +24,29 @@ class String { } } - String() = default; + String() { + capacity_ = 1; + array_of_string_ = + static_cast(std::malloc(capacity_ * sizeof(char))); + array_of_string_[0] = '\0'; + } String(const String& new_string) : String(new_string.data()) {} String(const char kElement) : String(1, kElement) {} - String& operator+=(const String& right_string) { - while (size_of_string_ + right_string.size_of_string_ >= capacity_) { - capacity_ = 2 * capacity_ + 1; + + String& reserve(const String& new_string) { + if (capacity_ < size_of_string_ + new_string.size_of_string_) { + capacity_ = std::max(2 * capacity_ + 1, + size_of_string_ + new_string.size_of_string_); array_of_string_ = static_cast( std::realloc(array_of_string_, capacity_ * sizeof(char))); } + return *this; + } + + String& operator+=(const String& right_string) { + reserve(right_string); memcpy(array_of_string_ + size_of_string_, right_string.array_of_string_, right_string.size_of_string_); size_of_string_ += right_string.size_of_string_; @@ -45,45 +54,24 @@ class String { return *this; } - String& operator=(const String& string_new) { - if (string_new.size_of_string_ == 0) { - std::free(array_of_string_); - array_of_string_ = nullptr; - return *this; - } - if (capacity_ < string_new.capacity_) { - capacity_ = string_new.capacity_ + 1; - array_of_string_ = static_cast( - std::realloc(array_of_string_, capacity_ * sizeof(char))); - } - size_of_string_ = string_new.size_of_string_; - memcpy(array_of_string_, string_new.array_of_string_, size_of_string_); + String& swap(String& string_new) { + std::swap(size_of_string_, string_new.size_of_string_); + std::swap(array_of_string_, string_new.array_of_string_); + std::swap(capacity_, string_new.capacity_); return *this; } - - char& operator[](size_t index) { - if (index < size_of_string_) { - return array_of_string_[index]; - } - static char value = '\0'; - return value; + + String& operator=(const String& right_string) { + String new_string{right_string}; + return swap(new_string); } + const char& operator[](size_t index) const { return array_of_string_[index]; } - const char& operator[](size_t index) const { - if (index < size_of_string_) { - return array_of_string_[index]; - } - static char value = '\0'; - return value; - } + char& operator[](size_t index) { return array_of_string_[index]; } String substr(size_t start, size_t count) const { if (start + count < size_of_string_ + 1) { - String new_string; - new_string.capacity_ = 2 * count + 1; - new_string.array_of_string_ = - static_cast(std::malloc(new_string.capacity_ * sizeof(char))); - new_string.size_of_string_ = count; + String new_string(count); memcpy(new_string.array_of_string_, array_of_string_ + start, count); new_string[size_of_string_] = '\0'; return new_string; @@ -92,58 +80,15 @@ class String { } size_t find(const String kSubstring) const { - size_t count = 0; - size_t index_substring = 0; - size_t index = 0; - while (index < size_of_string_) { - if (array_of_string_[index] == kSubstring[index_substring]) { - ++count; - ++index_substring; - if (count == kSubstring.size()) { - return index - count + 1; - } - } else { - index -= count; - count = 0; - index_substring = 0; - } - ++index; - } - return size_of_string_; + return universalfind(true, kSubstring); } size_t rfind(const String kSubstring) const { - size_t count = 0; - size_t index_substring = kSubstring.size_of_string_ - 1; - size_t index = size_of_string_ - 1; - while (index < size_of_string_) { - if (array_of_string_[index] == kSubstring[index_substring]) { - ++count; - --index_substring; - if (count == kSubstring.size()) { - return index; - } - } else { - index += count; - count = 0; - index_substring = kSubstring.size_of_string_ - 1; - } - --index; - } - return size_of_string_; + return universalfind(false, kSubstring); } void push_back(char element) { - if (capacity_ == 0) { - capacity_ = 2; - array_of_string_ = static_cast( - realloc(array_of_string_, capacity_ * sizeof(char))); - } - if (size_of_string_ + 1 >= capacity_) { - capacity_ <<= 1; - array_of_string_ = static_cast( - realloc(array_of_string_, capacity_ * sizeof(char))); - } + reserve(element); array_of_string_[size_of_string_] = element; array_of_string_[size_of_string_ + 1] = '\0'; ++size_of_string_; @@ -161,8 +106,8 @@ class String { void shrink_to_fit() { capacity_ = size_of_string_ + 1; - array_of_string_ = static_cast( - realloc(array_of_string_, size_of_string_ * sizeof(char))); + array_of_string_ = + static_cast(realloc(array_of_string_, capacity_ * sizeof(char))); } size_t length() const { return size_of_string_; } @@ -195,6 +140,39 @@ class String { size_t size_of_string_ = 0; size_t capacity_ = 0; char* array_of_string_ = nullptr; + + size_t static change(bool first, size_t value_first, size_t value_second) { + if (first) { + return value_first; + } + return value_second; + } + + size_t universalfind(bool first, const String kSubstring) const { + size_t count = 0; + size_t index_substring = 0; + size_t index = 0; + if (!first) { + index_substring = change(first, 0, kSubstring.size_of_string_ - 1); + index = change(first, 0, size_of_string_ - 1); + } + while (index < size_of_string_) { + if (array_of_string_[index] == kSubstring[index_substring]) { + ++count; + index_substring = + change(first, index_substring + 1, index_substring - 1); + if (count == kSubstring.size()) { + return change(first, index - count + 1, index); + } + } else { + index = change(first, index - count, index + count); + index_substring = change(first, 0, kSubstring.size_of_string_ - 1); + count = 0; + } + index = change(first, index + 1, index - 1); + } + return size_of_string_; + } }; String operator+(const String& left_string, const String& right_string) { @@ -205,46 +183,22 @@ String operator+(const String& left_string, const String& right_string) { bool operator<(const String& left_string, const String& right_string) { for (size_t index = 0; index < std::max(left_string.size(), right_string.size()); ++index) { - if (right_string[index] < left_string[index]) { - return false; - } - if (left_string[index] < right_string[index]) { - return true; + if (left_string[index] != right_string[index]) { + return left_string[index] < right_string[index]; } } return false; } bool operator>(const String& left_string, const String& right_string) { - for (size_t index = 0; - index < std::max(left_string.size(), right_string.size()); ++index) { - if (right_string[index] < left_string[index]) { - return true; - } - if (left_string[index] < right_string[index]) { - return false; - } - } - return false; + return right_string < left_string; } bool operator<=(const String& left_string, const String& right_string) { - for (size_t index = 0; - index < std::max(left_string.size(), right_string.size()); ++index) { - if (right_string[index] < left_string[index]) { - return false; - } - } - return true; + return !(right_string < left_string); } bool operator>=(const String& left_string, const String& right_string) { - for (size_t index = 0; - index < std::max(left_string.size(), right_string.size()); ++index) { - if (left_string[index] < right_string[index]) { - return false; - } - } - return true; + return !(left_string < right_string); } bool operator==(const String& left_string, const String& right_string) { @@ -260,15 +214,7 @@ bool operator==(const String& left_string, const String& right_string) { } bool operator!=(const String& left_string, const String& right_string) { - if (left_string.size() != right_string.size()) { - return true; - } - for (size_t index = 0; index < left_string.size(); ++index) { - if (left_string[index] != right_string[index]) { - return true; - } - } - return false; + return !(left_string == right_string); } std::ostream& operator<<(std::ostream& output, const String& new_string) {