diff_match_patch.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108
  1. /*
  2. * Copyright 2008 Google Inc. All Rights Reserved.
  3. * Author: [email protected] (Neil Fraser)
  4. * Author: [email protected] (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. #include <algorithm>
  22. #include <limits>
  23. // Code known to compile and run with Qt 4.3 through Qt 4.7.
  24. #include <D:\Prog\Qt_4.7\include\QtCore\QtCore>
  25. #include <time.h>
  26. #include "diff_match_patch (original).h"
  27. //////////////////////////
  28. //
  29. // Diff Class
  30. //
  31. //////////////////////////
  32. /**
  33. * Constructor. Initializes the diff with the provided values.
  34. * @param operation One of INSERT, DELETE or EQUAL
  35. * @param text The text being applied
  36. */
  37. Diff::Diff(Operation _operation, const QString &_text) :
  38. operation(_operation), text(_text) {
  39. // Construct a diff with the specified operation and text.
  40. }
  41. Diff::Diff() {
  42. }
  43. QString Diff::strOperation(Operation op) {
  44. switch (op) {
  45. case INSERT:
  46. return "INSERT";
  47. case DELETE:
  48. return "DELETE";
  49. case EQUAL:
  50. return "EQUAL";
  51. }
  52. throw "Invalid operation.";
  53. }
  54. /**
  55. * Display a human-readable version of this Diff.
  56. * @return text version
  57. */
  58. QString Diff::toString() const {
  59. QString prettyText = text;
  60. // Replace linebreaks with Pilcrow signs.
  61. prettyText.replace('\n', L'\u00b6');
  62. return QString("Diff(") + strOperation(operation) + QString(",\"")
  63. + prettyText + QString("\")");
  64. }
  65. /**
  66. * Is this Diff equivalent to another Diff?
  67. * @param d Another Diff to compare against
  68. * @return true or false
  69. */
  70. bool Diff::operator==(const Diff &d) const {
  71. return (d.operation == this->operation) && (d.text == this->text);
  72. }
  73. bool Diff::operator!=(const Diff &d) const {
  74. return !(operator == (d));
  75. }
  76. /////////////////////////////////////////////
  77. //
  78. // Patch Class
  79. //
  80. /////////////////////////////////////////////
  81. /**
  82. * Constructor. Initializes with an empty list of diffs.
  83. */
  84. Patch::Patch() :
  85. start1(0), start2(0),
  86. length1(0), length2(0) {
  87. }
  88. bool Patch::isNull() const {
  89. if (start1 == 0 && start2 == 0 && length1 == 0 && length2 == 0
  90. && diffs.size() == 0) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * Emmulate GNU diff's format.
  97. * Header: @@ -382,8 +481,9 @@
  98. * Indicies are printed as 1-based, not 0-based.
  99. * @return The GNU diff string
  100. */
  101. QString Patch::toString() {
  102. QString coords1, coords2;
  103. if (length1 == 0) {
  104. coords1 = QString::number(start1) + QString(",0");
  105. } else if (length1 == 1) {
  106. coords1 = QString::number(start1 + 1);
  107. } else {
  108. coords1 = QString::number(start1 + 1) + QString(",")
  109. + QString::number(length1);
  110. }
  111. if (length2 == 0) {
  112. coords2 = QString::number(start2) + QString(",0");
  113. } else if (length2 == 1) {
  114. coords2 = QString::number(start2 + 1);
  115. } else {
  116. coords2 = QString::number(start2 + 1) + QString(",")
  117. + QString::number(length2);
  118. }
  119. QString text;
  120. text = QString("@@ -") + coords1 + QString(" +") + coords2
  121. + QString(" @@\n");
  122. // Escape the body of the patch with %xx notation.
  123. foreach (Diff aDiff, diffs) {
  124. switch (aDiff.operation) {
  125. case INSERT:
  126. text += QString('+');
  127. break;
  128. case DELETE:
  129. text += QString('-');
  130. break;
  131. case EQUAL:
  132. text += QString(' ');
  133. break;
  134. }
  135. text += QString(QUrl::toPercentEncoding(aDiff.text, " !~*'();/?:@&=+$,#"))
  136. + QString("\n");
  137. }
  138. return text;
  139. }
  140. /////////////////////////////////////////////
  141. //
  142. // diff_match_patch Class
  143. //
  144. /////////////////////////////////////////////
  145. diff_match_patch::diff_match_patch() :
  146. Diff_Timeout(1.0f),
  147. Diff_EditCost(4),
  148. Match_Threshold(0.5f),
  149. Match_Distance(1000),
  150. Patch_DeleteThreshold(0.5f),
  151. Patch_Margin(4),
  152. Match_MaxBits(32) {
  153. }
  154. QList<Diff> diff_match_patch::diff_main(const QString &text1,
  155. const QString &text2) {
  156. return diff_main(text1, text2, true);
  157. }
  158. QList<Diff> diff_match_patch::diff_main(const QString &text1,
  159. const QString &text2, bool checklines) {
  160. // Set a deadline by which time the diff must be complete.
  161. clock_t deadline;
  162. if (Diff_Timeout <= 0) {
  163. deadline = std::numeric_limits<clock_t>::max();
  164. } else {
  165. deadline = clock() + (clock_t)(Diff_Timeout * CLOCKS_PER_SEC);
  166. }
  167. return diff_main(text1, text2, checklines, deadline);
  168. }
  169. QList<Diff> diff_match_patch::diff_main(const QString &text1,
  170. const QString &text2, bool checklines, clock_t deadline) {
  171. // Check for null inputs.
  172. if (text1.isNull() || text2.isNull()) {
  173. throw "Null inputs. (diff_main)";
  174. }
  175. // Check for equality (speedup).
  176. QList<Diff> diffs;
  177. if (text1 == text2) {
  178. if (!text1.isEmpty()) {
  179. diffs.append(Diff(EQUAL, text1));
  180. }
  181. return diffs;
  182. }
  183. // Trim off common prefix (speedup).
  184. int commonlength = diff_commonPrefix(text1, text2);
  185. const QString &commonprefix = text1.left(commonlength);
  186. QString textChopped1 = text1.mid(commonlength);
  187. QString textChopped2 = text2.mid(commonlength);
  188. // Trim off common suffix (speedup).
  189. commonlength = diff_commonSuffix(textChopped1, textChopped2);
  190. const QString &commonsuffix = textChopped1.right(commonlength);
  191. textChopped1 = textChopped1.left(textChopped1.length() - commonlength);
  192. textChopped2 = textChopped2.left(textChopped2.length() - commonlength);
  193. // Compute the diff on the middle block.
  194. diffs = diff_compute(textChopped1, textChopped2, checklines, deadline);
  195. // Restore the prefix and suffix.
  196. if (!commonprefix.isEmpty()) {
  197. diffs.prepend(Diff(EQUAL, commonprefix));
  198. }
  199. if (!commonsuffix.isEmpty()) {
  200. diffs.append(Diff(EQUAL, commonsuffix));
  201. }
  202. diff_cleanupMerge(diffs);
  203. return diffs;
  204. }
  205. QList<Diff> diff_match_patch::diff_compute(QString text1, QString text2,
  206. bool checklines, clock_t deadline) {
  207. QList<Diff> diffs;
  208. if (text1.isEmpty()) {
  209. // Just add some text (speedup).
  210. diffs.append(Diff(INSERT, text2));
  211. return diffs;
  212. }
  213. if (text2.isEmpty()) {
  214. // Just delete some text (speedup).
  215. diffs.append(Diff(DELETE, text1));
  216. return diffs;
  217. }
  218. {
  219. const QString longtext = text1.length() > text2.length() ? text1 : text2;
  220. const QString shorttext = text1.length() > text2.length() ? text2 : text1;
  221. const int i = longtext.indexOf(shorttext);
  222. if (i != -1) {
  223. // Shorter text is inside the longer text (speedup).
  224. const Operation op = (text1.length() > text2.length()) ? DELETE : INSERT;
  225. diffs.append(Diff(op, longtext.left(i)));
  226. diffs.append(Diff(EQUAL, shorttext));
  227. diffs.append(Diff(op, safeMid(longtext, i + shorttext.length())));
  228. return diffs;
  229. }
  230. if (shorttext.length() == 1) {
  231. // Single character string.
  232. // After the previous speedup, the character can't be an equality.
  233. diffs.append(Diff(DELETE, text1));
  234. diffs.append(Diff(INSERT, text2));
  235. return diffs;
  236. }
  237. // Garbage collect longtext and shorttext by scoping out.
  238. }
  239. // Check to see if the problem can be split in two.
  240. const QStringList hm = diff_halfMatch(text1, text2);
  241. if (hm.count() > 0) {
  242. // A half-match was found, sort out the return data.
  243. const QString text1_a = hm[0];
  244. const QString text1_b = hm[1];
  245. const QString text2_a = hm[2];
  246. const QString text2_b = hm[3];
  247. const QString mid_common = hm[4];
  248. // Send both pairs off for separate processing.
  249. const QList<Diff> diffs_a = diff_main(text1_a, text2_a,
  250. checklines, deadline);
  251. const QList<Diff> diffs_b = diff_main(text1_b, text2_b,
  252. checklines, deadline);
  253. // Merge the results.
  254. diffs = diffs_a;
  255. diffs.append(Diff(EQUAL, mid_common));
  256. diffs += diffs_b;
  257. return diffs;
  258. }
  259. // Perform a real diff.
  260. if (checklines && text1.length() > 100 && text2.length() > 100) {
  261. return diff_lineMode(text1, text2, deadline);
  262. }
  263. return diff_bisect(text1, text2, deadline);
  264. }
  265. QList<Diff> diff_match_patch::diff_lineMode(QString text1, QString text2,
  266. clock_t deadline) {
  267. // Scan the text on a line-by-line basis first.
  268. const QList<QVariant> b = diff_linesToChars(text1, text2);
  269. text1 = b[0].toString();
  270. text2 = b[1].toString();
  271. QStringList linearray = b[2].toStringList();
  272. QList<Diff> diffs = diff_main(text1, text2, false, deadline);
  273. // Convert the diff back to original text.
  274. diff_charsToLines(diffs, linearray);
  275. // Eliminate freak matches (e.g. blank lines)
  276. diff_cleanupSemantic(diffs);
  277. // Rediff any replacement blocks, this time character-by-character.
  278. // Add a dummy entry at the end.
  279. diffs.append(Diff(EQUAL, ""));
  280. int count_delete = 0;
  281. int count_insert = 0;
  282. QString text_delete = "";
  283. QString text_insert = "";
  284. QMutableListIterator<Diff> pointer(diffs);
  285. Diff *thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  286. while (thisDiff != NULL) {
  287. switch (thisDiff->operation) {
  288. case INSERT:
  289. count_insert++;
  290. text_insert += thisDiff->text;
  291. break;
  292. case DELETE:
  293. count_delete++;
  294. text_delete += thisDiff->text;
  295. break;
  296. case EQUAL:
  297. // Upon reaching an equality, check for prior redundancies.
  298. if (count_delete >= 1 && count_insert >= 1) {
  299. // Delete the offending records and add the merged ones.
  300. pointer.previous();
  301. for (int j = 0; j < count_delete + count_insert; j++) {
  302. pointer.previous();
  303. pointer.remove();
  304. }
  305. foreach(Diff newDiff,
  306. diff_main(text_delete, text_insert, false, deadline)) {
  307. pointer.insert(newDiff);
  308. }
  309. }
  310. count_insert = 0;
  311. count_delete = 0;
  312. text_delete = "";
  313. text_insert = "";
  314. break;
  315. }
  316. thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  317. }
  318. diffs.removeLast(); // Remove the dummy entry at the end.
  319. return diffs;
  320. }
  321. QList<Diff> diff_match_patch::diff_bisect(const QString &text1,
  322. const QString &text2, clock_t deadline) {
  323. // Cache the text lengths to prevent multiple calls.
  324. const int text1_length = text1.length();
  325. const int text2_length = text2.length();
  326. const int max_d = (text1_length + text2_length + 1) / 2;
  327. const int v_offset = max_d;
  328. const int v_length = 2 * max_d;
  329. int *v1 = new int[v_length];
  330. int *v2 = new int[v_length];
  331. for (int x = 0; x < v_length; x++) {
  332. v1[x] = -1;
  333. v2[x] = -1;
  334. }
  335. v1[v_offset + 1] = 0;
  336. v2[v_offset + 1] = 0;
  337. const int delta = text1_length - text2_length;
  338. // If the total number of characters is odd, then the front path will
  339. // collide with the reverse path.
  340. const bool front = (delta % 2 != 0);
  341. // Offsets for start and end of k loop.
  342. // Prevents mapping of space beyond the grid.
  343. int k1start = 0;
  344. int k1end = 0;
  345. int k2start = 0;
  346. int k2end = 0;
  347. for (int d = 0; d < max_d; d++) {
  348. // Bail out if deadline is reached.
  349. if (clock() > deadline) {
  350. break;
  351. }
  352. // Walk the front path one step.
  353. for (int k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
  354. const int k1_offset = v_offset + k1;
  355. int x1;
  356. if (k1 == -d || (k1 != d && v1[k1_offset - 1] < v1[k1_offset + 1])) {
  357. x1 = v1[k1_offset + 1];
  358. } else {
  359. x1 = v1[k1_offset - 1] + 1;
  360. }
  361. int y1 = x1 - k1;
  362. while (x1 < text1_length && y1 < text2_length
  363. && text1[x1] == text2[y1]) {
  364. x1++;
  365. y1++;
  366. }
  367. v1[k1_offset] = x1;
  368. if (x1 > text1_length) {
  369. // Ran off the right of the graph.
  370. k1end += 2;
  371. } else if (y1 > text2_length) {
  372. // Ran off the bottom of the graph.
  373. k1start += 2;
  374. } else if (front) {
  375. int k2_offset = v_offset + delta - k1;
  376. if (k2_offset >= 0 && k2_offset < v_length && v2[k2_offset] != -1) {
  377. // Mirror x2 onto top-left coordinate system.
  378. int x2 = text1_length - v2[k2_offset];
  379. if (x1 >= x2) {
  380. // Overlap detected.
  381. delete [] v1;
  382. delete [] v2;
  383. return diff_bisectSplit(text1, text2, x1, y1, deadline);
  384. }
  385. }
  386. }
  387. }
  388. // Walk the reverse path one step.
  389. for (int k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {
  390. const int k2_offset = v_offset + k2;
  391. int x2;
  392. if (k2 == -d || (k2 != d && v2[k2_offset - 1] < v2[k2_offset + 1])) {
  393. x2 = v2[k2_offset + 1];
  394. } else {
  395. x2 = v2[k2_offset - 1] + 1;
  396. }
  397. int y2 = x2 - k2;
  398. while (x2 < text1_length && y2 < text2_length
  399. && text1[text1_length - x2 - 1] == text2[text2_length - y2 - 1]) {
  400. x2++;
  401. y2++;
  402. }
  403. v2[k2_offset] = x2;
  404. if (x2 > text1_length) {
  405. // Ran off the left of the graph.
  406. k2end += 2;
  407. } else if (y2 > text2_length) {
  408. // Ran off the top of the graph.
  409. k2start += 2;
  410. } else if (!front) {
  411. int k1_offset = v_offset + delta - k2;
  412. if (k1_offset >= 0 && k1_offset < v_length && v1[k1_offset] != -1) {
  413. int x1 = v1[k1_offset];
  414. int y1 = v_offset + x1 - k1_offset;
  415. // Mirror x2 onto top-left coordinate system.
  416. x2 = text1_length - x2;
  417. if (x1 >= x2) {
  418. // Overlap detected.
  419. delete [] v1;
  420. delete [] v2;
  421. return diff_bisectSplit(text1, text2, x1, y1, deadline);
  422. }
  423. }
  424. }
  425. }
  426. }
  427. delete [] v1;
  428. delete [] v2;
  429. // Diff took too long and hit the deadline or
  430. // number of diffs equals number of characters, no commonality at all.
  431. QList<Diff> diffs;
  432. diffs.append(Diff(DELETE, text1));
  433. diffs.append(Diff(INSERT, text2));
  434. return diffs;
  435. }
  436. QList<Diff> diff_match_patch::diff_bisectSplit(const QString &text1,
  437. const QString &text2, int x, int y, clock_t deadline) {
  438. QString text1a = text1.left(x);
  439. QString text2a = text2.left(y);
  440. QString text1b = safeMid(text1, x);
  441. QString text2b = safeMid(text2, y);
  442. // Compute both diffs serially.
  443. QList<Diff> diffs = diff_main(text1a, text2a, false, deadline);
  444. QList<Diff> diffsb = diff_main(text1b, text2b, false, deadline);
  445. return diffs + diffsb;
  446. }
  447. QList<QVariant> diff_match_patch::diff_linesToChars(const QString &text1,
  448. const QString &text2) {
  449. QStringList lineArray;
  450. QMap<QString, int> lineHash;
  451. // e.g. linearray[4] == "Hello\n"
  452. // e.g. linehash.get("Hello\n") == 4
  453. // "\x00" is a valid character, but various debuggers don't like it.
  454. // So we'll insert a junk entry to avoid generating a null character.
  455. lineArray.append("");
  456. const QString chars1 = diff_linesToCharsMunge(text1, lineArray, lineHash);
  457. const QString chars2 = diff_linesToCharsMunge(text2, lineArray, lineHash);
  458. QList<QVariant> listRet;
  459. listRet.append(QVariant::fromValue(chars1));
  460. listRet.append(QVariant::fromValue(chars2));
  461. listRet.append(QVariant::fromValue(lineArray));
  462. return listRet;
  463. }
  464. QString diff_match_patch::diff_linesToCharsMunge(const QString &text,
  465. QStringList &lineArray,
  466. QMap<QString, int> &lineHash) {
  467. int lineStart = 0;
  468. int lineEnd = -1;
  469. QString line;
  470. QString chars;
  471. // Walk the text, pulling out a substring for each line.
  472. // text.split('\n') would would temporarily double our memory footprint.
  473. // Modifying text would create many large strings to garbage collect.
  474. while (lineEnd < text.length() - 1) {
  475. lineEnd = text.indexOf('\n', lineStart);
  476. if (lineEnd == -1) {
  477. lineEnd = text.length() - 1;
  478. }
  479. line = safeMid(text, lineStart, lineEnd + 1 - lineStart);
  480. lineStart = lineEnd + 1;
  481. if (lineHash.contains(line)) {
  482. chars += QChar(static_cast<ushort>(lineHash.value(line)));
  483. } else {
  484. lineArray.append(line);
  485. lineHash.insert(line, lineArray.size() - 1);
  486. chars += QChar(static_cast<ushort>(lineArray.size() - 1));
  487. }
  488. }
  489. return chars;
  490. }
  491. void diff_match_patch::diff_charsToLines(QList<Diff> &diffs,
  492. const QStringList &lineArray) {
  493. // Qt has no mutable foreach construct.
  494. QMutableListIterator<Diff> i(diffs);
  495. while (i.hasNext()) {
  496. Diff &diff = i.next();
  497. QString text;
  498. for (int y = 0; y < diff.text.length(); y++) {
  499. text += lineArray.value(static_cast<ushort>(diff.text[y].unicode()));
  500. }
  501. diff.text = text;
  502. }
  503. }
  504. int diff_match_patch::diff_commonPrefix(const QString &text1,
  505. const QString &text2) {
  506. // Performance analysis: http://neil.fraser.name/news/2007/10/09/
  507. const int n = std::min(text1.length(), text2.length());
  508. for (int i = 0; i < n; i++) {
  509. if (text1[i] != text2[i]) {
  510. return i;
  511. }
  512. }
  513. return n;
  514. }
  515. int diff_match_patch::diff_commonSuffix(const QString &text1,
  516. const QString &text2) {
  517. // Performance analysis: http://neil.fraser.name/news/2007/10/09/
  518. const int text1_length = text1.length();
  519. const int text2_length = text2.length();
  520. const int n = std::min(text1_length, text2_length);
  521. for (int i = 1; i <= n; i++) {
  522. if (text1[text1_length - i] != text2[text2_length - i]) {
  523. return i - 1;
  524. }
  525. }
  526. return n;
  527. }
  528. int diff_match_patch::diff_commonOverlap(const QString &text1,
  529. const QString &text2) {
  530. // Cache the text lengths to prevent multiple calls.
  531. const int text1_length = text1.length();
  532. const int text2_length = text2.length();
  533. // Eliminate the null case.
  534. if (text1_length == 0 || text2_length == 0) {
  535. return 0;
  536. }
  537. // Truncate the longer string.
  538. QString text1_trunc = text1;
  539. QString text2_trunc = text2;
  540. if (text1_length > text2_length) {
  541. text1_trunc = text1.right(text2_length);
  542. } else if (text1_length < text2_length) {
  543. text2_trunc = text2.left(text1_length);
  544. }
  545. const int text_length = std::min(text1_length, text2_length);
  546. // Quick check for the worst case.
  547. if (text1_trunc == text2_trunc) {
  548. return text_length;
  549. }
  550. // Start by looking for a single character match
  551. // and increase length until no match is found.
  552. // Performance analysis: http://neil.fraser.name/news/2010/11/04/
  553. int best = 0;
  554. int length = 1;
  555. while (true) {
  556. QString pattern = text1_trunc.right(length);
  557. int found = text2_trunc.indexOf(pattern);
  558. if (found == -1) {
  559. return best;
  560. }
  561. length += found;
  562. if (found == 0 || text1_trunc.right(length) == text2_trunc.left(length)) {
  563. best = length;
  564. length++;
  565. }
  566. }
  567. }
  568. QStringList diff_match_patch::diff_halfMatch(const QString &text1,
  569. const QString &text2) {
  570. if (Diff_Timeout <= 0) {
  571. // Don't risk returning a non-optimal diff if we have unlimited time.
  572. return QStringList();
  573. }
  574. const QString longtext = text1.length() > text2.length() ? text1 : text2;
  575. const QString shorttext = text1.length() > text2.length() ? text2 : text1;
  576. if (longtext.length() < 4 || shorttext.length() * 2 < longtext.length()) {
  577. return QStringList(); // Pointless.
  578. }
  579. // First check if the second quarter is the seed for a half-match.
  580. const QStringList hm1 = diff_halfMatchI(longtext, shorttext,
  581. (longtext.length() + 3) / 4);
  582. // Check again based on the third quarter.
  583. const QStringList hm2 = diff_halfMatchI(longtext, shorttext,
  584. (longtext.length() + 1) / 2);
  585. QStringList hm;
  586. if (hm1.isEmpty() && hm2.isEmpty()) {
  587. return QStringList();
  588. } else if (hm2.isEmpty()) {
  589. hm = hm1;
  590. } else if (hm1.isEmpty()) {
  591. hm = hm2;
  592. } else {
  593. // Both matched. Select the longest.
  594. hm = hm1[4].length() > hm2[4].length() ? hm1 : hm2;
  595. }
  596. // A half-match was found, sort out the return data.
  597. if (text1.length() > text2.length()) {
  598. return hm;
  599. } else {
  600. QStringList listRet;
  601. listRet << hm[2] << hm[3] << hm[0] << hm[1] << hm[4];
  602. return listRet;
  603. }
  604. }
  605. QStringList diff_match_patch::diff_halfMatchI(const QString &longtext,
  606. const QString &shorttext,
  607. int i) {
  608. // Start with a 1/4 length substring at position i as a seed.
  609. const QString seed = safeMid(longtext, i, longtext.length() / 4);
  610. int j = -1;
  611. QString best_common;
  612. QString best_longtext_a, best_longtext_b;
  613. QString best_shorttext_a, best_shorttext_b;
  614. while ((j = shorttext.indexOf(seed, j + 1)) != -1) {
  615. const int prefixLength = diff_commonPrefix(safeMid(longtext, i),
  616. safeMid(shorttext, j));
  617. const int suffixLength = diff_commonSuffix(longtext.left(i),
  618. shorttext.left(j));
  619. if (best_common.length() < suffixLength + prefixLength) {
  620. best_common = safeMid(shorttext, j - suffixLength, suffixLength)
  621. + safeMid(shorttext, j, prefixLength);
  622. best_longtext_a = longtext.left(i - suffixLength);
  623. best_longtext_b = safeMid(longtext, i + prefixLength);
  624. best_shorttext_a = shorttext.left(j - suffixLength);
  625. best_shorttext_b = safeMid(shorttext, j + prefixLength);
  626. }
  627. }
  628. if (best_common.length() * 2 >= longtext.length()) {
  629. QStringList listRet;
  630. listRet << best_longtext_a << best_longtext_b << best_shorttext_a
  631. << best_shorttext_b << best_common;
  632. return listRet;
  633. } else {
  634. return QStringList();
  635. }
  636. }
  637. void diff_match_patch::diff_cleanupSemantic(QList<Diff> &diffs) {
  638. if (diffs.isEmpty()) {
  639. return;
  640. }
  641. bool changes = false;
  642. QStack<Diff> equalities; // Stack of equalities.
  643. QString lastequality; // Always equal to equalities.lastElement().text
  644. QMutableListIterator<Diff> pointer(diffs);
  645. // Number of characters that changed prior to the equality.
  646. int length_insertions1 = 0;
  647. int length_deletions1 = 0;
  648. // Number of characters that changed after the equality.
  649. int length_insertions2 = 0;
  650. int length_deletions2 = 0;
  651. Diff *thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  652. while (thisDiff != NULL) {
  653. if (thisDiff->operation == EQUAL) {
  654. // Equality found.
  655. equalities.push(*thisDiff);
  656. length_insertions1 = length_insertions2;
  657. length_deletions1 = length_deletions2;
  658. length_insertions2 = 0;
  659. length_deletions2 = 0;
  660. lastequality = thisDiff->text;
  661. } else {
  662. // An insertion or deletion.
  663. if (thisDiff->operation == INSERT) {
  664. length_insertions2 += thisDiff->text.length();
  665. } else {
  666. length_deletions2 += thisDiff->text.length();
  667. }
  668. // Eliminate an equality that is smaller or equal to the edits on both
  669. // sides of it.
  670. if (!lastequality.isNull()
  671. && (lastequality.length()
  672. <= std::max(length_insertions1, length_deletions1))
  673. && (lastequality.length()
  674. <= std::max(length_insertions2, length_deletions2))) {
  675. // printf("Splitting: '%s'\n", qPrintable(lastequality));
  676. // Walk back to offending equality.
  677. while (*thisDiff != equalities.top()) {
  678. thisDiff = &pointer.previous();
  679. }
  680. pointer.next();
  681. // Replace equality with a delete.
  682. pointer.setValue(Diff(DELETE, lastequality));
  683. // Insert a corresponding an insert.
  684. pointer.insert(Diff(INSERT, lastequality));
  685. equalities.pop(); // Throw away the equality we just deleted.
  686. if (!equalities.isEmpty()) {
  687. // Throw away the previous equality (it needs to be reevaluated).
  688. equalities.pop();
  689. }
  690. if (equalities.isEmpty()) {
  691. // There are no previous equalities, walk back to the start.
  692. while (pointer.hasPrevious()) {
  693. pointer.previous();
  694. }
  695. } else {
  696. // There is a safe equality we can fall back to.
  697. thisDiff = &equalities.top();
  698. while (*thisDiff != pointer.previous()) {
  699. // Intentionally empty loop.
  700. }
  701. }
  702. length_insertions1 = 0; // Reset the counters.
  703. length_deletions1 = 0;
  704. length_insertions2 = 0;
  705. length_deletions2 = 0;
  706. lastequality = QString();
  707. changes = true;
  708. }
  709. }
  710. thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  711. }
  712. // Normalize the diff.
  713. if (changes) {
  714. diff_cleanupMerge(diffs);
  715. }
  716. diff_cleanupSemanticLossless(diffs);
  717. // Find any overlaps between deletions and insertions.
  718. // e.g: <del>abcxxx</del><ins>xxxdef</ins>
  719. // -> <del>abc</del>xxx<ins>def</ins>
  720. // e.g: <del>xxxabc</del><ins>defxxx</ins>
  721. // -> <ins>def</ins>xxx<del>abc</del>
  722. // Only extract an overlap if it is as big as the edit ahead or behind it.
  723. pointer.toFront();
  724. Diff *prevDiff = NULL;
  725. thisDiff = NULL;
  726. if (pointer.hasNext()) {
  727. prevDiff = &pointer.next();
  728. if (pointer.hasNext()) {
  729. thisDiff = &pointer.next();
  730. }
  731. }
  732. while (thisDiff != NULL) {
  733. if (prevDiff->operation == DELETE &&
  734. thisDiff->operation == INSERT) {
  735. QString deletion = prevDiff->text;
  736. QString insertion = thisDiff->text;
  737. int overlap_length1 = diff_commonOverlap(deletion, insertion);
  738. int overlap_length2 = diff_commonOverlap(insertion, deletion);
  739. if (overlap_length1 >= overlap_length2) {
  740. if (overlap_length1 >= deletion.length() / 2.0 ||
  741. overlap_length1 >= insertion.length() / 2.0) {
  742. // Overlap found. Insert an equality and trim the surrounding edits.
  743. pointer.previous();
  744. pointer.insert(Diff(EQUAL, insertion.left(overlap_length1)));
  745. prevDiff->text =
  746. deletion.left(deletion.length() - overlap_length1);
  747. thisDiff->text = safeMid(insertion, overlap_length1);
  748. // pointer.insert inserts the element before the cursor, so there is
  749. // no need to step past the new element.
  750. }
  751. } else {
  752. if (overlap_length2 >= deletion.length() / 2.0 ||
  753. overlap_length2 >= insertion.length() / 2.0) {
  754. // Reverse overlap found.
  755. // Insert an equality and swap and trim the surrounding edits.
  756. pointer.previous();
  757. pointer.insert(Diff(EQUAL, deletion.left(overlap_length2)));
  758. prevDiff->operation = INSERT;
  759. prevDiff->text =
  760. insertion.left(insertion.length() - overlap_length2);
  761. thisDiff->operation = DELETE;
  762. thisDiff->text = safeMid(deletion, overlap_length2);
  763. // pointer.insert inserts the element before the cursor, so there is
  764. // no need to step past the new element.
  765. }
  766. }
  767. thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  768. }
  769. prevDiff = thisDiff;
  770. thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  771. }
  772. }
  773. void diff_match_patch::diff_cleanupSemanticLossless(QList<Diff> &diffs) {
  774. QString equality1, edit, equality2;
  775. QString commonString;
  776. int commonOffset;
  777. int score, bestScore;
  778. QString bestEquality1, bestEdit, bestEquality2;
  779. // Create a new iterator at the start.
  780. QMutableListIterator<Diff> pointer(diffs);
  781. Diff *prevDiff = pointer.hasNext() ? &pointer.next() : NULL;
  782. Diff *thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  783. Diff *nextDiff = pointer.hasNext() ? &pointer.next() : NULL;
  784. // Intentionally ignore the first and last element (don't need checking).
  785. while (nextDiff != NULL) {
  786. if (prevDiff->operation == EQUAL &&
  787. nextDiff->operation == EQUAL) {
  788. // This is a single edit surrounded by equalities.
  789. equality1 = prevDiff->text;
  790. edit = thisDiff->text;
  791. equality2 = nextDiff->text;
  792. // First, shift the edit as far left as possible.
  793. commonOffset = diff_commonSuffix(equality1, edit);
  794. if (commonOffset != 0) {
  795. commonString = safeMid(edit, edit.length() - commonOffset);
  796. equality1 = equality1.left(equality1.length() - commonOffset);
  797. edit = commonString + edit.left(edit.length() - commonOffset);
  798. equality2 = commonString + equality2;
  799. }
  800. // Second, step character by character right, looking for the best fit.
  801. bestEquality1 = equality1;
  802. bestEdit = edit;
  803. bestEquality2 = equality2;
  804. bestScore = diff_cleanupSemanticScore(equality1, edit)
  805. + diff_cleanupSemanticScore(edit, equality2);
  806. while (!edit.isEmpty() && !equality2.isEmpty()
  807. && edit[0] == equality2[0]) {
  808. equality1 += edit[0];
  809. edit = safeMid(edit, 1) + equality2[0];
  810. equality2 = safeMid(equality2, 1);
  811. score = diff_cleanupSemanticScore(equality1, edit)
  812. + diff_cleanupSemanticScore(edit, equality2);
  813. // The >= encourages trailing rather than leading whitespace on edits.
  814. if (score >= bestScore) {
  815. bestScore = score;
  816. bestEquality1 = equality1;
  817. bestEdit = edit;
  818. bestEquality2 = equality2;
  819. }
  820. }
  821. if (prevDiff->text != bestEquality1) {
  822. // We have an improvement, save it back to the diff.
  823. if (!bestEquality1.isEmpty()) {
  824. prevDiff->text = bestEquality1;
  825. } else {
  826. pointer.previous(); // Walk past nextDiff.
  827. pointer.previous(); // Walk past thisDiff.
  828. pointer.previous(); // Walk past prevDiff.
  829. pointer.remove(); // Delete prevDiff.
  830. pointer.next(); // Walk past thisDiff.
  831. pointer.next(); // Walk past nextDiff.
  832. }
  833. thisDiff->text = bestEdit;
  834. if (!bestEquality2.isEmpty()) {
  835. nextDiff->text = bestEquality2;
  836. } else {
  837. pointer.remove(); // Delete nextDiff.
  838. nextDiff = thisDiff;
  839. thisDiff = prevDiff;
  840. }
  841. }
  842. }
  843. prevDiff = thisDiff;
  844. thisDiff = nextDiff;
  845. nextDiff = pointer.hasNext() ? &pointer.next() : NULL;
  846. }
  847. }
  848. int diff_match_patch::diff_cleanupSemanticScore(const QString &one,
  849. const QString &two) {
  850. if (one.isEmpty() || two.isEmpty()) {
  851. // Edges are the best.
  852. return 6;
  853. }
  854. // Each port of this function behaves slightly differently due to
  855. // subtle differences in each language's definition of things like
  856. // 'whitespace'. Since this function's purpose is largely cosmetic,
  857. // the choice has been made to use each language's native features
  858. // rather than force total conformity.
  859. QChar char1 = one[one.length() - 1];
  860. QChar char2 = two[0];
  861. bool nonAlphaNumeric1 = !char1.isLetterOrNumber();
  862. bool nonAlphaNumeric2 = !char2.isLetterOrNumber();
  863. bool whitespace1 = nonAlphaNumeric1 && char1.isSpace();
  864. bool whitespace2 = nonAlphaNumeric2 && char2.isSpace();
  865. bool lineBreak1 = whitespace1 && char1.category() == QChar::Other_Control;
  866. bool lineBreak2 = whitespace2 && char2.category() == QChar::Other_Control;
  867. bool blankLine1 = lineBreak1 && BLANKLINEEND.indexIn(one) != -1;
  868. bool blankLine2 = lineBreak2 && BLANKLINESTART.indexIn(two) != -1;
  869. if (blankLine1 || blankLine2) {
  870. // Five points for blank lines.
  871. return 5;
  872. } else if (lineBreak1 || lineBreak2) {
  873. // Four points for line breaks.
  874. return 4;
  875. } else if (nonAlphaNumeric1 && !whitespace1 && whitespace2) {
  876. // Three points for end of sentences.
  877. return 3;
  878. } else if (whitespace1 || whitespace2) {
  879. // Two points for whitespace.
  880. return 2;
  881. } else if (nonAlphaNumeric1 || nonAlphaNumeric2) {
  882. // One point for non-alphanumeric.
  883. return 1;
  884. }
  885. return 0;
  886. }
  887. // Define some regex patterns for matching boundaries.
  888. QRegExp diff_match_patch::BLANKLINEEND = QRegExp("\\n\\r?\\n$");
  889. QRegExp diff_match_patch::BLANKLINESTART = QRegExp("^\\r?\\n\\r?\\n");
  890. void diff_match_patch::diff_cleanupEfficiency(QList<Diff> &diffs) {
  891. if (diffs.isEmpty()) {
  892. return;
  893. }
  894. bool changes = false;
  895. QStack<Diff> equalities; // Stack of equalities.
  896. QString lastequality; // Always equal to equalities.lastElement().text
  897. QMutableListIterator<Diff> pointer(diffs);
  898. // Is there an insertion operation before the last equality.
  899. bool pre_ins = false;
  900. // Is there a deletion operation before the last equality.
  901. bool pre_del = false;
  902. // Is there an insertion operation after the last equality.
  903. bool post_ins = false;
  904. // Is there a deletion operation after the last equality.
  905. bool post_del = false;
  906. Diff *thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  907. Diff *safeDiff = thisDiff;
  908. while (thisDiff != NULL) {
  909. if (thisDiff->operation == EQUAL) {
  910. // Equality found.
  911. if (thisDiff->text.length() < Diff_EditCost && (post_ins || post_del)) {
  912. // Candidate found.
  913. equalities.push(*thisDiff);
  914. pre_ins = post_ins;
  915. pre_del = post_del;
  916. lastequality = thisDiff->text;
  917. } else {
  918. // Not a candidate, and can never become one.
  919. equalities.clear();
  920. lastequality = QString();
  921. safeDiff = thisDiff;
  922. }
  923. post_ins = post_del = false;
  924. } else {
  925. // An insertion or deletion.
  926. if (thisDiff->operation == DELETE) {
  927. post_del = true;
  928. } else {
  929. post_ins = true;
  930. }
  931. /*
  932. * Five types to be split:
  933. * <ins>A</ins><del>B</del>XY<ins>C</ins><del>D</del>
  934. * <ins>A</ins>X<ins>C</ins><del>D</del>
  935. * <ins>A</ins><del>B</del>X<ins>C</ins>
  936. * <ins>A</del>X<ins>C</ins><del>D</del>
  937. * <ins>A</ins><del>B</del>X<del>C</del>
  938. */
  939. if (!lastequality.isNull()
  940. && ((pre_ins && pre_del && post_ins && post_del)
  941. || ((lastequality.length() < Diff_EditCost / 2)
  942. && ((pre_ins ? 1 : 0) + (pre_del ? 1 : 0)
  943. + (post_ins ? 1 : 0) + (post_del ? 1 : 0)) == 3))) {
  944. // printf("Splitting: '%s'\n", qPrintable(lastequality));
  945. // Walk back to offending equality.
  946. while (*thisDiff != equalities.top()) {
  947. thisDiff = &pointer.previous();
  948. }
  949. pointer.next();
  950. // Replace equality with a delete.
  951. pointer.setValue(Diff(DELETE, lastequality));
  952. // Insert a corresponding an insert.
  953. pointer.insert(Diff(INSERT, lastequality));
  954. thisDiff = &pointer.previous();
  955. pointer.next();
  956. equalities.pop(); // Throw away the equality we just deleted.
  957. lastequality = QString();
  958. if (pre_ins && pre_del) {
  959. // No changes made which could affect previous entry, keep going.
  960. post_ins = post_del = true;
  961. equalities.clear();
  962. safeDiff = thisDiff;
  963. } else {
  964. if (!equalities.isEmpty()) {
  965. // Throw away the previous equality (it needs to be reevaluated).
  966. equalities.pop();
  967. }
  968. if (equalities.isEmpty()) {
  969. // There are no previous questionable equalities,
  970. // walk back to the last known safe diff.
  971. thisDiff = safeDiff;
  972. } else {
  973. // There is an equality we can fall back to.
  974. thisDiff = &equalities.top();
  975. }
  976. while (*thisDiff != pointer.previous()) {
  977. // Intentionally empty loop.
  978. }
  979. post_ins = post_del = false;
  980. }
  981. changes = true;
  982. }
  983. }
  984. thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  985. }
  986. if (changes) {
  987. diff_cleanupMerge(diffs);
  988. }
  989. }
  990. void diff_match_patch::diff_cleanupMerge(QList<Diff> &diffs) {
  991. diffs.append(Diff(EQUAL, "")); // Add a dummy entry at the end.
  992. QMutableListIterator<Diff> pointer(diffs);
  993. int count_delete = 0;
  994. int count_insert = 0;
  995. QString text_delete = "";
  996. QString text_insert = "";
  997. Diff *thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  998. Diff *prevEqual = NULL;
  999. int commonlength;
  1000. while (thisDiff != NULL) {
  1001. switch (thisDiff->operation) {
  1002. case INSERT:
  1003. count_insert++;
  1004. text_insert += thisDiff->text;
  1005. prevEqual = NULL;
  1006. break;
  1007. case DELETE:
  1008. count_delete++;
  1009. text_delete += thisDiff->text;
  1010. prevEqual = NULL;
  1011. break;
  1012. case EQUAL:
  1013. if (count_delete + count_insert > 1) {
  1014. bool both_types = count_delete != 0 && count_insert != 0;
  1015. // Delete the offending records.
  1016. pointer.previous(); // Reverse direction.
  1017. while (count_delete-- > 0) {
  1018. pointer.previous();
  1019. pointer.remove();
  1020. }
  1021. while (count_insert-- > 0) {
  1022. pointer.previous();
  1023. pointer.remove();
  1024. }
  1025. if (both_types) {
  1026. // Factor out any common prefixies.
  1027. commonlength = diff_commonPrefix(text_insert, text_delete);
  1028. if (commonlength != 0) {
  1029. if (pointer.hasPrevious()) {
  1030. thisDiff = &pointer.previous();
  1031. if (thisDiff->operation != EQUAL) {
  1032. throw "Previous diff should have been an equality.";
  1033. }
  1034. thisDiff->text += text_insert.left(commonlength);
  1035. pointer.next();
  1036. } else {
  1037. pointer.insert(Diff(EQUAL, text_insert.left(commonlength)));
  1038. }
  1039. text_insert = safeMid(text_insert, commonlength);
  1040. text_delete = safeMid(text_delete, commonlength);
  1041. }
  1042. // Factor out any common suffixies.
  1043. commonlength = diff_commonSuffix(text_insert, text_delete);
  1044. if (commonlength != 0) {
  1045. thisDiff = &pointer.next();
  1046. thisDiff->text = safeMid(text_insert, text_insert.length()
  1047. - commonlength) + thisDiff->text;
  1048. text_insert = text_insert.left(text_insert.length()
  1049. - commonlength);
  1050. text_delete = text_delete.left(text_delete.length()
  1051. - commonlength);
  1052. pointer.previous();
  1053. }
  1054. }
  1055. // Insert the merged records.
  1056. if (!text_delete.isEmpty()) {
  1057. pointer.insert(Diff(DELETE, text_delete));
  1058. }
  1059. if (!text_insert.isEmpty()) {
  1060. pointer.insert(Diff(INSERT, text_insert));
  1061. }
  1062. // Step forward to the equality.
  1063. thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  1064. } else if (prevEqual != NULL) {
  1065. // Merge this equality with the previous one.
  1066. prevEqual->text += thisDiff->text;
  1067. pointer.remove();
  1068. thisDiff = &pointer.previous();
  1069. pointer.next(); // Forward direction
  1070. }
  1071. count_insert = 0;
  1072. count_delete = 0;
  1073. text_delete = "";
  1074. text_insert = "";
  1075. prevEqual = thisDiff;
  1076. break;
  1077. }
  1078. thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  1079. }
  1080. if (diffs.back().text.isEmpty()) {
  1081. diffs.removeLast(); // Remove the dummy entry at the end.
  1082. }
  1083. /*
  1084. * Second pass: look for single edits surrounded on both sides by equalities
  1085. * which can be shifted sideways to eliminate an equality.
  1086. * e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC
  1087. */
  1088. bool changes = false;
  1089. // Create a new iterator at the start.
  1090. // (As opposed to walking the current one back.)
  1091. pointer.toFront();
  1092. Diff *prevDiff = pointer.hasNext() ? &pointer.next() : NULL;
  1093. thisDiff = pointer.hasNext() ? &pointer.next() : NULL;
  1094. Diff *nextDiff = pointer.hasNext() ? &pointer.next() : NULL;
  1095. // Intentionally ignore the first and last element (don't need checking).
  1096. while (nextDiff != NULL) {
  1097. if (prevDiff->operation == EQUAL &&
  1098. nextDiff->operation == EQUAL) {
  1099. // This is a single edit surrounded by equalities.
  1100. if (thisDiff->text.endsWith(prevDiff->text)) {
  1101. // Shift the edit over the previous equality.
  1102. thisDiff->text = prevDiff->text
  1103. + thisDiff->text.left(thisDiff->text.length()
  1104. - prevDiff->text.length());
  1105. nextDiff->text = prevDiff->text + nextDiff->text;
  1106. pointer.previous(); // Walk past nextDiff.
  1107. pointer.previous(); // Walk past thisDiff.
  1108. pointer.previous(); // Walk past prevDiff.
  1109. pointer.remove(); // Delete prevDiff.
  1110. pointer.next(); // Walk past thisDiff.
  1111. thisDiff = &pointer.next(); // Walk past nextDiff.
  1112. nextDiff = pointer.hasNext() ? &pointer.next() : NULL;
  1113. changes = true;
  1114. } else if (thisDiff->text.startsWith(nextDiff->text)) {
  1115. // Shift the edit over the next equality.
  1116. prevDiff->text += nextDiff->text;
  1117. thisDiff->text = safeMid(thisDiff->text, nextDiff->text.length())
  1118. + nextDiff->text;
  1119. pointer.remove(); // Delete nextDiff.
  1120. nextDiff = pointer.hasNext() ? &pointer.next() : NULL;
  1121. changes = true;
  1122. }
  1123. }
  1124. prevDiff = thisDiff;
  1125. thisDiff = nextDiff;
  1126. nextDiff = pointer.hasNext() ? &pointer.next() : NULL;
  1127. }
  1128. // If shifts were made, the diff needs reordering and another shift sweep.
  1129. if (changes) {
  1130. diff_cleanupMerge(diffs);
  1131. }
  1132. }
  1133. int diff_match_patch::diff_xIndex(const QList<Diff> &diffs, int loc) {
  1134. int chars1 = 0;
  1135. int chars2 = 0;
  1136. int last_chars1 = 0;
  1137. int last_chars2 = 0;
  1138. Diff lastDiff;
  1139. foreach(Diff aDiff, diffs) {
  1140. if (aDiff.operation != INSERT) {
  1141. // Equality or deletion.
  1142. chars1 += aDiff.text.length();
  1143. }
  1144. if (aDiff.operation != DELETE) {
  1145. // Equality or insertion.
  1146. chars2 += aDiff.text.length();
  1147. }
  1148. if (chars1 > loc) {
  1149. // Overshot the location.
  1150. lastDiff = aDiff;
  1151. break;
  1152. }
  1153. last_chars1 = chars1;
  1154. last_chars2 = chars2;
  1155. }
  1156. if (lastDiff.operation == DELETE) {
  1157. // The location was deleted.
  1158. return last_chars2;
  1159. }
  1160. // Add the remaining character length.
  1161. return last_chars2 + (loc - last_chars1);
  1162. }
  1163. QString diff_match_patch::diff_prettyHtml(const QList<Diff> &diffs) {
  1164. QString html;
  1165. QString text;
  1166. foreach(Diff aDiff, diffs) {
  1167. text = aDiff.text;
  1168. text.replace("&", "&amp;").replace("<", "&lt;")
  1169. .replace(">", "&gt;").replace("\n", "&para;<br>");
  1170. switch (aDiff.operation) {
  1171. case INSERT:
  1172. html += QString("<ins style=\"background:#e6ffe6;\">") + text
  1173. + QString("</ins>");
  1174. break;
  1175. case DELETE:
  1176. html += QString("<del style=\"background:#ffe6e6;\">") + text
  1177. + QString("</del>");
  1178. break;
  1179. case EQUAL:
  1180. html += QString("<span>") + text + QString("</span>");
  1181. break;
  1182. }
  1183. }
  1184. return html;
  1185. }
  1186. QString diff_match_patch::diff_text1(const QList<Diff> &diffs) {
  1187. QString text;
  1188. foreach(Diff aDiff, diffs) {
  1189. if (aDiff.operation != INSERT) {
  1190. text += aDiff.text;
  1191. }
  1192. }
  1193. return text;
  1194. }
  1195. QString diff_match_patch::diff_text2(const QList<Diff> &diffs) {
  1196. QString text;
  1197. foreach(Diff aDiff, diffs) {
  1198. if (aDiff.operation != DELETE) {
  1199. text += aDiff.text;
  1200. }
  1201. }
  1202. return text;
  1203. }
  1204. int diff_match_patch::diff_levenshtein(const QList<Diff> &diffs) {
  1205. int levenshtein = 0;
  1206. int insertions = 0;
  1207. int deletions = 0;
  1208. foreach(Diff aDiff, diffs) {
  1209. switch (aDiff.operation) {
  1210. case INSERT:
  1211. insertions += aDiff.text.length();
  1212. break;
  1213. case DELETE:
  1214. deletions += aDiff.text.length();
  1215. break;
  1216. case EQUAL:
  1217. // A deletion and an insertion is one substitution.
  1218. levenshtein += std::max(insertions, deletions);
  1219. insertions = 0;
  1220. deletions = 0;
  1221. break;
  1222. }
  1223. }
  1224. levenshtein += std::max(insertions, deletions);
  1225. return levenshtein;
  1226. }
  1227. QString diff_match_patch::diff_toDelta(const QList<Diff> &diffs) {
  1228. QString text;
  1229. foreach(Diff aDiff, diffs) {
  1230. switch (aDiff.operation) {
  1231. case INSERT: {
  1232. QString encoded = QString(QUrl::toPercentEncoding(aDiff.text,
  1233. " !~*'();/?:@&=+$,#"));
  1234. text += QString("+") + encoded + QString("\t");
  1235. break;
  1236. }
  1237. case DELETE:
  1238. text += QString("-") + QString::number(aDiff.text.length())
  1239. + QString("\t");
  1240. break;
  1241. case EQUAL:
  1242. text += QString("=") + QString::number(aDiff.text.length())
  1243. + QString("\t");
  1244. break;
  1245. }
  1246. }
  1247. if (!text.isEmpty()) {
  1248. // Strip off trailing tab character.
  1249. text = text.left(text.length() - 1);
  1250. }
  1251. return text;
  1252. }
  1253. QList<Diff> diff_match_patch::diff_fromDelta(const QString &text1,
  1254. const QString &delta) {
  1255. QList<Diff> diffs;
  1256. int pointer = 0; // Cursor in text1
  1257. QStringList tokens = delta.split("\t");
  1258. foreach(QString token, tokens) {
  1259. if (token.isEmpty()) {
  1260. // Blank tokens are ok (from a trailing \t).
  1261. continue;
  1262. }
  1263. // Each token begins with a one character parameter which specifies the
  1264. // operation of this token (delete, insert, equality).
  1265. QString param = safeMid(token, 1);
  1266. switch (token[0].toAscii()) {
  1267. case '+':
  1268. param = QUrl::fromPercentEncoding(qPrintable(param));
  1269. diffs.append(Diff(INSERT, param));
  1270. break;
  1271. case '-':
  1272. // Fall through.
  1273. case '=': {
  1274. int n;
  1275. n = param.toInt();
  1276. if (n < 0) {
  1277. throw QString("Negative number in diff_fromDelta: %1").arg(param);
  1278. }
  1279. QString text;
  1280. text = safeMid(text1, pointer, n);
  1281. pointer += n;
  1282. if (token[0] == QChar('=')) {
  1283. diffs.append(Diff(EQUAL, text));
  1284. } else {
  1285. diffs.append(Diff(DELETE, text));
  1286. }
  1287. break;
  1288. }
  1289. default:
  1290. throw QString("Invalid diff operation in diff_fromDelta: %1")
  1291. .arg(token[0]);
  1292. }
  1293. }
  1294. if (pointer != text1.length()) {
  1295. throw QString("Delta length (%1) smaller than source text length (%2)")
  1296. .arg(pointer).arg(text1.length());
  1297. }
  1298. return diffs;
  1299. }
  1300. // MATCH FUNCTIONS
  1301. int diff_match_patch::match_main(const QString &text, const QString &pattern,
  1302. int loc) {
  1303. // Check for null inputs.
  1304. if (text.isNull() || pattern.isNull()) {
  1305. throw "Null inputs. (match_main)";
  1306. }
  1307. loc = std::max(0, std::min(loc, text.length()));
  1308. if (text == pattern) {
  1309. // Shortcut (potentially not guaranteed by the algorithm)
  1310. return 0;
  1311. } else if (text.isEmpty()) {
  1312. // Nothing to match.
  1313. return -1;
  1314. } else if (loc + pattern.length() <= text.length()
  1315. && safeMid(text, loc, pattern.length()) == pattern) {
  1316. // Perfect match at the perfect spot! (Includes case of null pattern)
  1317. return loc;
  1318. } else {
  1319. // Do a fuzzy compare.
  1320. return match_bitap(text, pattern, loc);
  1321. }
  1322. }
  1323. int diff_match_patch::match_bitap(const QString &text, const QString &pattern,
  1324. int loc) {
  1325. if (!(Match_MaxBits == 0 || pattern.length() <= Match_MaxBits)) {
  1326. throw "Pattern too long for this application.";
  1327. }
  1328. // Initialise the alphabet.
  1329. QMap<QChar, int> s = match_alphabet(pattern);
  1330. // Highest score beyond which we give up.
  1331. double score_threshold = Match_Threshold;
  1332. // Is there a nearby exact match? (speedup)
  1333. int best_loc = text.indexOf(pattern, loc);
  1334. if (best_loc != -1) {
  1335. score_threshold = std::min(match_bitapScore(0, best_loc, loc, pattern),
  1336. score_threshold);
  1337. // What about in the other direction? (speedup)
  1338. best_loc = text.lastIndexOf(pattern, loc + pattern.length());
  1339. if (best_loc != -1) {
  1340. score_threshold = std::min(match_bitapScore(0, best_loc, loc, pattern),
  1341. score_threshold);
  1342. }
  1343. }
  1344. // Initialise the bit arrays.
  1345. int matchmask = 1 << (pattern.length() - 1);
  1346. best_loc = -1;
  1347. int bin_min, bin_mid;
  1348. int bin_max = pattern.length() + text.length();
  1349. int *rd;
  1350. int *last_rd = NULL;
  1351. for (int d = 0; d < pattern.length(); d++) {
  1352. // Scan for the best match; each iteration allows for one more error.
  1353. // Run a binary search to determine how far from 'loc' we can stray at
  1354. // this error level.
  1355. bin_min = 0;
  1356. bin_mid = bin_max;
  1357. while (bin_min < bin_mid) {
  1358. if (match_bitapScore(d, loc + bin_mid, loc, pattern)
  1359. <= score_threshold) {
  1360. bin_min = bin_mid;
  1361. } else {
  1362. bin_max = bin_mid;
  1363. }
  1364. bin_mid = (bin_max - bin_min) / 2 + bin_min;
  1365. }
  1366. // Use the result from this iteration as the maximum for the next.
  1367. bin_max = bin_mid;
  1368. int start = std::max(1, loc - bin_mid + 1);
  1369. int finish = std::min(loc + bin_mid, text.length()) + pattern.length();
  1370. rd = new int[finish + 2];
  1371. rd[finish + 1] = (1 << d) - 1;
  1372. for (int j = finish; j >= start; j--) {
  1373. int charMatch;
  1374. if (text.length() <= j - 1) {
  1375. // Out of range.
  1376. charMatch = 0;
  1377. } else {
  1378. charMatch = s.value(text[j - 1], 0);
  1379. }
  1380. if (d == 0) {
  1381. // First pass: exact match.
  1382. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch;
  1383. } else {
  1384. // Subsequent passes: fuzzy match.
  1385. rd[j] = ((rd[j + 1] << 1) | 1) & charMatch
  1386. | (((last_rd[j + 1] | last_rd[j]) << 1) | 1)
  1387. | last_rd[j + 1];
  1388. }
  1389. if ((rd[j] & matchmask) != 0) {
  1390. double score = match_bitapScore(d, j - 1, loc, pattern);
  1391. // This match will almost certainly be better than any existing
  1392. // match. But check anyway.
  1393. if (score <= score_threshold) {
  1394. // Told you so.
  1395. score_threshold = score;
  1396. best_loc = j - 1;
  1397. if (best_loc > loc) {
  1398. // When passing loc, don't exceed our current distance from loc.
  1399. start = std::max(1, 2 * loc - best_loc);
  1400. } else {
  1401. // Already passed loc, downhill from here on in.
  1402. break;
  1403. }
  1404. }
  1405. }
  1406. }
  1407. if (match_bitapScore(d + 1, loc, loc, pattern) > score_threshold) {
  1408. // No hope for a (better) match at greater error levels.
  1409. break;
  1410. }
  1411. delete [] last_rd;
  1412. last_rd = rd;
  1413. }
  1414. delete [] last_rd;
  1415. delete [] rd;
  1416. return best_loc;
  1417. }
  1418. double diff_match_patch::match_bitapScore(int e, int x, int loc,
  1419. const QString &pattern) {
  1420. const float accuracy = static_cast<float> (e) / pattern.length();
  1421. const int proximity = qAbs(loc - x);
  1422. if (Match_Distance == 0) {
  1423. // Dodge divide by zero error.
  1424. return proximity == 0 ? accuracy : 1.0;
  1425. }
  1426. return accuracy + (proximity / static_cast<float> (Match_Distance));
  1427. }
  1428. QMap<QChar, int> diff_match_patch::match_alphabet(const QString &pattern) {
  1429. QMap<QChar, int> s;
  1430. int i;
  1431. for (i = 0; i < pattern.length(); i++) {
  1432. QChar c = pattern[i];
  1433. s.insert(c, 0);
  1434. }
  1435. for (i = 0; i < pattern.length(); i++) {
  1436. QChar c = pattern[i];
  1437. s.insert(c, s.value(c) | (1 << (pattern.length() - i - 1)));
  1438. }
  1439. return s;
  1440. }
  1441. // PATCH FUNCTIONS
  1442. void diff_match_patch::patch_addContext(Patch &patch, const QString &text) {
  1443. if (text.isEmpty()) {
  1444. return;
  1445. }
  1446. QString pattern = safeMid(text, patch.start2, patch.length1);
  1447. int padding = 0;
  1448. // Look for the first and last matches of pattern in text. If two different
  1449. // matches are found, increase the pattern length.
  1450. while (text.indexOf(pattern) != text.lastIndexOf(pattern)
  1451. && pattern.length() < Match_MaxBits - Patch_Margin - Patch_Margin) {
  1452. padding += Patch_Margin;
  1453. pattern = safeMid(text, std::max(0, patch.start2 - padding),
  1454. std::min(text.length(), patch.start2 + patch.length1 + padding)
  1455. - std::max(0, patch.start2 - padding));
  1456. }
  1457. // Add one chunk for good luck.
  1458. padding += Patch_Margin;
  1459. // Add the prefix.
  1460. QString prefix = safeMid(text, std::max(0, patch.start2 - padding),
  1461. patch.start2 - std::max(0, patch.start2 - padding));
  1462. if (!prefix.isEmpty()) {
  1463. patch.diffs.prepend(Diff(EQUAL, prefix));
  1464. }
  1465. // Add the suffix.
  1466. QString suffix = safeMid(text, patch.start2 + patch.length1,
  1467. std::min(text.length(), patch.start2 + patch.length1 + padding)
  1468. - (patch.start2 + patch.length1));
  1469. if (!suffix.isEmpty()) {
  1470. patch.diffs.append(Diff(EQUAL, suffix));
  1471. }
  1472. // Roll back the start points.
  1473. patch.start1 -= prefix.length();
  1474. patch.start2 -= prefix.length();
  1475. // Extend the lengths.
  1476. patch.length1 += prefix.length() + suffix.length();
  1477. patch.length2 += prefix.length() + suffix.length();
  1478. }
  1479. QList<Patch> diff_match_patch::patch_make(const QString &text1,
  1480. const QString &text2) {
  1481. // Check for null inputs.
  1482. if (text1.isNull() || text2.isNull()) {
  1483. throw "Null inputs. (patch_make)";
  1484. }
  1485. // No diffs provided, compute our own.
  1486. QList<Diff> diffs = diff_main(text1, text2, true);
  1487. if (diffs.size() > 2) {
  1488. diff_cleanupSemantic(diffs);
  1489. diff_cleanupEfficiency(diffs);
  1490. }
  1491. return patch_make(text1, diffs);
  1492. }
  1493. QList<Patch> diff_match_patch::patch_make(const QList<Diff> &diffs) {
  1494. // No origin string provided, compute our own.
  1495. const QString text1 = diff_text1(diffs);
  1496. return patch_make(text1, diffs);
  1497. }
  1498. QList<Patch> diff_match_patch::patch_make(const QString &text1,
  1499. const QString &text2,
  1500. const QList<Diff> &diffs) {
  1501. // text2 is entirely unused.
  1502. return patch_make(text1, diffs);
  1503. Q_UNUSED(text2)
  1504. }
  1505. QList<Patch> diff_match_patch::patch_make(const QString &text1,
  1506. const QList<Diff> &diffs) {
  1507. // Check for null inputs.
  1508. if (text1.isNull()) {
  1509. throw "Null inputs. (patch_make)";
  1510. }
  1511. QList<Patch> patches;
  1512. if (diffs.isEmpty()) {
  1513. return patches; // Get rid of the null case.
  1514. }
  1515. Patch patch;
  1516. int char_count1 = 0; // Number of characters into the text1 string.
  1517. int char_count2 = 0; // Number of characters into the text2 string.
  1518. // Start with text1 (prepatch_text) and apply the diffs until we arrive at
  1519. // text2 (postpatch_text). We recreate the patches one by one to determine
  1520. // context info.
  1521. QString prepatch_text = text1;
  1522. QString postpatch_text = text1;
  1523. foreach(Diff aDiff, diffs) {
  1524. if (patch.diffs.isEmpty() && aDiff.operation != EQUAL) {
  1525. // A new patch starts here.
  1526. patch.start1 = char_count1;
  1527. patch.start2 = char_count2;
  1528. }
  1529. switch (aDiff.operation) {
  1530. case INSERT:
  1531. patch.diffs.append(aDiff);
  1532. patch.length2 += aDiff.text.length();
  1533. postpatch_text = postpatch_text.left(char_count2)
  1534. + aDiff.text + safeMid(postpatch_text, char_count2);
  1535. break;
  1536. case DELETE:
  1537. patch.length1 += aDiff.text.length();
  1538. patch.diffs.append(aDiff);
  1539. postpatch_text = postpatch_text.left(char_count2)
  1540. + safeMid(postpatch_text, char_count2 + aDiff.text.length());
  1541. break;
  1542. case EQUAL:
  1543. if (aDiff.text.length() <= 2 * Patch_Margin
  1544. && !patch.diffs.isEmpty() && !(aDiff == diffs.back())) {
  1545. // Small equality inside a patch.
  1546. patch.diffs.append(aDiff);
  1547. patch.length1 += aDiff.text.length();
  1548. patch.length2 += aDiff.text.length();
  1549. }
  1550. if (aDiff.text.length() >= 2 * Patch_Margin) {
  1551. // Time for a new patch.
  1552. if (!patch.diffs.isEmpty()) {
  1553. patch_addContext(patch, prepatch_text);
  1554. patches.append(patch);
  1555. patch = Patch();
  1556. // Unlike Unidiff, our patch lists have a rolling context.
  1557. // http://code.google.com/p/google-diff-match-patch/wiki/Unidiff
  1558. // Update prepatch text & pos to reflect the application of the
  1559. // just completed patch.
  1560. prepatch_text = postpatch_text;
  1561. char_count1 = char_count2;
  1562. }
  1563. }
  1564. break;
  1565. }
  1566. // Update the current character count.
  1567. if (aDiff.operation != INSERT) {
  1568. char_count1 += aDiff.text.length();
  1569. }
  1570. if (aDiff.operation != DELETE) {
  1571. char_count2 += aDiff.text.length();
  1572. }
  1573. }
  1574. // Pick up the leftover patch if not empty.
  1575. if (!patch.diffs.isEmpty()) {
  1576. patch_addContext(patch, prepatch_text);
  1577. patches.append(patch);
  1578. }
  1579. return patches;
  1580. }
  1581. QList<Patch> diff_match_patch::patch_deepCopy(QList<Patch> &patches) {
  1582. QList<Patch> patchesCopy;
  1583. foreach(Patch aPatch, patches) {
  1584. Patch patchCopy = Patch();
  1585. foreach(Diff aDiff, aPatch.diffs) {
  1586. Diff diffCopy = Diff(aDiff.operation, aDiff.text);
  1587. patchCopy.diffs.append(diffCopy);
  1588. }
  1589. patchCopy.start1 = aPatch.start1;
  1590. patchCopy.start2 = aPatch.start2;
  1591. patchCopy.length1 = aPatch.length1;
  1592. patchCopy.length2 = aPatch.length2;
  1593. patchesCopy.append(patchCopy);
  1594. }
  1595. return patchesCopy;
  1596. }
  1597. QPair<QString, QVector<bool> > diff_match_patch::patch_apply(
  1598. QList<Patch> &patches, const QString &sourceText) {
  1599. QString text = sourceText; // Copy to preserve original.
  1600. if (patches.isEmpty()) {
  1601. return QPair<QString,QVector<bool> >(text, QVector<bool>(0));
  1602. }
  1603. // Deep copy the patches so that no changes are made to originals.
  1604. QList<Patch> patchesCopy = patch_deepCopy(patches);
  1605. QString nullPadding = patch_addPadding(patchesCopy);
  1606. text = nullPadding + text + nullPadding;
  1607. patch_splitMax(patchesCopy);
  1608. int x = 0;
  1609. // delta keeps track of the offset between the expected and actual location
  1610. // of the previous patch. If there are patches expected at positions 10 and
  1611. // 20, but the first patch was found at 12, delta is 2 and the second patch
  1612. // has an effective expected position of 22.
  1613. int delta = 0;
  1614. QVector<bool> results(patchesCopy.size());
  1615. foreach(Patch aPatch, patchesCopy) {
  1616. int expected_loc = aPatch.start2 + delta;
  1617. QString text1 = diff_text1(aPatch.diffs);
  1618. int start_loc;
  1619. int end_loc = -1;
  1620. if (text1.length() > Match_MaxBits) {
  1621. // patch_splitMax will only provide an oversized pattern in the case of
  1622. // a monster delete.
  1623. start_loc = match_main(text, text1.left(Match_MaxBits), expected_loc);
  1624. if (start_loc != -1) {
  1625. end_loc = match_main(text, text1.right(Match_MaxBits),
  1626. expected_loc + text1.length() - Match_MaxBits);
  1627. if (end_loc == -1 || start_loc >= end_loc) {
  1628. // Can't find valid trailing context. Drop this patch.
  1629. start_loc = -1;
  1630. }
  1631. }
  1632. } else {
  1633. start_loc = match_main(text, text1, expected_loc);
  1634. }
  1635. if (start_loc == -1) {
  1636. // No match found. :(
  1637. results[x] = false;
  1638. // Subtract the delta for this failed patch from subsequent patches.
  1639. delta -= aPatch.length2 - aPatch.length1;
  1640. } else {
  1641. // Found a match. :)
  1642. results[x] = true;
  1643. delta = start_loc - expected_loc;
  1644. QString text2;
  1645. if (end_loc == -1) {
  1646. text2 = safeMid(text, start_loc, text1.length());
  1647. } else {
  1648. text2 = safeMid(text, start_loc, end_loc + Match_MaxBits - start_loc);
  1649. }
  1650. if (text1 == text2) {
  1651. // Perfect match, just shove the replacement text in.
  1652. text = text.left(start_loc) + diff_text2(aPatch.diffs)
  1653. + safeMid(text, start_loc + text1.length());
  1654. } else {
  1655. // Imperfect match. Run a diff to get a framework of equivalent
  1656. // indices.
  1657. QList<Diff> diffs = diff_main(text1, text2, false);
  1658. if (text1.length() > Match_MaxBits
  1659. && diff_levenshtein(diffs) / static_cast<float> (text1.length())
  1660. > Patch_DeleteThreshold) {
  1661. // The end points match, but the content is unacceptably bad.
  1662. results[x] = false;
  1663. } else {
  1664. diff_cleanupSemanticLossless(diffs);
  1665. int index1 = 0;
  1666. foreach(Diff aDiff, aPatch.diffs) {
  1667. if (aDiff.operation != EQUAL) {
  1668. int index2 = diff_xIndex(diffs, index1);
  1669. if (aDiff.operation == INSERT) {
  1670. // Insertion
  1671. text = text.left(start_loc + index2) + aDiff.text
  1672. + safeMid(text, start_loc + index2);
  1673. } else if (aDiff.operation == DELETE) {
  1674. // Deletion
  1675. text = text.left(start_loc + index2)
  1676. + safeMid(text, start_loc + diff_xIndex(diffs,
  1677. index1 + aDiff.text.length()));
  1678. }
  1679. }
  1680. if (aDiff.operation != DELETE) {
  1681. index1 += aDiff.text.length();
  1682. }
  1683. }
  1684. }
  1685. }
  1686. }
  1687. x++;
  1688. }
  1689. // Strip the padding off.
  1690. text = safeMid(text, nullPadding.length(), text.length()
  1691. - 2 * nullPadding.length());
  1692. return QPair<QString, QVector<bool> >(text, results);
  1693. }
  1694. QString diff_match_patch::patch_addPadding(QList<Patch> &patches) {
  1695. short paddingLength = Patch_Margin;
  1696. QString nullPadding = "";
  1697. for (short x = 1; x <= paddingLength; x++) {
  1698. nullPadding += QChar((ushort)x);
  1699. }
  1700. // Bump all the patches forward.
  1701. QMutableListIterator<Patch> pointer(patches);
  1702. while (pointer.hasNext()) {
  1703. Patch &aPatch = pointer.next();
  1704. aPatch.start1 += paddingLength;
  1705. aPatch.start2 += paddingLength;
  1706. }
  1707. // Add some padding on start of first diff.
  1708. Patch &firstPatch = patches.first();
  1709. QList<Diff> &firstPatchDiffs = firstPatch.diffs;
  1710. if (firstPatchDiffs.empty() || firstPatchDiffs.first().operation != EQUAL) {
  1711. // Add nullPadding equality.
  1712. firstPatchDiffs.prepend(Diff(EQUAL, nullPadding));
  1713. firstPatch.start1 -= paddingLength; // Should be 0.
  1714. firstPatch.start2 -= paddingLength; // Should be 0.
  1715. firstPatch.length1 += paddingLength;
  1716. firstPatch.length2 += paddingLength;
  1717. } else if (paddingLength > firstPatchDiffs.first().text.length()) {
  1718. // Grow first equality.
  1719. Diff &firstDiff = firstPatchDiffs.first();
  1720. int extraLength = paddingLength - firstDiff.text.length();
  1721. firstDiff.text = safeMid(nullPadding, firstDiff.text.length(),
  1722. paddingLength - firstDiff.text.length()) + firstDiff.text;
  1723. firstPatch.start1 -= extraLength;
  1724. firstPatch.start2 -= extraLength;
  1725. firstPatch.length1 += extraLength;
  1726. firstPatch.length2 += extraLength;
  1727. }
  1728. // Add some padding on end of last diff.
  1729. Patch &lastPatch = patches.first();
  1730. QList<Diff> &lastPatchDiffs = lastPatch.diffs;
  1731. if (lastPatchDiffs.empty() || lastPatchDiffs.last().operation != EQUAL) {
  1732. // Add nullPadding equality.
  1733. lastPatchDiffs.append(Diff(EQUAL, nullPadding));
  1734. lastPatch.length1 += paddingLength;
  1735. lastPatch.length2 += paddingLength;
  1736. } else if (paddingLength > lastPatchDiffs.last().text.length()) {
  1737. // Grow last equality.
  1738. Diff &lastDiff = lastPatchDiffs.last();
  1739. int extraLength = paddingLength - lastDiff.text.length();
  1740. lastDiff.text += nullPadding.left(extraLength);
  1741. lastPatch.length1 += extraLength;
  1742. lastPatch.length2 += extraLength;
  1743. }
  1744. return nullPadding;
  1745. }
  1746. void diff_match_patch::patch_splitMax(QList<Patch> &patches) {
  1747. short patch_size = Match_MaxBits;
  1748. QString precontext, postcontext;
  1749. Patch patch;
  1750. int start1, start2;
  1751. bool empty;
  1752. Operation diff_type;
  1753. QString diff_text;
  1754. QMutableListIterator<Patch> pointer(patches);
  1755. Patch bigpatch;
  1756. if (pointer.hasNext()) {
  1757. bigpatch = pointer.next();
  1758. }
  1759. while (!bigpatch.isNull()) {
  1760. if (bigpatch.length1 <= patch_size) {
  1761. bigpatch = pointer.hasNext() ? pointer.next() : Patch();
  1762. continue;
  1763. }
  1764. // Remove the big old patch.
  1765. pointer.remove();
  1766. start1 = bigpatch.start1;
  1767. start2 = bigpatch.start2;
  1768. precontext = "";
  1769. while (!bigpatch.diffs.isEmpty()) {
  1770. // Create one of several smaller patches.
  1771. patch = Patch();
  1772. empty = true;
  1773. patch.start1 = start1 - precontext.length();
  1774. patch.start2 = start2 - precontext.length();
  1775. if (!precontext.isEmpty()) {
  1776. patch.length1 = patch.length2 = precontext.length();
  1777. patch.diffs.append(Diff(EQUAL, precontext));
  1778. }
  1779. while (!bigpatch.diffs.isEmpty()
  1780. && patch.length1 < patch_size - Patch_Margin) {
  1781. diff_type = bigpatch.diffs.front().operation;
  1782. diff_text = bigpatch.diffs.front().text;
  1783. if (diff_type == INSERT) {
  1784. // Insertions are harmless.
  1785. patch.length2 += diff_text.length();
  1786. start2 += diff_text.length();
  1787. patch.diffs.append(bigpatch.diffs.front());
  1788. bigpatch.diffs.removeFirst();
  1789. empty = false;
  1790. } else if (diff_type == DELETE && patch.diffs.size() == 1
  1791. && patch.diffs.front().operation == EQUAL
  1792. && diff_text.length() > 2 * patch_size) {
  1793. // This is a large deletion. Let it pass in one chunk.
  1794. patch.length1 += diff_text.length();
  1795. start1 += diff_text.length();
  1796. empty = false;
  1797. patch.diffs.append(Diff(diff_type, diff_text));
  1798. bigpatch.diffs.removeFirst();
  1799. } else {
  1800. // Deletion or equality. Only take as much as we can stomach.
  1801. diff_text = diff_text.left(std::min(diff_text.length(),
  1802. patch_size - patch.length1 - Patch_Margin));
  1803. patch.length1 += diff_text.length();
  1804. start1 += diff_text.length();
  1805. if (diff_type == EQUAL) {
  1806. patch.length2 += diff_text.length();
  1807. start2 += diff_text.length();
  1808. } else {
  1809. empty = false;
  1810. }
  1811. patch.diffs.append(Diff(diff_type, diff_text));
  1812. if (diff_text == bigpatch.diffs.front().text) {
  1813. bigpatch.diffs.removeFirst();
  1814. } else {
  1815. bigpatch.diffs.front().text = safeMid(bigpatch.diffs.front().text,
  1816. diff_text.length());
  1817. }
  1818. }
  1819. }
  1820. // Compute the head context for the next patch.
  1821. precontext = diff_text2(patch.diffs);
  1822. precontext = safeMid(precontext, precontext.length() - Patch_Margin);
  1823. // Append the end context for this patch.
  1824. if (diff_text1(bigpatch.diffs).length() > Patch_Margin) {
  1825. postcontext = diff_text1(bigpatch.diffs).left(Patch_Margin);
  1826. } else {
  1827. postcontext = diff_text1(bigpatch.diffs);
  1828. }
  1829. if (!postcontext.isEmpty()) {
  1830. patch.length1 += postcontext.length();
  1831. patch.length2 += postcontext.length();
  1832. if (!patch.diffs.isEmpty()
  1833. && patch.diffs.back().operation == EQUAL) {
  1834. patch.diffs.back().text += postcontext;
  1835. } else {
  1836. patch.diffs.append(Diff(EQUAL, postcontext));
  1837. }
  1838. }
  1839. if (!empty) {
  1840. pointer.insert(patch);
  1841. }
  1842. }
  1843. bigpatch = pointer.hasNext() ? pointer.next() : Patch();
  1844. }
  1845. }
  1846. QString diff_match_patch::patch_toText(const QList<Patch> &patches) {
  1847. QString text;
  1848. foreach(Patch aPatch, patches) {
  1849. text.append(aPatch.toString());
  1850. }
  1851. return text;
  1852. }
  1853. QList<Patch> diff_match_patch::patch_fromText(const QString &textline) {
  1854. QList<Patch> patches;
  1855. if (textline.isEmpty()) {
  1856. return patches;
  1857. }
  1858. QStringList text = textline.split("\n", QString::SkipEmptyParts);
  1859. Patch patch;
  1860. QRegExp patchHeader("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$");
  1861. char sign;
  1862. QString line;
  1863. while (!text.isEmpty()) {
  1864. if (!patchHeader.exactMatch(text.front())) {
  1865. throw QString("Invalid patch string: %1").arg(text.front());
  1866. }
  1867. patch = Patch();
  1868. patch.start1 = patchHeader.cap(1).toInt();
  1869. if (patchHeader.cap(2).isEmpty()) {
  1870. patch.start1--;
  1871. patch.length1 = 1;
  1872. } else if (patchHeader.cap(2) == "0") {
  1873. patch.length1 = 0;
  1874. } else {
  1875. patch.start1--;
  1876. patch.length1 = patchHeader.cap(2).toInt();
  1877. }
  1878. patch.start2 = patchHeader.cap(3).toInt();
  1879. if (patchHeader.cap(4).isEmpty()) {
  1880. patch.start2--;
  1881. patch.length2 = 1;
  1882. } else if (patchHeader.cap(4) == "0") {
  1883. patch.length2 = 0;
  1884. } else {
  1885. patch.start2--;
  1886. patch.length2 = patchHeader.cap(4).toInt();
  1887. }
  1888. text.removeFirst();
  1889. while (!text.isEmpty()) {
  1890. if (text.front().isEmpty()) {
  1891. text.removeFirst();
  1892. continue;
  1893. }
  1894. sign = text.front()[0].toAscii();
  1895. line = safeMid(text.front(), 1);
  1896. line = line.replace("+", "%2B"); // decode would change all "+" to " "
  1897. line = QUrl::fromPercentEncoding(qPrintable(line));
  1898. if (sign == '-') {
  1899. // Deletion.
  1900. patch.diffs.append(Diff(DELETE, line));
  1901. } else if (sign == '+') {
  1902. // Insertion.
  1903. patch.diffs.append(Diff(INSERT, line));
  1904. } else if (sign == ' ') {
  1905. // Minor equality.
  1906. patch.diffs.append(Diff(EQUAL, line));
  1907. } else if (sign == '@') {
  1908. // Start of next patch.
  1909. break;
  1910. } else {
  1911. // WTF?
  1912. throw QString("Invalid patch mode '%1' in: %2").arg(sign).arg(line);
  1913. return QList<Patch>();
  1914. }
  1915. text.removeFirst();
  1916. }
  1917. patches.append(patch);
  1918. }
  1919. return patches;
  1920. }