juce_TextDiff.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. struct TextDiffHelpers
  22. {
  23. enum { minLengthToMatch = 3 };
  24. struct StringRegion
  25. {
  26. StringRegion (const String& s) noexcept
  27. : text (s.getCharPointer()), start (0), length (s.length()) {}
  28. StringRegion (const String::CharPointerType t, int s, int len) noexcept
  29. : text (t), start (s), length (len) {}
  30. String::CharPointerType text;
  31. int start, length;
  32. };
  33. static void addInsertion (TextDiff& td, const String::CharPointerType text, int index, int length)
  34. {
  35. TextDiff::Change c;
  36. c.insertedText = String (text, (size_t) length);
  37. c.start = index;
  38. c.length = length;
  39. td.changes.add (c);
  40. }
  41. static void addDeletion (TextDiff& td, int index, int length)
  42. {
  43. TextDiff::Change c;
  44. c.start = index;
  45. c.length = length;
  46. td.changes.add (c);
  47. }
  48. static void diffSkippingCommonStart (TextDiff& td, const StringRegion& a, const StringRegion& b)
  49. {
  50. String::CharPointerType sa (a.text);
  51. String::CharPointerType sb (b.text);
  52. const int maxLen = jmax (a.length, b.length);
  53. for (int i = 0; i < maxLen; ++i, ++sa, ++sb)
  54. {
  55. if (*sa != *sb)
  56. {
  57. diffRecursively (td, StringRegion (sa, a.start + i, a.length - i),
  58. StringRegion (sb, b.start + i, b.length - i));
  59. break;
  60. }
  61. }
  62. }
  63. static void diffRecursively (TextDiff& td, const StringRegion& a, const StringRegion& b)
  64. {
  65. int indexA, indexB;
  66. const int len = findLongestCommonSubstring (a.text, a.length,
  67. b.text, b.length,
  68. indexA, indexB);
  69. if (len >= minLengthToMatch)
  70. {
  71. if (indexA > 0 && indexB > 0)
  72. diffSkippingCommonStart (td, StringRegion (a.text, a.start, indexA),
  73. StringRegion (b.text, b.start, indexB));
  74. else if (indexA > 0)
  75. addDeletion (td, b.start, indexA);
  76. else if (indexB > 0)
  77. addInsertion (td, b.text, b.start, indexB);
  78. diffRecursively (td, StringRegion (a.text + indexA + len, a.start + indexA + len, a.length - indexA - len),
  79. StringRegion (b.text + indexB + len, b.start + indexB + len, b.length - indexB - len));
  80. }
  81. else
  82. {
  83. if (a.length > 0) addDeletion (td, b.start, a.length);
  84. if (b.length > 0) addInsertion (td, b.text, b.start, b.length);
  85. }
  86. }
  87. static int findLongestCommonSubstring (String::CharPointerType a, const int lenA,
  88. const String::CharPointerType b, const int lenB,
  89. int& indexInA, int& indexInB)
  90. {
  91. if (lenA == 0 || lenB == 0)
  92. return 0;
  93. HeapBlock<int> lines;
  94. lines.calloc (2 + 2 * (size_t) lenB);
  95. int* l0 = lines;
  96. int* l1 = l0 + lenB + 1;
  97. int loopsWithoutImprovement = 0;
  98. int bestLength = 0;
  99. indexInA = indexInB = 0;
  100. for (int i = 0; i < lenA; ++i)
  101. {
  102. const juce_wchar ca = a.getAndAdvance();
  103. String::CharPointerType b2 (b);
  104. for (int j = 0; j < lenB; ++j)
  105. {
  106. if (ca != b2.getAndAdvance())
  107. {
  108. l1[j + 1] = 0;
  109. }
  110. else
  111. {
  112. const int len = l0[j] + 1;
  113. l1[j + 1] = len;
  114. if (len > bestLength)
  115. {
  116. loopsWithoutImprovement = 0;
  117. bestLength = len;
  118. indexInA = i;
  119. indexInB = j;
  120. }
  121. }
  122. }
  123. if (++loopsWithoutImprovement > 100)
  124. break;
  125. std::swap (l0, l1);
  126. }
  127. indexInA -= bestLength - 1;
  128. indexInB -= bestLength - 1;
  129. return bestLength;
  130. }
  131. };
  132. TextDiff::TextDiff (const String& original, const String& target)
  133. {
  134. TextDiffHelpers::diffSkippingCommonStart (*this, original, target);
  135. }
  136. String TextDiff::appliedTo (String text) const
  137. {
  138. for (int i = 0; i < changes.size(); ++i)
  139. text = changes.getReference(i).appliedTo (text);
  140. return text;
  141. }
  142. bool TextDiff::Change::isDeletion() const noexcept
  143. {
  144. return insertedText.isEmpty();
  145. }
  146. String TextDiff::Change::appliedTo (const String& text) const noexcept
  147. {
  148. return text.substring (0, start) + (isDeletion() ? text.substring (start + length)
  149. : (insertedText + text.substring (start)));
  150. }
  151. //==============================================================================
  152. //==============================================================================
  153. #if JUCE_UNIT_TESTS
  154. class DiffTests : public UnitTest
  155. {
  156. public:
  157. DiffTests() : UnitTest ("TextDiff class") {}
  158. static String createString (Random& r)
  159. {
  160. juce_wchar buffer[50] = { 0 };
  161. for (int i = r.nextInt (49); --i >= 0;)
  162. {
  163. if (r.nextInt (10) == 0)
  164. {
  165. do
  166. {
  167. buffer[i] = (juce_wchar) (1 + r.nextInt (0x10ffff - 1));
  168. }
  169. while (! CharPointer_UTF16::canRepresent (buffer[i]));
  170. }
  171. else
  172. buffer[i] = (juce_wchar) ('a' + r.nextInt (3));
  173. }
  174. return CharPointer_UTF32 (buffer);
  175. }
  176. void testDiff (const String& a, const String& b)
  177. {
  178. TextDiff diff (a, b);
  179. const String result (diff.appliedTo (a));
  180. expectEquals (result, b);
  181. }
  182. void runTest()
  183. {
  184. beginTest ("TextDiff");
  185. Random r = getRandom();
  186. testDiff (String::empty, String::empty);
  187. testDiff ("x", String::empty);
  188. testDiff (String::empty, "x");
  189. testDiff ("x", "x");
  190. testDiff ("x", "y");
  191. testDiff ("xxx", "x");
  192. testDiff ("x", "xxx");
  193. for (int i = 5000; --i >= 0;)
  194. {
  195. String s (createString (r));
  196. testDiff (s, createString (r));
  197. testDiff (s + createString (r), s + createString (r));
  198. }
  199. }
  200. };
  201. static DiffTests diffTests;
  202. #endif