Compare commits
	
		
			3 commits
		
	
	
		
			66cefdfd63
			...
			2da7623dd3
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 2da7623dd3 | ||
|  | ec50d811ff | ||
|  | d73bcb8c03 | 
					 5 changed files with 286 additions and 194 deletions
				
			
		|  | @ -1,16 +1,18 @@ | |||
| #include <iostream> | ||||
| #include "miptstring.cpp" | ||||
| #include "miptstringview.cpp" | ||||
| 
 | ||||
| #include "miptstring.h" | ||||
| #include "miptstringview.h" | ||||
| using namespace std; | ||||
| 
 | ||||
| int main() { | ||||
|         mipt::String str = "Meow!!"; | ||||
|         cout << str << endl; | ||||
|         mipt::StringView sv = str; | ||||
|         cout << "string view: " << sv << endl;  | ||||
|         char a[] = "nyaa!"; | ||||
|         mipt::StringView sv1 = a; | ||||
|        cout << "string view: " << sv1 << endl;  | ||||
|        cout << sv1.at(200) << endl; | ||||
|     mipt::String a = "abcd"; | ||||
|     mipt::String b = "abce"; | ||||
|     mipt::StringView av = a; | ||||
|     mipt::StringView bv = b; | ||||
|     //cout << (b < a) << endl;
 | ||||
|     //cout << (bv < av) << endl;
 | ||||
|     cout << av.substr(1,10) << endl;  | ||||
|     av.remove_suffix(2); | ||||
|     cout << av << endl; | ||||
|     mipt::String meow = av; | ||||
|     cout << "sv to string: " << meow << endl; | ||||
| } | ||||
|  |  | |||
|  | @ -1,9 +1,9 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <iostream> | ||||
| #include <algorithm> | ||||
| #include <cstdlib> | ||||
| #include <cstring> | ||||
| #include "miptstring.h" | ||||
| #include "miptstringview.h" | ||||
| using std::cout, std::cin, std::endl, std::size_t; | ||||
| 
 | ||||
| namespace mipt{ | ||||
|  | @ -19,28 +19,25 @@ char* errorCheckedMalloc(size_t newCapacity) | |||
|     return result; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| class String  | ||||
| { | ||||
| private: | ||||
| 
 | ||||
|     size_t mSize        {0}; | ||||
|     size_t mCapacity    {0}; | ||||
|     char* mpData        {nullptr}; | ||||
| 
 | ||||
| public: | ||||
| 
 | ||||
|     String(const char* str)  | ||||
| String::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::String() : String("") {} | ||||
| String::String(const String& s) : String(s.cStr()) {} | ||||
| String::String(const StringView& sv) { | ||||
|         mSize = sv.size(); | ||||
|         (*this).reserve(mSize); | ||||
|         for(int i = 0; i < mSize; ++i) | ||||
|             mpData[i] = sv[i]; | ||||
|         mpData[mSize] = '\0'; | ||||
|          | ||||
|     String(size_t n, char a) | ||||
| } | ||||
| 
 | ||||
| String::String(size_t n, char a) | ||||
| { | ||||
|     resize(n); | ||||
| 
 | ||||
|  | @ -48,12 +45,12 @@ public: | |||
|         mpData[i] = a; | ||||
| } | ||||
| 
 | ||||
|     ~String() | ||||
| String::~String() | ||||
| { | ||||
|     std::free(mpData); | ||||
| } | ||||
| 
 | ||||
|     void reserve(size_t capacity) | ||||
| void String::reserve(size_t capacity) | ||||
| { | ||||
|     if (capacity <= mCapacity) | ||||
|         return; | ||||
|  | @ -69,7 +66,7 @@ public: | |||
| } | ||||
| 
 | ||||
| 
 | ||||
|     void resize(size_t size) | ||||
| void String::resize(size_t size) | ||||
| { | ||||
|     reserve(size + 1); | ||||
|     mSize = size; | ||||
|  | @ -77,7 +74,7 @@ public: | |||
| } | ||||
| 
 | ||||
| 
 | ||||
|     String& operator=(const String& right) | ||||
| String& String::operator=(const String& right) | ||||
| { | ||||
|     if (this == &right) | ||||
|         return *this; | ||||
|  | @ -91,7 +88,7 @@ public: | |||
| } | ||||
| 
 | ||||
| 
 | ||||
|     String operator+(const String& b) | ||||
| String String::operator+(const String& b) | ||||
| { | ||||
|     String result; | ||||
|     result.resize(mSize + b.mSize); | ||||
|  | @ -103,13 +100,13 @@ public: | |||
|     return result; | ||||
| } | ||||
| 
 | ||||
|     String& operator+=(const String& right) | ||||
| String& String::operator+=(const String& right) | ||||
| { | ||||
|     *this = *this + right; | ||||
|     return *this; | ||||
| } | ||||
| 
 | ||||
|     bool operator==(const String& right) const | ||||
| bool String::operator==(const String& right) const | ||||
| { | ||||
|     if (mSize != right.mSize) | ||||
|         return false; | ||||
|  | @ -121,7 +118,7 @@ public: | |||
|     return i == mSize; | ||||
| } | ||||
| 
 | ||||
|     bool operator<(const String& right) const | ||||
| bool String::operator<(const String& right) const | ||||
| { | ||||
|     size_t i = 0; | ||||
|     while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i]) | ||||
|  | @ -130,7 +127,7 @@ public: | |||
|     return mpData[i] < right.mpData[i]; | ||||
| } | ||||
| 
 | ||||
