Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "string.hpp"
- #include <type_traits>
- String::String(size_t size, char character) {
- capacity_ = size;
- size_ = size;
- if (size == 0) {
- capacity_ = 0;
- size_ = 0;
- string_ = nullptr;
- } else {
- string_ = new char[size + 1];
- for (size_t i = 0; i < size; ++i) {
- string_[i] = character;
- }
- string_[size] = '\0';
- }
- }
- String::String(const char* const_str) {
- if (const_str == nullptr) {
- size_ = 0;
- capacity_ = 0;
- string_ = nullptr;
- } else {
- const size_t kLength = strlen(const_str);
- string_ = new char[kLength + 1];
- capacity_ = kLength;
- size_ = capacity_;
- for (size_t i = 0; i < kLength; ++i) {
- string_[i] = const_str[i];
- }
- string_[size_] = '\0';
- }
- }
- String::String(const String& source) {
- if (source.capacity_ == 0) {
- string_ = nullptr;
- size_ = 0;
- capacity_ = 0;
- } else {
- if (source.string_ == nullptr) {
- const size_t kSize = source.capacity_;
- string_ = new char[kSize + 1];
- size_ = 0;
- capacity_ = source.capacity_;
- } else {
- const size_t kSize = source.capacity_;
- string_ = new char[kSize + 1]; // here
- const size_t kEdge = source.size_;
- for (size_t i = 0; i < kEdge; ++i) {
- string_[i] = source[i];
- }
- string_[kEdge] = '\0';
- size_ = source.size_;
- capacity_ = source.capacity_;
- }
- }
- }
- String& String::operator=(const String& src) {
- String copy = src;
- Swap(copy);
- return *this;
- }
- void String::Swap(String& src) {
- char* temptr = string_;
- size_t tempsize = size_;
- size_t tempcapacity = capacity_;
- capacity_ = src.capacity_;
- size_ = src.size_;
- string_ = src.string_;
- src.string_ = temptr;
- src.size_ = tempsize;
- src.capacity_ = tempcapacity;
- }
- void String::PushBack(char character) {
- if (size_ == capacity_) {
- const size_t kCap = capacity_ + capacity_ + 100;
- Reserve(kCap);
- string_[size_] = character;
- size_++;
- string_[size_] = '\0';
- } else {
- string_[size_] = character;
- size_++;
- string_[size_] = '\0';
- }
- }
- void String::Resize(size_t new_size) {
- if (new_size == capacity_) {
- return;
- }
- char* temp = new char[new_size + 1];
- size_t edge = size_;
- if (size_ > new_size) {
- edge = new_size;
- }
- const size_t kEdge = edge;
- for (size_t i = 0; i < kEdge; ++i) {
- temp[i] = string_[i];
- }
- temp[kEdge] = '\0';
- capacity_ = new_size;
- delete[] string_;
- string_ = temp;
- size_ = new_size;
- }
- void String::Resize(size_t new_size, char character) { // here
- if (new_size == capacity_) {
- return;
- }
- char* temp = new char[new_size + 1];
- if (string_ != nullptr) {
- size_t edge = size_;
- if (size_ > new_size) {
- edge = new_size;
- }
- const size_t kEdge = edge;
- for (size_t i = 0; i < kEdge; ++i) {
- temp[i] = string_[i];
- }
- temp[kEdge] = '\0';
- }
- capacity_ = new_size;
- delete[] string_;
- string_ = temp;
- size_ = new_size;
- const size_t kOldend = strlen(string_) - 1;
- for (size_t i = kOldend; i < new_size; ++i) {
- string_[i] = character;
- }
- string_[new_size] = '\0';
- }
- void String::PopBack() {
- if (!Empty()) {
- Back() = '\0';
- string_[size_] = 0;
- size_--;
- }
- }
- String::String() {
- size_ = static_cast<unsigned long>(0);
- capacity_ = static_cast<unsigned long>(0);
- string_ = nullptr;
- }
- String::~String() { delete[] string_; }
- unsigned long String::Size() const { return size_; }
- unsigned long& String::Size() { return size_; }
- unsigned long& String::Capacity() { return capacity_; }
- unsigned long String::Capacity() const { return capacity_; }
- const char* String::Data() { return const_cast<const char*>(string_); }
- const char* String::Data() const { return const_cast<const char*>(string_); }
- bool String::Empty() const { return size_ == 0; }
- char& String::operator[](size_t index) { return string_[index]; }
- const char& String::operator[](size_t index) const { return string_[index]; }
- char& String::Front() { return string_[0]; }
- const char& String::Front() const { return string_[0]; }
- const char& String::Back() const { return string_[strlen(string_) - 1]; }
- char& String::Back() { return string_[strlen(string_) - 1]; }
- void String::Clear() {
- size_ = 0;
- // capacity_ = 0;
- // string_ = {0};
- }
- void String::ShrinkToFit() {
- if (size_ < capacity_) {
- const size_t kCap = size_;
- char* temp = new char[kCap + 1];
- if (string_ != nullptr) {
- // memcpy(temp, string_, kCap);
- for (size_t i = 0; i < kCap; ++i) {
- temp[i] = string_[i];
- }
- }
- temp[kCap] = '\0';
- capacity_ = kCap;
- delete[] string_;
- string_ = temp;
- size_ = kCap;
- }
- }
- void String::Reserve(size_t new_cap) {
- if (capacity_ < new_cap) {
- char* temp = new char[new_cap + 1];
- if (string_ != nullptr) {
- // memcpy(temp, string_, strlen(string_));
- const size_t kEdge = strlen(string_);
- for (size_t i = 0; i < kEdge; ++i) {
- temp[i] = string_[i];
- }
- temp[kEdge] = '\0';
- }
- delete[] string_;
- string_ = temp;
- capacity_ = new_cap;
- }
- }
- std::ostream& operator<<(std::ostream& os, const String& str) {
- if (str.string_ == nullptr) {
- return os;
- }
- os << str.string_;
- return os;
- }
- std::istream& operator>>(std::istream& is, String& str) {
- char symbol = 0;
- while (is.get(symbol)) {
- str.PushBack(symbol);
- }
- return is;
- }
- String String::operator+(const String& other) const {
- String temp = *this;
- temp += other;
- return temp;
- }
- String& String::operator+=(const String& other) {
- if (capacity_ < size_ + other.size_) {
- Reserve(size_ + other.size_);
- }
- for (size_t i = 0; i < other.size_; ++i) {
- PushBack(other[i]);
- }
- return *this;
- }
- String& String::operator*=(size_t n) {
- if (n == 0) {
- Clear();
- return *this;
- }
- Reserve(size_ * n + 1);
- String copy = *this;
- for (size_t i = 0; i < n - 1; ++i) {
- *this += copy;
- }
- // string_[size_] = '\0';
- return *this;
- }
- String String::operator*(size_t n) const {
- String copy = *this;
- return copy *= n;
- }
- String String::Join(const std::vector<String>& strings) const {
- String result;
- // result += *this;
- for (size_t i = 0; i < strings.size(); ++i) {
- result += strings[i];
- if (i < strings.size() - 1) {
- result += *this;
- }
- }
- return result;
- }
- std::vector<String> String::Split(const char* separator) const {
- std::vector<String> result;
- String temp;
- for (size_t i = 0; i < size_; ++i) {
- if (string_[i] == separator[0]) {
- String check;
- for (size_t j = i; j < strlen(separator) + i; ++j) {
- check.PushBack(string_[j]);
- }
- if (check.Empty() && strlen(separator) == 0) {
- temp.ShrinkToFit();
- result.push_back(temp);
- temp.Free();
- } else if (strcmp(check.Data(), separator) == 0) {
- temp.ShrinkToFit();
- result.push_back(temp);
- temp.Free();
- } else if (strcmp(check.Data(), separator) != 0) {
- for (size_t k = i; k < i + strlen(separator); ++k) {
- temp.PushBack(string_[k]);
- }
- }
- i += strlen(separator) - 1;
- } else {
- temp.PushBack(string_[i]);
- }
- }
- temp.ShrinkToFit();
- result.push_back(temp);
- return result;
- }
- void String::Free() { size_ = 0; }
- bool String::operator>=(const String& other) const {
- return strcmp(const_cast<const char*>(string_),
- const_cast<const char*>(other.string_)) >= 0;
- }
- bool String::operator>(const String& other) const {
- return strcmp(const_cast<const char*>(string_),
- const_cast<const char*>(other.string_)) > 0;
- }
- bool String::operator<(const String& other) const {
- return strcmp(const_cast<const char*>(string_),
- const_cast<const char*>(other.string_)) < 0;
- }
- bool String::operator!=(const String& other) const {
- return strcmp(const_cast<const char*>(string_),
- const_cast<const char*>(other.string_)) != 0;
- }
- bool String::operator==(const String& other) const {
- if (size_ == 0 && other.size_ == 0) {
- return true;
- }
- return strcmp(const_cast<const char*>(string_),
- const_cast<const char*>(other.string_)) == 0;
- }
- bool String::operator<=(const String& other) const {
- return strcmp(const_cast<const char*>(string_),
- const_cast<const char*>(other.string_)) <= 0;
- }
- int main() {
- String s;
- s.Size() = -1;
- s.Capacity() = -1;
- if (s.Size() > 0) {
- std::cout << "ok\n";
- }
- if (s.Capacity() > 0) {
- std::cout << "ok\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement