splitted
This commit is contained in:
		
							parent
							
								
									d73bcb8c03
								
							
						
					
					
						commit
						ec50d811ff
					
				
					 4 changed files with 235 additions and 191 deletions
				
			
		|  | @ -1,7 +1,7 @@ | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include "miptstring.cpp" | //#include "miptstring.cpp"
 | ||||||
| #include "miptstringview.cpp" | //#include "miptstringview.cpp"
 | ||||||
| 
 | #include "mipt.h" | ||||||
| 
 | 
 | ||||||
| using namespace std; | using namespace std; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										65
									
								
								seminar03_initialization/08_stringview/mipt.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								seminar03_initialization/08_stringview/mipt.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,65 @@ | ||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | #include <iostream> | ||||||
|  | 
 | ||||||
|  | namespace mipt { | ||||||
|  | 
 | ||||||
|  | class String  | ||||||
|  | { | ||||||
|  | private: | ||||||
|  |     size_t mSize        {0}; | ||||||
|  |     size_t mCapacity    {0}; | ||||||
|  |     char* mpData        {nullptr}; | ||||||
|  | public: | ||||||
|  |     String(const char* str); | ||||||
|  |     String(); | ||||||
|  |     String(const String& s); | ||||||
|  |     String(const mipt::StringView& sv); | ||||||
|  |     String(size_t n, char a); | ||||||
|  |     ~String(); | ||||||
|  |     void reserve(size_t capacity); | ||||||
|  |     void resize(size_t size); | ||||||
|  |     String& operator=(const String& right); | ||||||
|  |     String operator+(const String& b); | ||||||
|  |     String& operator+=(const String& right); | ||||||
|  |     bool operator==(const String& right) const; | ||||||
|  |     bool operator<(const String& right) const; | ||||||
|  |     bool operator<=(const String& right) const; | ||||||
|  |     bool operator!=(const String& right) const; | ||||||
|  |     bool operator>(const String& right) const; | ||||||
|  |     bool operator>=(const String& right) const; | ||||||
|  |     char& operator[](size_t i); | ||||||
|  |     const char& operator[](size_t i) const; | ||||||
|  |     char& at(size_t i); | ||||||
|  |     void clear(); | ||||||
|  |     void addCharacter(char c); | ||||||
|  |     size_t getSize()        const;  | ||||||
|  |     size_t getCapacity()    const; | ||||||
|  |     const char* cStr()      const; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | std::ostream& operator<<(std::ostream& out, const String& s); | ||||||
|  | std::istream& operator>>(std::istream& in, String& s);  | ||||||
|  | 
 | ||||||
|  | class StringView  | ||||||
|  | { | ||||||
|  | private: | ||||||
|  |     const char* mpData; | ||||||
|  |     size_t mSize; | ||||||
|  | public:   | ||||||
|  |     StringView(); | ||||||
|  |     StringView(const StringView& str); | ||||||
|  |     StringView(const mipt::String& s); | ||||||
|  |     StringView(const char* s); | ||||||
|  |     const char& at(size_t i); | ||||||
|  |     const char& operator[](size_t i) const; | ||||||
|  |     bool operator<(const StringView& right) const; | ||||||
|  |     size_t size() const; | ||||||
|  |     StringView substr(size_t pos, size_t count); | ||||||
|  |     void remove_prefix(size_t n);  | ||||||
|  |     void remove_suffix(size_t n); | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | std::ostream& operator<<(std::ostream& out, const StringView& sv); | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | @ -1,11 +1,8 @@ | ||||||
| #pragma once |  | ||||||
| 
 |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <algorithm> | #include <algorithm> | ||||||
| #include <cstdlib> | #include <cstdlib> | ||||||
| #include <cstring> | #include <cstring> | ||||||
| //#include "miptstringview.cpp"
 | #include "mipt.h" | ||||||
| 
 |  | ||||||
| using std::cout, std::cin, std::endl, std::size_t; | using std::cout, std::cin, std::endl, std::size_t; | ||||||
| 
 | 
 | ||||||
| namespace mipt{ | namespace mipt{ | ||||||
|  | @ -21,27 +18,16 @@ char* errorCheckedMalloc(size_t newCapacity) | ||||||
|     return result; |     return result; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 | String::String(const char* str)  | ||||||
| class String  |  | ||||||
| { | { | ||||||
| private: |     size_t strSize = std::strlen(str); | ||||||
|  |     resize(strSize); | ||||||
|  |     std::memcpy(mpData, str, mSize); | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
|     size_t mSize        {0}; | String::String() : String("") {} | ||||||
|     size_t mCapacity    {0}; | String::String(const String& s) : String(s.cStr()) {} | ||||||
|     char* mpData        {nullptr}; | String::String(const StringView& sv) { | ||||||
| 
 |  | ||||||
| public: |  | ||||||
| 
 |  | ||||||
|     String(const char* str)  |  | ||||||
|     { |  | ||||||
|         size_t strSize = std::strlen(str); |  | ||||||
|         resize(strSize); |  | ||||||
|         std::memcpy(mpData, str, mSize); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     String() : String("") {} |  | ||||||
|     String(const String& s) : String(s.cStr()) {} |  | ||||||
| /*    String(const mipt::StringView& sv) {
 |  | ||||||
|         mSize = sv.size(); |         mSize = sv.size(); | ||||||
|         self.reserve(mSize); |         self.reserve(mSize); | ||||||
|         for(int i = 0; i < mSize; ++i) |         for(int i = 0; i < mSize; ++i) | ||||||
|  | @ -49,164 +35,164 @@ public: | ||||||
|         mpData[mSize] = '\0'; |         mpData[mSize] = '\0'; | ||||||
|          |          | ||||||
|     } |     } | ||||||
| */ |  | ||||||
|     String(size_t n, char a) |  | ||||||
|     { |  | ||||||
|         resize(n); |  | ||||||
| 
 | 
 | ||||||
|         for (size_t i = 0; i < mSize; ++i) | String::String(size_t n, char a) | ||||||
|             mpData[i] = a; | { | ||||||
|     } |     resize(n); | ||||||
| 
 | 
 | ||||||
|     ~String() |     for (size_t i = 0; i < mSize; ++i) | ||||||
|     { |         mpData[i] = a; | ||||||
|         std::free(mpData); | } | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     void reserve(size_t capacity) | String::~String() | ||||||
|     { | { | ||||||
|         if (capacity <= mCapacity) |     std::free(mpData); | ||||||
|             return; | } | ||||||
| 
 | 
 | ||||||
|         mCapacity = std::max(2 * mCapacity, capacity); | void String::reserve(size_t capacity) | ||||||
|         char* newData = errorCheckedMalloc(mCapacity); | { | ||||||
|  |     if (capacity <= mCapacity) | ||||||
|  |         return; | ||||||
| 
 | 
 | ||||||
|         if (mpData) |     mCapacity = std::max(2 * mCapacity, capacity); | ||||||
|             std::memcpy(newData, mpData, mSize + 1); |     char* newData = errorCheckedMalloc(mCapacity); | ||||||
| 
 | 
 | ||||||
|         std::free(mpData); |     if (mpData) | ||||||
|         mpData = newData; |         std::memcpy(newData, mpData, mSize + 1); | ||||||
|     } | 
 | ||||||
|  |     std::free(mpData); | ||||||
|  |     mpData = newData; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     void resize(size_t size) | void String::resize(size_t size) | ||||||
|     { | { | ||||||
|         reserve(size + 1); |     reserve(size + 1); | ||||||
|         mSize = size; |     mSize = size; | ||||||
|         mpData[mSize] = '\0'; |     mpData[mSize] = '\0'; | ||||||
|     } | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     String& operator=(const String& right) | String& String::operator=(const String& right) | ||||||
|     { | { | ||||||
|         if (this == &right) |     if (this == &right) | ||||||
|             return *this; |  | ||||||
| 
 |  | ||||||
|         mSize = right.mSize; |  | ||||||
|         resize(mSize); |  | ||||||
| 
 |  | ||||||
|         std::memcpy(mpData, right.mpData, mSize + 1); |  | ||||||
| 
 |  | ||||||
|         return *this; |         return *this; | ||||||
|     } | 
 | ||||||
|  |     mSize = right.mSize; | ||||||
|  |     resize(mSize); | ||||||
|  | 
 | ||||||
|  |     std::memcpy(mpData, right.mpData, mSize + 1); | ||||||
|  | 
 | ||||||
|  |     return *this; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     String operator+(const String& b) | String String::operator+(const String& b) | ||||||
|  | { | ||||||
|  |     String result; | ||||||
|  |     result.resize(mSize + b.mSize); | ||||||
|  | 
 | ||||||
|  |     std::memcpy(result.mpData, mpData, mSize); | ||||||
|  |     std::memcpy(result.mpData + mSize, b.mpData, b.mSize); | ||||||
|  |     result.mpData[result.mSize] = '\0'; | ||||||
|  | 
 | ||||||
|  |     return result; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | String& String::operator+=(const String& right) | ||||||
|  | { | ||||||
|  |     *this = *this + right; | ||||||
|  |     return *this; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool String::operator==(const String& right) const | ||||||
|  | { | ||||||
|  |     if (mSize != right.mSize) | ||||||
|  |         return false; | ||||||
|  | 
 | ||||||
|  |     size_t i = 0; | ||||||
|  |     while (i < mSize && mpData[i] == right.mpData[i]) | ||||||
|  |         i++; | ||||||
|  | 
 | ||||||
|  |     return i == mSize; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool String::operator<(const String& right) const | ||||||
|  | { | ||||||
|  |     size_t i = 0; | ||||||
|  |     while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i]) | ||||||
|  |         i++; | ||||||
|  | 
 | ||||||
|  |     return mpData[i] < right.mpData[i]; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool String::operator<=(const String& right) const | ||||||
|  | { | ||||||
|  |     size_t i = 0; | ||||||
|  |     while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i]) | ||||||
|  |         i++; | ||||||
|  | 
 | ||||||
|  |     return mpData[i] <= right.mpData[i]; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool String::operator!=(const String& right) const | ||||||
|  | { | ||||||
|  |     return !(*this == right); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool String::operator>(const String& right) const | ||||||
|  | { | ||||||
|  |     return !(*this <= right); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool String::operator>=(const String& right) const | ||||||
|  | { | ||||||
|  |     return !(*this < right); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | char& String::operator[](size_t i) | ||||||
|  | { | ||||||
|  |     return mpData[i]; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | const char& String::operator[](size_t i) const | ||||||
|  | { | ||||||
|  |     return mpData[i]; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | char& String::at(size_t i) | ||||||
|  | { | ||||||
|  |     if (i >= mSize) | ||||||
|     { |     { | ||||||
|         String result; |         cout << "Error! Index is out of bounds." << endl; | ||||||
|         result.resize(mSize + b.mSize); |  | ||||||
| 
 |  | ||||||
|         std::memcpy(result.mpData, mpData, mSize); |  | ||||||
|         std::memcpy(result.mpData + mSize, b.mpData, b.mSize); |  | ||||||
|         result.mpData[result.mSize] = '\0'; |  | ||||||
| 
 |  | ||||||
|         return result; |  | ||||||
|     } |     } | ||||||
|  |     return mpData[i]; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
|     String& operator+=(const String& right) | void String::clear() | ||||||
|     { | { | ||||||
|         *this = *this + right; |     std::free(mpData); | ||||||
|         return *this; |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
|     bool operator==(const String& right) const |     mSize = 0; | ||||||
|     { |     mCapacity = 1; | ||||||
|         if (mSize != right.mSize) |     mpData = errorCheckedMalloc(mCapacity); | ||||||
|             return false; |     mpData[0] = '\0'; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
|         size_t i = 0; | void String::addCharacter(char c) | ||||||
|         while (i < mSize && mpData[i] == right.mpData[i]) | { | ||||||
|             i++; |     if (mSize + 1 == mCapacity) | ||||||
|  |         reserve(2 * mCapacity); | ||||||
| 
 | 
 | ||||||
|         return i == mSize; |     mpData[mSize] = c; | ||||||
|     } |     resize(mSize + 1); | ||||||
| 
 | } | ||||||
|     bool operator<(const String& right) const |  | ||||||
|     { |  | ||||||
|         size_t i = 0; |  | ||||||
|         while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i]) |  | ||||||
|             i++; |  | ||||||
| 
 |  | ||||||
|         return mpData[i] < right.mpData[i]; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     bool operator<=(const String& right) const |  | ||||||
|     { |  | ||||||
|         size_t i = 0; |  | ||||||
|         while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i]) |  | ||||||
|             i++; |  | ||||||
| 
 |  | ||||||
|         return mpData[i] <= right.mpData[i]; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     bool operator!=(const String& right) const |  | ||||||
|     { |  | ||||||
|         return !(*this == right); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     bool operator>(const String& right) const |  | ||||||
|     { |  | ||||||
|         return !(*this <= right); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     bool operator>=(const String& right) const |  | ||||||
|     { |  | ||||||
|         return !(*this < right); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     char& operator[](size_t i) |  | ||||||
|     { |  | ||||||
|         return mpData[i]; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     const char& operator[](size_t i) const |  | ||||||
|     { |  | ||||||
|         return mpData[i]; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     char& at(size_t i) |  | ||||||
|     { |  | ||||||
|         if (i >= mSize) |  | ||||||
|         { |  | ||||||
|             cout << "Error! Index is out of bounds." << endl; |  | ||||||
|         } |  | ||||||
|         return mpData[i]; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     void clear() |  | ||||||
|     { |  | ||||||
|         std::free(mpData); |  | ||||||
| 
 |  | ||||||
|         mSize = 0; |  | ||||||
|         mCapacity = 1; |  | ||||||
|         mpData = errorCheckedMalloc(mCapacity); |  | ||||||
|         mpData[0] = '\0'; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     void addCharacter(char c) |  | ||||||
|     { |  | ||||||
|         if (mSize + 1 == mCapacity) |  | ||||||
|             reserve(2 * mCapacity); |  | ||||||
| 
 |  | ||||||
|         mpData[mSize] = c; |  | ||||||
|         resize(mSize + 1); |  | ||||||
|     } |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|     size_t getSize()        const   {return mSize;} | size_t String::getSize()        const   {return mSize;} | ||||||
|     size_t getCapacity()    const   {return mCapacity;} | size_t String::getCapacity()    const   {return mCapacity;} | ||||||
|     const char* cStr()      const   {return mpData;} | const char* String::cStr()      const   {return mpData;} | ||||||
| }; | 
 | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| std::ostream& operator<<(std::ostream& out, const String& s)  | std::ostream& operator<<(std::ostream& out, const String& s)  | ||||||
|  | @ -228,5 +214,4 @@ std::istream& operator>>(std::istream& in, String& s) | ||||||
|     return in; |     return in; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,37 +1,29 @@ | ||||||
| #pragma once |  | ||||||
| 
 |  | ||||||
| #include <iostream> | #include <iostream> | ||||||
| #include <algorithm> | #include <algorithm> | ||||||
| #include <cstdlib> | #include <cstdlib> | ||||||
| #include <cstring> | #include <cstring> | ||||||
|  | #include "mipt.h" | ||||||
| using std::cout, std::cin, std::endl, std::size_t; | using std::cout, std::cin, std::endl, std::size_t; | ||||||
| 
 | 
 | ||||||
| namespace mipt { | namespace mipt { | ||||||
| 
 | 
 | ||||||
| class StringView  |     StringView::StringView() {; | ||||||
| { |  | ||||||
| private: |  | ||||||
|     const char* mpData; |  | ||||||
|     size_t mSize; |  | ||||||
|    |  | ||||||
| public:   |  | ||||||
|     StringView() {; |  | ||||||
|         mSize = 0; |         mSize = 0; | ||||||
|         mpData = nullptr; |         mpData = nullptr; | ||||||
|     } |     } | ||||||
|     StringView(const StringView& str) { |     StringView::StringView(const StringView& str) { | ||||||
|         mSize = str.mSize; |         mSize = str.mSize; | ||||||
|         mpData = str.mpData; |         mpData = str.mpData; | ||||||
|     } |     } | ||||||
|     StringView(const mipt::String& s) { |     StringView::StringView(const mipt::String& s) { | ||||||
|         mSize = s.getSize(); |         mSize = s.getSize(); | ||||||
|         mpData = s.cStr();   |         mpData = s.cStr();   | ||||||
|     } |     } | ||||||
|     StringView(const char* s) { |     StringView::StringView(const char* s) { | ||||||
|         mpData = s; |         mpData = s; | ||||||
|         mSize = strlen(s);  |         mSize = strlen(s);  | ||||||
|     } |     } | ||||||
|     const char& at(size_t i) |     const char& StringView::at(size_t i) | ||||||
|     { |     { | ||||||
|         if (i >= mSize) |         if (i >= mSize) | ||||||
|         { |         { | ||||||
|  | @ -40,11 +32,11 @@ public: | ||||||
|         } |         } | ||||||
|         return mpData[i]; |         return mpData[i]; | ||||||
|     } |     } | ||||||
|     const char& operator[](size_t i) |     const char& StringView::operator[](size_t i) const | ||||||
|     { |     { | ||||||
|         return mpData[i]; |         return mpData[i]; | ||||||
|     } |     } | ||||||
|     bool operator<(const StringView& right) const |     bool StringView::operator<(const StringView& right) const | ||||||
|     { |     { | ||||||
|         size_t i = 0; |         size_t i = 0; | ||||||
|         while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i]) |         while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i]) | ||||||
|  | @ -52,10 +44,10 @@ public: | ||||||
| 
 | 
 | ||||||
|         return mpData[i] < right.mpData[i]; |         return mpData[i] < right.mpData[i]; | ||||||
|     } |     } | ||||||
|     size_t size() { |     size_t StringView::size() const { | ||||||
|         return mSize; |         return mSize; | ||||||
|     } |     } | ||||||
|     StringView substr(size_t pos, size_t count) { |     StringView StringView::substr(size_t pos, size_t count) { | ||||||
|         if (pos > mSize)  |         if (pos > mSize)  | ||||||
|                 throw std::out_of_range("Error! Index is out of bounds."); |                 throw std::out_of_range("Error! Index is out of bounds."); | ||||||
|         if (pos + count > mSize) |         if (pos + count > mSize) | ||||||
|  | @ -65,22 +57,24 @@ public: | ||||||
|         result.mSize = count; |         result.mSize = count; | ||||||
|         return result; |         return result; | ||||||
|     } |     } | ||||||
|     void remove_prefix(size_t n) { |     void StringView::remove_prefix(size_t n) { | ||||||
|             mSize -= n; |             mSize -= n; | ||||||
|     } |     } | ||||||
|     void remove_suffix(size_t n) { |     void StringView::remove_suffix(size_t n) { | ||||||
|             mSize -= n; |             mSize -= n; | ||||||
|             mpData += n; |             mpData += n; | ||||||
|     } |     } | ||||||
|      | /*std::ostream& StringView::operator<<(std::ostream& out, mipt::StringView sv) {
 | ||||||
| }; |     size_t size = sv.size(); | ||||||
| 
 |     for (int i = 0; i < size; ++i) | ||||||
| }; |             out << sv[i]; | ||||||
| 
 |     return out;  | ||||||
| 
 | }*/ | ||||||
| std::ostream& operator<<(std::ostream& out, mipt::StringView sv) { | std::ostream& operator<<(std::ostream& out, const mipt::StringView& sv) { | ||||||
|     size_t size = sv.size(); |     size_t size = sv.size(); | ||||||
|     for (int i = 0; i < size; ++i) |     for (int i = 0; i < size; ++i) | ||||||
|             out << sv[i]; |             out << sv[i]; | ||||||
|     return out;  |     return out;  | ||||||
| } | } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  |  | ||||||
		Reference in a new issue