diff --git a/_test/append1.go b/_test/append1.go new file mode 100644 index 00000000..6111264f --- /dev/null +++ b/_test/append1.go @@ -0,0 +1,15 @@ +package main + +import ( + "bufio" + "bytes" +) + +func main() { + s := bufio.NewScanner(bytes.NewReader([]byte("Hello\nTest\nLine3"))) + s.Scan() + println(string(append(s.Bytes(), []byte(" World")...))) +} + +// Output: +// Hello World diff --git a/_test/append2.go b/_test/append2.go new file mode 100644 index 00000000..4aae3858 --- /dev/null +++ b/_test/append2.go @@ -0,0 +1,15 @@ +package main + +import ( + "bufio" + "bytes" +) + +func main() { + s := bufio.NewScanner(bytes.NewReader([]byte("Hello\nTest\nLine3"))) + s.Scan() + println(string(append(s.Bytes(), " World"...))) +} + +// Output: +// Hello World diff --git a/_test/issue-880.go b/_test/issue-880.go new file mode 100644 index 00000000..8403fa96 --- /dev/null +++ b/_test/issue-880.go @@ -0,0 +1,23 @@ +package main + +import ( + "bufio" + "bytes" +) + +func main() { + var buf1 = make([]byte, 1024) + var buf2 []byte + buf1 = []byte("Hallo\nTest\nLine3") + + s := bufio.NewScanner(bytes.NewReader(buf1)) + for s.Scan() { + buf2 = append(buf2, append(s.Bytes(), []byte("\n")...)...) + } + print(string(buf2)) +} + +// Output: +// Hallo +// Test +// Line3 diff --git a/interp/run.go b/interp/run.go index 70fa1d36..c5a82cce 100644 --- a/interp/run.go +++ b/interp/run.go @@ -2430,11 +2430,16 @@ func appendSlice(n *node) { } func _append(n *node) { - if c1, c2 := n.child[1], n.child[2]; len(n.child) == 3 && c2.typ.cat == arrayT && c2.typ.val.id() == n.typ.val.id() || - isByteArray(c1.typ.TypeOf()) && isString(c2.typ.TypeOf()) { - appendSlice(n) - return + if len(n.child) == 3 { + c1, c2 := n.child[1], n.child[2] + if (c1.typ.cat == valueT || c2.typ.cat == valueT) && c1.typ.rtype == c2.typ.rtype || + c2.typ.cat == arrayT && c2.typ.val.id() == n.typ.val.id() || + isByteArray(c1.typ.TypeOf()) && isString(c2.typ.TypeOf()) { + appendSlice(n) + return + } } + dest := genValueOutput(n, n.typ.rtype) value := genValue(n.child[1]) next := getExec(n.tnext)