summaryrefslogtreecommitdiffhomepage
path: root/diff.cpp
blob: 35ff2707fd66f7f3f75697c15ca54d5190f0177e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "diff.h"

#include <algorithm>
#include <iostream>
#include <sstream>

#include <boost/property_tree/xml_parser.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/algorithm/string.hpp>

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<binary_from_base64<std::string::const_iterator>, 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<transform_width<std::string::const_iterator, 6, 8>>;
 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()
{
}

Diff::Diff(const std::string& old_version, const std::string& new_version)
{
 create(old_version, new_version);
}

std::string Diff::apply(const std::string& old_version) const
{
 std::string result{old_version};

 if (m_pos0 <= m_pos1 && m_pos1 <= old_version.size()) {
  result.erase(m_pos0, m_pos1 - m_pos0);
  result.insert(m_pos0, decode64(m_data));
 }
 
 return result;
}

void Diff::create(const std::string& old_version, const std::string& new_version)
{
 auto front_mismatch{std::mismatch(old_version.cbegin(), old_version.cend(), new_version.cbegin(), new_version.cend())};
 std::string::difference_type old_pos0 {front_mismatch.first - old_version.cbegin()};
 auto& new_pos0 {old_pos0};

 // equal
 if (old_pos0 == old_version.size() && new_pos0 == new_version.size()) {
  m_pos0 = 0;
  m_pos1 = 0;
  m_data.clear();
  return;
 }

 // append at end
 if (old_pos0 == old_version.size()) {
  m_pos0 = old_pos0;
  m_pos1 = old_pos0;
  m_data = encode64(new_version.substr(new_pos0));
  return;
 }

 // remove from end
 if (new_pos0 == new_version.size()) {
  m_pos0 = old_pos0;
  m_pos1 = old_version.size();
  m_data.clear();
  return;
 }

 auto back_mismatch{std::mismatch(old_version.crbegin(), old_version.crend(), new_version.crbegin(), new_version.crend())};
 // i.e. the indices starting from which we can assume equality
 size_t old_pos1 {old_version.size() - (back_mismatch.first - old_version.crbegin())};
 size_t new_pos1 {new_version.size() - (back_mismatch.second - new_version.crbegin())};

 // complete equality is already handled above
 
 // insert at start
 if (old_pos1 == 0) {
  m_pos0 = 0;
  m_pos1 = 0;
  m_data = encode64(new_version.substr(0, new_pos1));
  return;
 }

 // remove from start
 if (new_pos1 == 0) {
  m_pos0 = 0;
  m_pos1 = old_pos1;
  m_data.clear();
  return;
 }

 // re-adjust if search crossed
 if (old_pos0 > old_pos1) {
  old_pos0 = old_pos1;
  new_pos0 = old_pos1;
 }
 if (new_pos0 > new_pos1) {
  old_pos0 = new_pos1;
  new_pos0 = new_pos1;
 }

 // insert in the middle
 if (old_pos0 == old_pos1) {
  m_pos0 = old_pos0;
  m_pos1 = old_pos0;
  m_data = encode64(new_version.substr(new_pos0, new_pos1 - new_pos0));
  return;
 }
 
 // remove from the middle
 if (new_pos0 == new_pos1) {
  m_pos0 = old_pos0;
  m_pos1 = old_pos1;
  m_data.clear();
  return;
 }

 // last resort: remove and add in the middle
 m_pos0 = old_pos0;
 m_pos1 = old_pos1;
 m_data = encode64(new_version.substr(old_pos0, new_pos1 - new_pos0));
}

Diff::Diff(const boost::property_tree::ptree& ptree)
{
 create(ptree);
}

void Diff::create(const boost::property_tree::ptree& ptree)
{
 m_pos0 = ptree.get<int>("diff.start");
 m_pos1 = ptree.get<int>("diff.end");

 if (m_pos0 > m_pos1)
  throw std::runtime_error("Bad range in diff");

 m_data = ptree.get<std::string>("diff.data");
}

Diff::Diff(const std::string& xml)
{
 create(xml);
}

void Diff::create(const std::string& xml)
{
 pt::ptree ptree;
 std::istringstream ss{xml};
 pt::read_xml(ss, ptree);

 create(ptree);
}

bool Diff::empty() const
{
 return m_pos0 == m_pos1 && m_data.empty();
}

boost::property_tree::ptree Diff::get_structure() const
{
 pt::ptree ptree;
 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;
}

std::string Diff::get_xml() const
{
 pt::ptree ptree{get_structure()};

 std::ostringstream oss;
 // write_xml_element instead of write_xml to omit <!xml...> header
 //pt::xml_parser::write_xml(oss, xml);
 pt::xml_parser::write_xml_element(oss, {}, ptree, -1, boost::property_tree::xml_writer_settings<pt::ptree::key_type>{});
 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());
 }

}