cubicdaiya / dtl

diff template library written by C++
Other
281 stars 50 forks source link

When traversing SES_DELETE in uniHunks, beforeIdx suddenly decreases #19

Open qaqcatz opened 5 months ago

qaqcatz commented 5 months ago

Hello, I am traversing the uniHunks in dtl and attempting to print out each element (type, beforeIdx, afterIdx).

However, I noticed that when traversing consecutive SES_DELETE elements, the value of beforeIdx was reduced from 15321 to 1452:

...
-1 15321 0
-1 1452 0
...

Additionally, I tested with other diff tools and found that there is no text change in line 1452.

This is my code:

#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include <fstream>
#include "dtl/dtl.hpp"

namespace fs = std::filesystem;

void fatal(const std::string &msg) {
    std::cerr << msg << std::endl;
    exit(1);
}

void run(const fs::path &oldPath,
         const fs::path &newPath) {
    std::vector<std::string> oldLines;
    std::vector<std::string> newLines;

    std::ifstream oldFileFs(oldPath);
    if (!oldFileFs.is_open()) {
        fatal("Diff error: can not open oldFile: " +
                             oldPath.string());
    }
    std::string line;
    while (getline(oldFileFs, line)) {
        oldLines.push_back(line);
    }

    std::ifstream newFileFs(newPath);
    if (!newFileFs.is_open()) {
        fatal("Diff error: can not open newFile: " +
                             newPath.string());
    }
    while (getline(newFileFs, line)) {
        newLines.push_back(line);
    }

    dtl::Diff<std::string, std::vector<std::string>> diff(oldLines, newLines);
    diff.onHuge();
    diff.compose();
    diff.composeUnifiedHunks();
    auto uniHunks = diff.getUniHunks();

    for (const auto &hunk: uniHunks) {
        for (auto &diffLine: hunk.change) {
            auto &diffLineInfo = diffLine.second;

            std::cerr << diffLineInfo.type << " " <<
            diffLineInfo.beforeIdx << " " << diffLineInfo.afterIdx << std::endl;
        }
    }

}

int main() {
    run("../old.txt", "../new.txt");
}

This is my test files:

new.txt old.txt

qaqcatz commented 5 months ago

By the way, the result in getSes().getSequence() is correct, It seems that there is some problem within composeUnifiedHunks.