From 4b6c9b539466dbec8b0b8cde1f1e2aa67677f6e9 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Sun, 14 Jun 2015 17:34:02 +0200 Subject: Change usage of copy() Copy copies the smaller of len(src) and len(dst). We can leverage this behaviour to avoid using extra elses. --- unxml.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/unxml.go b/unxml.go index fee5690..a312410 100644 --- a/unxml.go +++ b/unxml.go @@ -164,17 +164,14 @@ func (r *ElementReader) Read(out []byte) (int, error) { continue } text := r.xr.tokenizer.Text() - lentext := len(text) - if lentext < lenout { - //fmt.Printf("HAD SPACE: %q, count: %d\n", text, r.xr.count) - n = copy(out[r.xr.count:], text) - r.xr.count += n - lenout -= n - } else { + //fmt.Printf("HAD SPACE: %q, count: %d\n", text, r.xr.count) + n = copy(out[r.xr.count:], text) + r.xr.count += n + lenout -= n + if n < len(text) { //fmt.Printf("HAD NO SPACE: wrote: %q, count: %d, lenout: %d\n", text[:lenout], r.xr.count, lenout) - n = copy(out[r.xr.count:], text[:lenout]) - r.xr.count += n - r.xr.lastread = text[lenout:] + r.xr.lastread = text[n:] + //fmt.Printf("lastread is now: %q\n", text[lenout:]) return r.xr.count, err } -- cgit v1.2.1-18-gbd029