diff_match_patch.cpp 69 KB

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