Skip to content

Commit

Permalink
Add some dtl-cpp stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersbakken committed Aug 3, 2013
1 parent c925cd8 commit 0a5008d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ gr
/src/rtags-esprima.js
/.ninja_deps
/compile_commands.json
/tests/dtlcpptest/dtlcpptest
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
[submodule "src/estraverse"]
path = src/estraverse
url = https://github.com/Constellation/estraverse.git
[submodule "src/dtl-cpp"]
path = src/dtl-cpp
url = https://code.google.com/p/dtl-cpp/
1 change: 1 addition & 0 deletions src/dtl-cpp
Submodule dtl-cpp added at 3ae7a4
3 changes: 3 additions & 0 deletions tests/dtlcpptest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 2.8)
include_directories(${CMAKE_CURRENT_LIST_DIR}/../../src/dtl-cpp/dtl)
add_executable(dtlcpptest main.cpp)
98 changes: 98 additions & 0 deletions tests/dtlcpptest/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <string>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <stdint.h>
#include "dtl.hpp"

uint32_t translatePoint(uint32_t offset, const dtl::Diff<char, std::string> &diff)
{
const uint32_t old = offset;
const dtl::Ses<char> ses = diff.getSes();
const std::vector<std::pair<char, dtl::elemInfo> > seq = ses.getSequence();
int i = 0;
for (std::vector<std::pair<char, dtl::elemInfo> >::const_iterator it = seq.begin(); it != seq.end(); ++it) {
if (it->second.type == 1) {
if (it->second.afterIdx >= old || !offset) {
// printf("0 Breaking at(%c) because %d %lld\n", it->first, offset, it->second.afterIdx);
break;
}
--offset;
// printf("Reducing offset to %d for character %lld %lld %c\n", offset,
// it->second.afterIdx, it->second.beforeIdx, it->first);
} else if (it->second.type == -1) {
if (it->second.beforeIdx >= offset) {
// printf("1 Breaking at(%c) because %d %lld\n", it->first, offset, it->second.beforeIdx);
break;
}
++offset;
// printf("Increasing offset to %d for character %lld %lld %c\n", offset,
// it->second.afterIdx, it->second.beforeIdx, it->first);
} else if (it->second.afterIdx > old) {
// printf("Breaking at %c because of %lld/%d\n", it->first, it->second.afterIdx, offset);
break;
}

// printf("%d: %c %d %d %d\n", i++, it->first, it->second.type, it->second

// printf("%c %lld %lld %d\n", it->first,
// it->second.beforeIdx,
// it->second.afterIdx,
// it->second.type);
}
// printf("%d => %d\n", old, offset);
return offset;
}

int main(int argc, char **argv)
{
std::string a, b;
bool string = false;
uint32_t offset = 10;
for (int i=1; i<argc; ++i) {
if (!strcmp(argv[i], "--string") || !strcmp(argv[i], "-s")) {
string = true;
} else if (a.empty()) {
a = argv[i];
} else if (b.empty()) {
b = argv[i];
} else {
offset = atoi(argv[i]);
}
}
if (a.empty() || b.empty()) {
fprintf(stderr, "Not enough data\n");
return 1;
}

if (!string) {
std::string *files[] = { &a, &b, 0 };
for (int i=0; files[i]; ++i) {
FILE *f = fopen(files[i]->c_str(), "r");
if (!f) {
fprintf(stderr, "Can't open %s for reading\n", files[i]->c_str());
return 1;
}
files[i]->clear();
char buf[1024];
while (true) {
const int r = fread(buf, sizeof(char), sizeof(buf), f);
if (r <= 0)
break;
files[i]->append(buf, r);
}
fclose(f);
}
}

dtl::Diff<char, std::string> diff(a, b);
diff.compose();
diff.composeUnifiedHunks();
// diff.printSES(std::cout);
dtl::Diff<char, std::string>::printUnifiedFormat(diff.getUniHunks());
const uint32_t translated = translatePoint(offset, diff);
printf("%d => %d : '%c' => '%c'\n", offset, translated, b.at(offset), a.at(translated));

return 0;
}

0 comments on commit 0a5008d

Please sign in to comment.