1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    The code included in this file is provided under the terms of the ISC license
11    http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12    To use, copy, modify, and/or distribute this software for any purpose with or
13    without fee is hereby granted provided that the above copyright notice and
14    this permission notice appear in all copies.
15 
16    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18    DISCLAIMED.
19 
20   ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 /**
27     Calculates and applies a sequence of changes to convert one text string into
28     another.
29 
30     Once created, the TextDiff object contains an array of change objects, where
31     each change can be either an insertion or a deletion. When applied in order
32     to the original string, these changes will convert it to the target string.
33 
34     @tags{Core}
35 */
36 class JUCE_API TextDiff
37 {
38 public:
39     /** Creates a set of diffs for converting the original string into the target. */
40     TextDiff (const String& original,
41               const String& target);
42 
43     /** Applies this sequence of changes to the original string, producing the
44         target string that was specified when generating them.
45 
46         Obviously it only makes sense to call this function with the string that
47         was originally passed to the constructor. Any other input will produce an
48         undefined result.
49     */
50     String appliedTo (String text) const;
51 
52     /** Describes a change, which can be either an insertion or deletion. */
53     struct Change
54     {
55         String insertedText; /**< If this change is a deletion, this string will be empty; otherwise,
56                                   it'll be the text that should be inserted at the index specified by start. */
57         int start;    /**< Specifies the character index in a string at which text should be inserted or deleted. */
58         int length;   /**< If this change is a deletion, this specifies the number of characters to delete. For an
59                            insertion, this is the length of the new text being inserted. */
60 
61         /** Returns true if this change is a deletion, or false for an insertion. */
62         bool isDeletion() const noexcept;
63 
64         /** Returns the result of applying this change to a string. */
65         String appliedTo (const String& original) const noexcept;
66     };
67 
68     /** The list of changes required to perform the transformation.
69         Applying each of these, in order, to the original string will produce the target.
70     */
71     Array<Change> changes;
72 };
73 
74 } // namespace juce
75