Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class String
- {
- uint32_t size;
- uint32_t capacity;
- char* str;
- public:
- String(const char* strP)
- : size{ strP ? strlen(strP) : 0 }, capacity{ size + 1 }, str{ new char[capacity] }
- {
- if (strP)
- {
- strcpy_s(str, capacity, strP);
- }
- else
- {
- *str = '\0';
- }
- };
- String(uint32_t reserveSize)
- : size{ 0 }, capacity{ reserveSize }, str{new char[reserveSize]}
- {
- *str = '\0';
- };
- String(const String& stringP);
- String& operator=(const String& stringP);
- String& operator=(const char* strP);
- uint32_t length()const { return size; };
- uint32_t getCapacity()const { return capacity; };
- const char* c_str()const { return str; };
- String& reserve(uint32_t reserveSize);
- String& shrinkToFit();
- friend std::ostream& operator<<(std::ostream& out, const String& lhs);
- ~String() { delete[] str; };
- };
- String::String(const String& stringP)
- :size{ stringP.size }, capacity{ size + 1 }, str{ new char[capacity] }
- {
- strcpy_s(str, capacity, stringP.str);
- };
- String& String::operator=(const String& stringP)
- {
- if (this != &stringP)
- {
- size = stringP.size;
- if (size + 1 > capacity)
- {
- delete[] str;
- capacity = size + 1;
- str = new char[capacity];
- }
- strcpy_s(str, capacity, stringP.str);
- }
- return *this;
- }
- String& String::operator=(const char* strP)
- {
- size = strlen(strP);
- if (size + 1 > capacity)
- {
- delete[] str;
- capacity = size + 1;
- str = new char[capacity];
- }
- strcpy_s(str, capacity, strP);
- return *this;
- }
- String& String::reserve(uint32_t reserveSize)
- {
- if (str and (reserveSize > capacity))
- {
- char* newStr{ new char[reserveSize] };
- strcpy_s(newStr, capacity, str);
- delete[] str;
- str = newStr;
- capacity = reserveSize;
- }
- return *this;
- }
- String& String::shrinkToFit()
- {
- if (capacity > size + 1)
- {
- capacity = size + 1;
- char* newStr{ new char[capacity] };
- strcpy_s(newStr, capacity, str);
- delete[] str;
- str = newStr;
- }
- return *this;
- }
- std::ostream& operator<<(std::ostream& out, const String& lhs)
- {
- out << lhs.str;
- return out;
- }
- int main()
- {
- char str[1024]{ "Hello!" };
- String str1{ "First string!" };
- std::cout << " str1 = " << str1
- << " str1.length = " << str1.length()
- << " str1.capacity = " << str1.getCapacity() << '\n';
- std::cout << '\n';
- String str2{""};
- std::cout << " str2 = " << str2
- << " str2.length = " << str2.length()
- << " str2.capacity = " << str2.getCapacity() << '\n';
- std::cout << '\n';
- str2 = "Assign test! :)";
- str2.reserve(100);
- str2.reserve(100);
- str2 = "Some really, really, REALLLLY long string!";
- str2.shrinkToFit();
- str2.shrinkToFit();
- std::cout << " str2 = " << str2
- << " str2.length = " << str2.length()
- << " str2.capacity = " << str2.getCapacity() << '\n';
- std::cout << '\n';
- String str3{ 5 };
- str3 = "Hi!";
- std::cout << " str3 = " << str3
- << " str3.length = " << str3.length()
- << " str3.capacity = " << str3.getCapacity() << '\n';
- String str4{""};
- str4 = str3;
- std::cout << " str4 = " << str4
- << " str4.length = " << str4.length()
- << " str4.capacity = " << str4.getCapacity() << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement