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.

290 lines
8.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. struct TextDiffHelpers
  20. {
  21. enum { minLengthToMatch = 3,
  22. maxComplexity = 16 * 1024 * 1024 };
  23. struct StringRegion
  24. {
  25. StringRegion (const String& s) noexcept
  26. : text (s.getCharPointer()), start (0), length (s.length()) {}
  27. StringRegion (String::CharPointerType t, int s, int len) noexcept
  28. : text (t), start (s), length (len) {}
  29. void incrementStart() noexcept { ++text; ++start; --length; }
  30. String::CharPointerType text;
  31. int start, length;
  32. };
  33. static void addInsertion (TextDiff& td, 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 = 0;
  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, StringRegion a, StringRegion b)
  49. {
  50. for (;;)
  51. {
  52. auto ca = *a.text;
  53. auto cb = *b.text;
  54. if (ca != cb || ca == 0)
  55. break;
  56. a.incrementStart();
  57. b.incrementStart();
  58. }
  59. diffRecursively (td, a, b);
  60. }
  61. static void diffRecursively (TextDiff& td, StringRegion a, StringRegion b)
  62. {
  63. int indexA = 0, indexB = 0;
  64. auto len = findLongestCommonSubstring (a.text, a.length, indexA,
  65. b.text, b.length, indexB);
  66. if (len >= minLengthToMatch)
  67. {
  68. if (indexA > 0 && indexB > 0)
  69. diffSkippingCommonStart (td, StringRegion (a.text, a.start, indexA),
  70. StringRegion (b.text, b.start, indexB));
  71. else if (indexA > 0)
  72. addDeletion (td, b.start, indexA);
  73. else if (indexB > 0)
  74. addInsertion (td, b.text, b.start, indexB);
  75. diffRecursively (td, StringRegion (a.text + (indexA + len), a.start + indexA + len, a.length - indexA - len),
  76. StringRegion (b.text + (indexB + len), b.start + indexB + len, b.length - indexB - len));
  77. }
  78. else
  79. {
  80. if (a.length > 0) addDeletion (td, b.start, a.length);
  81. if (b.length > 0) addInsertion (td, b.text, b.start, b.length);
  82. }
  83. }
  84. static int findLongestCommonSubstring (String::CharPointerType a, const int lenA, int& indexInA,
  85. String::CharPointerType b, const int lenB, int& indexInB) noexcept
  86. {
  87. if (lenA == 0 || lenB == 0)
  88. return 0;
  89. if (lenA * lenB > maxComplexity)
  90. return findCommonSuffix (a, lenA, indexInA,
  91. b, lenB, indexInB);
  92. auto scratchSpace = sizeof (int) * (2 + 2 * (size_t) lenB);
  93. if (scratchSpace < 4096)
  94. {
  95. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6255)
  96. auto* scratch = (int*) alloca (scratchSpace);
  97. JUCE_END_IGNORE_WARNINGS_MSVC
  98. return findLongestCommonSubstring (a, lenA, indexInA, b, lenB, indexInB, scratchSpace, scratch);
  99. }
  100. HeapBlock<int> scratch (scratchSpace);
  101. return findLongestCommonSubstring (a, lenA, indexInA, b, lenB, indexInB, scratchSpace, scratch);
  102. }
  103. static int findLongestCommonSubstring (String::CharPointerType a, const int lenA, int& indexInA,
  104. String::CharPointerType b, const int lenB, int& indexInB,
  105. const size_t scratchSpace, int* const lines) noexcept
  106. {
  107. zeromem (lines, scratchSpace);
  108. auto* l0 = lines;
  109. auto* l1 = l0 + lenB + 1;
  110. int loopsWithoutImprovement = 0;
  111. int bestLength = 0;
  112. for (int i = 0; i < lenA; ++i)
  113. {
  114. auto ca = a.getAndAdvance();
  115. auto b2 = b;
  116. for (int j = 0; j < lenB; ++j)
  117. {
  118. if (ca != b2.getAndAdvance())
  119. {
  120. l1[j + 1] = 0;
  121. }
  122. else
  123. {
  124. auto len = l0[j] + 1;
  125. l1[j + 1] = len;
  126. if (len > bestLength)
  127. {
  128. loopsWithoutImprovement = 0;
  129. bestLength = len;
  130. indexInA = i;
  131. indexInB = j;
  132. }
  133. }
  134. }
  135. if (++loopsWithoutImprovement > 100)
  136. break;
  137. std::swap (l0, l1);
  138. }
  139. indexInA -= bestLength - 1;
  140. indexInB -= bestLength - 1;
  141. return bestLength;
  142. }
  143. static int findCommonSuffix (String::CharPointerType a, int lenA, int& indexInA,
  144. String::CharPointerType b, int lenB, int& indexInB) noexcept
  145. {
  146. int length = 0;
  147. a += lenA - 1;
  148. b += lenB - 1;
  149. while (length < lenA && length < lenB && *a == *b)
  150. {
  151. --a;
  152. --b;
  153. ++length;
  154. }
  155. indexInA = lenA - length;
  156. indexInB = lenB - length;
  157. return length;
  158. }
  159. };
  160. TextDiff::TextDiff (const String& original, const String& target)
  161. {
  162. TextDiffHelpers::diffSkippingCommonStart (*this, original, target);
  163. }
  164. String TextDiff::appliedTo (String text) const
  165. {
  166. for (auto& c : changes)
  167. text = c.appliedTo (text);
  168. return text;
  169. }
  170. bool TextDiff::Change::isDeletion() const noexcept
  171. {
  172. return insertedText.isEmpty();
  173. }
  174. String TextDiff::Change::appliedTo (const String& text) const noexcept
  175. {
  176. return text.replaceSection (start, length, insertedText);
  177. }
  178. //==============================================================================
  179. //==============================================================================
  180. #if JUCE_UNIT_TESTS
  181. class DiffTests final : public UnitTest
  182. {
  183. public:
  184. DiffTests()
  185. : UnitTest ("TextDiff class", UnitTestCategories::text)
  186. {}
  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. auto result = diff.appliedTo (a);
  209. expectEquals (result, b);
  210. }
  211. void runTest() override
  212. {
  213. beginTest ("TextDiff");
  214. auto 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. auto 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
  232. } // namespace juce