Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
/**
* DocumentRow.cpp
*
* Author: Jamey Calabrese
*/
#include "DocumentRow.h"
using namespace std;
DocumentRow :: DocumentRow(int maxWidth)
: maxWidth(maxWidth)
{
}
DocumentRow :: ~DocumentRow()
{
ClearRow();
}
void DocumentRow :: ClearRow()
{
row.clear();
}
int DocumentRow :: GetLength()
{
int length = row.size() != 0 ? row.size()-1 : 0;
for (shared_ptr<string> word : row) {
length += word->length();
}
return length;
}
int DocumentRow :: GetFirstWordLength()
{
int length = 0;
if (row.size()) {
length = row[0]->length();
}
return length;
}
int DocumentRow :: GetLastWordLength()
{
int length = 0;
if (row.size()) {
auto itr = row.rbegin();
length = (*itr)->length();
}
return length;
}
bool DocumentRow :: AppendWord(shared_ptr<string> word)
{
bool wordFits;
int length = GetLength();
if (!length) {
wordFits = word->length() <= maxWidth;
} else {
wordFits = (length + 1 + word->length()) <= maxWidth;
}
if (wordFits) { row.push_back(word); }
return wordFits;
}
string DocumentRow :: GetString()
{
string line;
for (int i = 0; i<row.size(); ++i) {
for (char c : *(row[i])) {
line.push_back(c);
}
line.push_back(' ');
}
if (row.size()) { line.pop_back(); }
return line;
}