Ubuntu Pastebin

Paste from Horacio DurĂ¡n at Thu, 9 Mar 2017 18:08:11 +0000

Download as text
 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
)

type diffBuffer struct {
	buffer []string
}

func (db *diffBuffer) Add(s string) {
	db.buffer = append(db.buffer, s)
}

func parse(input string) ([]string, []string) {
	obtained := diffBuffer{[]string{}}
	expected := diffBuffer{[]string{}}
	buffer := &obtained
	for _, l := range strings.Split(input, "\n") {
		if strings.HasPrefix(l, "... obtained string =") {
			continue
		}
		if strings.HasPrefix(l, "... expected string =") {
			buffer = &expected
			continue
		}
		buffer.Add(l)
	}
	return obtained.buffer, expected.buffer
}

func findMismatch(obtained, expected []string) (string, string, int) {
	lo := len(obtained)
	le := len(expected)
	l := lo
	if le < l {
		l = le
	}
	for i := 0; i < l; i++ {
		if obtained[i] != expected[i] {
			return obtained[i], expected[i], i
		}
	}
	if lo > le {
		return obtained[le], "", le
	}
	if lo < le {
		return expected[lo], "", lo
	}
	return "", "", -1
}

func main() {
	data, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		log.Fatal(err)
	}
	val, pat := parse(string(data))
	o, e, i := findMismatch(val, pat)
	if i < 0 {
		fmt.Print("no diff\n")
		return
	}
	fmt.Printf("at possition %d\n", i)
	fmt.Printf("obtained:\n%s\n", o)
	fmt.Printf("expected:\n%s", e)
}
Download as text