File: base_string.go

package info (click to toggle)
golang-github-poy-onpar 0.3.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 456 kB
  • sloc: makefile: 3
file content (53 lines) | stat: -rw-r--r-- 976 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package diff

import "github.com/poy/onpar/diff/str"

type baseDiff struct {
	actual, expected []rune

	sections []str.DiffSection
	cost     float64
}

func (d *baseDiff) equal() bool {
	if len(d.actual) != len(d.expected) {
		return false
	}
	for i, ar := range d.actual {
		if ar != d.expected[i] {
			return false
		}
	}
	return true
}

func (d *baseDiff) calculate() {
	if d.sections != nil {
		return
	}
	d.sections = []str.DiffSection{{Actual: d.actual, Expected: d.expected}}
	if d.equal() {
		d.sections[0].Type = str.TypeMatch
		return
	}
	d.sections[0].Type = str.TypeReplace
	if len(d.actual) > len(d.expected) {
		d.cost = float64(len(d.actual))
		return
	}
	d.cost = float64(len(d.expected))
}

func (d *baseDiff) Cost() float64 {
	d.calculate()
	return d.cost
}

func (d *baseDiff) Sections() []str.DiffSection {
	d.calculate()
	return d.sections
}

func baseStringDiff(actual, expected []rune) str.Diff {
	return &baseDiff{actual: actual, expected: expected}
}