|
|
|
@ -1,11 +1,8 @@
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
//#include "miptstringview.cpp"
|
|
|
|
|
|
|
|
|
|
#include "mipt.h"
|
|
|
|
|
using std::cout, std::cin, std::endl, std::size_t;
|
|
|
|
|
|
|
|
|
|
namespace mipt{
|
|
|
|
@ -21,27 +18,16 @@ char* errorCheckedMalloc(size_t newCapacity)
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class String
|
|
|
|
|
String::String(const char* str)
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
size_t mSize {0};
|
|
|
|
|
size_t mCapacity {0};
|
|
|
|
|
char* mpData {nullptr};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
String(const char* str)
|
|
|
|
|
{
|
|
|
|
|
size_t strSize = std::strlen(str);
|
|
|
|
|
resize(strSize);
|
|
|
|
|
std::memcpy(mpData, str, mSize);
|
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
String::String() : String("") {}
|
|
|
|
|
String::String(const String& s) : String(s.cStr()) {}
|
|
|
|
|
String::String(const StringView& sv) {
|
|
|
|
|
mSize = sv.size();
|
|
|
|
|
self.reserve(mSize);
|
|
|
|
|
for(int i = 0; i < mSize; ++i)
|
|
|
|
@ -49,164 +35,164 @@ public:
|
|
|
|
|
mpData[mSize] = '\0';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
String(size_t n, char a)
|
|
|
|
|
{
|
|
|
|
|
resize(n);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < mSize; ++i)
|
|
|
|
|
mpData[i] = a;
|
|
|
|
|
}
|
|
|
|
|
String::String(size_t n, char a)
|
|
|
|
|
{
|
|
|
|
|
resize(n);
|
|
|
|
|
|
|
|
|
|
~String()
|
|
|
|
|
{
|
|
|
|
|
std::free(mpData);
|
|
|
|
|
}
|
|
|
|
|
for (size_t i = 0; i < mSize; ++i)
|
|
|
|
|
mpData[i] = a;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void reserve(size_t capacity)
|
|
|
|
|
{
|
|
|
|
|
if (capacity <= mCapacity)
|
|
|
|
|
return;
|
|
|
|
|
String::~String()
|
|
|
|
|
{
|
|
|
|
|
std::free(mpData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mCapacity = std::max(2 * mCapacity, capacity);
|
|
|
|
|
char* newData = errorCheckedMalloc(mCapacity);
|
|
|
|
|
void String::reserve(size_t capacity)
|
|
|
|
|
{
|
|
|
|
|
if (capacity <= mCapacity)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (mpData)
|
|
|
|
|
std::memcpy(newData, mpData, mSize + 1);
|
|
|
|
|
mCapacity = std::max(2 * mCapacity, capacity);
|
|
|
|
|
char* newData = errorCheckedMalloc(mCapacity);
|
|
|
|
|
|
|
|
|
|
std::free(mpData);
|
|
|
|
|
mpData = newData;
|
|
|
|
|
}
|
|
|
|
|
if (mpData)
|
|
|
|
|
std::memcpy(newData, mpData, mSize + 1);
|
|
|
|
|
|
|
|
|
|
std::free(mpData);
|
|
|
|
|
mpData = newData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void resize(size_t size)
|
|
|
|
|
{
|
|
|
|
|
reserve(size + 1);
|
|
|
|
|
mSize = size;
|
|
|
|
|
mpData[mSize] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void String::resize(size_t size)
|
|
|
|
|
{
|
|
|
|
|
reserve(size + 1);
|
|
|
|
|
mSize = size;
|
|
|
|
|
mpData[mSize] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String& operator=(const String& right)
|
|
|
|
|
{
|
|
|
|
|
if (this == &right)
|
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
|
|
mSize = right.mSize;
|
|
|
|
|
resize(mSize);
|
|
|
|
|
String& String::operator=(const String& right)
|
|
|
|
|
{
|
|
|
|
|
if (this == &right)
|
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
|
|
std::memcpy(mpData, right.mpData, mSize + 1);
|
|
|
|
|
mSize = right.mSize;
|
|
|
|
|
resize(mSize);
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
std::memcpy(mpData, right.mpData, mSize + 1);
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
String String::operator+(const String& b)
|
|
|
|
|
{
|
|
|
|
|
String result;
|
|
|
|
|
result.resize(mSize + b.mSize);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
std::memcpy(result.mpData, mpData, mSize);
|
|
|
|
|
std::memcpy(result.mpData + mSize, b.mpData, b.mSize);
|
|
|
|
|
result.mpData[result.mSize] = '\0';
|
|
|
|
|
|
|
|
|
|
String& operator+=(const String& right)
|
|
|
|
|
{
|
|
|
|
|
*this = *this + right;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator==(const String& right) const
|
|
|
|
|
{
|
|
|
|
|
if (mSize != right.mSize)
|
|
|
|
|
return false;
|
|
|
|
|
String& String::operator+=(const String& right)
|
|
|
|
|
{
|
|
|
|
|
*this = *this + right;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
while (i < mSize && mpData[i] == right.mpData[i])
|
|
|
|
|
i++;
|
|
|
|
|
bool String::operator==(const String& right) const
|
|
|
|
|
{
|
|
|
|
|
if (mSize != right.mSize)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return i == mSize;
|
|
|
|
|
}
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
while (i < mSize && mpData[i] == right.mpData[i])
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
bool operator<(const String& right) const
|
|
|
|
|
{
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i])
|
|
|
|
|
i++;
|
|
|
|
|
return i == mSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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++;
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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++;
|
|
|
|
|
|
|
|
|
|
bool operator!=(const String& right) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this == right);
|
|
|
|
|
}
|
|
|
|
|
return mpData[i] <= right.mpData[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator>(const String& right) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this <= right);
|
|
|
|
|
}
|
|
|
|
|
bool String::operator!=(const String& right) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this == right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool operator>=(const String& right) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this < right);
|
|
|
|
|
}
|
|
|
|
|
bool String::operator>(const String& right) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this <= right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char& operator[](size_t i)
|
|
|
|
|
{
|
|
|
|
|
return mpData[i];
|
|
|
|
|
}
|
|
|
|
|
bool String::operator>=(const String& right) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this < right);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char& operator[](size_t i) const
|
|
|
|
|
{
|
|
|
|
|
return mpData[i];
|
|
|
|
|
}
|
|
|
|
|
char& String::operator[](size_t i)
|
|
|
|
|
{
|
|
|
|
|
return mpData[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char& at(size_t i)
|
|
|
|
|
const char& String::operator[](size_t i) const
|
|
|
|
|
{
|
|
|
|
|
return mpData[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char& String::at(size_t i)
|
|
|
|
|
{
|
|
|
|
|
if (i >= mSize)
|
|
|
|
|
{
|
|
|
|
|
if (i >= mSize)
|
|
|
|
|
{
|
|
|
|
|
cout << "Error! Index is out of bounds." << endl;
|
|
|
|
|
}
|
|
|
|
|
return mpData[i];
|
|
|
|
|
cout << "Error! Index is out of bounds." << endl;
|
|
|
|
|
}
|
|
|
|
|
return mpData[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
|
{
|
|
|
|
|
std::free(mpData);
|
|
|
|
|
void String::clear()
|
|
|
|
|
{
|
|
|
|
|
std::free(mpData);
|
|
|
|
|
|
|
|
|
|
mSize = 0;
|
|
|
|
|
mCapacity = 1;
|
|
|
|
|
mpData = errorCheckedMalloc(mCapacity);
|
|
|
|
|
mpData[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
mSize = 0;
|
|
|
|
|
mCapacity = 1;
|
|
|
|
|
mpData = errorCheckedMalloc(mCapacity);
|
|
|
|
|
mpData[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addCharacter(char c)
|
|
|
|
|
{
|
|
|
|
|
if (mSize + 1 == mCapacity)
|
|
|
|
|
reserve(2 * mCapacity);
|
|
|
|
|
void String::addCharacter(char c)
|
|
|
|
|
{
|
|
|
|
|
if (mSize + 1 == mCapacity)
|
|
|
|
|
reserve(2 * mCapacity);
|
|
|
|
|
|
|
|
|
|
mpData[mSize] = c;
|
|
|
|
|
resize(mSize + 1);
|
|
|
|
|
}
|
|
|
|
|
mpData[mSize] = c;
|
|
|
|
|
resize(mSize + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
@ -228,5 +214,4 @@ std::istream& operator>>(std::istream& in, String& s)
|
|
|
|
|
return in;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|