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.

287 lines
9.0KB

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