How to create line difference vectors?

a little vague question, but I'm looking for pointers as to how I can generate String diff vectors in C ++. The script is such that given the paragraph, I want to keep various differences (Edit, cut copy paste, etc.) which goes in draft mode to view the audit history.

Any hints in this regard would be really appreciated.

+2


a source to share


1 answer


Idea using C ++ polymorphism:

class Action
{
    public:
    virtual void revert(std::string& base) = 0;
};

class InsertAction : public Action
{
    private:
    int pos, len;
    public:
    InsertAction(int pos, std::string& base, const std::string& in) : len(in.size()), pos(pos)
    {
        base.insert(pos, in);
    }

    virtual void revert(std::string& base)
    {
        base.erase(pos,len);
    }
};

int main()
{
    std::string text("hello !");
    std::cout << text << std::endl;
    Action* action = new InsertAction(5, text, " world");
    std::cout << text << std::endl;
    action->revert(text);
    std::cout << text << std::endl;
    delete action;
}

      



Then you can add and call actions from the LIFO queue as you like. This is a simple example, you could also try to bind it more to a string rather than always passing as a parameter, but that depends on your own design. I know this is not "real", but I think this solution will be more closely related to the problem, and then actually preserve the overall line differences.

+4


a source







All Articles