diff --git a/byteview.go b/byteview.go index 0e9584b..73c8337 100644 --- a/byteview.go +++ b/byteview.go @@ -29,6 +29,7 @@ import ( // // A ByteView is meant to be used as a value type, not // a pointer (like a time.Time). +// 封装了一个string与byte[]的统一接口,也就是说用byteview提供的接口,可以屏蔽掉string与byte[]的不同,使用时可以不用考虑是string还是byte[] type ByteView struct { // If b is non-nil, b is used, else s is used. // 当b是nil 的时候,s作为存储内容的容器 @@ -45,6 +46,7 @@ func (v ByteView) Len() int { } // ByteSlice returns a copy of the data as a byte slice. +// 按[]byte返回一个拷贝 func (v ByteView) ByteSlice() []byte { if v.b != nil { return cloneBytes(v.b) @@ -53,6 +55,7 @@ func (v ByteView) ByteSlice() []byte { } // String returns the data as a string, making a copy if necessary. +// 按string返回一个拷贝 func (v ByteView) String() string { if v.b != nil { return string(v.b) @@ -61,6 +64,7 @@ func (v ByteView) String() string { } // At returns the byte at index i. +// 返回第i个byte func (v ByteView) At(i int) byte { if v.b != nil { return v.b[i] @@ -69,6 +73,7 @@ func (v ByteView) At(i int) byte { } // Slice slices the view between the provided from and to indices. +// 返回ByteView的某个片断,不拷贝 func (v ByteView) Slice(from, to int) ByteView { if v.b != nil { return ByteView{b: v.b[from:to]} @@ -77,6 +82,7 @@ func (v ByteView) Slice(from, to int) ByteView { } // SliceFrom slices the view from the provided index until the end. +// 返回ByteView的从某个位置开始的片断,不拷贝 func (v ByteView) SliceFrom(from int) ByteView { if v.b != nil { return ByteView{b: v.b[from:]} @@ -85,6 +91,7 @@ func (v ByteView) SliceFrom(from int) ByteView { } // Copy copies b into dest and returns the number of bytes copied. +// 将ByteView按[]byte拷贝出来 func (v ByteView) Copy(dest []byte) int { if v.b != nil { return copy(dest, v.b) @@ -92,8 +99,8 @@ func (v ByteView) Copy(dest []byte) int { return copy(dest, v.s) } -// Equal returns whether the bytes in b are the same as the bytes in -// b2. +// Equal returns whether the bytes in b are the same as the bytes in b2. +// 判断2个ByteView是否相等 func (v ByteView) Equal(b2 ByteView) bool { if b2.b == nil { return v.EqualString(b2.s) @@ -101,8 +108,8 @@ func (v ByteView) Equal(b2 ByteView) bool { return v.EqualBytes(b2.b) } -// EqualString returns whether the bytes in b are the same as the bytes -// in s. +// EqualString returns whether the bytes in b are the same as the bytes in s. +// 判断ByteView是否和string相等 func (v ByteView) EqualString(s string) bool { if v.b == nil { return v.s == s @@ -121,6 +128,7 @@ func (v ByteView) EqualString(s string) bool { // EqualBytes returns whether the bytes in b are the same as the bytes // in b2. +// 判断ByteView是否和[]byte相等 func (v ByteView) EqualBytes(b2 []byte) bool { if v.b != nil { return bytes.Equal(v.b, b2) @@ -138,6 +146,7 @@ func (v ByteView) EqualBytes(b2 []byte) bool { } // Reader returns an io.ReadSeeker for the bytes in v. +// 对ByteView创建一个io.ReadSeeker func (v ByteView) Reader() io.ReadSeeker { if v.b != nil { return bytes.NewReader(v.b) @@ -146,6 +155,7 @@ func (v ByteView) Reader() io.ReadSeeker { } // ReadAt implements io.ReaderAt on the bytes in v. +// 读取从off开始的后面的数据,其实下面调用的SliceFrom,这是封装成了io.Reader的一个ReadAt方法的形式 func (v ByteView) ReadAt(p []byte, off int64) (n int, err error) { if off < 0 { return 0, errors.New("view: invalid offset") @@ -161,6 +171,7 @@ func (v ByteView) ReadAt(p []byte, off int64) (n int, err error) { } // WriteTo implements io.WriterTo on the bytes in v. +// 向w流中写入v func (v ByteView) WriteTo(w io.Writer) (n int64, err error) { var m int if v.b != nil { diff --git a/sinks.go b/sinks.go index 0f8877c..b14cfc7 100644 --- a/sinks.go +++ b/sinks.go @@ -212,10 +212,12 @@ func (s *protoSink) SetProto(m proto.Message) error { // AllocatingByteSliceSink returns a Sink that allocates // a byte slice to hold the received value and assigns // it to *dst. The memory is not retained by groupcache. +// 分配一个字节切片来保存接收到的数据 func AllocatingByteSliceSink(dst *[]byte) Sink { return &allocBytesSink{dst: dst} } +// 有一个字节切片指针类型的属性dst type allocBytesSink struct { dst *[]byte v ByteView @@ -225,6 +227,7 @@ func (s *allocBytesSink) view() (ByteView, error) { return s.v, nil } +// 设置allocBytesSink的v,同时复制v中的b或者s丢给dst func (s *allocBytesSink) setView(v ByteView) error { if v.b != nil { *s.dst = cloneBytes(v.b) @@ -235,6 +238,7 @@ func (s *allocBytesSink) setView(v ByteView) error { return nil } +// 这个得从下面的setBytesOwned开始往上看 func (s *allocBytesSink) SetProto(m proto.Message) error { b, err := proto.Marshal(m) if err != nil { @@ -243,10 +247,12 @@ func (s *allocBytesSink) SetProto(m proto.Message) error { return s.setBytesOwned(b) } +// 复制一份b,然后调用setBytesOwned func (s *allocBytesSink) SetBytes(b []byte) error { return s.setBytesOwned(cloneBytes(b)) } +// 使用b设置allocBytesSink的dst和ByteView func (s *allocBytesSink) setBytesOwned(b []byte) error { if s.dst == nil { return errors.New("nil AllocatingByteSliceSink *[]byte dst") @@ -257,6 +263,7 @@ func (s *allocBytesSink) setBytesOwned(b []byte) error { return nil } +// 字符串转成[]byte后进行和上面类似的操作 func (s *allocBytesSink) SetString(v string) error { if s.dst == nil { return errors.New("nil AllocatingByteSliceSink *[]byte dst")