示例#1
0
文件: godiff.go 项目: postfix/go-diff
func (sf *stringFrag) calcDiff(that diffFragment) int {
	switch g := that.(type) {
	case *stringFrag:
		s1, s2 := strings.TrimSpace(sf.source), strings.TrimSpace(g.source)
		if len(s1)+len(s2) == 0 {
			return 0
		} // if
		wt := sf.weight + g.weight
		return ed.String(s1, s2) * wt / mathp.MaxI(len(s1), len(s2))
	} // switch

	return sf.Weight() + that.Weight()
}
示例#2
0
func AssertStringsEqual(t *testing.T, name string, act, exp []string) {
	if villa.StringSlice(exp).Equals(act) {
		return
	}
	t.Errorf("%s unexpected(exp: %d lines, act %d lines)!", name, len(exp), len(act))
	t.Logf("exp ---  act +++")
	t.Logf("Difference:")
	_, matA, matB := ed.EditDistanceFFull(len(exp), len(act), func(iA, iB int) int {
		sa, sb := exp[iA], act[iB]
		if sa == sb {
			return 0
		}
		return ed.String(sa, sb)
	}, func(iA int) int {
		return len(exp[iA]) + 1
	}, func(iB int) int {
		return len(act[iB]) + 1
	})
	for i, j := 0, 0; i < len(exp) || j < len(act); {
		switch {
		case j >= len(act) || i < len(exp) && matA[i] < 0:
			t.Logf("--- %3d: %s", i+1, showText(exp[i]))
			i++
		case i >= len(exp) || j < len(act) && matB[j] < 0:
			t.Logf("+++ %3d: %s", j+1, showText(act[j]))
			j++
		default:
			if exp[i] != act[j] {
				t.Logf("--- %3d: %s", i+1, showText(exp[i]))
				t.Logf("+++ %3d: %s", j+1, showText(act[j]))
			} // else
			i++
			j++
		}
	} // for i, j
}
示例#3
0
文件: tm.go 项目: kortschak/go-diff
func DiffOfStrings(a, b string, mx int) int {
	if a == b {
		return 0
	} // if
	return ed.String(a, b) * mx / max(len(a), len(b))
}