From 55cf7a2bed5269c34809001c8d1b7f8d5f5954f4 Mon Sep 17 00:00:00 2001 From: Andrii Tkachenko Date: Thu, 8 Feb 2018 07:24:58 +0100 Subject: [PATCH] xlnt. rich_text copy constructor. --- include/xlnt/cell/rich_text.hpp | 20 +++++++++++++++----- source/cell/rich_text.cpp | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/include/xlnt/cell/rich_text.hpp b/include/xlnt/cell/rich_text.hpp index 37849580..546910aa 100644 --- a/include/xlnt/cell/rich_text.hpp +++ b/include/xlnt/cell/rich_text.hpp @@ -43,10 +43,15 @@ public: /// rich_text() = default; - /// - /// Constructs a rich text object with the given text and no font. - /// - rich_text(const std::string &plain_text); + /// + /// Constructs a rich text object with the given text and no font. + /// + rich_text(const std::string &plain_text); + + /// + /// Constructs a rich text object from other + /// + rich_text(const rich_text &other); /// /// Constructs a rich text object with the given text and font. @@ -89,7 +94,12 @@ public: /// void add_run(const rich_text_run &t); - /// + /// + /// Copies rich text object from other + /// + rich_text& operator=(const rich_text &rhs); + + /// /// Returns true if the runs that make up this text are identical to those in rhs. /// bool operator==(const rich_text &rhs) const; diff --git a/source/cell/rich_text.cpp b/source/cell/rich_text.cpp index 44bb5583..d8c3015e 100644 --- a/source/cell/rich_text.cpp +++ b/source/cell/rich_text.cpp @@ -38,6 +38,18 @@ rich_text::rich_text(const std::string &plain_text, const class font &text_font) { } +rich_text::rich_text(const rich_text &other) +{ + (*this) = other; +} + +rich_text& rich_text::operator=(const rich_text &rhs) +{ + runs_.clear(); + runs_ = rhs.runs_; + return (*this); +} + rich_text::rich_text(const rich_text_run &single_run) { add_run(single_run); @@ -56,6 +68,9 @@ void rich_text::plain_text(const std::string &s) std::string rich_text::plain_text() const { + if (runs_.size() == 1) + return runs_.begin()->first; + return std::accumulate(runs_.begin(), runs_.end(), std::string(), [](const std::string &a, const rich_text_run &run) { return a + run.first; }); }