working stringview

master
nihonium 2 years ago
parent 66cefdfd63
commit d73bcb8c03
No known key found for this signature in database
GPG Key ID: 0251623741027CFC

@ -2,15 +2,17 @@
#include "miptstring.cpp" #include "miptstring.cpp"
#include "miptstringview.cpp" #include "miptstringview.cpp"
using namespace std; using namespace std;
int main() { int main() {
mipt::String str = "Meow!!"; mipt::String a = "abcd";
cout << str << endl; mipt::String b = "abce";
mipt::StringView sv = str; mipt::StringView av = a;
cout << "string view: " << sv << endl; mipt::StringView bv = b;
char a[] = "nyaa!"; //cout << (b < a) << endl;
mipt::StringView sv1 = a; //cout << (bv < av) << endl;
cout << "string view: " << sv1 << endl; cout << av.substr(1,10) << endl;
cout << sv1.at(200) << endl; av.remove_suffix(5);
cout << av << endl;
} }

@ -4,6 +4,8 @@
#include <algorithm> #include <algorithm>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
//#include "miptstringview.cpp"
using std::cout, std::cin, std::endl, std::size_t; using std::cout, std::cin, std::endl, std::size_t;
namespace mipt{ namespace mipt{
@ -39,7 +41,15 @@ public:
String() : String("") {} String() : String("") {}
String(const String& s) : String(s.cStr()) {} String(const String& s) : String(s.cStr()) {}
/* String(const mipt::StringView& sv) {
mSize = sv.size();
self.reserve(mSize);
for(int i = 0; i < mSize; ++i)
mpData[i] = sv[i];
mpData[mSize] = '\0';
}
*/
String(size_t n, char a) String(size_t n, char a)
{ {
resize(n); resize(n);
@ -218,4 +228,5 @@ std::istream& operator>>(std::istream& in, String& s)
return in; return in;
} }
} }

@ -44,12 +44,39 @@ public:
{ {
return mpData[i]; return mpData[i];
} }
bool 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 size() { size_t size() {
return mSize; return mSize;
} }
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;
}
void remove_prefix(size_t n) {
mSize -= n;
}
void remove_suffix(size_t n) {
mSize -= n;
mpData += n;
}
};
}; };
}
std::ostream& operator<<(std::ostream& out, mipt::StringView sv) { std::ostream& operator<<(std::ostream& out, mipt::StringView sv) {
size_t size = sv.size(); size_t size = sv.size();