2 Copyright 2012 Google Inc.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
26 // A ByteView holds an immutable view of bytes.
27 // Internally it wraps either a []byte or a string,
28 // but that detail is invisible to callers.
30 // A ByteView is meant to be used as a value type, not
31 // a pointer (like a time.Time).
32 type ByteView struct {
33 // If b is non-nil, b is used, else s is used.
38 // Len returns the view's length.
39 func (v ByteView) Len() int {
46 // ByteSlice returns a copy of the data as a byte slice.
47 func (v ByteView) ByteSlice() []byte {
49 return cloneBytes(v.b)
54 // String returns the data as a string, making a copy if necessary.
55 func (v ByteView) String() string {
62 // At returns the byte at index i.
63 func (v ByteView) At(i int) byte {
70 // Slice slices the view between the provided from and to indices.
71 func (v ByteView) Slice(from, to int) ByteView {
73 return ByteView{b: v.b[from:to]}
75 return ByteView{s: v.s[from:to]}
78 // SliceFrom slices the view from the provided index until the end.
79 func (v ByteView) SliceFrom(from int) ByteView {
81 return ByteView{b: v.b[from:]}
83 return ByteView{s: v.s[from:]}
86 // Copy copies b into dest and returns the number of bytes copied.
87 func (v ByteView) Copy(dest []byte) int {
89 return copy(dest, v.b)
91 return copy(dest, v.s)
94 // Equal returns whether the bytes in b are the same as the bytes in
96 func (v ByteView) Equal(b2 ByteView) bool {
98 return v.EqualString(b2.s)
100 return v.EqualBytes(b2.b)
103 // EqualString returns whether the bytes in b are the same as the bytes
105 func (v ByteView) EqualString(s string) bool {
113 for i, bi := range v.b {
121 // EqualBytes returns whether the bytes in b are the same as the bytes
123 func (v ByteView) EqualBytes(b2 []byte) bool {
125 return bytes.Equal(v.b, b2)
131 for i, bi := range b2 {
139 // Reader returns an io.ReadSeeker for the bytes in v.
140 func (v ByteView) Reader() io.ReadSeeker {
142 return bytes.NewReader(v.b)
144 return strings.NewReader(v.s)
147 // ReadAt implements io.ReaderAt on the bytes in v.
148 func (v ByteView) ReadAt(p []byte, off int64) (n int, err error) {
150 return 0, errors.New("view: invalid offset")
152 if off >= int64(v.Len()) {
155 n = v.SliceFrom(int(off)).Copy(p)
162 // WriteTo implements io.WriterTo on the bytes in v.
163 func (v ByteView) WriteTo(w io.Writer) (n int64, err error) {
166 m, err = w.Write(v.b)
168 m, err = io.WriteString(w, v.s)
170 if err == nil && m < v.Len() {
171 err = io.ErrShortWrite