Commit 05dd1054 authored by jan.koester's avatar jan.koester
Browse files

optimations

parent 4b73a138
Loading
Loading
Loading
Loading
+52 −46
Original line number Diff line number Diff line
@@ -98,6 +98,8 @@ libhtmlpp::HtmlString::HtmlString(char str) : HtmlString(){

libhtmlpp::HtmlString::HtmlString(const std::string& str) : HtmlString(){
    std::copy(str.begin(),str.end(),std::insert_iterator<std::vector<char>>(_Data,_Data.begin()));
    _Data.erase(std::remove(_Data.begin(), _Data.end(), '\0'), _Data.end());
    _Data.push_back('\0');
}

libhtmlpp::HtmlString::~HtmlString(){
@@ -115,6 +117,8 @@ void libhtmlpp::HtmlString::append(const std::string& src) {
    if(src.empty())
        return;
    std::copy(src.begin(),src.end(),std::back_inserter(_Data));
    _Data.erase(std::remove(_Data.begin(), _Data.end(), '\0'), _Data.end());
    _Data.push_back('\0');
}

void libhtmlpp::HtmlString::append(libhtmlpp::HtmlString& hstring) {
@@ -148,7 +152,7 @@ libhtmlpp::HtmlString & libhtmlpp::HtmlString::operator+=(libhtmlpp::HtmlString&

libhtmlpp::HtmlString & libhtmlpp::HtmlString::operator=(const std::string &src){
    clear();
    std::copy(src.begin(),src.end(),std::insert_iterator<std::vector<char>>(_Data,_Data.begin()));
    append(src);
    return *this;
}

@@ -168,7 +172,7 @@ libhtmlpp::HtmlString& libhtmlpp::HtmlString::operator<<(const char* src) {
}

libhtmlpp::HtmlString& libhtmlpp::HtmlString::operator<<(const std::string &src) {
    std::copy(src.begin(),src.end(),std::back_inserter(_Data));
    append(src);
    return *this;
}

@@ -213,19 +217,15 @@ size_t libhtmlpp::HtmlString::size() const{
    return _Data.size();
}

const std::string libhtmlpp::HtmlString::str(){
    _Data.erase(std::remove(_Data.begin(), _Data.end(), '\0'), _Data.end());
    _Data.push_back('\0');
const std::string libhtmlpp::HtmlString::str() const{
    return std::string(_Data.begin(),_Data.end());
}

const char *libhtmlpp::HtmlString::c_str(){
    _Data.erase(std::remove(_Data.begin(), _Data.end(), '\0'), _Data.end());
    _Data.push_back('\0');
const char *libhtmlpp::HtmlString::c_str() const{
    return _Data.data();
}

const std::vector<char>& libhtmlpp::HtmlString::data() {
const std::vector<char>& libhtmlpp::HtmlString::data() const{
    return _Data;
}

@@ -1774,33 +1774,13 @@ libhtmlpp::HtmlTable::~HtmlTable(){
}

libhtmlpp::HtmlTable::Row &libhtmlpp::HtmlTable::operator<<(const Row& row){
    // Creates a new, owning smart pointer for the destination row.
    std::unique_ptr<Row> newRow = std::make_unique<Row>();

    // Deep Copy of Columns (Correct: iterates source, creates new unique_ptr destination chain)
    for(Column *curco=row._firstColumn.get(); curco; curco=curco->_nextColumn.get()){
    std::unique_ptr<Row> newRow = std::make_unique<Row>(row);

        auto newColumn = std::make_unique<Column>(); // New unique_ptr column
        newColumn->Data = curco->Data;

        if(!newRow->_firstColumn){
            // First element: takes ownership
            newRow->_firstColumn = std::move(newColumn);
            newRow->_lastColumn = newRow->_firstColumn.get();
        }else{
            // Subsequent elements: links to the last raw pointer's unique_ptr member
            newRow->_lastColumn->_nextColumn = std::move(newColumn);
            newRow->_lastColumn = newRow->_lastColumn->_nextColumn.get();
        }
    }
    newRow->_count = row._count;

    // Transfer Row Ownership (Correct: moves unique_ptr into unique_ptr members)
    if (!_firstRow) {
        _firstRow = std::move(newRow); // Assuming _firstRow is std::unique_ptr<Row>
        _firstRow = std::move(newRow);
        _lastRow = _firstRow.get();
    } else {
        _lastRow->_nextRow = std::move(newRow); // Assuming Row::_nextRow is std::unique_ptr<Row>
        _lastRow->_nextRow = std::move(newRow);
        _lastRow = _lastRow->_nextRow.get();
    }

@@ -1808,8 +1788,6 @@ libhtmlpp::HtmlTable::Row &libhtmlpp::HtmlTable::operator<<(const Row& row){
    return *_lastRow;
}



libhtmlpp::HtmlTable::Row & libhtmlpp::HtmlTable::operator[](size_t pos){
    if(!_firstRow || _count<pos){
        libhtmlpp::HTMLException exp;
@@ -1830,20 +1808,17 @@ void libhtmlpp::HtmlTable::insert(libhtmlpp::HtmlElement* element){
    element->setTagname("table");
    for(Row *crow=_firstRow.get(); crow; crow=crow->_nextRow.get()){
        HtmlElement hrow("tr");

        for(Column *ccol=crow->_firstColumn.get(); ccol; ccol=ccol->_nextColumn.get() ){
            HtmlString buf;
            buf << "<td>";
            buf << ccol->Data.str();
            buf << "</td>";
            Element &element(buf.parse());
            hrow.appendChild(element);
            HtmlElement hcol("td");
            TextElement cellContent(ccol->Data.c_str());
            hcol.appendChild(cellContent);
            hrow.appendChild(hcol);
        }

        element->appendChild(&hrow);
    }
}


void libhtmlpp::HtmlTable::parse(libhtmlpp::HtmlElement* element){
}

@@ -1890,14 +1865,24 @@ libhtmlpp::HtmlTable::Column::Column(const libhtmlpp::HtmlTable::Column& col){
    Data = col.Data;
}

libhtmlpp::HtmlTable::Column::Column(const libhtmlpp::HtmlString &data){
libhtmlpp::HtmlTable::Column::Column(const std::string &data){
    _nextColumn=nullptr;
    Data=data;
}

libhtmlpp::HtmlTable::Column::Column(const HtmlString &data){
    _nextColumn=nullptr;
    Data=data.str();
}

libhtmlpp::HtmlTable::Column::~Column(){
}

libhtmlpp::HtmlTable::Column::Column(libhtmlpp::HtmlTable::Column&& col) noexcept {
    _nextColumn = std::move(col._nextColumn);
    Data = std::move(col.Data);
}

libhtmlpp::HtmlTable::Row::Row(){
    _firstColumn=nullptr;
    _lastColumn=nullptr;
@@ -1915,10 +1900,24 @@ libhtmlpp::HtmlTable::Row::Row(const libhtmlpp::HtmlTable::Row& row){
    _count=0;

    for(Column *curel=row._firstColumn.get(); curel; curel=curel->_nextColumn.get()){
        *this << curel->Data;
        *this << HtmlString(curel->Data);
    }
}

libhtmlpp::HtmlTable::Row& libhtmlpp::HtmlTable::Row::operator<<(Column &&col){
    std::unique_ptr<Column> ptr = std::make_unique<Column>(std::move(col));

    if(_firstColumn){
        _lastColumn->_nextColumn=std::move(ptr);
        _lastColumn=_lastColumn->_nextColumn.get();
    }else{
        _firstColumn= std::move(ptr);
        _lastColumn=_firstColumn.get();
    }
    ++_count;
    return *this;
}

libhtmlpp::HtmlTable::Row& libhtmlpp::HtmlTable::Row::operator<<(const Column &col){
    std::unique_ptr<Column> ptr = std::make_unique<Column>(col);

@@ -1933,12 +1932,19 @@ libhtmlpp::HtmlTable::Row& libhtmlpp::HtmlTable::Row::operator<<(const Column &c
    return *this;
}

libhtmlpp::HtmlTable::Row &libhtmlpp::HtmlTable::Row::operator<<(const libhtmlpp::HtmlString value){
libhtmlpp::HtmlTable::Row &libhtmlpp::HtmlTable::Row::operator<<(const libhtmlpp::HtmlString &value){
    Column col(value);
    *this << col;
    return *this;
}

libhtmlpp::HtmlTable::Row &libhtmlpp::HtmlTable::Row::operator<<(const std::string &value){
    Column col(value);
    *this << col;
    return *this;
}


libhtmlpp::HtmlTable::Row &libhtmlpp::HtmlTable::Row::operator<<(const char* value){
    HtmlString buf;
    buf << value;
+9 −5
Original line number Diff line number Diff line
@@ -280,9 +280,9 @@ namespace libhtmlpp {
        void                      clear();
        bool                      empty();

        const std::vector<char>&  data();
        const std::string                  str();
        const char                         *c_str();
        const std::vector<char>&  data() const;
        const std::string                  str() const;
        const char                         *c_str() const;

        libhtmlpp::Element &parse();

@@ -322,11 +322,13 @@ namespace libhtmlpp {

        class Column {
        public:
            HtmlString  Data;
            std::string  Data;
             ~Column();
             Column(const Column &col);
             Column();
            Column(const std::string &data);
            Column(const HtmlString &data);
            Column(Column&& col) noexcept;
        private:
            std::unique_ptr<Column> _nextColumn;
            friend class HtmlTable;
@@ -338,8 +340,10 @@ namespace libhtmlpp {
            Row(const Row &row);
            ~Row();

            Row &operator<<(Column &&col);
            Row& operator<<(const Column &col);
            Row& operator<<(const HtmlString  value);
            Row& operator<<(const HtmlString  &value);
            Row& operator<<(const std::string  &value);
            Row& operator<<(const char* value);
            Row& operator<<(int value);