23std::size_t
replace_all(std::string &inout, std::string_view what, std::string_view with)
26 for (std::string::size_type pos{};
27 inout.npos != (pos = inout.find(what.data(), pos, what.length()));
28 pos += with.length(), ++count)
30 inout.replace(pos, what.length(), with.data(), with.length());
58int main(
int argc,
char **argv)
62 std::cout <<
"Usage: " << argv[0] <<
" (input file) (output file)" << std::endl;
68 if (!infile.is_open())
70 std::cout <<
"Error: the input file is corrupted" << std::endl;
74 bool ReadCode =
false;
75 bool ReadReplacement =
false;
77 while (std::getline(infile,
line))
87 if (
line.back() ==
':')
89 std::string tokens =
line;
93 std::regex word_regex(
"([^\\s]+)");
94 auto words_begin = std::sregex_iterator(tokens.begin(), tokens.end(), word_regex);
95 auto words_end = std::sregex_iterator();
96 std::vector<std::string> matched_words;
97 for (
auto i = words_begin; i != words_end; ++i)
99 matched_words.push_back((*i).str());
102 if (matched_words.size() == 0)
104 std::cout <<
"Warning: no tokens found in line " << linecount << std::endl;
108 if (matched_words.size() == 1 && matched_words[0] ==
"Code")
111 ReadReplacement =
false;
115 Token.emplace_back(matched_words);
118 ReadReplacement =
true;
131 std::regex word_regex(
"([^\\s]+)");
132 auto words_begin = std::sregex_iterator(
line.begin(),
line.end(), word_regex);
133 auto words_end = std::sregex_iterator();
134 std::vector<std::string> vreplacement;
135 for (
auto i = words_begin; i != words_end; ++i)
137 vreplacement.push_back((*i).str());
140 if (vreplacement.size() >
Token.back().size())
142 std::cout <<
"Warning: more replacements than needed in line " << linecount << std::endl;
143 for (
size_t i = 0; i < vreplacement.size() -
Token.back().size(); i++)
144 vreplacement.pop_back();
147 if (vreplacement.size() <
Token.back().size())
149 std::cout <<
"Warning: less replacements than needed in line " << linecount << std::endl;
150 for (
size_t i = 0; i <
Token.back().size() - vreplacement.size(); i++)
151 vreplacement.emplace_back(
"");
181 std::ofstream outfile;
182 outfile.open(argv[2]);