scaffold  1.0.0
Generates files and directories based on templates and user input
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
diff_match_patch.h
1 /*
2  * Copyright 2008 Google Inc. All Rights Reserved.
3  * Author: fraser@google.com (Neil Fraser)
4  * Author: mikeslemmer@gmail.com (Mike Slemmer)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Diff Match and Patch
19  * http://code.google.com/p/google-diff-match-patch/
20  */
21 
22 #ifndef DIFF_MATCH_PATCH_H
23 #define DIFF_MATCH_PATCH_H
24 
25 /*
26  * Functions for diff, match and patch.
27  * Computes the difference between two texts to create a patch.
28  * Applies the patch onto another text, allowing for errors.
29  *
30  * @author fraser@google.com (Neil Fraser)
31  *
32  * Qt/C++ port by mikeslemmer@gmail.com (Mike Slemmer):
33  *
34  * Code known to compile and run with Qt 4.3 through Qt 4.7.
35  *
36  * Here is a trivial sample program which works properly when linked with this
37  * library:
38  *
39 
40  #include <QtCore>
41  #include <QString>
42  #include <QList>
43  #include <QMap>
44  #include <QVariant>
45  #include "diff_match_patch.h"
46  int main(int argc, char **argv) {
47  diff_match_patch dmp;
48  QString str1 = QString("First string in diff");
49  QString str2 = QString("Second string in diff");
50 
51  QString strPatch = dmp.patch_toText(dmp.patch_make(str1, str2));
52  QPair<QString, QVector<bool> > out
53  = dmp.patch_apply(dmp.patch_fromText(strPatch), str1);
54  QString strResult = out.first;
55 
56  // here, strResult will equal str2 above.
57  return 0;
58  }
59 
60  */
61 
62 
69 enum Operation {
70  DELETE, INSERT, EQUAL
71 };
72 
73 
77 class Diff {
78  public:
79  Operation operation;
80  // One of: INSERT, DELETE or EQUAL.
81  QString text;
82  // The text associated with this diff operation.
83 
89  Diff(Operation _operation, const QString &_text);
90  Diff();
91  inline bool isNull() const;
92  QString toString() const;
93  bool operator==(const Diff &d) const;
94  bool operator!=(const Diff &d) const;
95 
96  static QString strOperation(Operation op);
97 };
98 
99 
103 class Patch {
104  public:
105  QList<Diff> diffs;
106  int start1;
107  int start2;
108  int length1;
109  int length2;
110 
114  Patch();
115  bool isNull() const;
116  QString toString();
117 };
118 
119 
125 
126  friend class diff_match_patch_test;
127 
128  public:
129  // Defaults.
130  // Set these on your diff_match_patch instance to override the defaults.
131 
132  // Number of seconds to map a diff before giving up (0 for infinity).
133  float Diff_Timeout;
134  // Cost of an empty edit operation in terms of edit characters.
135  short Diff_EditCost;
136  // At what point is no match declared (0.0 = perfection, 1.0 = very loose).
137  float Match_Threshold;
138  // How far to search for a match (0 = exact location, 1000+ = broad match).
139  // A match this many characters away from the expected location will add
140  // 1.0 to the score (0.0 is a perfect match).
141  int Match_Distance;
142  // When deleting a large block of text (over ~64 characters), how close does
143  // the contents have to match the expected contents. (0.0 = perfection,
144  // 1.0 = very loose). Note that Match_Threshold controls how closely the
145  // end points of a delete need to match.
146  float Patch_DeleteThreshold;
147  // Chunk size for context length.
148  short Patch_Margin;
149 
150  // The number of bits in an int.
151  short Match_MaxBits;
152 
153  private:
154  // Define some regex patterns for matching boundaries.
155  static QRegExp BLANKLINEEND;
156  static QRegExp BLANKLINESTART;
157 
158 
159  public:
160 
162 
163  // DIFF FUNCTIONS
164 
165 
175  QList<Diff> diff_main(const QString &text1, const QString &text2);
176 
186  QList<Diff> diff_main(const QString &text1, const QString &text2, bool checklines);
187 
200  private:
201  QList<Diff> diff_main(const QString &text1, const QString &text2, bool checklines, clock_t deadline);
202 
214  private:
215  QList<Diff> diff_compute(QString text1, QString text2, bool checklines, clock_t deadline);
216 
226  private:
227  QList<Diff> diff_lineMode(QString text1, QString text2, clock_t deadline);
228 
237  protected:
238  QList<Diff> diff_bisect(const QString &text1, const QString &text2, clock_t deadline);
239 
250  private:
251  QList<Diff> diff_bisectSplit(const QString &text1, const QString &text2, int x, int y, clock_t deadline);
252 
262  protected:
263  QList<QVariant> diff_linesToChars(const QString &text1, const QString &text2); // return elems 0 and 1 are QString, elem 2 is QStringList
264 
273  private:
274  QString diff_linesToCharsMunge(const QString &text, QStringList &lineArray,
275  QMap<QString, int> &lineHash);
276 
283  private:
284  void diff_charsToLines(QList<Diff> &diffs, const QStringList &lineArray);
285 
292  public:
293  int diff_commonPrefix(const QString &text1, const QString &text2);
294 
301  public:
302  int diff_commonSuffix(const QString &text1, const QString &text2);
303 
311  protected:
312  int diff_commonOverlap(const QString &text1, const QString &text2);
313 
324  protected:
325  QStringList diff_halfMatch(const QString &text1, const QString &text2);
326 
337  private:
338  QStringList diff_halfMatchI(const QString &longtext, const QString &shorttext, int i);
339 
344  public:
345  void diff_cleanupSemantic(QList<Diff> &diffs);
346 
353  public:
354  void diff_cleanupSemanticLossless(QList<Diff> &diffs);
355 
364  private:
365  int diff_cleanupSemanticScore(const QString &one, const QString &two);
366 
371  public:
372  void diff_cleanupEfficiency(QList<Diff> &diffs);
373 
379  public:
380  void diff_cleanupMerge(QList<Diff> &diffs);
381 
390  public:
391  int diff_xIndex(const QList<Diff> &diffs, int loc);
392 
398  public:
399  QString diff_prettyHtml(const QList<Diff> &diffs);
400 
406  public:
407  QString diff_text1(const QList<Diff> &diffs);
408 
414  public:
415  QString diff_text2(const QList<Diff> &diffs);
416 
423  public:
424  int diff_levenshtein(const QList<Diff> &diffs);
425 
434  public:
435  QString diff_toDelta(const QList<Diff> &diffs);
436 
445  public:
446  QList<Diff> diff_fromDelta(const QString &text1, const QString &delta);
447 
448 
449  // MATCH FUNCTIONS
450 
451 
460  public:
461  int match_main(const QString &text, const QString &pattern, int loc);
462 
471  protected:
472  int match_bitap(const QString &text, const QString &pattern, int loc);
473 
482  private:
483  double match_bitapScore(int e, int x, int loc, const QString &pattern);
484 
490  protected:
491  QMap<QChar, int> match_alphabet(const QString &pattern);
492 
493 
494  // PATCH FUNCTIONS
495 
496 
503  protected:
504  void patch_addContext(Patch &patch, const QString &text);
505 
513  public:
514  QList<Patch> patch_make(const QString &text1, const QString &text2);
515 
522  public:
523  QList<Patch> patch_make(const QList<Diff> &diffs);
524 
534  public:
535  QList<Patch> patch_make(const QString &text1, const QString &text2, const QList<Diff> &diffs);
536 
544  public:
545  QList<Patch> patch_make(const QString &text1, const QList<Diff> &diffs);
546 
552  public:
553  QList<Patch> patch_deepCopy(QList<Patch> &patches);
554 
563  public:
564  QPair<QString,QVector<bool> > patch_apply(QList<Patch> &patches, const QString &text);
565 
572  public:
573  QString patch_addPadding(QList<Patch> &patches);
574 
581  public:
582  void patch_splitMax(QList<Patch> &patches);
583 
589  public:
590  QString patch_toText(const QList<Patch> &patches);
591 
599  public:
600  QList<Patch> patch_fromText(const QString &textline);
601 
609  private:
610  static inline QString safeMid(const QString &str, int pos) {
611  return (pos == str.length()) ? QString("") : str.mid(pos);
612  }
613 
622  private:
623  static inline QString safeMid(const QString &str, int pos, int len) {
624  return (pos == str.length()) ? QString("") : str.mid(pos, len);
625  }
626 };
627 
628 #endif // DIFF_MATCH_PATCH_H