byteview.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. Copyright 2012 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package groupcache
  14. import (
  15. "bytes"
  16. "errors"
  17. "io"
  18. "strings"
  19. )
  20. // A ByteView holds an immutable view of bytes.
  21. // Internally it wraps either a []byte or a string,
  22. // but that detail is invisible to callers.
  23. //
  24. // A ByteView is meant to be used as a value type, not
  25. // a pointer (like a time.Time).
  26. type ByteView struct {
  27. // If b is non-nil, b is used, else s is used.
  28. b []byte
  29. s string
  30. }
  31. // Len returns the view's length.
  32. func (v ByteView) Len() int {
  33. if v.b != nil {
  34. return len(v.b)
  35. }
  36. return len(v.s)
  37. }
  38. // ByteSlice returns a copy of the data as a byte slice.
  39. func (v ByteView) ByteSlice() []byte {
  40. if v.b != nil {
  41. return cloneBytes(v.b)
  42. }
  43. return []byte(v.s)
  44. }
  45. // String returns the data as a string, making a copy if necessary.
  46. func (v ByteView) String() string {
  47. if v.b != nil {
  48. return string(v.b)
  49. }
  50. return v.s
  51. }
  52. // At returns the byte at index i.
  53. func (v ByteView) At(i int) byte {
  54. if v.b != nil {
  55. return v.b[i]
  56. }
  57. return v.s[i]
  58. }
  59. // Slice slices the view between the provided from and to indices.
  60. func (v ByteView) Slice(from, to int) ByteView {
  61. if v.b != nil {
  62. return ByteView{b: v.b[from:to]}
  63. }
  64. return ByteView{s: v.s[from:to]}
  65. }
  66. // SliceFrom slices the view from the provided index until the end.
  67. func (v ByteView) SliceFrom(from int) ByteView {
  68. if v.b != nil {
  69. return ByteView{b: v.b[from:]}
  70. }
  71. return ByteView{s: v.s[from:]}
  72. }
  73. // Copy copies b into dest and returns the number of bytes copied.
  74. func (v ByteView) Copy(dest []byte) int {
  75. if v.b != nil {
  76. return copy(dest, v.b)
  77. }
  78. return copy(dest, v.s)
  79. }
  80. // Equal returns whether the bytes in b are the same as the bytes in
  81. // b2.
  82. func (v ByteView) Equal(b2 ByteView) bool {
  83. if b2.b == nil {
  84. return v.EqualString(b2.s)
  85. }
  86. return v.EqualBytes(b2.b)
  87. }
  88. // EqualString returns whether the bytes in b are the same as the bytes
  89. // in s.
  90. func (v ByteView) EqualString(s string) bool {
  91. if v.b == nil {
  92. return v.s == s
  93. }
  94. l := v.Len()
  95. if len(s) != l {
  96. return false
  97. }
  98. for i, bi := range v.b {
  99. if bi != s[i] {
  100. return false
  101. }
  102. }
  103. return true
  104. }
  105. // EqualBytes returns whether the bytes in b are the same as the bytes
  106. // in b2.
  107. func (v ByteView) EqualBytes(b2 []byte) bool {
  108. if v.b != nil {
  109. return bytes.Equal(v.b, b2)
  110. }
  111. l := v.Len()
  112. if len(b2) != l {
  113. return false
  114. }
  115. for i, bi := range b2 {
  116. if bi != v.s[i] {
  117. return false
  118. }
  119. }
  120. return true
  121. }
  122. // Reader returns an io.ReadSeeker for the bytes in v.
  123. func (v ByteView) Reader() io.ReadSeeker {
  124. if v.b != nil {
  125. return bytes.NewReader(v.b)
  126. }
  127. return strings.NewReader(v.s)
  128. }
  129. // ReadAt implements io.ReaderAt on the bytes in v.
  130. func (v ByteView) ReadAt(p []byte, off int64) (n int, err error) {
  131. if off < 0 {
  132. return 0, errors.New("view: invalid offset")
  133. }
  134. if off >= int64(v.Len()) {
  135. return 0, io.EOF
  136. }
  137. n = v.SliceFrom(int(off)).Copy(p)
  138. if n < len(p) {
  139. err = io.EOF
  140. }
  141. return
  142. }