difflib.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* Package difflib is a partial port of Python difflib module.
  2. Original source: https://github.com/pmezard/go-difflib
  3. This file is trimmed to only the parts used by this repository.
  4. */
  5. package difflib // import "gotest.tools/internal/difflib"
  6. func min(a, b int) int {
  7. if a < b {
  8. return a
  9. }
  10. return b
  11. }
  12. func max(a, b int) int {
  13. if a > b {
  14. return a
  15. }
  16. return b
  17. }
  18. type Match struct {
  19. A int
  20. B int
  21. Size int
  22. }
  23. type OpCode struct {
  24. Tag byte
  25. I1 int
  26. I2 int
  27. J1 int
  28. J2 int
  29. }
  30. // SequenceMatcher compares sequence of strings. The basic
  31. // algorithm predates, and is a little fancier than, an algorithm
  32. // published in the late 1980's by Ratcliff and Obershelp under the
  33. // hyperbolic name "gestalt pattern matching". The basic idea is to find
  34. // the longest contiguous matching subsequence that contains no "junk"
  35. // elements (R-O doesn't address junk). The same idea is then applied
  36. // recursively to the pieces of the sequences to the left and to the right
  37. // of the matching subsequence. This does not yield minimal edit
  38. // sequences, but does tend to yield matches that "look right" to people.
  39. //
  40. // SequenceMatcher tries to compute a "human-friendly diff" between two
  41. // sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the
  42. // longest *contiguous* & junk-free matching subsequence. That's what
  43. // catches peoples' eyes. The Windows(tm) windiff has another interesting
  44. // notion, pairing up elements that appear uniquely in each sequence.
  45. // That, and the method here, appear to yield more intuitive difference
  46. // reports than does diff. This method appears to be the least vulnerable
  47. // to synching up on blocks of "junk lines", though (like blank lines in
  48. // ordinary text files, or maybe "<P>" lines in HTML files). That may be
  49. // because this is the only method of the 3 that has a *concept* of
  50. // "junk" <wink>.
  51. //
  52. // Timing: Basic R-O is cubic time worst case and quadratic time expected
  53. // case. SequenceMatcher is quadratic time for the worst case and has
  54. // expected-case behavior dependent in a complicated way on how many
  55. // elements the sequences have in common; best case time is linear.
  56. type SequenceMatcher struct {
  57. a []string
  58. b []string
  59. b2j map[string][]int
  60. IsJunk func(string) bool
  61. autoJunk bool
  62. bJunk map[string]struct{}
  63. matchingBlocks []Match
  64. fullBCount map[string]int
  65. bPopular map[string]struct{}
  66. opCodes []OpCode
  67. }
  68. func NewMatcher(a, b []string) *SequenceMatcher {
  69. m := SequenceMatcher{autoJunk: true}
  70. m.SetSeqs(a, b)
  71. return &m
  72. }
  73. // Set two sequences to be compared.
  74. func (m *SequenceMatcher) SetSeqs(a, b []string) {
  75. m.SetSeq1(a)
  76. m.SetSeq2(b)
  77. }
  78. // Set the first sequence to be compared. The second sequence to be compared is
  79. // not changed.
  80. //
  81. // SequenceMatcher computes and caches detailed information about the second
  82. // sequence, so if you want to compare one sequence S against many sequences,
  83. // use .SetSeq2(s) once and call .SetSeq1(x) repeatedly for each of the other
  84. // sequences.
  85. //
  86. // See also SetSeqs() and SetSeq2().
  87. func (m *SequenceMatcher) SetSeq1(a []string) {
  88. if &a == &m.a {
  89. return
  90. }
  91. m.a = a
  92. m.matchingBlocks = nil
  93. m.opCodes = nil
  94. }
  95. // Set the second sequence to be compared. The first sequence to be compared is
  96. // not changed.
  97. func (m *SequenceMatcher) SetSeq2(b []string) {
  98. if &b == &m.b {
  99. return
  100. }
  101. m.b = b
  102. m.matchingBlocks = nil
  103. m.opCodes = nil
  104. m.fullBCount = nil
  105. m.chainB()
  106. }
  107. func (m *SequenceMatcher) chainB() {
  108. // Populate line -> index mapping
  109. b2j := map[string][]int{}
  110. for i, s := range m.b {
  111. indices := b2j[s]
  112. indices = append(indices, i)
  113. b2j[s] = indices
  114. }
  115. // Purge junk elements
  116. m.bJunk = map[string]struct{}{}
  117. if m.IsJunk != nil {
  118. junk := m.bJunk
  119. for s, _ := range b2j {
  120. if m.IsJunk(s) {
  121. junk[s] = struct{}{}
  122. }
  123. }
  124. for s, _ := range junk {
  125. delete(b2j, s)
  126. }
  127. }
  128. // Purge remaining popular elements
  129. popular := map[string]struct{}{}
  130. n := len(m.b)
  131. if m.autoJunk && n >= 200 {
  132. ntest := n/100 + 1
  133. for s, indices := range b2j {
  134. if len(indices) > ntest {
  135. popular[s] = struct{}{}
  136. }
  137. }
  138. for s, _ := range popular {
  139. delete(b2j, s)
  140. }
  141. }
  142. m.bPopular = popular
  143. m.b2j = b2j
  144. }
  145. func (m *SequenceMatcher) isBJunk(s string) bool {
  146. _, ok := m.bJunk[s]
  147. return ok
  148. }
  149. // Find longest matching block in a[alo:ahi] and b[blo:bhi].
  150. //
  151. // If IsJunk is not defined:
  152. //
  153. // Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
  154. // alo <= i <= i+k <= ahi
  155. // blo <= j <= j+k <= bhi
  156. // and for all (i',j',k') meeting those conditions,
  157. // k >= k'
  158. // i <= i'
  159. // and if i == i', j <= j'
  160. //
  161. // In other words, of all maximal matching blocks, return one that
  162. // starts earliest in a, and of all those maximal matching blocks that
  163. // start earliest in a, return the one that starts earliest in b.
  164. //
  165. // If IsJunk is defined, first the longest matching block is
  166. // determined as above, but with the additional restriction that no
  167. // junk element appears in the block. Then that block is extended as
  168. // far as possible by matching (only) junk elements on both sides. So
  169. // the resulting block never matches on junk except as identical junk
  170. // happens to be adjacent to an "interesting" match.
  171. //
  172. // If no blocks match, return (alo, blo, 0).
  173. func (m *SequenceMatcher) findLongestMatch(alo, ahi, blo, bhi int) Match {
  174. // CAUTION: stripping common prefix or suffix would be incorrect.
  175. // E.g.,
  176. // ab
  177. // acab
  178. // Longest matching block is "ab", but if common prefix is
  179. // stripped, it's "a" (tied with "b"). UNIX(tm) diff does so
  180. // strip, so ends up claiming that ab is changed to acab by
  181. // inserting "ca" in the middle. That's minimal but unintuitive:
  182. // "it's obvious" that someone inserted "ac" at the front.
  183. // Windiff ends up at the same place as diff, but by pairing up
  184. // the unique 'b's and then matching the first two 'a's.
  185. besti, bestj, bestsize := alo, blo, 0
  186. // find longest junk-free match
  187. // during an iteration of the loop, j2len[j] = length of longest
  188. // junk-free match ending with a[i-1] and b[j]
  189. j2len := map[int]int{}
  190. for i := alo; i != ahi; i++ {
  191. // look at all instances of a[i] in b; note that because
  192. // b2j has no junk keys, the loop is skipped if a[i] is junk
  193. newj2len := map[int]int{}
  194. for _, j := range m.b2j[m.a[i]] {
  195. // a[i] matches b[j]
  196. if j < blo {
  197. continue
  198. }
  199. if j >= bhi {
  200. break
  201. }
  202. k := j2len[j-1] + 1
  203. newj2len[j] = k
  204. if k > bestsize {
  205. besti, bestj, bestsize = i-k+1, j-k+1, k
  206. }
  207. }
  208. j2len = newj2len
  209. }
  210. // Extend the best by non-junk elements on each end. In particular,
  211. // "popular" non-junk elements aren't in b2j, which greatly speeds
  212. // the inner loop above, but also means "the best" match so far
  213. // doesn't contain any junk *or* popular non-junk elements.
  214. for besti > alo && bestj > blo && !m.isBJunk(m.b[bestj-1]) &&
  215. m.a[besti-1] == m.b[bestj-1] {
  216. besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
  217. }
  218. for besti+bestsize < ahi && bestj+bestsize < bhi &&
  219. !m.isBJunk(m.b[bestj+bestsize]) &&
  220. m.a[besti+bestsize] == m.b[bestj+bestsize] {
  221. bestsize += 1
  222. }
  223. // Now that we have a wholly interesting match (albeit possibly
  224. // empty!), we may as well suck up the matching junk on each
  225. // side of it too. Can't think of a good reason not to, and it
  226. // saves post-processing the (possibly considerable) expense of
  227. // figuring out what to do with it. In the case of an empty
  228. // interesting match, this is clearly the right thing to do,
  229. // because no other kind of match is possible in the regions.
  230. for besti > alo && bestj > blo && m.isBJunk(m.b[bestj-1]) &&
  231. m.a[besti-1] == m.b[bestj-1] {
  232. besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
  233. }
  234. for besti+bestsize < ahi && bestj+bestsize < bhi &&
  235. m.isBJunk(m.b[bestj+bestsize]) &&
  236. m.a[besti+bestsize] == m.b[bestj+bestsize] {
  237. bestsize += 1
  238. }
  239. return Match{A: besti, B: bestj, Size: bestsize}
  240. }
  241. // Return list of triples describing matching subsequences.
  242. //
  243. // Each triple is of the form (i, j, n), and means that
  244. // a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in
  245. // i and in j. It's also guaranteed that if (i, j, n) and (i', j', n') are
  246. // adjacent triples in the list, and the second is not the last triple in the
  247. // list, then i+n != i' or j+n != j'. IOW, adjacent triples never describe
  248. // adjacent equal blocks.
  249. //
  250. // The last triple is a dummy, (len(a), len(b), 0), and is the only
  251. // triple with n==0.
  252. func (m *SequenceMatcher) GetMatchingBlocks() []Match {
  253. if m.matchingBlocks != nil {
  254. return m.matchingBlocks
  255. }
  256. var matchBlocks func(alo, ahi, blo, bhi int, matched []Match) []Match
  257. matchBlocks = func(alo, ahi, blo, bhi int, matched []Match) []Match {
  258. match := m.findLongestMatch(alo, ahi, blo, bhi)
  259. i, j, k := match.A, match.B, match.Size
  260. if match.Size > 0 {
  261. if alo < i && blo < j {
  262. matched = matchBlocks(alo, i, blo, j, matched)
  263. }
  264. matched = append(matched, match)
  265. if i+k < ahi && j+k < bhi {
  266. matched = matchBlocks(i+k, ahi, j+k, bhi, matched)
  267. }
  268. }
  269. return matched
  270. }
  271. matched := matchBlocks(0, len(m.a), 0, len(m.b), nil)
  272. // It's possible that we have adjacent equal blocks in the
  273. // matching_blocks list now.
  274. nonAdjacent := []Match{}
  275. i1, j1, k1 := 0, 0, 0
  276. for _, b := range matched {
  277. // Is this block adjacent to i1, j1, k1?
  278. i2, j2, k2 := b.A, b.B, b.Size
  279. if i1+k1 == i2 && j1+k1 == j2 {
  280. // Yes, so collapse them -- this just increases the length of
  281. // the first block by the length of the second, and the first
  282. // block so lengthened remains the block to compare against.
  283. k1 += k2
  284. } else {
  285. // Not adjacent. Remember the first block (k1==0 means it's
  286. // the dummy we started with), and make the second block the
  287. // new block to compare against.
  288. if k1 > 0 {
  289. nonAdjacent = append(nonAdjacent, Match{i1, j1, k1})
  290. }
  291. i1, j1, k1 = i2, j2, k2
  292. }
  293. }
  294. if k1 > 0 {
  295. nonAdjacent = append(nonAdjacent, Match{i1, j1, k1})
  296. }
  297. nonAdjacent = append(nonAdjacent, Match{len(m.a), len(m.b), 0})
  298. m.matchingBlocks = nonAdjacent
  299. return m.matchingBlocks
  300. }
  301. // Return list of 5-tuples describing how to turn a into b.
  302. //
  303. // Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple
  304. // has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the
  305. // tuple preceding it, and likewise for j1 == the previous j2.
  306. //
  307. // The tags are characters, with these meanings:
  308. //
  309. // 'r' (replace): a[i1:i2] should be replaced by b[j1:j2]
  310. //
  311. // 'd' (delete): a[i1:i2] should be deleted, j1==j2 in this case.
  312. //
  313. // 'i' (insert): b[j1:j2] should be inserted at a[i1:i1], i1==i2 in this case.
  314. //
  315. // 'e' (equal): a[i1:i2] == b[j1:j2]
  316. func (m *SequenceMatcher) GetOpCodes() []OpCode {
  317. if m.opCodes != nil {
  318. return m.opCodes
  319. }
  320. i, j := 0, 0
  321. matching := m.GetMatchingBlocks()
  322. opCodes := make([]OpCode, 0, len(matching))
  323. for _, m := range matching {
  324. // invariant: we've pumped out correct diffs to change
  325. // a[:i] into b[:j], and the next matching block is
  326. // a[ai:ai+size] == b[bj:bj+size]. So we need to pump
  327. // out a diff to change a[i:ai] into b[j:bj], pump out
  328. // the matching block, and move (i,j) beyond the match
  329. ai, bj, size := m.A, m.B, m.Size
  330. tag := byte(0)
  331. if i < ai && j < bj {
  332. tag = 'r'
  333. } else if i < ai {
  334. tag = 'd'
  335. } else if j < bj {
  336. tag = 'i'
  337. }
  338. if tag > 0 {
  339. opCodes = append(opCodes, OpCode{tag, i, ai, j, bj})
  340. }
  341. i, j = ai+size, bj+size
  342. // the list of matching blocks is terminated by a
  343. // sentinel with size 0
  344. if size > 0 {
  345. opCodes = append(opCodes, OpCode{'e', ai, i, bj, j})
  346. }
  347. }
  348. m.opCodes = opCodes
  349. return m.opCodes
  350. }
  351. // Isolate change clusters by eliminating ranges with no changes.
  352. //
  353. // Return a generator of groups with up to n lines of context.
  354. // Each group is in the same format as returned by GetOpCodes().
  355. func (m *SequenceMatcher) GetGroupedOpCodes(n int) [][]OpCode {
  356. if n < 0 {
  357. n = 3
  358. }
  359. codes := m.GetOpCodes()
  360. if len(codes) == 0 {
  361. codes = []OpCode{OpCode{'e', 0, 1, 0, 1}}
  362. }
  363. // Fixup leading and trailing groups if they show no changes.
  364. if codes[0].Tag == 'e' {
  365. c := codes[0]
  366. i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
  367. codes[0] = OpCode{c.Tag, max(i1, i2-n), i2, max(j1, j2-n), j2}
  368. }
  369. if codes[len(codes)-1].Tag == 'e' {
  370. c := codes[len(codes)-1]
  371. i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
  372. codes[len(codes)-1] = OpCode{c.Tag, i1, min(i2, i1+n), j1, min(j2, j1+n)}
  373. }
  374. nn := n + n
  375. groups := [][]OpCode{}
  376. group := []OpCode{}
  377. for _, c := range codes {
  378. i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
  379. // End the current group and start a new one whenever
  380. // there is a large range with no changes.
  381. if c.Tag == 'e' && i2-i1 > nn {
  382. group = append(group, OpCode{c.Tag, i1, min(i2, i1+n),
  383. j1, min(j2, j1+n)})
  384. groups = append(groups, group)
  385. group = []OpCode{}
  386. i1, j1 = max(i1, i2-n), max(j1, j2-n)
  387. }
  388. group = append(group, OpCode{c.Tag, i1, i2, j1, j2})
  389. }
  390. if len(group) > 0 && !(len(group) == 1 && group[0].Tag == 'e') {
  391. groups = append(groups, group)
  392. }
  393. return groups
  394. }