Commit bcffd738 authored by aggsol's avatar aggsol 🙉

fix writer bug

parent cc7a52eb
......@@ -36,8 +36,14 @@ namespace bodhi
*
* Taken from: https://stackoverflow.com/questions/4643512/replace-substring-with-another-substring-c
*/
inline std::string replaceSubStr(std::string& str, const std::string& target, const std::string& sub)
inline std::string replaceSubStr(std::string& str, const std::string& target,
const std::string& sub)
{
if(target == sub || target.empty())
{
return str;
}
size_t index = 0;
while (true)
{
......
......@@ -49,7 +49,7 @@ namespace bodhi
std::default_random_engine generator(seed);
const int minDelta = std::max<int>(3, numbers.size()/10);
const int maxAttempts = std::max<int>(10, numbers.size()/10);
const int maxAttempts = std::max<int>(10, numbers.size()/4);
std::cout << "Shuffle: minDelta=" << minDelta << " maxAttempts=" << maxAttempts << "\n";
......
......@@ -58,7 +58,8 @@ namespace
<< std::endl;
}
void writeSections(std::map<int, bodhi::Section>& sections, const std::map<int, int> resultMapping,
void writeSections(std::map<int, bodhi::Section>& sections,
const std::map<int, int> resultMapping,
std::ostream& strm)
{
std::map<int, bodhi::Section> final;
......@@ -66,31 +67,61 @@ namespace
for(auto& s: sections)
{
assert(s.first == s.second.m_number);
int shuffled = resultMapping.at(s.first);
const int oldSection = s.first;
const int shuffled = resultMapping.at(s.first);
std::cerr << s.first << " -> " << shuffled << "\n";
s.second.m_number = shuffled;
// First pass substitution
std::map<std::string, std::string> placeholders;
for(auto& c: s.second.m_children)
{
std::string before = "<<section-";
before += std::to_string(c);
before += ">>";
std::string placeholder = "<<temp-";
placeholder += std::to_string(c);
placeholder += "-";
placeholder += std::to_string(resultMapping.at(c));
placeholder += ">>";
std::string after = "<<section-";
after += std::to_string(resultMapping.at(c));
after += ">>";
placeholders[placeholder] = after;
if(oldSection == 22)
{
std::cerr << "before='" << before << "'\t "
<< "temp ='" << placeholder << "'\t "
<< "after ='" << after << "'\n";
}
for(auto& l: s.second.m_lines)
{
l = bodhi::replaceSubStr(l, before, after);
l = bodhi::replaceSubStr(l, before, placeholder);
}
}
final[shuffled] = s.second;
// Second pass substitution
for(auto& l: s.second.m_lines)
{
for(auto& p: placeholders)
{
l = bodhi::replaceSubStr(l, p.first, p.second);
}
}
final[shuffled] = s.second;
}
for(auto& f: final)
{
assert(f.first == f.second.m_number);
strm << "[[section-" << f.second.m_number << "]]\n"
<< "=== " << f.second.m_number << "\n";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment