Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include "String.h"
  4.  
  5. String::String()
  6. {
  7.     initialization(nullptr);
  8. }
  9.  
  10. String::String(const char* cStyleString)
  11. {
  12.     initialization(cStyleString);
  13. }
  14.  
  15. String::String(const char symbol, const unsigned int amount)
  16. {
  17.     char* newString = new char[amount + 1];
  18.     for (int i = 0; i < amount; ++i)
  19.     {
  20.         newString[i] = symbol;
  21.     }
  22.     newString[amount] = '\0';
  23.     initialization(newString);
  24.     delete[] newString;
  25. }
  26.  
  27. String::String(const String& string)
  28. {
  29.     initialization(string.mData);
  30. }
  31.  
  32. String::~String()
  33. {
  34.     delete[] mData;
  35. }
  36.  
  37. void String::append(const char* cStyleString)
  38. {
  39.     if (cStyleString != nullptr)
  40.     {
  41.         char* result = concatenation(mData, cStyleString);
  42.         delete[] mData;
  43.         initialization(result);
  44.         delete[] result;
  45.     }
  46. }
  47.  
  48. void String::append(const String& string)
  49. {
  50.     this->append(string.mData);
  51. }
  52.  
  53. String& String::operator=(const String& string)
  54. {
  55.     if (this != &string)
  56.     {
  57.         delete[] mData;
  58.         initialization(string.mData);
  59.     }
  60.     return *this;
  61. }
  62.  
  63. String& String::operator+=(const String& string)
  64. {
  65.     this->append(string.mData);
  66.     return *this;
  67. }
  68.  
  69. String String::operator+(const String& string)
  70. {
  71.     String localString(*this);
  72.     localString.append(string);
  73.     return localString;
  74. }
  75.  
  76. void String::print() const
  77. {
  78.     std::cout << *this;
  79. }
  80.  
  81. unsigned int String::size() const
  82. {
  83.     return mDataSize;
  84. }
  85.  
  86. void String::clear()
  87. {
  88.     delete[] mData;
  89.     mData = nullptr;
  90.     mDataSize = 0;
  91. }
  92.  
  93. void String::initialization(const char* cStyleString)
  94. {
  95.     if (cStyleString)
  96.     {
  97.         mDataSize = (unsigned int)strlen(cStyleString);
  98.         mData = new char[mDataSize + 1];
  99.         strcpy(mData, cStyleString);
  100.         mData[mDataSize] = '\0';
  101.     }
  102.     else
  103.     {
  104.         mData = nullptr;
  105.         mDataSize = 0;
  106.     }
  107. }
  108.  
  109. char* String::concatenation(const char* firstString, const char* secondString)
  110. {
  111.     unsigned int firstSize = (firstString) ? (unsigned int)strlen(firstString) : 0;
  112.     unsigned int secondSize = (secondString) ? (unsigned int)strlen(secondString) : 0;
  113.  
  114.     char* result = new char[firstSize + secondSize + 1];
  115.  
  116.     for (int i = 0; i < firstSize; ++i)
  117.     {
  118.         result[i] = firstString[i];
  119.     }
  120.  
  121.     for (int i = 0; i < secondSize; ++i)
  122.     {
  123.         result[i + firstSize] = secondString[i];
  124.     }
  125.  
  126.     result[firstSize + secondSize] = '\0';
  127.     return result;
  128. }
  129.  
  130. std::ostream& operator<<(std::ostream& wave, const String& string)
  131. {
  132.     if (string.mData)
  133.     {
  134.         wave << string.mData;
  135.     }
  136.     return wave;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement