Skip to content

Instantly share code, notes, and snippets.

@m-ou-se
Created April 9, 2013 12:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-ou-se/5345492 to your computer and use it in GitHub Desktop.
Save m-ou-se/5345492 to your computer and use it in GitHub Desktop.
Ugly hacky code-duplication finder.
#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
struct line {
std::string file;
size_t line_number;
std::string text;
line(std::string f, size_t l, std::string t) : file(f), line_number(l), text(t) {}
};
int main(int argc, char * * argv) {
size_t min_match_length = 30;
std::map<std::string, std::vector<size_t>> m;
std::vector<line> v;
for (int i = 1; i < argc; ++i) {
std::string fn = argv[i];
std::ifstream f{fn};
std::string s;
size_t l = 0;
while (std::getline(f, s)) {
++l;
s.erase(std::remove_if(s.begin(), s.end(), [](char c) { return std::isspace(c); }), s.end());
if (!s.empty()) v.emplace_back(fn, l, s);
}
}
for (size_t i = 0; i < v.size(); ++i) {
auto const & s = v[i].text;
auto & o = m[s];
bool found_match = false;
for (size_t j : o) {
size_t m = s.size();
size_t n = 1;
while (v[i+n].text == v[j+n].text) {
m += v[i+n].text.size();
++n;
}
if (m >= min_match_length) {
found_match = true;
std::cout << v[i].file << ":" << v[i].line_number << ": Duplicate of ... " << std::endl;
std::cout << v[j].file << ":" << v[j].line_number << ": ... this." << std::endl;
i += n;
break;
}
}
if (!found_match) o.push_back(i);
}
}
/*
Some duplicated shit to test with:
for (size_t i = 0; i < v.size(); ++i) {
auto const & s = v[i].text;
auto & o = m[s];
bool found_match = false;
for (size_t j : o) {
size_t m = s.size();
size_t n = 1;
while (v[i+n].text == v[j+n].text) {
m += v[i+n].text.size();
++n;
}
for (size_t i = 0; i < v.size(); ++i) {
auto const & s = v[i].text;
auto & o = m[s];
bool found_match = false;
for (size_t j : o) {
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment