From 8d1b4f06375bf676c5cf825ba4b116271f3d44c5 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 30 Jan 2023 21:05:59 +0100 Subject: whiteboard v1.5 --- diff.cpp | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'diff.cpp') diff --git a/diff.cpp b/diff.cpp index 85d3167..35ff270 100644 --- a/diff.cpp +++ b/diff.cpp @@ -5,9 +5,44 @@ #include #include +#include +#include +#include +#include namespace pt = boost::property_tree; +namespace { + +// prevent usage of same functions from libreichwein to keep unit self-contained + +std::string decode64(const std::string &val) +{ +#if 0 + using namespace boost::archive::iterators; + using It = transform_width, 8, 6>; + return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)), It(std::end(val))), [](char c) { + return c == '\0'; + }); +#else + return val; +#endif +} + +std::string encode64(const std::string &val) +{ +#if 0 + using namespace boost::archive::iterators; + using It = base64_from_binary>; + auto tmp = std::string(It(std::begin(val)), It(std::end(val))); + return tmp.append((3 - val.size() % 3) % 3, '='); +#else + return val; +#endif +} + +} // namespace + Diff::Diff() { } @@ -23,7 +58,7 @@ std::string Diff::apply(const std::string& old_version) const if (m_pos0 <= m_pos1 && m_pos1 <= old_version.size()) { result.erase(m_pos0, m_pos1 - m_pos0); - result.insert(m_pos0, m_data); + result.insert(m_pos0, decode64(m_data)); } return result; @@ -47,7 +82,7 @@ void Diff::create(const std::string& old_version, const std::string& new_version if (old_pos0 == old_version.size()) { m_pos0 = old_pos0; m_pos1 = old_pos0; - m_data = new_version.substr(new_pos0); + m_data = encode64(new_version.substr(new_pos0)); return; } @@ -70,7 +105,7 @@ void Diff::create(const std::string& old_version, const std::string& new_version if (old_pos1 == 0) { m_pos0 = 0; m_pos1 = 0; - m_data = new_version.substr(0, new_pos1); + m_data = encode64(new_version.substr(0, new_pos1)); return; } @@ -96,7 +131,7 @@ void Diff::create(const std::string& old_version, const std::string& new_version if (old_pos0 == old_pos1) { m_pos0 = old_pos0; m_pos1 = old_pos0; - m_data = new_version.substr(new_pos0, new_pos1 - new_pos0); + m_data = encode64(new_version.substr(new_pos0, new_pos1 - new_pos0)); return; } @@ -111,7 +146,7 @@ void Diff::create(const std::string& old_version, const std::string& new_version // last resort: remove and add in the middle m_pos0 = old_pos0; m_pos1 = old_pos1; - m_data = new_version.substr(old_pos0, new_pos1 - new_pos0); + m_data = encode64(new_version.substr(old_pos0, new_pos1 - new_pos0)); } Diff::Diff(const boost::property_tree::ptree& ptree) @@ -139,7 +174,7 @@ void Diff::create(const std::string& xml) { pt::ptree ptree; std::istringstream ss{xml}; - pt::read_xml(ss, ptree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); + pt::read_xml(ss, ptree); create(ptree); } -- cgit v1.2.3