From a61c702d91d7444ce0bb094ddccc70f72416500b Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 28 Jan 2023 15:07:14 +0100 Subject: Added WebAssembly for C++ implementation of Diff --- diff.cpp | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'diff.cpp') diff --git a/diff.cpp b/diff.cpp index b3ed5ce..ee17fad 100644 --- a/diff.cpp +++ b/diff.cpp @@ -102,12 +102,28 @@ void Diff::create(const std::string& old_version, const std::string& new_version m_data = new_version.substr(old_pos0, new_pos1 - new_pos0); } +Diff::Diff(const std::string& xml) +{ + create(xml); +} + +void Diff::create(const std::string& xml) +{ + pt::ptree tree; + std::istringstream ss{xml}; + pt::read_xml(ss, tree, pt::xml_parser::no_comments | pt::xml_parser::trim_whitespace); + + m_pos0 = tree.get("diff.start"); + m_pos1 = tree.get("diff.end"); + m_data = tree.get("diff.data"); +} + boost::property_tree::ptree Diff::get_structure() const { pt::ptree ptree; - ptree.put("diff.chunk.start", std::to_string(m_pos0)); - ptree.put("diff.chunk.end", std::to_string(m_pos1)); - ptree.put("diff.chunk.data", m_data); + ptree.put("diff.start", std::to_string(m_pos0)); + ptree.put("diff.end", std::to_string(m_pos1)); + ptree.put("diff.data", m_data); return ptree; } @@ -123,3 +139,19 @@ std::string Diff::get_xml() const return oss.str(); } +extern "C" { + + const char* diff_create(const char* old_version, const char* new_version) + { + Diff diff{old_version, new_version}; + return strdup(diff.get_xml().c_str()); + } + + const char* diff_apply(const char* old_version, const char* diff) + { + Diff d{diff}; + + return strdup(d.apply(old_version).c_str()); + } + +} -- cgit v1.2.3