The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
8.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. struct TextDiffHelpers
  18. {
  19. enum { minLengthToMatch = 3,
  20. maxComplexity = 16 * 1024 * 1024 };
  21. struct StringRegion
  22. {
  23. StringRegion (const String& s) noexcept
  24. : text (s.getCharPointer()), start (0), length (s.length()) {}
  25. StringRegion (const String::CharPointerType t, int s, int len) noexcept
  26. : text (t), start (s), length (len) {}
  27. void incrementStart() noexcept { ++text; ++start; --length; }
  28. String::CharPointerType text;
  29. int start, length;
  30. };
  31. static void addInsertion (TextDiff& td, const String::CharPointerType text, int index, int length)
  32. {
  33. TextDiff::Change c;
  34. c.insertedText = String (text, (size_t) length);
  35. c.start = index;
  36. c.length = 0;
  37. td.changes.add (c);
  38. }
  39. static void addDeletion (TextDiff& td, int index, int length)
  40. {
  41. TextDiff::Change c;
  42. c.start = index;
  43. c.length = length;
  44. td.changes.add (c);
  45. }
  46. static void diffSkippingCommonStart (TextDiff& td, StringRegion a, StringRegion b)
  47. {
  48. for (;;)
  49. {
  50. const juce_wchar ca = *a.text;
  51. const juce_wchar cb = *b.text;
  52. if (ca != cb || ca == 0)
  53. break;
  54. a.incrementStart();
  55. b.incrementStart();
  56. }
  57. diffRecursively (td, a, b);
  58. }
  59. static void diffRecursively (TextDiff& td, StringRegion a, StringRegion b)
  60. {
  61. int indexA = 0, indexB = 0;
  62. const int len = findLongestCommonSubstring (a.text, a.length, indexA,
  63. b.text, b.length, indexB);
  64. if (len >= minLengthToMatch)
  65. {
  66. if (indexA > 0 && indexB > 0)
  67. diffSkippingCommonStart (td, StringRegion (a.text, a.start, indexA),
  68. StringRegion (b.text, b.start, indexB));
  69. else if (indexA > 0)
  70. addDeletion (td, b.start, indexA);
  71. else if (indexB > 0)
  72. addInsertion (td, b.text, b.start, indexB);
  73. diffRecursively (td, StringRegion (a.text + (indexA + len), a.start + indexA + len, a.length - indexA - len),
  74. StringRegion (b.text + (indexB + len), b.start + indexB + len, b.length - indexB - len));
  75. }
  76. else
  77. {
  78. if (a.length > 0) addDeletion (td, b.start, a.length);
  79. if (b.length > 0) addInsertion (td, b.text, b.start, b.length);
  80. }
  81. }
  82. static int findLongestCommonSubstring (String::CharPointerType a, const int lenA, int& indexInA,
  83. String::CharPointerType b, const int lenB, int& indexInB) noexcept
  84. {
  85. if (lenA == 0 || lenB == 0)
  86. return 0;
  87. if (lenA * lenB > maxComplexity)
  88. return findCommonSuffix (a, lenA, indexInA,
  89. b, lenB, indexInB);
  90. const size_t scratchSpace = sizeof (int) * (2 + 2 * (size_t) lenB);
  91. if (scratchSpace < 4096)
  92. {
  93. int* scratch = (int*) alloca (scratchSpace);
  94. return findLongestCommonSubstring (a, lenA, indexInA, b, lenB, indexInB, scratchSpace, scratch);
  95. }
  96. HeapBlock<int> scratch (scratchSpace);
  97. return findLongestCommonSubstring (a, lenA, indexInA, b, lenB, indexInB, scratchSpace, scratch);
  98. }
  99. static int findLongestCommonSubstring (String::CharPointerType a, const int lenA, int& indexInA,
  100. String::CharPointerType b, const int lenB, int& indexInB,
  101. const size_t scratchSpace, int* const lines) noexcept
  102. {
  103. zeromem (lines, scratchSpace);
  104. int* l0 = lines;
  105. int* l1 = l0 + lenB + 1;
  106. int loopsWithoutImprovement = 0;
  107. int bestLength = 0;
  108. for (int i = 0; i < lenA; ++i)
  109. {
  110. const juce_wchar ca = a.getAndAdvance();
  111. String::CharPointerType b2 (b);
  112. for (int j = 0; j < lenB; ++j)
  113. {
  114. if (ca != b2.getAndAdvance())
  115. {
  116. l1[j + 1] = 0;
  117. }
  118. else
  119. {
  120. const int len = l0[j] + 1;
  121. l1[j + 1] = len;
  122. if (len > bestLength)
  123. {
  124. loopsWithoutImprovement = 0;
  125. bestLength = len;
  126. indexInA = i;
  127. indexInB = j;
  128. }
  129. }
  130. }
  131. if (++loopsWithoutImprovement > 100)
  132. break;
  133. std::swap (l0, l1);
  134. }
  135. indexInA -= bestLength - 1;
  136. indexInB -= bestLength - 1;
  137. return bestLength;
  138. }
  139. static int findCommonSuffix (String::CharPointerType a, const int lenA, int& indexInA,
  140. String::CharPointerType b, const int lenB, int& indexInB) noexcept
  141. {
  142. int length = 0;
  143. a += lenA - 1;
  144. b += lenB - 1;
  145. while (length < lenA && length < lenB && *a == *b)
  146. {
  147. --a;
  148. --b;
  149. ++length;
  150. }
  151. indexInA = lenA - length;
  152. indexInB = lenB - length;
  153. return length;
  154. }
  155. };
  156. TextDiff::TextDiff (const String& original, const String& target)
  157. {
  158. TextDiffHelpers::diffSkippingCommonStart (*this, original, target);
  159. }
  160. String TextDiff::appliedTo (String text) const
  161. {
  162. for (int i = 0; i < changes.size(); ++i)
  163. text = changes.getReference(i).appliedTo (text);
  164. return text;
  165. }
  166. bool TextDiff::Change::isDeletion() const noexcept
  167. {
  168. return insertedText.isEmpty();
  169. }
  170. String TextDiff::Change::appliedTo (const String& text) const noexcept
  171. {
  172. return text.replaceSection (start, length, insertedText);
  173. }
  174. //==============================================================================
  175. //==============================================================================
  176. #if JUCE_UNIT_TESTS
  177. class DiffTests : public UnitTest
  178. {
  179. public:
  180. DiffTests() : UnitTest ("TextDiff class") {}
  181. static String createString (Random& r)
  182. {
  183. juce_wchar buffer[500] = { 0 };
  184. for (int i = r.nextInt (numElementsInArray (buffer) - 1); --i >= 0;)
  185. {
  186. if (r.nextInt (10) == 0)
  187. {
  188. do
  189. {
  190. buffer[i] = (juce_wchar) (1 + r.nextInt (0x10ffff - 1));
  191. }
  192. while (! CharPointer_UTF16::canRepresent (buffer[i]));
  193. }
  194. else
  195. buffer[i] = (juce_wchar) ('a' + r.nextInt (3));
  196. }
  197. return CharPointer_UTF32 (buffer);
  198. }
  199. void testDiff (const String& a, const String& b)
  200. {
  201. TextDiff diff (a, b);
  202. const String result (diff.appliedTo (a));
  203. expectEquals (result, b);
  204. }
  205. void runTest() override
  206. {
  207. beginTest ("TextDiff");
  208. Random r = getRandom();
  209. testDiff (String(), String());
  210. testDiff ("x", String());
  211. testDiff (String(), "x");
  212. testDiff ("x", "x");
  213. testDiff ("x", "y");
  214. testDiff ("xxx", "x");
  215. testDiff ("x", "xxx");
  216. for (int i = 1000; --i >= 0;)
  217. {
  218. String s (createString (r));
  219. testDiff (s, createString (r));
  220. testDiff (s + createString (r), s + createString (r));
  221. }
  222. }
  223. };
  224. static DiffTests diffTests;
  225. #endif