2022-10-22 18:26:37 +03:00
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2022-10-22 22:03:40 +03:00
|
|
|
#include "miptstring.h"
|
|
|
|
#include "miptstringview.h"
|
2022-10-22 18:26:37 +03:00
|
|
|
using std::cout, std::cin, std::endl, std::size_t;
|
|
|
|
|
|
|
|
namespace mipt {
|
|
|
|
|
2022-10-22 21:32:50 +03:00
|
|
|
StringView::StringView() {;
|
2022-10-22 18:26:37 +03:00
|
|
|
mSize = 0;
|
|
|
|
mpData = nullptr;
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
StringView::StringView(const StringView& str) {
|
2022-10-22 18:26:37 +03:00
|
|
|
mSize = str.mSize;
|
|
|
|
mpData = str.mpData;
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
StringView::StringView(const mipt::String& s) {
|
2022-10-22 18:26:37 +03:00
|
|
|
mSize = s.getSize();
|
|
|
|
mpData = s.cStr();
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
StringView::StringView(const char* s) {
|
2022-10-22 18:26:37 +03:00
|
|
|
mpData = s;
|
|
|
|
mSize = strlen(s);
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
const char& StringView::at(size_t i)
|
2022-10-22 18:26:37 +03:00
|
|
|
{
|
|
|
|
if (i >= mSize)
|
|
|
|
{
|
|
|
|
throw std::out_of_range("Error! Index is out of bounds.");
|
|
|
|
std::exit(1);
|
|
|
|
}
|
|
|
|
return mpData[i];
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
const char& StringView::operator[](size_t i) const
|
2022-10-22 18:26:37 +03:00
|
|
|
{
|
|
|
|
return mpData[i];
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
bool StringView::operator<(const StringView& right) const
|
2022-10-22 19:15:34 +03:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i])
|
|
|
|
i++;
|
|
|
|
|
|
|
|
return mpData[i] < right.mpData[i];
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
size_t StringView::size() const {
|
2022-10-22 18:26:37 +03:00
|
|
|
return mSize;
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
StringView StringView::substr(size_t pos, size_t count) {
|
2022-10-22 19:15:34 +03:00
|
|
|
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;
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
void StringView::remove_prefix(size_t n) {
|
2022-10-22 19:15:34 +03:00
|
|
|
mSize -= n;
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
void StringView::remove_suffix(size_t n) {
|
2022-10-22 19:15:34 +03:00
|
|
|
mSize -= n;
|
|
|
|
mpData += n;
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
/*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) {
|
2022-10-22 18:26:37 +03:00
|
|
|
size_t size = sv.size();
|
|
|
|
for (int i = 0; i < size; ++i)
|
|
|
|
out << sv[i];
|
|
|
|
return out;
|
|
|
|
}
|
2022-10-22 21:32:50 +03:00
|
|
|
};
|
|
|
|
|