operation.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2014-2022 Ulrich Kunitz. 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 lzma
  5. import (
  6. "fmt"
  7. "unicode"
  8. )
  9. // operation represents an operation on the dictionary during encoding or
  10. // decoding.
  11. type operation interface {
  12. Len() int
  13. }
  14. // rep represents a repetition at the given distance and the given length
  15. type match struct {
  16. // supports all possible distance values, including the eos marker
  17. distance int64
  18. // length
  19. n int
  20. }
  21. // Len returns the number of bytes matched.
  22. func (m match) Len() int {
  23. return m.n
  24. }
  25. // String returns a string representation for the repetition.
  26. func (m match) String() string {
  27. return fmt.Sprintf("M{%d,%d}", m.distance, m.n)
  28. }
  29. // lit represents a single byte literal.
  30. type lit struct {
  31. b byte
  32. }
  33. // Len returns 1 for the single byte literal.
  34. func (l lit) Len() int {
  35. return 1
  36. }
  37. // String returns a string representation for the literal.
  38. func (l lit) String() string {
  39. var c byte
  40. if unicode.IsPrint(rune(l.b)) {
  41. c = l.b
  42. } else {
  43. c = '.'
  44. }
  45. return fmt.Sprintf("L{%c/%02x}", c, l.b)
  46. }