|     bool operator<=(const String& right) const | ||||
| bool String::operator<=(const String& right) const | ||||
| { | ||||
|     size_t i = 0; | ||||
|     while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i]) | ||||
|  | @ -139,32 +136,32 @@ public: | |||
|     return mpData[i] <= right.mpData[i]; | ||||
| } | ||||
| 
 | ||||
|     bool operator!=(const String& right) const | ||||
| bool String::operator!=(const String& right) const | ||||
| { | ||||
|     return !(*this == right); | ||||
| } | ||||
| 
 | ||||
|     bool operator>(const String& right) const | ||||
| bool String::operator>(const String& right) const | ||||
| { | ||||
|     return !(*this <= right); | ||||
| } | ||||
| 
 | ||||
|     bool operator>=(const String& right) const | ||||
| bool String::operator>=(const String& right) const | ||||
| { | ||||
|     return !(*this < right); | ||||
| } | ||||
| 
 | ||||
|     char& operator[](size_t i) | ||||
| char& String::operator[](size_t i) | ||||
| { | ||||
|     return mpData[i]; | ||||
| } | ||||
| 
 | ||||
|     const char& operator[](size_t i) const | ||||
| const char& String::operator[](size_t i) const | ||||
| { | ||||
|     return mpData[i]; | ||||
| } | ||||
| 
 | ||||
|     char& at(size_t i) | ||||
| char& String::at(size_t i) | ||||
| { | ||||
|     if (i >= mSize) | ||||
|     { | ||||
|  | @ -173,7 +170,7 @@ public: | |||
|     return mpData[i]; | ||||
| } | ||||
| 
 | ||||
|     void clear() | ||||
| void String::clear() | ||||
| { | ||||
|     std::free(mpData); | ||||
| 
 | ||||
|  | @ -183,7 +180,7 @@ public: | |||
|     mpData[0] = '\0'; | ||||
| } | ||||
| 
 | ||||
|     void addCharacter(char c) | ||||
| void String::addCharacter(char c) | ||||
| { | ||||
|     if (mSize + 1 == mCapacity) | ||||
|         reserve(2 * mCapacity); | ||||
|  | @ -193,10 +190,10 @@ public: | |||
| } | ||||
| 
 | ||||
| 
 | ||||
|     size_t getSize()        const   {return mSize;} | ||||
|     size_t getCapacity()    const   {return mCapacity;} | ||||
|     const char* cStr()      const   {return mpData;} | ||||
| }; | ||||
| size_t String::getSize()        const   {return mSize;} | ||||
| size_t String::getCapacity()    const   {return mCapacity;} | ||||
| const char* String::cStr()      const   {return mpData;} | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| std::ostream& operator<<(std::ostream& out, const String& s)  | ||||
|  |  | |||
							
								
								
									
										43
									
								
								seminar03_initialization/08_stringview/miptstring.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								seminar03_initialization/08_stringview/miptstring.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,43 @@ | |||
