@@ -24,11 +24,10 @@ // Forward declaration as it is required for recursion var write func(data []byte) error write = func(data []byte) error { var ( - err error blockSize int = 8 bufferLength int = len(ctx.input) ) // Force a flush if we are called with no data to write @@ -57,19 +56,19 @@ } // The current buffer + new data overflow the block size // Complete the block, flush it ... ctx.input = append(ctx.input, data[:blockSize-bufferLength]...) - err = write(nil) - if err != nil { + if err := write(nil); err != nil { return err } // ... and stage the rest of the data in the buffer ctx.input = append(ctx.input, data[blockSize-bufferLength:]...) return nil } + // TODO allocate this on ctx.buffer ... var buf []byte = make([]byte, 1, blockSize+1) for block := 0; block < len(data)/blockSize; block++ { for i := 0; i < blockSize; i++ { var current byte = data[(block*blockSize)+i] if ctx.table[ctx.hash] == current { @@ -81,22 +80,19 @@ buf = append(buf, current) } ctx.hash = (ctx.hash << 4) ^ uint16(current) } - _, err = writer.Write(buf) - if err != nil { + if _, err := writer.Write(buf); err != nil { return err } // Reset the flags and buffer for the next iteration - buf[0] ^= buf[0] - buf = buf[:1] + buf, buf[0] = buf[:1], 0 } - var remaining int = len(data) % blockSize - if remaining > 0 { + if remaining := len(data) % blockSize; remaining > 0 { ctx.input = ctx.buffer[:remaining] copy(ctx.input, data[len(data)-remaining:]) } else { ctx.input = ctx.buffer[:0] }