1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package difflib
17
18 import (
19 "bufio"
20 "bytes"
21 "fmt"
22 "io"
23 "strings"
24 )
25
26 func min(a, b int) int {
27 if a < b {
28 return a
29 }
30 return b
31 }
32
33 func max(a, b int) int {
34 if a > b {
35 return a
36 }
37 return b
38 }
39
40 func calculateRatio(matches, length int) float64 {
41 if length > 0 {
42 return 2.0 * float64(matches) / float64(length)
43 }
44 return 1.0
45 }
46
47 type Match struct {
48 A int
49 B int
50 Size int
51 }
52
53 type OpCode struct {
54 Tag byte
55 I1 int
56 I2 int
57 J1 int
58 J2 int
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 type SequenceMatcher struct {
88 a []string
89 b []string
90 b2j map[string][]int
91 IsJunk func(string) bool
92 autoJunk bool
93 bJunk map[string]struct{}
94 matchingBlocks []Match
95 fullBCount map[string]int
96 bPopular map[string]struct{}
97 opCodes []OpCode
98 }
99
100 func NewMatcher(a, b []string) *SequenceMatcher {
101 m := SequenceMatcher{autoJunk: true}
102 m.SetSeqs(a, b)
103 return &m
104 }
105
106 func NewMatcherWithJunk(a, b []string, autoJunk bool,
107 isJunk func(string) bool) *SequenceMatcher {
108
109 m := SequenceMatcher{IsJunk: isJunk, autoJunk: autoJunk}
110 m.SetSeqs(a, b)
111 return &m
112 }
113
114
115 func (m *SequenceMatcher) SetSeqs(a, b []string) {
116 m.SetSeq1(a)
117 m.SetSeq2(b)
118 }
119
120
121
122
123
124
125
126
127
128
129 func (m *SequenceMatcher) SetSeq1(a []string) {
130 if &a == &m.a {
131 return
132 }
133 m.a = a
134 m.matchingBlocks = nil
135 m.opCodes = nil
136 }
137
138
139
140 func (m *SequenceMatcher) SetSeq2(b []string) {
141 if &b == &m.b {
142 return
143 }
144 m.b = b
145 m.matchingBlocks = nil
146 m.opCodes = nil
147 m.fullBCount = nil
148 m.chainB()
149 }
150
151 func (m *SequenceMatcher) chainB() {
152
153 b2j := map[string][]int{}
154 for i, s := range m.b {
155 indices := b2j[s]
156 indices = append(indices, i)
157 b2j[s] = indices
158 }
159
160
161 m.bJunk = map[string]struct{}{}
162 if m.IsJunk != nil {
163 junk := m.bJunk
164 for s, _ := range b2j {
165 if m.IsJunk(s) {
166 junk[s] = struct{}{}
167 }
168 }
169 for s, _ := range junk {
170 delete(b2j, s)
171 }
172 }
173
174
175 popular := map[string]struct{}{}
176 n := len(m.b)
177 if m.autoJunk && n >= 200 {
178 ntest := n/100 + 1
179 for s, indices := range b2j {
180 if len(indices) > ntest {
181 popular[s] = struct{}{}
182 }
183 }
184 for s, _ := range popular {
185 delete(b2j, s)
186 }
187 }
188 m.bPopular = popular
189 m.b2j = b2j
190 }
191
192 func (m *SequenceMatcher) isBJunk(s string) bool {
193 _, ok := m.bJunk[s]
194 return ok
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 func (m *SequenceMatcher) findLongestMatch(alo, ahi, blo, bhi int) Match {
222
223
224
225
226
227
228
229
230
231
232
233 besti, bestj, bestsize := alo, blo, 0
234
235
236
237
238 j2len := map[int]int{}
239 for i := alo; i != ahi; i++ {
240
241
242 newj2len := map[int]int{}
243 for _, j := range m.b2j[m.a[i]] {
244
245 if j < blo {
246 continue
247 }
248 if j >= bhi {
249 break
250 }
251 k := j2len[j-1] + 1
252 newj2len[j] = k
253 if k > bestsize {
254 besti, bestj, bestsize = i-k+1, j-k+1, k
255 }
256 }
257 j2len = newj2len
258 }
259
260
261
262
263
264 for besti > alo && bestj > blo && !m.isBJunk(m.b[bestj-1]) &&
265 m.a[besti-1] == m.b[bestj-1] {
266 besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
267 }
268 for besti+bestsize < ahi && bestj+bestsize < bhi &&
269 !m.isBJunk(m.b[bestj+bestsize]) &&
270 m.a[besti+bestsize] == m.b[bestj+bestsize] {
271 bestsize += 1
272 }
273
274
275
276
277
278
279
280
281 for besti > alo && bestj > blo && m.isBJunk(m.b[bestj-1]) &&
282 m.a[besti-1] == m.b[bestj-1] {
283 besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
284 }
285 for besti+bestsize < ahi && bestj+bestsize < bhi &&
286 m.isBJunk(m.b[bestj+bestsize]) &&
287 m.a[besti+bestsize] == m.b[bestj+bestsize] {
288 bestsize += 1
289 }
290
291 return Match{A: besti, B: bestj, Size: bestsize}
292 }
293
294
295
296
297
298
299
300
301
302
303
304
305 func (m *SequenceMatcher) GetMatchingBlocks() []Match {
306 if m.matchingBlocks != nil {
307 return m.matchingBlocks
308 }
309
310 var matchBlocks func(alo, ahi, blo, bhi int, matched []Match) []Match
311 matchBlocks = func(alo, ahi, blo, bhi int, matched []Match) []Match {
312 match := m.findLongestMatch(alo, ahi, blo, bhi)
313 i, j, k := match.A, match.B, match.Size
314 if match.Size > 0 {
315 if alo < i && blo < j {
316 matched = matchBlocks(alo, i, blo, j, matched)
317 }
318 matched = append(matched, match)
319 if i+k < ahi && j+k < bhi {
320 matched = matchBlocks(i+k, ahi, j+k, bhi, matched)
321 }
322 }
323 return matched
324 }
325 matched := matchBlocks(0, len(m.a), 0, len(m.b), nil)
326
327
328
329 nonAdjacent := []Match{}
330 i1, j1, k1 := 0, 0, 0
331 for _, b := range matched {
332
333 i2, j2, k2 := b.A, b.B, b.Size
334 if i1+k1 == i2 && j1+k1 == j2 {
335
336
337
338 k1 += k2
339 } else {
340
341
342
343 if k1 > 0 {
344 nonAdjacent = append(nonAdjacent, Match{i1, j1, k1})
345 }
346 i1, j1, k1 = i2, j2, k2
347 }
348 }
349 if k1 > 0 {
350 nonAdjacent = append(nonAdjacent, Match{i1, j1, k1})
351 }
352
353 nonAdjacent = append(nonAdjacent, Match{len(m.a), len(m.b), 0})
354 m.matchingBlocks = nonAdjacent
355 return m.matchingBlocks
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 func (m *SequenceMatcher) GetOpCodes() []OpCode {
374 if m.opCodes != nil {
375 return m.opCodes
376 }
377 i, j := 0, 0
378 matching := m.GetMatchingBlocks()
379 opCodes := make([]OpCode, 0, len(matching))
380 for _, m := range matching {
381
382
383
384
385
386 ai, bj, size := m.A, m.B, m.Size
387 tag := byte(0)
388 if i < ai && j < bj {
389 tag = 'r'
390 } else if i < ai {
391 tag = 'd'
392 } else if j < bj {
393 tag = 'i'
394 }
395 if tag > 0 {
396 opCodes = append(opCodes, OpCode{tag, i, ai, j, bj})
397 }
398 i, j = ai+size, bj+size
399
400
401 if size > 0 {
402 opCodes = append(opCodes, OpCode{'e', ai, i, bj, j})
403 }
404 }
405 m.opCodes = opCodes
406 return m.opCodes
407 }
408
409
410
411
412
413 func (m *SequenceMatcher) GetGroupedOpCodes(n int) [][]OpCode {
414 if n < 0 {
415 n = 3
416 }
417 codes := m.GetOpCodes()
418 if len(codes) == 0 {
419 codes = []OpCode{OpCode{'e', 0, 1, 0, 1}}
420 }
421
422 if codes[0].Tag == 'e' {
423 c := codes[0]
424 i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
425 codes[0] = OpCode{c.Tag, max(i1, i2-n), i2, max(j1, j2-n), j2}
426 }
427 if codes[len(codes)-1].Tag == 'e' {
428 c := codes[len(codes)-1]
429 i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
430 codes[len(codes)-1] = OpCode{c.Tag, i1, min(i2, i1+n), j1, min(j2, j1+n)}
431 }
432 nn := n + n
433 groups := [][]OpCode{}
434 group := []OpCode{}
435 for _, c := range codes {
436 i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
437
438
439 if c.Tag == 'e' && i2-i1 > nn {
440 group = append(group, OpCode{c.Tag, i1, min(i2, i1+n),
441 j1, min(j2, j1+n)})
442 groups = append(groups, group)
443 group = []OpCode{}
444 i1, j1 = max(i1, i2-n), max(j1, j2-n)
445 }
446 group = append(group, OpCode{c.Tag, i1, i2, j1, j2})
447 }
448 if len(group) > 0 && !(len(group) == 1 && group[0].Tag == 'e') {
449 groups = append(groups, group)
450 }
451 return groups
452 }
453
454
455
456
457
458
459
460
461
462
463
464
465 func (m *SequenceMatcher) Ratio() float64 {
466 matches := 0
467 for _, m := range m.GetMatchingBlocks() {
468 matches += m.Size
469 }
470 return calculateRatio(matches, len(m.a)+len(m.b))
471 }
472
473
474
475
476
477 func (m *SequenceMatcher) QuickRatio() float64 {
478
479
480
481 if m.fullBCount == nil {
482 m.fullBCount = map[string]int{}
483 for _, s := range m.b {
484 m.fullBCount[s] = m.fullBCount[s] + 1
485 }
486 }
487
488
489
490 avail := map[string]int{}
491 matches := 0
492 for _, s := range m.a {
493 n, ok := avail[s]
494 if !ok {
495 n = m.fullBCount[s]
496 }
497 avail[s] = n - 1
498 if n > 0 {
499 matches += 1
500 }
501 }
502 return calculateRatio(matches, len(m.a)+len(m.b))
503 }
504
505
506
507
508
509 func (m *SequenceMatcher) RealQuickRatio() float64 {
510 la, lb := len(m.a), len(m.b)
511 return calculateRatio(min(la, lb), la+lb)
512 }
513
514
515 func formatRangeUnified(start, stop int) string {
516
517 beginning := start + 1
518 length := stop - start
519 if length == 1 {
520 return fmt.Sprintf("%d", beginning)
521 }
522 if length == 0 {
523 beginning -= 1
524 }
525 return fmt.Sprintf("%d,%d", beginning, length)
526 }
527
528
529 type UnifiedDiff struct {
530 A []string
531 FromFile string
532 FromDate string
533 B []string
534 ToFile string
535 ToDate string
536 Eol string
537 Context int
538 }
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559 func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
560 buf := bufio.NewWriter(writer)
561 defer buf.Flush()
562 wf := func(format string, args ...interface{}) error {
563 _, err := buf.WriteString(fmt.Sprintf(format, args...))
564 return err
565 }
566 ws := func(s string) error {
567 _, err := buf.WriteString(s)
568 return err
569 }
570
571 if len(diff.Eol) == 0 {
572 diff.Eol = "\n"
573 }
574
575 started := false
576 m := NewMatcher(diff.A, diff.B)
577 for _, g := range m.GetGroupedOpCodes(diff.Context) {
578 if !started {
579 started = true
580 fromDate := ""
581 if len(diff.FromDate) > 0 {
582 fromDate = "\t" + diff.FromDate
583 }
584 toDate := ""
585 if len(diff.ToDate) > 0 {
586 toDate = "\t" + diff.ToDate
587 }
588 if diff.FromFile != "" || diff.ToFile != "" {
589 err := wf("--- %s%s%s", diff.FromFile, fromDate, diff.Eol)
590 if err != nil {
591 return err
592 }
593 err = wf("+++ %s%s%s", diff.ToFile, toDate, diff.Eol)
594 if err != nil {
595 return err
596 }
597 }
598 }
599 first, last := g[0], g[len(g)-1]
600 range1 := formatRangeUnified(first.I1, last.I2)
601 range2 := formatRangeUnified(first.J1, last.J2)
602 if err := wf("@@ -%s +%s @@%s", range1, range2, diff.Eol); err != nil {
603 return err
604 }
605 for _, c := range g {
606 i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
607 if c.Tag == 'e' {
608 for _, line := range diff.A[i1:i2] {
609 if err := ws(" " + line); err != nil {
610 return err
611 }
612 }
613 continue
614 }
615 if c.Tag == 'r' || c.Tag == 'd' {
616 for _, line := range diff.A[i1:i2] {
617 if err := ws("-" + line); err != nil {
618 return err
619 }
620 }
621 }
622 if c.Tag == 'r' || c.Tag == 'i' {
623 for _, line := range diff.B[j1:j2] {
624 if err := ws("+" + line); err != nil {
625 return err
626 }
627 }
628 }
629 }
630 }
631 return nil
632 }
633
634
635 func GetUnifiedDiffString(diff UnifiedDiff) (string, error) {
636 w := &bytes.Buffer{}
637 err := WriteUnifiedDiff(w, diff)
638 return string(w.Bytes()), err
639 }
640
641
642 func formatRangeContext(start, stop int) string {
643
644 beginning := start + 1
645 length := stop - start
646 if length == 0 {
647 beginning -= 1
648 }
649 if length <= 1 {
650 return fmt.Sprintf("%d", beginning)
651 }
652 return fmt.Sprintf("%d,%d", beginning, beginning+length-1)
653 }
654
655 type ContextDiff UnifiedDiff
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674 func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
675 buf := bufio.NewWriter(writer)
676 defer buf.Flush()
677 var diffErr error
678 wf := func(format string, args ...interface{}) {
679 _, err := buf.WriteString(fmt.Sprintf(format, args...))
680 if diffErr == nil && err != nil {
681 diffErr = err
682 }
683 }
684 ws := func(s string) {
685 _, err := buf.WriteString(s)
686 if diffErr == nil && err != nil {
687 diffErr = err
688 }
689 }
690
691 if len(diff.Eol) == 0 {
692 diff.Eol = "\n"
693 }
694
695 prefix := map[byte]string{
696 'i': "+ ",
697 'd': "- ",
698 'r': "! ",
699 'e': " ",
700 }
701
702 started := false
703 m := NewMatcher(diff.A, diff.B)
704 for _, g := range m.GetGroupedOpCodes(diff.Context) {
705 if !started {
706 started = true
707 fromDate := ""
708 if len(diff.FromDate) > 0 {
709 fromDate = "\t" + diff.FromDate
710 }
711 toDate := ""
712 if len(diff.ToDate) > 0 {
713 toDate = "\t" + diff.ToDate
714 }
715 if diff.FromFile != "" || diff.ToFile != "" {
716 wf("*** %s%s%s", diff.FromFile, fromDate, diff.Eol)
717 wf("--- %s%s%s", diff.ToFile, toDate, diff.Eol)
718 }
719 }
720
721 first, last := g[0], g[len(g)-1]
722 ws("***************" + diff.Eol)
723
724 range1 := formatRangeContext(first.I1, last.I2)
725 wf("*** %s ****%s", range1, diff.Eol)
726 for _, c := range g {
727 if c.Tag == 'r' || c.Tag == 'd' {
728 for _, cc := range g {
729 if cc.Tag == 'i' {
730 continue
731 }
732 for _, line := range diff.A[cc.I1:cc.I2] {
733 ws(prefix[cc.Tag] + line)
734 }
735 }
736 break
737 }
738 }
739
740 range2 := formatRangeContext(first.J1, last.J2)
741 wf("--- %s ----%s", range2, diff.Eol)
742 for _, c := range g {
743 if c.Tag == 'r' || c.Tag == 'i' {
744 for _, cc := range g {
745 if cc.Tag == 'd' {
746 continue
747 }
748 for _, line := range diff.B[cc.J1:cc.J2] {
749 ws(prefix[cc.Tag] + line)
750 }
751 }
752 break
753 }
754 }
755 }
756 return diffErr
757 }
758
759
760 func GetContextDiffString(diff ContextDiff) (string, error) {
761 w := &bytes.Buffer{}
762 err := WriteContextDiff(w, diff)
763 return string(w.Bytes()), err
764 }
765
766
767
768 func SplitLines(s string) []string {
769 lines := strings.SplitAfter(s, "\n")
770 lines[len(lines)-1] += "\n"
771 return lines
772 }
773
View as plain text