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 "miptstringview.cpp"
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(5);
cout << av << endl;
}

@ -4,6 +4,8 @@
#include <algorithm>
#include <cstdlib>
#include <cstring>
//#include "miptstringview.cpp"
using std::cout, std::cin, std::endl, std::size_t;
namespace mipt{
@ -39,7 +41,15 @@ public:
String() : String("") {}
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)
{
resize(n);
@ -218,4 +228,5 @@ std::istream& operator>>(std::istream& in, String& s)
return in;
}
}

@ -44,12 +44,39 @@ public:
{
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() {
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) {
size_t size = sv.size();