| #pragma once | ||||
| #include <iostream> | ||||
| 
 | ||||
| namespace mipt { | ||||
| 
 | ||||
| class StringView; | ||||
| 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);  | ||||
| } | ||||
|  | @ -1,37 +1,30 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <iostream> | ||||
| #include <algorithm> | ||||
| #include <cstdlib> | ||||
| #include <cstring> | ||||
| #include "miptstring.h" | ||||
| #include "miptstringview.h" | ||||
| using std::cout, std::cin, std::endl, std::size_t; | ||||
| 
 | ||||
| namespace mipt { | ||||
| 
 | ||||
| class StringView  | ||||
| { | ||||
| private: | ||||
|     const char* mpData; | ||||
|     size_t mSize; | ||||
|    | ||||
| public:   | ||||
|     StringView() {; | ||||
|     StringView::StringView() {; | ||||
|         mSize = 0; | ||||
|         mpData = nullptr; | ||||
|     } | ||||
|     StringView(const StringView& str) { | ||||
|     StringView::StringView(const StringView& str) { | ||||
|         mSize = str.mSize; | ||||
|         mpData = str.mpData; | ||||
|     } | ||||
|     StringView(const mipt::String& s) { | ||||
|     StringView::StringView(const mipt::String& s) { | ||||
|         mSize = s.getSize(); | ||||
|         mpData = s.cStr();   | ||||
|     } | ||||
|     StringView(const char* s) { | ||||
|     StringView::StringView(const char* s) { | ||||
|         mpData = s; | ||||
|         mSize = strlen(s);  | ||||
|     } | ||||
|     const char& at(size_t i) | ||||
|     const char& StringView::at(size_t i) | ||||
|     { | ||||
|         if (i >= mSize) | ||||
|         { | ||||
|  | @ -40,20 +33,49 @@ public: | |||
|         } | ||||
|         return mpData[i]; | ||||
|     } | ||||
|     const char& operator[](size_t i) | ||||
|     const char& StringView::operator[](size_t i) const | ||||
|     { | ||||
|         return mpData[i]; | ||||
|     } | ||||
|     size_t size() { | ||||
|     bool StringView::operator<(const StringView& right) const | ||||
|     { | ||||
|         size_t i = 0; | ||||
|         while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i]) | ||||
|             i++; | ||||
| 
 | ||||
|         return mpData[i] < right.mpData[i]; | ||||
|     } | ||||
|     size_t StringView::size() const { | ||||
|         return mSize; | ||||
|     } | ||||
| 
 | ||||
| }; | ||||
|     StringView StringView::substr(size_t pos, size_t count) { | ||||
|         if (pos > mSize)  | ||||
|                 throw std::out_of_range("Error! Index is out of bounds."); | ||||
|         if (pos + count > mSize) | ||||
|               count = mSize - pos; | ||||
|         StringView result; | ||||
|         result.mpData = mpData + pos; | ||||
|         result.mSize = count; | ||||
|         return result; | ||||
|     } | ||||
| 
 | ||||
| std::ostream& operator<<(std::ostream& out, mipt::StringView sv) { | ||||
|     void StringView::remove_prefix(size_t n) { | ||||
|             mSize -= n; | ||||
|     } | ||||
|     void StringView::remove_suffix(size_t n) { | ||||
|             mSize -= 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, const mipt::StringView& sv) { | ||||
|     size_t size = sv.size(); | ||||
|     for (int i = 0; i < size; ++i) | ||||
|             out << sv[i]; | ||||
|     return out;  | ||||
| } | ||||
| }; | ||||
| 
 | ||||
|  |  | |||
							
								
								
									
										28
									
								
								seminar03_initialization/08_stringview/miptstringview.h
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								seminar03_initialization/08_stringview/miptstringview.h
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,28 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <iostream> | ||||
| 
 | ||||
| namespace mipt { | ||||
| class String; | ||||
| 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); | ||||
| 
 | ||||
| } | ||||
		Reference in a new issue