You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
905 B

  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. package flate
  5. // forwardCopy is like the built-in copy function except that it always goes
  6. // forward from the start, even if the dst and src overlap.
  7. // It is equivalent to:
  8. // for i := 0; i < n; i++ {
  9. // mem[dst+i] = mem[src+i]
  10. // }
  11. func forwardCopy(mem []byte, dst, src, n int) {
  12. if dst <= src {
  13. copy(mem[dst:dst+n], mem[src:src+n])
  14. return
  15. }
  16. for {
  17. if dst >= src+n {
  18. copy(mem[dst:dst+n], mem[src:src+n])
  19. return
  20. }
  21. // There is some forward overlap. The destination
  22. // will be filled with a repeated pattern of mem[src:src+k].
  23. // We copy one instance of the pattern here, then repeat.
  24. // Each time around this loop k will double.
  25. k := dst - src
  26. copy(mem[dst:dst+k], mem[src:src+k])
  27. n -= k
  28. dst += k
  29. }
  30. }