dmp.go 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  1. /**
  2. * dmp.go
  3. *
  4. * Go language implementation of Google Diff, Match, and Patch library
  5. *
  6. * Original library is Copyright (c) 2006 Google Inc.
  7. * http://code.google.com/p/google-diff-match-patch/
  8. *
  9. * Copyright (c) 2012 Sergi Mansilla <sergi.mansilla@gmail.com>
  10. * https://github.com/sergi/go-diff
  11. *
  12. * See included LICENSE file for license details.
  13. */
  14. // Package diffmatchpatch offers robust algorithms to perform the
  15. // operations required for synchronizing plain text.
  16. package diffmatchpatch
  17. import (
  18. "bytes"
  19. "errors"
  20. "fmt"
  21. "html"
  22. "math"
  23. "net/url"
  24. "regexp"
  25. "strconv"
  26. "strings"
  27. "time"
  28. "unicode/utf8"
  29. )
  30. // The data structure representing a diff is an array of tuples:
  31. // [[DiffDelete, 'Hello'], [DiffInsert, 'Goodbye'], [DiffEqual, ' world.']]
  32. // which means: delete 'Hello', add 'Goodbye' and keep ' world.'
  33. type Operation int8
  34. const (
  35. DiffDelete Operation = -1
  36. DiffInsert Operation = 1
  37. DiffEqual Operation = 0
  38. )
  39. // unescaper unescapes selected chars for compatibility with JavaScript's encodeURI.
  40. // In speed critical applications this could be dropped since the
  41. // receiving application will certainly decode these fine.
  42. // Note that this function is case-sensitive. Thus "%3F" would not be
  43. // unescaped. But this is ok because it is only called with the output of
  44. // HttpUtility.UrlEncode which returns lowercase hex.
  45. //
  46. // Example: "%3f" -> "?", "%24" -> "$", etc.
  47. var unescaper = strings.NewReplacer(
  48. "%21", "!", "%7E", "~", "%27", "'",
  49. "%28", "(", "%29", ")", "%3B", ";",
  50. "%2F", "/", "%3F", "?", "%3A", ":",
  51. "%40", "@", "%26", "&", "%3D", "=",
  52. "%2B", "+", "%24", "$", "%2C", ",", "%23", "#", "%2A", "*")
  53. // Define some regex patterns for matching boundaries.
  54. var (
  55. nonAlphaNumericRegex_ = regexp.MustCompile(`[^a-zA-Z0-9]`)
  56. whitespaceRegex_ = regexp.MustCompile(`\s`)
  57. linebreakRegex_ = regexp.MustCompile(`[\r\n]`)
  58. blanklineEndRegex_ = regexp.MustCompile(`\n\r?\n$`)
  59. blanklineStartRegex_ = regexp.MustCompile(`^\r?\n\r?\n`)
  60. )
  61. func splice(slice []Diff, index int, amount int, elements ...Diff) []Diff {
  62. return append(slice[:index], append(elements, slice[index+amount:]...)...)
  63. }
  64. // indexOf returns the first index of pattern in str, starting at str[i].
  65. func indexOf(str string, pattern string, i int) int {
  66. if i > len(str)-1 {
  67. return -1
  68. }
  69. if i <= 0 {
  70. return strings.Index(str, pattern)
  71. }
  72. ind := strings.Index(str[i:], pattern)
  73. if ind == -1 {
  74. return -1
  75. }
  76. return ind + i
  77. }
  78. // lastIndexOf returns the last index of pattern in str, starting at str[i].
  79. func lastIndexOf(str string, pattern string, i int) int {
  80. if i < 0 {
  81. return -1
  82. }
  83. if i >= len(str) {
  84. return strings.LastIndex(str, pattern)
  85. }
  86. _, size := utf8.DecodeRuneInString(str[i:])
  87. return strings.LastIndex(str[:i+size], pattern)
  88. }
  89. // Return the index of pattern in target, starting at target[i].
  90. func runesIndexOf(target, pattern []rune, i int) int {
  91. if i > len(target)-1 {
  92. return -1
  93. }
  94. if i <= 0 {
  95. return runesIndex(target, pattern)
  96. }
  97. ind := runesIndex(target[i:], pattern)
  98. if ind == -1 {
  99. return -1
  100. }
  101. return ind + i
  102. }
  103. func min(x, y int) int {
  104. if x < y {
  105. return x
  106. }
  107. return y
  108. }
  109. func max(x, y int) int {
  110. if x > y {
  111. return x
  112. }
  113. return y
  114. }
  115. func runesEqual(r1, r2 []rune) bool {
  116. if len(r1) != len(r2) {
  117. return false
  118. }
  119. for i, c := range r1 {
  120. if c != r2[i] {
  121. return false
  122. }
  123. }
  124. return true
  125. }
  126. // The equivalent of strings.Index for rune slices.
  127. func runesIndex(r1, r2 []rune) int {
  128. last := len(r1) - len(r2)
  129. for i := 0; i <= last; i++ {
  130. if runesEqual(r1[i:i+len(r2)], r2) {
  131. return i
  132. }
  133. }
  134. return -1
  135. }
  136. // Diff represents one diff operation
  137. type Diff struct {
  138. Type Operation
  139. Text string
  140. }
  141. // Patch represents one patch operation.
  142. type Patch struct {
  143. diffs []Diff
  144. start1 int
  145. start2 int
  146. length1 int
  147. length2 int
  148. }
  149. // String emulates GNU diff's format.
  150. // Header: @@ -382,8 +481,9 @@
  151. // Indicies are printed as 1-based, not 0-based.
  152. func (p *Patch) String() string {
  153. var coords1, coords2 string
  154. if p.length1 == 0 {
  155. coords1 = strconv.Itoa(p.start1) + ",0"
  156. } else if p.length1 == 1 {
  157. coords1 = strconv.Itoa(p.start1 + 1)
  158. } else {
  159. coords1 = strconv.Itoa(p.start1+1) + "," + strconv.Itoa(p.length1)
  160. }
  161. if p.length2 == 0 {
  162. coords2 = strconv.Itoa(p.start2) + ",0"
  163. } else if p.length2 == 1 {
  164. coords2 = strconv.Itoa(p.start2 + 1)
  165. } else {
  166. coords2 = strconv.Itoa(p.start2+1) + "," + strconv.Itoa(p.length2)
  167. }
  168. var text bytes.Buffer
  169. text.WriteString("@@ -" + coords1 + " +" + coords2 + " @@\n")
  170. // Escape the body of the patch with %xx notation.
  171. for _, aDiff := range p.diffs {
  172. switch aDiff.Type {
  173. case DiffInsert:
  174. text.WriteString("+")
  175. case DiffDelete:
  176. text.WriteString("-")
  177. case DiffEqual:
  178. text.WriteString(" ")
  179. }
  180. text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1))
  181. text.WriteString("\n")
  182. }
  183. return unescaper.Replace(text.String())
  184. }
  185. type DiffMatchPatch struct {
  186. // Number of seconds to map a diff before giving up (0 for infinity).
  187. DiffTimeout time.Duration
  188. // Cost of an empty edit operation in terms of edit characters.
  189. DiffEditCost int
  190. // How far to search for a match (0 = exact location, 1000+ = broad match).
  191. // A match this many characters away from the expected location will add
  192. // 1.0 to the score (0.0 is a perfect match).
  193. MatchDistance int
  194. // When deleting a large block of text (over ~64 characters), how close do
  195. // the contents have to be to match the expected contents. (0.0 = perfection,
  196. // 1.0 = very loose). Note that Match_Threshold controls how closely the
  197. // end points of a delete need to match.
  198. PatchDeleteThreshold float64
  199. // Chunk size for context length.
  200. PatchMargin int
  201. // The number of bits in an int.
  202. MatchMaxBits int
  203. // At what point is no match declared (0.0 = perfection, 1.0 = very loose).
  204. MatchThreshold float64
  205. }
  206. // New creates a new DiffMatchPatch object with default parameters.
  207. func New() *DiffMatchPatch {
  208. // Defaults.
  209. return &DiffMatchPatch{
  210. DiffTimeout: time.Second,
  211. DiffEditCost: 4,
  212. MatchThreshold: 0.5,
  213. MatchDistance: 1000,
  214. PatchDeleteThreshold: 0.5,
  215. PatchMargin: 4,
  216. MatchMaxBits: 32,
  217. }
  218. }
  219. // DiffMain finds the differences between two texts.
  220. func (dmp *DiffMatchPatch) DiffMain(text1, text2 string, checklines bool) []Diff {
  221. var deadline time.Time
  222. if dmp.DiffTimeout <= 0 {
  223. deadline = time.Now().Add(24 * 365 * time.Hour)
  224. } else {
  225. deadline = time.Now().Add(dmp.DiffTimeout)
  226. }
  227. return dmp.diffMain(text1, text2, checklines, deadline)
  228. }
  229. func (dmp *DiffMatchPatch) diffMain(text1, text2 string, checklines bool, deadline time.Time) []Diff {
  230. return dmp.diffMainRunes([]rune(text1), []rune(text2), checklines, deadline)
  231. }
  232. // DiffMainRunes finds the differences between two rune sequences.
  233. func (dmp *DiffMatchPatch) DiffMainRunes(text1, text2 []rune, checklines bool) []Diff {
  234. var deadline time.Time
  235. if dmp.DiffTimeout <= 0 {
  236. deadline = time.Now().Add(24 * 365 * time.Hour)
  237. } else {
  238. deadline = time.Now().Add(dmp.DiffTimeout)
  239. }
  240. return dmp.diffMainRunes(text1, text2, checklines, deadline)
  241. }
  242. func (dmp *DiffMatchPatch) diffMainRunes(text1, text2 []rune, checklines bool, deadline time.Time) []Diff {
  243. if runesEqual(text1, text2) {
  244. var diffs []Diff
  245. if len(text1) > 0 {
  246. diffs = append(diffs, Diff{DiffEqual, string(text1)})
  247. }
  248. return diffs
  249. }
  250. // Trim off common prefix (speedup).
  251. commonlength := commonPrefixLength(text1, text2)
  252. commonprefix := text1[:commonlength]
  253. text1 = text1[commonlength:]
  254. text2 = text2[commonlength:]
  255. // Trim off common suffix (speedup).
  256. commonlength = commonSuffixLength(text1, text2)
  257. commonsuffix := text1[len(text1)-commonlength:]
  258. text1 = text1[:len(text1)-commonlength]
  259. text2 = text2[:len(text2)-commonlength]
  260. // Compute the diff on the middle block.
  261. diffs := dmp.diffCompute(text1, text2, checklines, deadline)
  262. // Restore the prefix and suffix.
  263. if len(commonprefix) != 0 {
  264. diffs = append([]Diff{Diff{DiffEqual, string(commonprefix)}}, diffs...)
  265. }
  266. if len(commonsuffix) != 0 {
  267. diffs = append(diffs, Diff{DiffEqual, string(commonsuffix)})
  268. }
  269. return dmp.DiffCleanupMerge(diffs)
  270. }
  271. // diffCompute finds the differences between two rune slices. Assumes that the texts do not
  272. // have any common prefix or suffix.
  273. func (dmp *DiffMatchPatch) diffCompute(text1, text2 []rune, checklines bool, deadline time.Time) []Diff {
  274. diffs := []Diff{}
  275. if len(text1) == 0 {
  276. // Just add some text (speedup).
  277. return append(diffs, Diff{DiffInsert, string(text2)})
  278. } else if len(text2) == 0 {
  279. // Just delete some text (speedup).
  280. return append(diffs, Diff{DiffDelete, string(text1)})
  281. }
  282. var longtext, shorttext []rune
  283. if len(text1) > len(text2) {
  284. longtext = text1
  285. shorttext = text2
  286. } else {
  287. longtext = text2
  288. shorttext = text1
  289. }
  290. if i := runesIndex(longtext, shorttext); i != -1 {
  291. op := DiffInsert
  292. // Swap insertions for deletions if diff is reversed.
  293. if len(text1) > len(text2) {
  294. op = DiffDelete
  295. }
  296. // Shorter text is inside the longer text (speedup).
  297. return []Diff{
  298. Diff{op, string(longtext[:i])},
  299. Diff{DiffEqual, string(shorttext)},
  300. Diff{op, string(longtext[i+len(shorttext):])},
  301. }
  302. } else if len(shorttext) == 1 {
  303. // Single character string.
  304. // After the previous speedup, the character can't be an equality.
  305. return []Diff{
  306. Diff{DiffDelete, string(text1)},
  307. Diff{DiffInsert, string(text2)},
  308. }
  309. // Check to see if the problem can be split in two.
  310. } else if hm := dmp.diffHalfMatch(text1, text2); hm != nil {
  311. // A half-match was found, sort out the return data.
  312. text1_a := hm[0]
  313. text1_b := hm[1]
  314. text2_a := hm[2]
  315. text2_b := hm[3]
  316. mid_common := hm[4]
  317. // Send both pairs off for separate processing.
  318. diffs_a := dmp.diffMainRunes(text1_a, text2_a, checklines, deadline)
  319. diffs_b := dmp.diffMainRunes(text1_b, text2_b, checklines, deadline)
  320. // Merge the results.
  321. return append(diffs_a, append([]Diff{Diff{DiffEqual, string(mid_common)}}, diffs_b...)...)
  322. } else if checklines && len(text1) > 100 && len(text2) > 100 {
  323. return dmp.diffLineMode(text1, text2, deadline)
  324. }
  325. return dmp.diffBisect(text1, text2, deadline)
  326. }
  327. // diffLineMode does a quick line-level diff on both []runes, then rediff the parts for
  328. // greater accuracy. This speedup can produce non-minimal diffs.
  329. func (dmp *DiffMatchPatch) diffLineMode(text1, text2 []rune, deadline time.Time) []Diff {
  330. // Scan the text on a line-by-line basis first.
  331. text1, text2, linearray := dmp.diffLinesToRunes(text1, text2)
  332. diffs := dmp.diffMainRunes(text1, text2, false, deadline)
  333. // Convert the diff back to original text.
  334. diffs = dmp.DiffCharsToLines(diffs, linearray)
  335. // Eliminate freak matches (e.g. blank lines)
  336. diffs = dmp.DiffCleanupSemantic(diffs)
  337. // Rediff any replacement blocks, this time character-by-character.
  338. // Add a dummy entry at the end.
  339. diffs = append(diffs, Diff{DiffEqual, ""})
  340. pointer := 0
  341. count_delete := 0
  342. count_insert := 0
  343. text_delete := ""
  344. text_insert := ""
  345. for pointer < len(diffs) {
  346. switch diffs[pointer].Type {
  347. case DiffInsert:
  348. count_insert++
  349. text_insert += diffs[pointer].Text
  350. case DiffDelete:
  351. count_delete++
  352. text_delete += diffs[pointer].Text
  353. case DiffEqual:
  354. // Upon reaching an equality, check for prior redundancies.
  355. if count_delete >= 1 && count_insert >= 1 {
  356. // Delete the offending records and add the merged ones.
  357. diffs = splice(diffs, pointer-count_delete-count_insert,
  358. count_delete+count_insert)
  359. pointer = pointer - count_delete - count_insert
  360. a := dmp.diffMain(text_delete, text_insert, false, deadline)
  361. for j := len(a) - 1; j >= 0; j-- {
  362. diffs = splice(diffs, pointer, 0, a[j])
  363. }
  364. pointer = pointer + len(a)
  365. }
  366. count_insert = 0
  367. count_delete = 0
  368. text_delete = ""
  369. text_insert = ""
  370. }
  371. pointer++
  372. }
  373. return diffs[:len(diffs)-1] // Remove the dummy entry at the end.
  374. }
  375. // DiffBisect finds the 'middle snake' of a diff, split the problem in two
  376. // and return the recursively constructed diff.
  377. // See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.
  378. func (dmp *DiffMatchPatch) DiffBisect(text1, text2 string, deadline time.Time) []Diff {
  379. // Unused in this code, but retained for interface compatibility.
  380. return dmp.diffBisect([]rune(text1), []rune(text2), deadline)
  381. }
  382. // diffBisect finds the 'middle snake' of a diff, splits the problem in two
  383. // and returns the recursively constructed diff.
  384. // See Myers's 1986 paper: An O(ND) Difference Algorithm and Its Variations.
  385. func (dmp *DiffMatchPatch) diffBisect(runes1, runes2 []rune, deadline time.Time) []Diff {
  386. // Cache the text lengths to prevent multiple calls.
  387. runes1_len, runes2_len := len(runes1), len(runes2)
  388. max_d := (runes1_len + runes2_len + 1) / 2
  389. v_offset := max_d
  390. v_length := 2 * max_d
  391. v1 := make([]int, v_length)
  392. v2 := make([]int, v_length)
  393. for i := range v1 {
  394. v1[i] = -1
  395. v2[i] = -1
  396. }
  397. v1[v_offset+1] = 0
  398. v2[v_offset+1] = 0
  399. delta := runes1_len - runes2_len
  400. // If the total number of characters is odd, then the front path will collide
  401. // with the reverse path.
  402. front := (delta%2 != 0)
  403. // Offsets for start and end of k loop.
  404. // Prevents mapping of space beyond the grid.
  405. k1start := 0
  406. k1end := 0
  407. k2start := 0
  408. k2end := 0
  409. for d := 0; d < max_d; d++ {
  410. // Bail out if deadline is reached.
  411. if time.Now().After(deadline) {
  412. break
  413. }
  414. // Walk the front path one step.
  415. for k1 := -d + k1start; k1 <= d-k1end; k1 += 2 {
  416. k1_offset := v_offset + k1
  417. var x1 int
  418. if k1 == -d || (k1 != d && v1[k1_offset-1] < v1[k1_offset+1]) {
  419. x1 = v1[k1_offset+1]
  420. } else {
  421. x1 = v1[k1_offset-1] + 1
  422. }
  423. y1 := x1 - k1
  424. for x1 < runes1_len && y1 < runes2_len {
  425. if runes1[x1] != runes2[y1] {
  426. break
  427. }
  428. x1++
  429. y1++
  430. }
  431. v1[k1_offset] = x1
  432. if x1 > runes1_len {
  433. // Ran off the right of the graph.
  434. k1end += 2
  435. } else if y1 > runes2_len {
  436. // Ran off the bottom of the graph.
  437. k1start += 2
  438. } else if front {
  439. k2_offset := v_offset + delta - k1
  440. if k2_offset >= 0 && k2_offset < v_length && v2[k2_offset] != -1 {
  441. // Mirror x2 onto top-left coordinate system.
  442. x2 := runes1_len - v2[k2_offset]
  443. if x1 >= x2 {
  444. // Overlap detected.
  445. return dmp.diffBisectSplit_(runes1, runes2, x1, y1, deadline)
  446. }
  447. }
  448. }
  449. }
  450. // Walk the reverse path one step.
  451. for k2 := -d + k2start; k2 <= d-k2end; k2 += 2 {
  452. k2_offset := v_offset + k2
  453. var x2 int
  454. if k2 == -d || (k2 != d && v2[k2_offset-1] < v2[k2_offset+1]) {
  455. x2 = v2[k2_offset+1]
  456. } else {
  457. x2 = v2[k2_offset-1] + 1
  458. }
  459. var y2 = x2 - k2
  460. for x2 < runes1_len && y2 < runes2_len {
  461. if runes1[runes1_len-x2-1] != runes2[runes2_len-y2-1] {
  462. break
  463. }
  464. x2++
  465. y2++
  466. }
  467. v2[k2_offset] = x2
  468. if x2 > runes1_len {
  469. // Ran off the left of the graph.
  470. k2end += 2
  471. } else if y2 > runes2_len {
  472. // Ran off the top of the graph.
  473. k2start += 2
  474. } else if !front {
  475. k1_offset := v_offset + delta - k2
  476. if k1_offset >= 0 && k1_offset < v_length && v1[k1_offset] != -1 {
  477. x1 := v1[k1_offset]
  478. y1 := v_offset + x1 - k1_offset
  479. // Mirror x2 onto top-left coordinate system.
  480. x2 = runes1_len - x2
  481. if x1 >= x2 {
  482. // Overlap detected.
  483. return dmp.diffBisectSplit_(runes1, runes2, x1, y1, deadline)
  484. }
  485. }
  486. }
  487. }
  488. }
  489. // Diff took too long and hit the deadline or
  490. // number of diffs equals number of characters, no commonality at all.
  491. return []Diff{
  492. Diff{DiffDelete, string(runes1)},
  493. Diff{DiffInsert, string(runes2)},
  494. }
  495. }
  496. func (dmp *DiffMatchPatch) diffBisectSplit_(runes1, runes2 []rune, x, y int,
  497. deadline time.Time) []Diff {
  498. runes1a := runes1[:x]
  499. runes2a := runes2[:y]
  500. runes1b := runes1[x:]
  501. runes2b := runes2[y:]
  502. // Compute both diffs serially.
  503. diffs := dmp.diffMainRunes(runes1a, runes2a, false, deadline)
  504. diffsb := dmp.diffMainRunes(runes1b, runes2b, false, deadline)
  505. return append(diffs, diffsb...)
  506. }
  507. // DiffLinesToChars split two texts into a list of strings. Reduces the texts to a string of
  508. // hashes where each Unicode character represents one line.
  509. // It's slightly faster to call DiffLinesToRunes first, followed by DiffMainRunes.
  510. func (dmp *DiffMatchPatch) DiffLinesToChars(text1, text2 string) (string, string, []string) {
  511. chars1, chars2, lineArray := dmp.DiffLinesToRunes(text1, text2)
  512. return string(chars1), string(chars2), lineArray
  513. }
  514. // DiffLinesToRunes splits two texts into a list of runes. Each rune represents one line.
  515. func (dmp *DiffMatchPatch) DiffLinesToRunes(text1, text2 string) ([]rune, []rune, []string) {
  516. // '\x00' is a valid character, but various debuggers don't like it.
  517. // So we'll insert a junk entry to avoid generating a null character.
  518. lineArray := []string{""} // e.g. lineArray[4] == 'Hello\n'
  519. lineHash := map[string]int{} // e.g. lineHash['Hello\n'] == 4
  520. chars1 := dmp.diffLinesToRunesMunge(text1, &lineArray, lineHash)
  521. chars2 := dmp.diffLinesToRunesMunge(text2, &lineArray, lineHash)
  522. return chars1, chars2, lineArray
  523. }
  524. func (dmp *DiffMatchPatch) diffLinesToRunes(text1, text2 []rune) ([]rune, []rune, []string) {
  525. return dmp.DiffLinesToRunes(string(text1), string(text2))
  526. }
  527. // diffLinesToRunesMunge splits a text into an array of strings. Reduces the
  528. // texts to a []rune where each Unicode character represents one line.
  529. // We use strings instead of []runes as input mainly because you can't use []rune as a map key.
  530. func (dmp *DiffMatchPatch) diffLinesToRunesMunge(text string, lineArray *[]string, lineHash map[string]int) []rune {
  531. // Walk the text, pulling out a substring for each line.
  532. // text.split('\n') would would temporarily double our memory footprint.
  533. // Modifying text would create many large strings to garbage collect.
  534. lineStart := 0
  535. lineEnd := -1
  536. runes := []rune{}
  537. for lineEnd < len(text)-1 {
  538. lineEnd = indexOf(text, "\n", lineStart)
  539. if lineEnd == -1 {
  540. lineEnd = len(text) - 1
  541. }
  542. line := text[lineStart : lineEnd+1]
  543. lineStart = lineEnd + 1
  544. lineValue_, ok := lineHash[line]
  545. if ok {
  546. runes = append(runes, rune(lineValue_))
  547. } else {
  548. *lineArray = append(*lineArray, line)
  549. lineHash[line] = len(*lineArray) - 1
  550. runes = append(runes, rune(len(*lineArray)-1))
  551. }
  552. }
  553. return runes
  554. }
  555. // DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of
  556. // text.
  557. func (dmp *DiffMatchPatch) DiffCharsToLines(diffs []Diff, lineArray []string) []Diff {
  558. hydrated := make([]Diff, 0, len(diffs))
  559. for _, aDiff := range diffs {
  560. chars := aDiff.Text
  561. text := make([]string, len(chars))
  562. for i, r := range chars {
  563. text[i] = lineArray[r]
  564. }
  565. aDiff.Text = strings.Join(text, "")
  566. hydrated = append(hydrated, aDiff)
  567. }
  568. return hydrated
  569. }
  570. // DiffCommonPrefix determines the common prefix length of two strings.
  571. func (dmp *DiffMatchPatch) DiffCommonPrefix(text1, text2 string) int {
  572. // Unused in this code, but retained for interface compatibility.
  573. return commonPrefixLength([]rune(text1), []rune(text2))
  574. }
  575. // DiffCommonSuffix determines the common suffix length of two strings.
  576. func (dmp *DiffMatchPatch) DiffCommonSuffix(text1, text2 string) int {
  577. // Unused in this code, but retained for interface compatibility.
  578. return commonSuffixLength([]rune(text1), []rune(text2))
  579. }
  580. // commonPrefixLength returns the length of the common prefix of two rune slices.
  581. func commonPrefixLength(text1, text2 []rune) int {
  582. short, long := text1, text2
  583. if len(short) > len(long) {
  584. short, long = long, short
  585. }
  586. for i, r := range short {
  587. if r != long[i] {
  588. return i
  589. }
  590. }
  591. return len(short)
  592. }
  593. // commonSuffixLength returns the length of the common suffix of two rune slices.
  594. func commonSuffixLength(text1, text2 []rune) int {
  595. n := min(len(text1), len(text2))
  596. for i := 0; i < n; i++ {
  597. if text1[len(text1)-i-1] != text2[len(text2)-i-1] {
  598. return i
  599. }
  600. }
  601. return n
  602. // Binary search.
  603. // Performance analysis: http://neil.fraser.name/news/2007/10/09/
  604. /*
  605. pointermin := 0
  606. pointermax := math.Min(len(text1), len(text2))
  607. pointermid := pointermax
  608. pointerend := 0
  609. for pointermin < pointermid {
  610. if text1[len(text1)-pointermid:len(text1)-pointerend] ==
  611. text2[len(text2)-pointermid:len(text2)-pointerend] {
  612. pointermin = pointermid
  613. pointerend = pointermin
  614. } else {
  615. pointermax = pointermid
  616. }
  617. pointermid = math.Floor((pointermax-pointermin)/2 + pointermin)
  618. }
  619. return pointermid
  620. */
  621. }
  622. // DiffCommonOverlap determines if the suffix of one string is the prefix of another.
  623. func (dmp *DiffMatchPatch) DiffCommonOverlap(text1 string, text2 string) int {
  624. // Cache the text lengths to prevent multiple calls.
  625. text1_length := len(text1)
  626. text2_length := len(text2)
  627. // Eliminate the null case.
  628. if text1_length == 0 || text2_length == 0 {
  629. return 0
  630. }
  631. // Truncate the longer string.
  632. if text1_length > text2_length {
  633. text1 = text1[text1_length-text2_length:]
  634. } else if text1_length < text2_length {
  635. text2 = text2[0:text1_length]
  636. }
  637. text_length := int(math.Min(float64(text1_length), float64(text2_length)))
  638. // Quick check for the worst case.
  639. if text1 == text2 {
  640. return text_length
  641. }
  642. // Start by looking for a single character match
  643. // and increase length until no match is found.
  644. // Performance analysis: http://neil.fraser.name/news/2010/11/04/
  645. best := 0
  646. length := 1
  647. for {
  648. pattern := text1[text_length-length:]
  649. found := strings.Index(text2, pattern)
  650. if found == -1 {
  651. return best
  652. }
  653. length += found
  654. if found == 0 || text1[text_length-length:] == text2[0:length] {
  655. best = length
  656. length++
  657. }
  658. }
  659. return 0
  660. }
  661. // DiffHalfMatch checks whether the two texts share a substring which is at
  662. // least half the length of the longer text. This speedup can produce non-minimal diffs.
  663. func (dmp *DiffMatchPatch) DiffHalfMatch(text1, text2 string) []string {
  664. // Unused in this code, but retained for interface compatibility.
  665. runeSlices := dmp.diffHalfMatch([]rune(text1), []rune(text2))
  666. if runeSlices == nil {
  667. return nil
  668. }
  669. result := make([]string, len(runeSlices))
  670. for i, r := range runeSlices {
  671. result[i] = string(r)
  672. }
  673. return result
  674. }
  675. func (dmp *DiffMatchPatch) diffHalfMatch(text1, text2 []rune) [][]rune {
  676. if dmp.DiffTimeout <= 0 {
  677. // Don't risk returning a non-optimal diff if we have unlimited time.
  678. return nil
  679. }
  680. var longtext, shorttext []rune
  681. if len(text1) > len(text2) {
  682. longtext = text1
  683. shorttext = text2
  684. } else {
  685. longtext = text2
  686. shorttext = text1
  687. }
  688. if len(longtext) < 4 || len(shorttext)*2 < len(longtext) {
  689. return nil // Pointless.
  690. }
  691. // First check if the second quarter is the seed for a half-match.
  692. hm1 := dmp.diffHalfMatchI(longtext, shorttext, int(float64(len(longtext)+3)/4))
  693. // Check again based on the third quarter.
  694. hm2 := dmp.diffHalfMatchI(longtext, shorttext, int(float64(len(longtext)+1)/2))
  695. hm := [][]rune{}
  696. if hm1 == nil && hm2 == nil {
  697. return nil
  698. } else if hm2 == nil {
  699. hm = hm1
  700. } else if hm1 == nil {
  701. hm = hm2
  702. } else {
  703. // Both matched. Select the longest.
  704. if len(hm1[4]) > len(hm2[4]) {
  705. hm = hm1
  706. } else {
  707. hm = hm2
  708. }
  709. }
  710. // A half-match was found, sort out the return data.
  711. if len(text1) > len(text2) {
  712. return hm
  713. } else {
  714. return [][]rune{hm[2], hm[3], hm[0], hm[1], hm[4]}
  715. }
  716. return nil
  717. }
  718. /**
  719. * Does a substring of shorttext exist within longtext such that the substring
  720. * is at least half the length of longtext?
  721. * @param {string} longtext Longer string.
  722. * @param {string} shorttext Shorter string.
  723. * @param {number} i Start index of quarter length substring within longtext.
  724. * @return {Array.<string>} Five element Array, containing the prefix of
  725. * longtext, the suffix of longtext, the prefix of shorttext, the suffix
  726. * of shorttext and the common middle. Or null if there was no match.
  727. * @private
  728. */
  729. func (dmp *DiffMatchPatch) diffHalfMatchI(l, s []rune, i int) [][]rune {
  730. // Start with a 1/4 length substring at position i as a seed.
  731. seed := l[i : i+len(l)/4]
  732. j := -1
  733. best_common := []rune{}
  734. best_longtext_a := []rune{}
  735. best_longtext_b := []rune{}
  736. best_shorttext_a := []rune{}
  737. best_shorttext_b := []rune{}
  738. if j < len(s) {
  739. j = runesIndexOf(s, seed, j+1)
  740. for {
  741. if j == -1 {
  742. break
  743. }
  744. prefixLength := commonPrefixLength(l[i:], s[j:])
  745. suffixLength := commonSuffixLength(l[:i], s[:j])
  746. if len(best_common) < suffixLength+prefixLength {
  747. best_common = concat(s[j-suffixLength:j], s[j:j+prefixLength])
  748. best_longtext_a = l[:i-suffixLength]
  749. best_longtext_b = l[i+prefixLength:]
  750. best_shorttext_a = s[:j-suffixLength]
  751. best_shorttext_b = s[j+prefixLength:]
  752. }
  753. j = runesIndexOf(s, seed, j+1)
  754. }
  755. }
  756. if len(best_common)*2 >= len(l) {
  757. return [][]rune{
  758. best_longtext_a,
  759. best_longtext_b,
  760. best_shorttext_a,
  761. best_shorttext_b,
  762. best_common,
  763. }
  764. }
  765. return nil
  766. }
  767. func concat(r1, r2 []rune) []rune {
  768. result := make([]rune, len(r1)+len(r2))
  769. copy(result, r1)
  770. copy(result[len(r1):], r2)
  771. return result
  772. }
  773. // Diff_cleanupSemantic reduces the number of edits by eliminating
  774. // semantically trivial equalities.
  775. func (dmp *DiffMatchPatch) DiffCleanupSemantic(diffs []Diff) []Diff {
  776. changes := false
  777. equalities := new(Stack) // Stack of indices where equalities are found.
  778. var lastequality string
  779. // Always equal to diffs[equalities[equalitiesLength - 1]][1]
  780. var pointer int // Index of current position.
  781. // Number of characters that changed prior to the equality.
  782. var length_insertions1, length_deletions1 int
  783. // Number of characters that changed after the equality.
  784. var length_insertions2, length_deletions2 int
  785. for pointer < len(diffs) {
  786. if diffs[pointer].Type == DiffEqual { // Equality found.
  787. equalities.Push(pointer)
  788. length_insertions1 = length_insertions2
  789. length_deletions1 = length_deletions2
  790. length_insertions2 = 0
  791. length_deletions2 = 0
  792. lastequality = diffs[pointer].Text
  793. } else { // An insertion or deletion.
  794. if diffs[pointer].Type == DiffInsert {
  795. length_insertions2 += len(diffs[pointer].Text)
  796. } else {
  797. length_deletions2 += len(diffs[pointer].Text)
  798. }
  799. // Eliminate an equality that is smaller or equal to the edits on both
  800. // sides of it.
  801. _difference1 := int(math.Max(float64(length_insertions1), float64(length_deletions1)))
  802. _difference2 := int(math.Max(float64(length_insertions2), float64(length_deletions2)))
  803. if len(lastequality) > 0 &&
  804. (len(lastequality) <= _difference1) &&
  805. (len(lastequality) <= _difference2) {
  806. // Duplicate record.
  807. insPoint := equalities.Peek().(int)
  808. diffs = append(
  809. diffs[:insPoint],
  810. append([]Diff{Diff{DiffDelete, lastequality}}, diffs[insPoint:]...)...)
  811. // Change second copy to insert.
  812. diffs[insPoint+1].Type = DiffInsert
  813. // Throw away the equality we just deleted.
  814. equalities.Pop()
  815. if equalities.Len() > 0 {
  816. equalities.Pop()
  817. pointer = equalities.Peek().(int)
  818. } else {
  819. pointer = -1
  820. }
  821. length_insertions1 = 0 // Reset the counters.
  822. length_deletions1 = 0
  823. length_insertions2 = 0
  824. length_deletions2 = 0
  825. lastequality = ""
  826. changes = true
  827. }
  828. }
  829. pointer++
  830. }
  831. // Normalize the diff.
  832. if changes {
  833. diffs = dmp.DiffCleanupMerge(diffs)
  834. }
  835. diffs = dmp.DiffCleanupSemanticLossless(diffs)
  836. // Find any overlaps between deletions and insertions.
  837. // e.g: <del>abcxxx</del><ins>xxxdef</ins>
  838. // -> <del>abc</del>xxx<ins>def</ins>
  839. // e.g: <del>xxxabc</del><ins>defxxx</ins>
  840. // -> <ins>def</ins>xxx<del>abc</del>
  841. // Only extract an overlap if it is as big as the edit ahead or behind it.
  842. pointer = 1
  843. for pointer < len(diffs) {
  844. if diffs[pointer-1].Type == DiffDelete &&
  845. diffs[pointer].Type == DiffInsert {
  846. deletion := diffs[pointer-1].Text
  847. insertion := diffs[pointer].Text
  848. overlap_length1 := dmp.DiffCommonOverlap(deletion, insertion)
  849. overlap_length2 := dmp.DiffCommonOverlap(insertion, deletion)
  850. if overlap_length1 >= overlap_length2 {
  851. if float64(overlap_length1) >= float64(len(deletion))/2 ||
  852. float64(overlap_length1) >= float64(len(insertion))/2 {
  853. // Overlap found. Insert an equality and trim the surrounding edits.
  854. diffs = append(
  855. diffs[:pointer],
  856. append([]Diff{Diff{DiffEqual, insertion[:overlap_length1]}}, diffs[pointer:]...)...)
  857. //diffs.splice(pointer, 0,
  858. // [DiffEqual, insertion[0 : overlap_length1)]]
  859. diffs[pointer-1].Text =
  860. deletion[0 : len(deletion)-overlap_length1]
  861. diffs[pointer+1].Text = insertion[overlap_length1:]
  862. pointer++
  863. }
  864. } else {
  865. if float64(overlap_length2) >= float64(len(deletion))/2 ||
  866. float64(overlap_length2) >= float64(len(insertion))/2 {
  867. // Reverse overlap found.
  868. // Insert an equality and swap and trim the surrounding edits.
  869. overlap := Diff{DiffEqual, insertion[overlap_length2:]}
  870. diffs = append(
  871. diffs[:pointer],
  872. append([]Diff{overlap}, diffs[pointer:]...)...)
  873. // diffs.splice(pointer, 0,
  874. // [DiffEqual, deletion[0 : overlap_length2)]]
  875. diffs[pointer-1].Type = DiffInsert
  876. diffs[pointer-1].Text = insertion[0 : len(insertion)-overlap_length2]
  877. diffs[pointer+1].Type = DiffDelete
  878. diffs[pointer+1].Text = deletion[overlap_length2:]
  879. pointer++
  880. }
  881. }
  882. pointer++
  883. }
  884. pointer++
  885. }
  886. return diffs
  887. }
  888. // Diff_cleanupSemanticLossless looks for single edits surrounded on both sides by equalities
  889. // which can be shifted sideways to align the edit to a word boundary.
  890. // e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
  891. func (dmp *DiffMatchPatch) DiffCleanupSemanticLossless(diffs []Diff) []Diff {
  892. /**
  893. * Given two strings, compute a score representing whether the internal
  894. * boundary falls on logical boundaries.
  895. * Scores range from 6 (best) to 0 (worst).
  896. * Closure, but does not reference any external variables.
  897. * @param {string} one First string.
  898. * @param {string} two Second string.
  899. * @return {number} The score.
  900. * @private
  901. */
  902. diffCleanupSemanticScore_ := func(one, two string) int {
  903. if len(one) == 0 || len(two) == 0 {
  904. // Edges are the best.
  905. return 6
  906. }
  907. // Each port of this function behaves slightly differently due to
  908. // subtle differences in each language's definition of things like
  909. // 'whitespace'. Since this function's purpose is largely cosmetic,
  910. // the choice has been made to use each language's native features
  911. // rather than force total conformity.
  912. rune1, _ := utf8.DecodeLastRuneInString(one)
  913. rune2, _ := utf8.DecodeRuneInString(two)
  914. char1 := string(rune1)
  915. char2 := string(rune2)
  916. nonAlphaNumeric1 := nonAlphaNumericRegex_.MatchString(char1)
  917. nonAlphaNumeric2 := nonAlphaNumericRegex_.MatchString(char2)
  918. whitespace1 := nonAlphaNumeric1 && whitespaceRegex_.MatchString(char1)
  919. whitespace2 := nonAlphaNumeric2 && whitespaceRegex_.MatchString(char2)
  920. lineBreak1 := whitespace1 && linebreakRegex_.MatchString(char1)
  921. lineBreak2 := whitespace2 && linebreakRegex_.MatchString(char2)
  922. blankLine1 := lineBreak1 && blanklineEndRegex_.MatchString(one)
  923. blankLine2 := lineBreak2 && blanklineEndRegex_.MatchString(two)
  924. if blankLine1 || blankLine2 {
  925. // Five points for blank lines.
  926. return 5
  927. } else if lineBreak1 || lineBreak2 {
  928. // Four points for line breaks.
  929. return 4
  930. } else if nonAlphaNumeric1 && !whitespace1 && whitespace2 {
  931. // Three points for end of sentences.
  932. return 3
  933. } else if whitespace1 || whitespace2 {
  934. // Two points for whitespace.
  935. return 2
  936. } else if nonAlphaNumeric1 || nonAlphaNumeric2 {
  937. // One point for non-alphanumeric.
  938. return 1
  939. }
  940. return 0
  941. }
  942. pointer := 1
  943. // Intentionally ignore the first and last element (don't need checking).
  944. for pointer < len(diffs)-1 {
  945. if diffs[pointer-1].Type == DiffEqual &&
  946. diffs[pointer+1].Type == DiffEqual {
  947. // This is a single edit surrounded by equalities.
  948. equality1 := diffs[pointer-1].Text
  949. edit := diffs[pointer].Text
  950. equality2 := diffs[pointer+1].Text
  951. // First, shift the edit as far left as possible.
  952. commonOffset := dmp.DiffCommonSuffix(equality1, edit)
  953. if commonOffset > 0 {
  954. commonString := edit[len(edit)-commonOffset:]
  955. equality1 = equality1[0 : len(equality1)-commonOffset]
  956. edit = commonString + edit[:len(edit)-commonOffset]
  957. equality2 = commonString + equality2
  958. }
  959. // Second, step character by character right, looking for the best fit.
  960. bestEquality1 := equality1
  961. bestEdit := edit
  962. bestEquality2 := equality2
  963. bestScore := diffCleanupSemanticScore_(equality1, edit) +
  964. diffCleanupSemanticScore_(edit, equality2)
  965. for len(edit) != 0 && len(equality2) != 0 {
  966. _, sz := utf8.DecodeRuneInString(edit)
  967. if len(equality2) < sz || edit[:sz] != equality2[:sz] {
  968. break
  969. }
  970. equality1 += edit[:sz]
  971. edit = edit[sz:] + equality2[:sz]
  972. equality2 = equality2[sz:]
  973. score := diffCleanupSemanticScore_(equality1, edit) +
  974. diffCleanupSemanticScore_(edit, equality2)
  975. // The >= encourages trailing rather than leading whitespace on
  976. // edits.
  977. if score >= bestScore {
  978. bestScore = score
  979. bestEquality1 = equality1
  980. bestEdit = edit
  981. bestEquality2 = equality2
  982. }
  983. }
  984. if diffs[pointer-1].Text != bestEquality1 {
  985. // We have an improvement, save it back to the diff.
  986. if len(bestEquality1) != 0 {
  987. diffs[pointer-1].Text = bestEquality1
  988. } else {
  989. diffs = splice(diffs, pointer-1, 1)
  990. pointer--
  991. }
  992. diffs[pointer].Text = bestEdit
  993. if len(bestEquality2) != 0 {
  994. diffs[pointer+1].Text = bestEquality2
  995. } else {
  996. //splice(diffs, pointer+1, 1)
  997. diffs = append(diffs[:pointer+1], diffs[pointer+2:]...)
  998. pointer--
  999. }
  1000. }
  1001. }
  1002. pointer++
  1003. }
  1004. return diffs
  1005. }
  1006. // Diff_cleanupEfficiency reduces the number of edits by eliminating
  1007. // operationally trivial equalities.
  1008. func (dmp *DiffMatchPatch) DiffCleanupEfficiency(diffs []Diff) []Diff {
  1009. changes := false
  1010. // Stack of indices where equalities are found.
  1011. equalities := new(Stack)
  1012. // Always equal to equalities[equalitiesLength-1][1]
  1013. lastequality := ""
  1014. pointer := 0 // Index of current position.
  1015. // Is there an insertion operation before the last equality.
  1016. pre_ins := false
  1017. // Is there a deletion operation before the last equality.
  1018. pre_del := false
  1019. // Is there an insertion operation after the last equality.
  1020. post_ins := false
  1021. // Is there a deletion operation after the last equality.
  1022. post_del := false
  1023. for pointer < len(diffs) {
  1024. if diffs[pointer].Type == DiffEqual { // Equality found.
  1025. if len(diffs[pointer].Text) < dmp.DiffEditCost &&
  1026. (post_ins || post_del) {
  1027. // Candidate found.
  1028. equalities.Push(pointer)
  1029. pre_ins = post_ins
  1030. pre_del = post_del
  1031. lastequality = diffs[pointer].Text
  1032. } else {
  1033. // Not a candidate, and can never become one.
  1034. equalities.Clear()
  1035. lastequality = ""
  1036. }
  1037. post_ins = false
  1038. post_del = false
  1039. } else { // An insertion or deletion.
  1040. if diffs[pointer].Type == DiffDelete {
  1041. post_del = true
  1042. } else {
  1043. post_ins = true
  1044. }
  1045. /*
  1046. * Five types to be split:
  1047. * <ins>A</ins><del>B</del>XY<ins>C</ins><del>D</del>
  1048. * <ins>A</ins>X<ins>C</ins><del>D</del>
  1049. * <ins>A</ins><del>B</del>X<ins>C</ins>
  1050. * <ins>A</del>X<ins>C</ins><del>D</del>
  1051. * <ins>A</ins><del>B</del>X<del>C</del>
  1052. */
  1053. var sum_pres int
  1054. if pre_ins {
  1055. sum_pres++
  1056. }
  1057. if pre_del {
  1058. sum_pres++
  1059. }
  1060. if post_ins {
  1061. sum_pres++
  1062. }
  1063. if post_del {
  1064. sum_pres++
  1065. }
  1066. if len(lastequality) > 0 &&
  1067. ((pre_ins && pre_del && post_ins && post_del) ||
  1068. ((len(lastequality) < dmp.DiffEditCost/2) && sum_pres == 3)) {
  1069. // Duplicate record.
  1070. diffs = append(diffs[:equalities.Peek().(int)],
  1071. append([]Diff{Diff{DiffDelete, lastequality}}, diffs[equalities.Peek().(int):]...)...)
  1072. // Change second copy to insert.
  1073. diffs[equalities.Peek().(int)+1].Type = DiffInsert
  1074. equalities.Pop() // Throw away the equality we just deleted.
  1075. lastequality = ""
  1076. if pre_ins && pre_del {
  1077. // No changes made which could affect previous entry, keep going.
  1078. post_ins = true
  1079. post_del = true
  1080. equalities.Clear()
  1081. } else {
  1082. if equalities.Len() > 0 {
  1083. equalities.Pop()
  1084. pointer = equalities.Peek().(int)
  1085. } else {
  1086. pointer = -1
  1087. }
  1088. post_ins = false
  1089. post_del = false
  1090. }
  1091. changes = true
  1092. }
  1093. }
  1094. pointer++
  1095. }
  1096. if changes {
  1097. diffs = dmp.DiffCleanupMerge(diffs)
  1098. }
  1099. return diffs
  1100. }
  1101. // Diff_cleanupMerge reorders and merges like edit sections. Merge equalities.
  1102. // Any edit section can move as long as it doesn't cross an equality.
  1103. func (dmp *DiffMatchPatch) DiffCleanupMerge(diffs []Diff) []Diff {
  1104. // Add a dummy entry at the end.
  1105. diffs = append(diffs, Diff{DiffEqual, ""})
  1106. pointer := 0
  1107. count_delete := 0
  1108. count_insert := 0
  1109. commonlength := 0
  1110. text_delete := ""
  1111. text_insert := ""
  1112. for pointer < len(diffs) {
  1113. switch diffs[pointer].Type {
  1114. case DiffInsert:
  1115. count_insert += 1
  1116. text_insert += diffs[pointer].Text
  1117. pointer += 1
  1118. break
  1119. case DiffDelete:
  1120. count_delete += 1
  1121. text_delete += diffs[pointer].Text
  1122. pointer += 1
  1123. break
  1124. case DiffEqual:
  1125. // Upon reaching an equality, check for prior redundancies.
  1126. if count_delete+count_insert > 1 {
  1127. if count_delete != 0 && count_insert != 0 {
  1128. // Factor out any common prefixies.
  1129. commonlength = dmp.DiffCommonPrefix(text_insert, text_delete)
  1130. if commonlength != 0 {
  1131. x := pointer - count_delete - count_insert
  1132. if x > 0 && diffs[x-1].Type == DiffEqual {
  1133. diffs[x-1].Text += text_insert[:commonlength]
  1134. } else {
  1135. diffs = append([]Diff{Diff{DiffEqual, text_insert[:commonlength]}}, diffs...)
  1136. pointer += 1
  1137. }
  1138. text_insert = text_insert[commonlength:]
  1139. text_delete = text_delete[commonlength:]
  1140. }
  1141. // Factor out any common suffixies.
  1142. commonlength = dmp.DiffCommonSuffix(text_insert, text_delete)
  1143. if commonlength != 0 {
  1144. insert_index := len(text_insert) - commonlength
  1145. delete_index := len(text_delete) - commonlength
  1146. diffs[pointer].Text = text_insert[insert_index:] + diffs[pointer].Text
  1147. text_insert = text_insert[:insert_index]
  1148. text_delete = text_delete[:delete_index]
  1149. }
  1150. }
  1151. // Delete the offending records and add the merged ones.
  1152. if count_delete == 0 {
  1153. diffs = splice(diffs, pointer-count_insert,
  1154. count_delete+count_insert,
  1155. Diff{DiffInsert, text_insert})
  1156. } else if count_insert == 0 {
  1157. diffs = splice(diffs, pointer-count_delete,
  1158. count_delete+count_insert,
  1159. Diff{DiffDelete, text_delete})
  1160. } else {
  1161. diffs = splice(diffs, pointer-count_delete-count_insert,
  1162. count_delete+count_insert,
  1163. Diff{DiffDelete, text_delete},
  1164. Diff{DiffInsert, text_insert})
  1165. }
  1166. pointer = pointer - count_delete - count_insert + 1
  1167. if count_delete != 0 {
  1168. pointer += 1
  1169. }
  1170. if count_insert != 0 {
  1171. pointer += 1
  1172. }
  1173. } else if pointer != 0 && diffs[pointer-1].Type == DiffEqual {
  1174. // Merge this equality with the previous one.
  1175. diffs[pointer-1].Text += diffs[pointer].Text
  1176. diffs = append(diffs[:pointer], diffs[pointer+1:]...)
  1177. } else {
  1178. pointer++
  1179. }
  1180. count_insert = 0
  1181. count_delete = 0
  1182. text_delete = ""
  1183. text_insert = ""
  1184. break
  1185. }
  1186. }
  1187. if len(diffs[len(diffs)-1].Text) == 0 {
  1188. diffs = diffs[0 : len(diffs)-1] // Remove the dummy entry at the end.
  1189. }
  1190. // Second pass: look for single edits surrounded on both sides by
  1191. // equalities which can be shifted sideways to eliminate an equality.
  1192. // e.g: A<ins>BA</ins>C -> <ins>AB</ins>AC
  1193. changes := false
  1194. pointer = 1
  1195. // Intentionally ignore the first and last element (don't need checking).
  1196. for pointer < (len(diffs) - 1) {
  1197. if diffs[pointer-1].Type == DiffEqual &&
  1198. diffs[pointer+1].Type == DiffEqual {
  1199. // This is a single edit surrounded by equalities.
  1200. if strings.HasSuffix(diffs[pointer].Text, diffs[pointer-1].Text) {
  1201. // Shift the edit over the previous equality.
  1202. diffs[pointer].Text = diffs[pointer-1].Text +
  1203. diffs[pointer].Text[:len(diffs[pointer].Text)-len(diffs[pointer-1].Text)]
  1204. diffs[pointer+1].Text = diffs[pointer-1].Text + diffs[pointer+1].Text
  1205. diffs = splice(diffs, pointer-1, 1)
  1206. changes = true
  1207. } else if strings.HasPrefix(diffs[pointer].Text, diffs[pointer+1].Text) {
  1208. // Shift the edit over the next equality.
  1209. diffs[pointer-1].Text += diffs[pointer+1].Text
  1210. diffs[pointer].Text =
  1211. diffs[pointer].Text[len(diffs[pointer+1].Text):] + diffs[pointer+1].Text
  1212. diffs = splice(diffs, pointer+1, 1)
  1213. changes = true
  1214. }
  1215. }
  1216. pointer++
  1217. }
  1218. // If shifts were made, the diff needs reordering and another shift sweep.
  1219. if changes {
  1220. diffs = dmp.DiffCleanupMerge(diffs)
  1221. }
  1222. return diffs
  1223. }
  1224. // Diff_xIndex. loc is a location in text1, comAdde and return the equivalent location in
  1225. // text2.
  1226. // e.g. "The cat" vs "The big cat", 1->1, 5->8
  1227. func (dmp *DiffMatchPatch) DiffXIndex(diffs []Diff, loc int) int {
  1228. chars1 := 0
  1229. chars2 := 0
  1230. last_chars1 := 0
  1231. last_chars2 := 0
  1232. lastDiff := Diff{}
  1233. for i := 0; i < len(diffs); i++ {
  1234. aDiff := diffs[i]
  1235. if aDiff.Type != DiffInsert {
  1236. // Equality or deletion.
  1237. chars1 += len(aDiff.Text)
  1238. }
  1239. if aDiff.Type != DiffDelete {
  1240. // Equality or insertion.
  1241. chars2 += len(aDiff.Text)
  1242. }
  1243. if chars1 > loc {
  1244. // Overshot the location.
  1245. lastDiff = aDiff
  1246. break
  1247. }
  1248. last_chars1 = chars1
  1249. last_chars2 = chars2
  1250. }
  1251. if lastDiff.Type == DiffDelete {
  1252. // The location was deleted.
  1253. return last_chars2
  1254. }
  1255. // Add the remaining character length.
  1256. return last_chars2 + (loc - last_chars1)
  1257. }
  1258. // DiffPrettyHtml converts a []Diff into a pretty HTML report.
  1259. // It is intended as an example from which to write one's own
  1260. // display functions.
  1261. func (dmp *DiffMatchPatch) DiffPrettyHtml(diffs []Diff) string {
  1262. var buff bytes.Buffer
  1263. for _, diff := range diffs {
  1264. text := strings.Replace(html.EscapeString(diff.Text), "\n", "&para;<br>", -1)
  1265. switch diff.Type {
  1266. case DiffInsert:
  1267. buff.WriteString("<ins style=\"background:#e6ffe6;\">")
  1268. buff.WriteString(text)
  1269. buff.WriteString("</ins>")
  1270. case DiffDelete:
  1271. buff.WriteString("<del style=\"background:#ffe6e6;\">")
  1272. buff.WriteString(text)
  1273. buff.WriteString("</del>")
  1274. case DiffEqual:
  1275. buff.WriteString("<span>")
  1276. buff.WriteString(text)
  1277. buff.WriteString("</span>")
  1278. }
  1279. }
  1280. return buff.String()
  1281. }
  1282. // Diff_text1 computes and returns the source text (all equalities and deletions).
  1283. func (dmp *DiffMatchPatch) DiffText1(diffs []Diff) string {
  1284. //StringBuilder text = new StringBuilder()
  1285. var text bytes.Buffer
  1286. for _, aDiff := range diffs {
  1287. if aDiff.Type != DiffInsert {
  1288. text.WriteString(aDiff.Text)
  1289. }
  1290. }
  1291. return text.String()
  1292. }
  1293. // Diff_text2 computes and returns the destination text (all equalities and insertions).
  1294. func (dmp *DiffMatchPatch) DiffText2(diffs []Diff) string {
  1295. var text bytes.Buffer
  1296. for _, aDiff := range diffs {
  1297. if aDiff.Type != DiffDelete {
  1298. text.WriteString(aDiff.Text)
  1299. }
  1300. }
  1301. return text.String()
  1302. }
  1303. // Diff_levenshtein computes the Levenshtein distance; the number of inserted, deleted or
  1304. // substituted characters.
  1305. func (dmp *DiffMatchPatch) DiffLevenshtein(diffs []Diff) int {
  1306. levenshtein := 0
  1307. insertions := 0
  1308. deletions := 0
  1309. for _, aDiff := range diffs {
  1310. switch aDiff.Type {
  1311. case DiffInsert:
  1312. insertions += len(aDiff.Text)
  1313. case DiffDelete:
  1314. deletions += len(aDiff.Text)
  1315. case DiffEqual:
  1316. // A deletion and an insertion is one substitution.
  1317. levenshtein += max(insertions, deletions)
  1318. insertions = 0
  1319. deletions = 0
  1320. }
  1321. }
  1322. levenshtein += max(insertions, deletions)
  1323. return levenshtein
  1324. }
  1325. // Diff_toDelta crushes the diff into an encoded string which describes the operations
  1326. // required to transform text1 into text2.
  1327. // E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'.
  1328. // Operations are tab-separated. Inserted text is escaped using %xx
  1329. // notation.
  1330. func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string {
  1331. var text bytes.Buffer
  1332. for _, aDiff := range diffs {
  1333. switch aDiff.Type {
  1334. case DiffInsert:
  1335. text.WriteString("+")
  1336. text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1))
  1337. text.WriteString("\t")
  1338. break
  1339. case DiffDelete:
  1340. text.WriteString("-")
  1341. text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text)))
  1342. text.WriteString("\t")
  1343. break
  1344. case DiffEqual:
  1345. text.WriteString("=")
  1346. text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text)))
  1347. text.WriteString("\t")
  1348. break
  1349. }
  1350. }
  1351. delta := text.String()
  1352. if len(delta) != 0 {
  1353. // Strip off trailing tab character.
  1354. delta = delta[0 : utf8.RuneCountInString(delta)-1]
  1355. delta = unescaper.Replace(delta)
  1356. }
  1357. return delta
  1358. }
  1359. // Diff_fromDelta. Given the original text1, and an encoded string which describes the
  1360. // operations required to transform text1 into text2, comAdde the full diff.
  1361. func (dmp *DiffMatchPatch) DiffFromDelta(text1, delta string) (diffs []Diff, err error) {
  1362. diffs = []Diff{}
  1363. defer func() {
  1364. if r := recover(); r != nil {
  1365. err = r.(error)
  1366. }
  1367. }()
  1368. pointer := 0 // Cursor in text1
  1369. tokens := strings.Split(delta, "\t")
  1370. for _, token := range tokens {
  1371. if len(token) == 0 {
  1372. // Blank tokens are ok (from a trailing \t).
  1373. continue
  1374. }
  1375. // Each token begins with a one character parameter which specifies the
  1376. // operation of this token (delete, insert, equality).
  1377. param := token[1:]
  1378. switch op := token[0]; op {
  1379. case '+':
  1380. // decode would Diff all "+" to " "
  1381. param = strings.Replace(param, "+", "%2b", -1)
  1382. param, err = url.QueryUnescape(param)
  1383. if err != nil {
  1384. return nil, err
  1385. }
  1386. if !utf8.ValidString(param) {
  1387. return nil, fmt.Errorf("invalid UTF-8 token: %q", param)
  1388. }
  1389. diffs = append(diffs, Diff{DiffInsert, param})
  1390. case '=', '-':
  1391. n, err := strconv.ParseInt(param, 10, 0)
  1392. if err != nil {
  1393. return diffs, err
  1394. } else if n < 0 {
  1395. return diffs, errors.New("Negative number in DiffFromDelta: " + param)
  1396. }
  1397. // remember that string slicing is by byte - we want by rune here.
  1398. text := string([]rune(text1)[pointer : pointer+int(n)])
  1399. pointer += int(n)
  1400. if op == '=' {
  1401. diffs = append(diffs, Diff{DiffEqual, text})
  1402. } else {
  1403. diffs = append(diffs, Diff{DiffDelete, text})
  1404. }
  1405. default:
  1406. // Anything else is an error.
  1407. return diffs, errors.New("Invalid diff operation in DiffFromDelta: " + string(token[0]))
  1408. }
  1409. }
  1410. if pointer != len([]rune(text1)) {
  1411. return diffs, fmt.Errorf("Delta length (%v) smaller than source text length (%v)", pointer, len(text1))
  1412. }
  1413. return diffs, err
  1414. }
  1415. // MATCH FUNCTIONS
  1416. // MatchMain locates the best instance of 'pattern' in 'text' near 'loc'.
  1417. // Returns -1 if no match found.
  1418. func (dmp *DiffMatchPatch) MatchMain(text, pattern string, loc int) int {
  1419. // Check for null inputs not needed since null can't be passed in C#.
  1420. loc = int(math.Max(0, math.Min(float64(loc), float64(len(text)))))
  1421. if text == pattern {
  1422. // Shortcut (potentially not guaranteed by the algorithm)
  1423. return 0
  1424. } else if len(text) == 0 {
  1425. // Nothing to match.
  1426. return -1
  1427. } else if loc+len(pattern) <= len(text) && text[loc:loc+len(pattern)] == pattern {
  1428. // Perfect match at the perfect spot! (Includes case of null pattern)
  1429. return loc
  1430. }
  1431. // Do a fuzzy compare.
  1432. return dmp.MatchBitap(text, pattern, loc)
  1433. }
  1434. // MatchBitap locates the best instance of 'pattern' in 'text' near 'loc' using the
  1435. // Bitap algorithm. Returns -1 if no match found.
  1436. func (dmp *DiffMatchPatch) MatchBitap(text, pattern string, loc int) int {
  1437. // Initialise the alphabet.
  1438. s := dmp.MatchAlphabet(pattern)
  1439. // Highest score beyond which we give up.
  1440. var score_threshold float64 = dmp.MatchThreshold
  1441. // Is there a nearby exact match? (speedup)
  1442. best_loc := indexOf(text, pattern, loc)
  1443. if best_loc != -1 {
  1444. score_threshold = math.Min(dmp.matchBitapScore(0, best_loc, loc,
  1445. pattern), score_threshold)
  1446. // What about in the other direction? (speedup)
  1447. best_loc = lastIndexOf(text, pattern, loc+len(pattern))
  1448. if best_loc != -1 {
  1449. score_threshold = math.Min(dmp.matchBitapScore(0, best_loc, loc,
  1450. pattern), score_threshold)
  1451. }
  1452. }
  1453. // Initialise the bit arrays.
  1454. matchmask := 1 << uint((len(pattern) - 1))
  1455. best_loc = -1
  1456. var bin_min, bin_mid int
  1457. bin_max := len(pattern) + len(text)
  1458. last_rd := []int{}
  1459. for d := 0; d < len(pattern); d++ {
  1460. // Scan for the best match; each iteration allows for one more error.
  1461. // Run a binary search to determine how far from 'loc' we can stray at
  1462. // this error level.
  1463. bin_min = 0
  1464. bin_mid = bin_max
  1465. for bin_min < bin_mid {
  1466. if dmp.matchBitapScore(d, loc+bin_mid, loc, pattern) <= score_threshold {
  1467. bin_min = bin_mid
  1468. } else {
  1469. bin_max = bin_mid
  1470. }
  1471. bin_mid = (bin_max-bin_min)/2 + bin_min
  1472. }
  1473. // Use the result from this iteration as the maximum for the next.
  1474. bin_max = bin_mid
  1475. start := int(math.Max(1, float64(loc-bin_mid+1)))
  1476. finish := int(math.Min(float64(loc+bin_mid), float64(len(text))) + float64(len(pattern)))
  1477. rd := make([]int, finish+2)
  1478. rd[finish+1] = (1 << uint(d)) - 1
  1479. for j := finish; j >= start; j-- {
  1480. var charMatch int
  1481. if len(text) <= j-1 {
  1482. // Out of range.
  1483. charMatch = 0
  1484. } else if _, ok := s[text[j-1]]; !ok {
  1485. charMatch = 0
  1486. } else {
  1487. charMatch = s[text[j-1]]
  1488. }
  1489. if d == 0 {
  1490. // First pass: exact match.
  1491. rd[j] = ((rd[j+1] << 1) | 1) & charMatch
  1492. } else {
  1493. // Subsequent passes: fuzzy match.
  1494. rd[j] = ((rd[j+1]<<1)|1)&charMatch | (((last_rd[j+1] | last_rd[j]) << 1) | 1) | last_rd[j+1]
  1495. }
  1496. if (rd[j] & matchmask) != 0 {
  1497. score := dmp.matchBitapScore(d, j-1, loc, pattern)
  1498. // This match will almost certainly be better than any existing
  1499. // match. But check anyway.
  1500. if score <= score_threshold {
  1501. // Told you so.
  1502. score_threshold = score
  1503. best_loc = j - 1
  1504. if best_loc > loc {
  1505. // When passing loc, don't exceed our current distance from loc.
  1506. start = int(math.Max(1, float64(2*loc-best_loc)))
  1507. } else {
  1508. // Already passed loc, downhill from here on in.
  1509. break
  1510. }
  1511. }
  1512. }
  1513. }
  1514. if dmp.matchBitapScore(d+1, loc, loc, pattern) > score_threshold {
  1515. // No hope for a (better) match at greater error levels.
  1516. break
  1517. }
  1518. last_rd = rd
  1519. }
  1520. return best_loc
  1521. }
  1522. // matchBitapScore computes and returns the score for a match with e errors and x location.
  1523. func (dmp *DiffMatchPatch) matchBitapScore(e, x, loc int, pattern string) float64 {
  1524. var accuracy float64 = float64(e) / float64(len(pattern))
  1525. proximity := math.Abs(float64(loc - x))
  1526. if dmp.MatchDistance == 0 {
  1527. // Dodge divide by zero error.
  1528. if proximity == 0 {
  1529. return accuracy
  1530. } else {
  1531. return 1.0
  1532. }
  1533. }
  1534. return accuracy + (proximity / float64(dmp.MatchDistance))
  1535. }
  1536. // MatchAlphabet initialises the alphabet for the Bitap algorithm.
  1537. func (dmp *DiffMatchPatch) MatchAlphabet(pattern string) map[byte]int {
  1538. s := map[byte]int{}
  1539. char_pattern := []byte(pattern)
  1540. for _, c := range char_pattern {
  1541. _, ok := s[c]
  1542. if !ok {
  1543. s[c] = 0
  1544. }
  1545. }
  1546. i := 0
  1547. for _, c := range char_pattern {
  1548. value := s[c] | int(uint(1)<<uint((len(pattern)-i-1)))
  1549. s[c] = value
  1550. i++
  1551. }
  1552. return s
  1553. }
  1554. // PATCH FUNCTIONS
  1555. // PatchAddContext increases the context until it is unique,
  1556. // but doesn't let the pattern expand beyond MatchMaxBits.
  1557. func (dmp *DiffMatchPatch) PatchAddContext(patch Patch, text string) Patch {
  1558. if len(text) == 0 {
  1559. return patch
  1560. }
  1561. pattern := text[patch.start2 : patch.start2+patch.length1]
  1562. padding := 0
  1563. // Look for the first and last matches of pattern in text. If two
  1564. // different matches are found, increase the pattern length.
  1565. for strings.Index(text, pattern) != strings.LastIndex(text, pattern) &&
  1566. len(pattern) < dmp.MatchMaxBits-2*dmp.PatchMargin {
  1567. padding += dmp.PatchMargin
  1568. maxStart := max(0, patch.start2-padding)
  1569. minEnd := min(len(text), patch.start2+patch.length1+padding)
  1570. pattern = text[maxStart:minEnd]
  1571. }
  1572. // Add one chunk for good luck.
  1573. padding += dmp.PatchMargin
  1574. // Add the prefix.
  1575. prefix := text[max(0, patch.start2-padding):patch.start2]
  1576. if len(prefix) != 0 {
  1577. patch.diffs = append([]Diff{Diff{DiffEqual, prefix}}, patch.diffs...)
  1578. }
  1579. // Add the suffix.
  1580. suffix := text[patch.start2+patch.length1 : min(len(text), patch.start2+patch.length1+padding)]
  1581. if len(suffix) != 0 {
  1582. patch.diffs = append(patch.diffs, Diff{DiffEqual, suffix})
  1583. }
  1584. // Roll back the start points.
  1585. patch.start1 -= len(prefix)
  1586. patch.start2 -= len(prefix)
  1587. // Extend the lengths.
  1588. patch.length1 += len(prefix) + len(suffix)
  1589. patch.length2 += len(prefix) + len(suffix)
  1590. return patch
  1591. }
  1592. func (dmp *DiffMatchPatch) PatchMake(opt ...interface{}) []Patch {
  1593. if len(opt) == 1 {
  1594. diffs, _ := opt[0].([]Diff)
  1595. text1 := dmp.DiffText1(diffs)
  1596. return dmp.PatchMake(text1, diffs)
  1597. } else if len(opt) == 2 {
  1598. text1 := opt[0].(string)
  1599. switch t := opt[1].(type) {
  1600. case string:
  1601. diffs := dmp.DiffMain(text1, t, true)
  1602. if len(diffs) > 2 {
  1603. diffs = dmp.DiffCleanupSemantic(diffs)
  1604. diffs = dmp.DiffCleanupEfficiency(diffs)
  1605. }
  1606. return dmp.PatchMake(text1, diffs)
  1607. case []Diff:
  1608. return dmp.patchMake2(text1, t)
  1609. }
  1610. } else if len(opt) == 3 {
  1611. return dmp.PatchMake(opt[0], opt[2])
  1612. }
  1613. return []Patch{}
  1614. }
  1615. // Compute a list of patches to turn text1 into text2.
  1616. // text2 is not provided, diffs are the delta between text1 and text2.
  1617. func (dmp *DiffMatchPatch) patchMake2(text1 string, diffs []Diff) []Patch {
  1618. // Check for null inputs not needed since null can't be passed in C#.
  1619. patches := []Patch{}
  1620. if len(diffs) == 0 {
  1621. return patches // Get rid of the null case.
  1622. }
  1623. patch := Patch{}
  1624. char_count1 := 0 // Number of characters into the text1 string.
  1625. char_count2 := 0 // Number of characters into the text2 string.
  1626. // Start with text1 (prepatch_text) and apply the diffs until we arrive at
  1627. // text2 (postpatch_text). We recreate the patches one by one to determine
  1628. // context info.
  1629. prepatch_text := text1
  1630. postpatch_text := text1
  1631. for i, aDiff := range diffs {
  1632. if len(patch.diffs) == 0 && aDiff.Type != DiffEqual {
  1633. // A new patch starts here.
  1634. patch.start1 = char_count1
  1635. patch.start2 = char_count2
  1636. }
  1637. switch aDiff.Type {
  1638. case DiffInsert:
  1639. patch.diffs = append(patch.diffs, aDiff)
  1640. patch.length2 += len(aDiff.Text)
  1641. postpatch_text = postpatch_text[:char_count2] +
  1642. aDiff.Text + postpatch_text[char_count2:]
  1643. case DiffDelete:
  1644. patch.length1 += len(aDiff.Text)
  1645. patch.diffs = append(patch.diffs, aDiff)
  1646. postpatch_text = postpatch_text[:char_count2] + postpatch_text[char_count2+len(aDiff.Text):]
  1647. case DiffEqual:
  1648. if len(aDiff.Text) <= 2*dmp.PatchMargin &&
  1649. len(patch.diffs) != 0 && i != len(diffs)-1 {
  1650. // Small equality inside a patch.
  1651. patch.diffs = append(patch.diffs, aDiff)
  1652. patch.length1 += len(aDiff.Text)
  1653. patch.length2 += len(aDiff.Text)
  1654. }
  1655. if len(aDiff.Text) >= 2*dmp.PatchMargin {
  1656. // Time for a new patch.
  1657. if len(patch.diffs) != 0 {
  1658. patch = dmp.PatchAddContext(patch, prepatch_text)
  1659. patches = append(patches, patch)
  1660. patch = Patch{}
  1661. // Unlike Unidiff, our patch lists have a rolling context.
  1662. // http://code.google.com/p/google-diff-match-patch/wiki/Unidiff
  1663. // Update prepatch text & pos to reflect the application of the
  1664. // just completed patch.
  1665. prepatch_text = postpatch_text
  1666. char_count1 = char_count2
  1667. }
  1668. }
  1669. }
  1670. // Update the current character count.
  1671. if aDiff.Type != DiffInsert {
  1672. char_count1 += len(aDiff.Text)
  1673. }
  1674. if aDiff.Type != DiffDelete {
  1675. char_count2 += len(aDiff.Text)
  1676. }
  1677. }
  1678. // Pick up the leftover patch if not empty.
  1679. if len(patch.diffs) != 0 {
  1680. patch = dmp.PatchAddContext(patch, prepatch_text)
  1681. patches = append(patches, patch)
  1682. }
  1683. return patches
  1684. }
  1685. // PatchDeepCopy returns an array that is identical to a
  1686. // given an array of patches.
  1687. func (dmp *DiffMatchPatch) PatchDeepCopy(patches []Patch) []Patch {
  1688. patchesCopy := []Patch{}
  1689. for _, aPatch := range patches {
  1690. patchCopy := Patch{}
  1691. for _, aDiff := range aPatch.diffs {
  1692. patchCopy.diffs = append(patchCopy.diffs, Diff{
  1693. aDiff.Type,
  1694. aDiff.Text,
  1695. })
  1696. }
  1697. patchCopy.start1 = aPatch.start1
  1698. patchCopy.start2 = aPatch.start2
  1699. patchCopy.length1 = aPatch.length1
  1700. patchCopy.length2 = aPatch.length2
  1701. patchesCopy = append(patchesCopy, patchCopy)
  1702. }
  1703. return patchesCopy
  1704. }
  1705. // PatchApply merges a set of patches onto the text. Returns a patched text, as well
  1706. // as an array of true/false values indicating which patches were applied.
  1707. func (dmp *DiffMatchPatch) PatchApply(patches []Patch, text string) (string, []bool) {
  1708. if len(patches) == 0 {
  1709. return text, []bool{}
  1710. }
  1711. // Deep copy the patches so that no changes are made to originals.
  1712. patches = dmp.PatchDeepCopy(patches)
  1713. nullPadding := dmp.PatchAddPadding(patches)
  1714. text = nullPadding + text + nullPadding
  1715. patches = dmp.PatchSplitMax(patches)
  1716. x := 0
  1717. // delta keeps track of the offset between the expected and actual
  1718. // location of the previous patch. If there are patches expected at
  1719. // positions 10 and 20, but the first patch was found at 12, delta is 2
  1720. // and the second patch has an effective expected position of 22.
  1721. delta := 0
  1722. results := make([]bool, len(patches))
  1723. for _, aPatch := range patches {
  1724. expected_loc := aPatch.start2 + delta
  1725. text1 := dmp.DiffText1(aPatch.diffs)
  1726. var start_loc int
  1727. end_loc := -1
  1728. if len(text1) > dmp.MatchMaxBits {
  1729. // PatchSplitMax will only provide an oversized pattern
  1730. // in the case of a monster delete.
  1731. start_loc = dmp.MatchMain(text, text1[:dmp.MatchMaxBits], expected_loc)
  1732. if start_loc != -1 {
  1733. end_loc = dmp.MatchMain(text,
  1734. text1[len(text1)-dmp.MatchMaxBits:], expected_loc+len(text1)-dmp.MatchMaxBits)
  1735. if end_loc == -1 || start_loc >= end_loc {
  1736. // Can't find valid trailing context. Drop this patch.
  1737. start_loc = -1
  1738. }
  1739. }
  1740. } else {
  1741. start_loc = dmp.MatchMain(text, text1, expected_loc)
  1742. }
  1743. if start_loc == -1 {
  1744. // No match found. :(
  1745. results[x] = false
  1746. // Subtract the delta for this failed patch from subsequent patches.
  1747. delta -= aPatch.length2 - aPatch.length1
  1748. } else {
  1749. // Found a match. :)
  1750. results[x] = true
  1751. delta = start_loc - expected_loc
  1752. var text2 string
  1753. if end_loc == -1 {
  1754. text2 = text[start_loc:int(math.Min(float64(start_loc+len(text1)), float64(len(text))))]
  1755. } else {
  1756. text2 = text[start_loc:int(math.Min(float64(end_loc+dmp.MatchMaxBits), float64(len(text))))]
  1757. }
  1758. if text1 == text2 {
  1759. // Perfect match, just shove the Replacement text in.
  1760. text = text[:start_loc] + dmp.DiffText2(aPatch.diffs) + text[start_loc+len(text1):]
  1761. } else {
  1762. // Imperfect match. Run a diff to get a framework of equivalent
  1763. // indices.
  1764. diffs := dmp.DiffMain(text1, text2, false)
  1765. if len(text1) > dmp.MatchMaxBits && float64(dmp.DiffLevenshtein(diffs))/float64(len(text1)) > dmp.PatchDeleteThreshold {
  1766. // The end points match, but the content is unacceptably bad.
  1767. results[x] = false
  1768. } else {
  1769. diffs = dmp.DiffCleanupSemanticLossless(diffs)
  1770. index1 := 0
  1771. for _, aDiff := range aPatch.diffs {
  1772. if aDiff.Type != DiffEqual {
  1773. index2 := dmp.DiffXIndex(diffs, index1)
  1774. if aDiff.Type == DiffInsert {
  1775. // Insertion
  1776. text = text[:start_loc+index2] + aDiff.Text + text[start_loc+index2:]
  1777. } else if aDiff.Type == DiffDelete {
  1778. // Deletion
  1779. start_index := start_loc + index2
  1780. text = text[:start_index] +
  1781. text[start_index+dmp.DiffXIndex(diffs, index1+len(aDiff.Text))-index2:]
  1782. }
  1783. }
  1784. if aDiff.Type != DiffDelete {
  1785. index1 += len(aDiff.Text)
  1786. }
  1787. }
  1788. }
  1789. }
  1790. }
  1791. x++
  1792. }
  1793. // Strip the padding off.
  1794. text = text[len(nullPadding) : len(nullPadding)+(len(text)-2*len(nullPadding))]
  1795. return text, results
  1796. }
  1797. // PatchAddPadding adds some padding on text start and end so that edges can match something.
  1798. // Intended to be called only from within patch_apply.
  1799. func (dmp *DiffMatchPatch) PatchAddPadding(patches []Patch) string {
  1800. paddingLength := dmp.PatchMargin
  1801. nullPadding := ""
  1802. for x := 1; x <= paddingLength; x++ {
  1803. nullPadding += string(x)
  1804. }
  1805. // Bump all the patches forward.
  1806. for i, _ := range patches {
  1807. patches[i].start1 += paddingLength
  1808. patches[i].start2 += paddingLength
  1809. }
  1810. // Add some padding on start of first diff.
  1811. if len(patches[0].diffs) == 0 || patches[0].diffs[0].Type != DiffEqual {
  1812. // Add nullPadding equality.
  1813. patches[0].diffs = append([]Diff{Diff{DiffEqual, nullPadding}}, patches[0].diffs...)
  1814. patches[0].start1 -= paddingLength // Should be 0.
  1815. patches[0].start2 -= paddingLength // Should be 0.
  1816. patches[0].length1 += paddingLength
  1817. patches[0].length2 += paddingLength
  1818. } else if paddingLength > len(patches[0].diffs[0].Text) {
  1819. // Grow first equality.
  1820. extraLength := paddingLength - len(patches[0].diffs[0].Text)
  1821. patches[0].diffs[0].Text = nullPadding[len(patches[0].diffs[0].Text):] + patches[0].diffs[0].Text
  1822. patches[0].start1 -= extraLength
  1823. patches[0].start2 -= extraLength
  1824. patches[0].length1 += extraLength
  1825. patches[0].length2 += extraLength
  1826. }
  1827. // Add some padding on end of last diff.
  1828. last := len(patches) - 1
  1829. if len(patches[last].diffs) == 0 || patches[last].diffs[len(patches[last].diffs)-1].Type != DiffEqual {
  1830. // Add nullPadding equality.
  1831. patches[last].diffs = append(patches[last].diffs, Diff{DiffEqual, nullPadding})
  1832. patches[last].length1 += paddingLength
  1833. patches[last].length2 += paddingLength
  1834. } else if paddingLength > len(patches[last].diffs[len(patches[last].diffs)-1].Text) {
  1835. // Grow last equality.
  1836. lastDiff := patches[last].diffs[len(patches[last].diffs)-1]
  1837. extraLength := paddingLength - len(lastDiff.Text)
  1838. patches[last].diffs[len(patches[last].diffs)-1].Text += nullPadding[:extraLength]
  1839. patches[last].length1 += extraLength
  1840. patches[last].length2 += extraLength
  1841. }
  1842. return nullPadding
  1843. }
  1844. // PatchSplitMax looks through the patches and breaks up any which are longer than the
  1845. // maximum limit of the match algorithm.
  1846. // Intended to be called only from within patch_apply.
  1847. func (dmp *DiffMatchPatch) PatchSplitMax(patches []Patch) []Patch {
  1848. patch_size := dmp.MatchMaxBits
  1849. for x := 0; x < len(patches); x++ {
  1850. if patches[x].length1 <= patch_size {
  1851. continue
  1852. }
  1853. bigpatch := patches[x]
  1854. // Remove the big old patch.
  1855. patches = append(patches[:x], patches[x+1:]...)
  1856. x -= 1
  1857. start1 := bigpatch.start1
  1858. start2 := bigpatch.start2
  1859. precontext := ""
  1860. for len(bigpatch.diffs) != 0 {
  1861. // Create one of several smaller patches.
  1862. patch := Patch{}
  1863. empty := true
  1864. patch.start1 = start1 - len(precontext)
  1865. patch.start2 = start2 - len(precontext)
  1866. if len(precontext) != 0 {
  1867. patch.length1 = len(precontext)
  1868. patch.length2 = len(precontext)
  1869. patch.diffs = append(patch.diffs, Diff{DiffEqual, precontext})
  1870. }
  1871. for len(bigpatch.diffs) != 0 && patch.length1 < patch_size-dmp.PatchMargin {
  1872. diff_type := bigpatch.diffs[0].Type
  1873. diff_text := bigpatch.diffs[0].Text
  1874. if diff_type == DiffInsert {
  1875. // Insertions are harmless.
  1876. patch.length2 += len(diff_text)
  1877. start2 += len(diff_text)
  1878. patch.diffs = append(patch.diffs, bigpatch.diffs[0])
  1879. bigpatch.diffs = bigpatch.diffs[1:]
  1880. empty = false
  1881. } else if diff_type == DiffDelete && len(patch.diffs) == 1 && patch.diffs[0].Type == DiffEqual && len(diff_text) > 2*patch_size {
  1882. // This is a large deletion. Let it pass in one chunk.
  1883. patch.length1 += len(diff_text)
  1884. start1 += len(diff_text)
  1885. empty = false
  1886. patch.diffs = append(patch.diffs, Diff{diff_type, diff_text})
  1887. bigpatch.diffs = bigpatch.diffs[1:]
  1888. } else {
  1889. // Deletion or equality. Only take as much as we can stomach.
  1890. diff_text = diff_text[:min(len(diff_text), patch_size-patch.length1-dmp.PatchMargin)]
  1891. patch.length1 += len(diff_text)
  1892. start1 += len(diff_text)
  1893. if diff_type == DiffEqual {
  1894. patch.length2 += len(diff_text)
  1895. start2 += len(diff_text)
  1896. } else {
  1897. empty = false
  1898. }
  1899. patch.diffs = append(patch.diffs, Diff{diff_type, diff_text})
  1900. if diff_text == bigpatch.diffs[0].Text {
  1901. bigpatch.diffs = bigpatch.diffs[1:]
  1902. } else {
  1903. bigpatch.diffs[0].Text =
  1904. bigpatch.diffs[0].Text[len(diff_text):]
  1905. }
  1906. }
  1907. }
  1908. // Compute the head context for the next patch.
  1909. precontext = dmp.DiffText2(patch.diffs)
  1910. precontext = precontext[max(0, len(precontext)-dmp.PatchMargin):]
  1911. postcontext := ""
  1912. // Append the end context for this patch.
  1913. if len(dmp.DiffText1(bigpatch.diffs)) > dmp.PatchMargin {
  1914. postcontext = dmp.DiffText1(bigpatch.diffs)[:dmp.PatchMargin]
  1915. } else {
  1916. postcontext = dmp.DiffText1(bigpatch.diffs)
  1917. }
  1918. if len(postcontext) != 0 {
  1919. patch.length1 += len(postcontext)
  1920. patch.length2 += len(postcontext)
  1921. if len(patch.diffs) != 0 && patch.diffs[len(patch.diffs)-1].Type == DiffEqual {
  1922. patch.diffs[len(patch.diffs)-1].Text += postcontext
  1923. } else {
  1924. patch.diffs = append(patch.diffs, Diff{DiffEqual, postcontext})
  1925. }
  1926. }
  1927. if !empty {
  1928. x += 1
  1929. patches = append(patches[:x], append([]Patch{patch}, patches[x:]...)...)
  1930. }
  1931. }
  1932. }
  1933. return patches
  1934. }
  1935. // PatchToText takes a list of patches and returns a textual representation.
  1936. func (dmp *DiffMatchPatch) PatchToText(patches []Patch) string {
  1937. var text bytes.Buffer
  1938. for _, aPatch := range patches {
  1939. text.WriteString(aPatch.String())
  1940. }
  1941. return text.String()
  1942. }
  1943. // PatchFromText parses a textual representation of patches and returns a List of Patch
  1944. // objects.
  1945. func (dmp *DiffMatchPatch) PatchFromText(textline string) ([]Patch, error) {
  1946. patches := []Patch{}
  1947. if len(textline) == 0 {
  1948. return patches, nil
  1949. }
  1950. text := strings.Split(textline, "\n")
  1951. textPointer := 0
  1952. patchHeader := regexp.MustCompile("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$")
  1953. var patch Patch
  1954. var sign uint8
  1955. var line string
  1956. for textPointer < len(text) {
  1957. if !patchHeader.MatchString(text[textPointer]) {
  1958. return patches, errors.New("Invalid patch string: " + text[textPointer])
  1959. }
  1960. patch = Patch{}
  1961. m := patchHeader.FindStringSubmatch(text[textPointer])
  1962. patch.start1, _ = strconv.Atoi(m[1])
  1963. if len(m[2]) == 0 {
  1964. patch.start1--
  1965. patch.length1 = 1
  1966. } else if m[2] == "0" {
  1967. patch.length1 = 0
  1968. } else {
  1969. patch.start1--
  1970. patch.length1, _ = strconv.Atoi(m[2])
  1971. }
  1972. patch.start2, _ = strconv.Atoi(m[3])
  1973. if len(m[4]) == 0 {
  1974. patch.start2--
  1975. patch.length2 = 1
  1976. } else if m[4] == "0" {
  1977. patch.length2 = 0
  1978. } else {
  1979. patch.start2--
  1980. patch.length2, _ = strconv.Atoi(m[4])
  1981. }
  1982. textPointer++
  1983. for textPointer < len(text) {
  1984. if len(text[textPointer]) > 0 {
  1985. sign = text[textPointer][0]
  1986. } else {
  1987. textPointer++
  1988. continue
  1989. }
  1990. line = text[textPointer][1:]
  1991. line = strings.Replace(line, "+", "%2b", -1)
  1992. line, _ = url.QueryUnescape(line)
  1993. if sign == '-' {
  1994. // Deletion.
  1995. patch.diffs = append(patch.diffs, Diff{DiffDelete, line})
  1996. } else if sign == '+' {
  1997. // Insertion.
  1998. patch.diffs = append(patch.diffs, Diff{DiffInsert, line})
  1999. } else if sign == ' ' {
  2000. // Minor equality.
  2001. patch.diffs = append(patch.diffs, Diff{DiffEqual, line})
  2002. } else if sign == '@' {
  2003. // Start of next patch.
  2004. break
  2005. } else {
  2006. // WTF?
  2007. return patches, errors.New("Invalid patch mode '" + string(sign) + "' in: " + string(line))
  2008. }
  2009. textPointer++
  2010. }
  2011. patches = append(patches, patch)
  2012. }
  2013. return patches, nil
  2014. }