#include #include "textblob.h" using namespace std; bool TextBlob::isInRange(i64 y,i64 x) const { return y>=0&&y<(i64)lines.size()&&x>=0&&x<(i64)lines[y].size(); } bool TextBlob::isInRangeP1(i64 y,i64 x) const { return y>=0&&y<=(i64)lines.size()&&x>=0&&(y==(i64)lines.size()?x==0:x<=(i64)lines[y].size()); } bool TextBlob::isInRange(i64 y) const { return y>=0&&y<(i64)lines.size(); } bool TextBlob::isInRangeP1(i64 y) const { return y>=0&&y<=(i64)lines.size(); } void TextBlob::checkInRange(i64 y,i64 x) const { if(!isInRange(y,x)){ throw out_of_range("Position y="+to_string(y)+" x="+to_string(x)+" not in range of TextBlob"); } } void TextBlob::checkInRangeP1(i64 y,i64 x) const { if(!isInRangeP1(y,x)){ throw out_of_range("Position y="+to_string(y)+" x="+to_string(x)+" not in range of TextBlob"); } } void TextBlob::checkInRange(i64 y) const { if(!isInRange(y)){ throw out_of_range("Position y="+to_string(y)+" not in range of TextBlob"); } } void TextBlob::checkInRangeP1(i64 y) const { if(!isInRangeP1(y)){ throw out_of_range("Position y="+to_string(y)+" not in range of TextBlob"); } } void TextBlob::clear(){ lines.clear(); } i64 TextBlob::read(istream &is){ lines.clear(); string ln; i64 count=0; while(getline(is,ln)){ count+=ln.size()+1; lines.push_back(move(ln)); } return count; } i64 TextBlob::write(ostream &os) const { i64 count=0; for(const string &line : lines){ os<=nchars)break; } if(y2==(i64)lines.size()){ throw out_of_range("Not enough characters present in TextBlob::erase(y,x,nchars)"); } string old; old.reserve(nchars); old.append(lines[y],x,string::npos); nchars-=old.size(); lines[y].erase(x); while(true){ if(nchars==0)return old; old+='\n'; nchars--; if(nchars==0){ if(y==(i64)lines.size()-1)return old; lines[y]+=lines[y+1]; lines.erase(lines.begin()+(y+1)); return old; } if(nchars<(i64)lines[y+1].size()){ old.append(lines[y+1],0,nchars); lines[y].append(lines[y+1],nchars,string::npos); lines.erase(lines.begin()+(y+1)); return old; } nchars-=lines[y+1].size(); old+=lines[y+1]; lines.erase(lines.begin()+(y+1)); } } string& TextBlob::operator[](i64 y){ checkInRange(y); return lines[y]; } const string& TextBlob::operator[](i64 y) const { checkInRange(y); return lines[y]; } string TextBlob::getLine(i64 y) const{ checkInRangeP1(y); if(y==(i64)lines.size())return {}; else return lines[y]; } i64 TextBlob::numLines() const { return lines.size(); } i64 TextBlob::lineLen(i64 y) const { checkInRangeP1(y); if(y==(i64)lines.size())return 0; return lines[y].size(); }