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)
}