github.com/aloncn/graphics-go@v0.0.1/src/compress/flate/copy.go (about)

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package flate
     6  
     7  // forwardCopy is like the built-in copy function except that it always goes
     8  // forward from the start, even if the dst and src overlap.
     9  // It is equivalent to:
    10  //   for i := 0; i < n; i++ {
    11  //     mem[dst+i] = mem[src+i]
    12  //   }
    13  func forwardCopy(mem []byte, dst, src, n int) {
    14  	if dst <= src {
    15  		copy(mem[dst:dst+n], mem[src:src+n])
    16  		return
    17  	}
    18  	for {
    19  		if dst >= src+n {
    20  			copy(mem[dst:dst+n], mem[src:src+n])
    21  			return
    22  		}
    23  		// There is some forward overlap.  The destination
    24  		// will be filled with a repeated pattern of mem[src:src+k].
    25  		// We copy one instance of the pattern here, then repeat.
    26  		// Each time around this loop k will double.
    27  		k := dst - src
    28  		copy(mem[dst:dst+k], mem[src:src+k])
    29  		n -= k
    30  		dst += k
    31  	}
    32  }