func BenchmarkDiffByteStrings(b *testing.B) { d1 := "lorem ipsum dolor sit amet consectetur" d2 := "lorem lovesum daenerys targaryen ami consecteture" for i := 0; i < b.N; i++ { diff.ByteStrings(d1, d2) } }
func ExampleGranular() { a := "hElLo!" b := "hello!" changes := diff.Granular(5, diff.ByteStrings(a, b)) // ignore small gaps in differences for l := len(changes) - 1; l >= 0; l-- { change := changes[l] b = b[:change.B] + "|" + b[change.B:change.B+change.Ins] + "|" + b[change.B+change.Ins:] } fmt.Println(b) // Output: // h|ell|o! }
func TestDiffByteStrings(t *testing.T) { a := "brown fox jumps over the lazy dog" b := "brwn faax junps ovver the lay dago" res := diff.ByteStrings(a, b) echange := []diff.Change{ {2, 2, 1, 0}, {7, 6, 1, 2}, {12, 12, 1, 1}, {18, 18, 0, 1}, {27, 28, 1, 0}, {31, 31, 0, 2}, {32, 34, 1, 0}, } for i, c := range res { t.Log(c) if c != echange[i] { t.Error("expected", echange[i], "got", c) } } }
func TestGranularStrings(t *testing.T) { a := "abcdefghijklmnopqrstuvwxyza" b := "AbCdeFghiJklmnOpqrstUvwxyzab" // each iteration of i increases granularity and will absorb one more lower-letter-followed-by-upper-letters sequence changesI := [][]diff.Change{ {{0, 0, 1, 1}, {2, 2, 1, 1}, {5, 5, 1, 1}, {9, 9, 1, 1}, {14, 14, 1, 1}, {20, 20, 1, 1}, {27, 27, 0, 1}}, {{0, 0, 3, 3}, {5, 5, 1, 1}, {9, 9, 1, 1}, {14, 14, 1, 1}, {20, 20, 1, 1}, {27, 27, 0, 1}}, {{0, 0, 6, 6}, {9, 9, 1, 1}, {14, 14, 1, 1}, {20, 20, 1, 1}, {27, 27, 0, 1}}, {{0, 0, 10, 10}, {14, 14, 1, 1}, {20, 20, 1, 1}, {27, 27, 0, 1}}, {{0, 0, 15, 15}, {20, 20, 1, 1}, {27, 27, 0, 1}}, {{0, 0, 21, 21}, {27, 27, 0, 1}}, {{0, 0, 27, 28}}, } for i := 0; i < len(changesI); i++ { diffs := diff.Granular(i, diff.ByteStrings(a, b)) if !diffsEqual(diffs, changesI[i]) { t.Errorf("expected %v, got %v", diffs, changesI[i]) } } }