开发者

how to generate String difference vectors?

开发者 https://www.devze.com 2022-12-27 08:59 出处:网络
a bit of a vague question but I am looking for pointers as to how can I generate String diff vectors in C++. The scenario is such that given a paragraph I want to store the various differences(Edit, c

a bit of a vague question but I am looking for pointers as to how can I generate String diff vectors in C++. The scenario is such that given a paragraph I want to store the various differences(Edit, cut copy paste etc.) it goes through in a draft mode to review Audit history.

Any hints in this regard will be really a开发者_高级运维ppreciated.


An 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;
}

You can then add and pop Actions from a LIFO queue as you want. It's a simple example, you could also try to link it more to a string instead of always passing at as a param, but that's up to your own design. I know it's not 'real' diffing, but I think this solution to be closer coupled to the problem then really storing general string differences.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号