Improve function naming

This commit is contained in:
Daniel 2022-08-02 09:26:37 +02:00
parent 01c4fc75da
commit 1444756302
2 changed files with 14 additions and 14 deletions

View file

@ -127,7 +127,7 @@ func (c *Container) CompileData() []byte {
// Get returns the given amount of bytes. Data MAY be copied and IS consumed. // Get returns the given amount of bytes. Data MAY be copied and IS consumed.
func (c *Container) Get(n int) ([]byte, error) { func (c *Container) Get(n int) ([]byte, error) {
buf := c.Gather(n) buf := c.Peek(n)
if len(buf) < n { if len(buf) < n {
return nil, errors.New("container: not enough data to return") return nil, errors.New("container: not enough data to return")
} }
@ -138,14 +138,14 @@ func (c *Container) Get(n int) ([]byte, error) {
// GetAll returns all data. Data MAY be copied and IS consumed. // GetAll returns all data. Data MAY be copied and IS consumed.
func (c *Container) GetAll() []byte { func (c *Container) GetAll() []byte {
// TODO: Improve. // TODO: Improve.
buf := c.Gather(c.Length()) buf := c.Peek(c.Length())
c.skip(len(buf)) c.skip(len(buf))
return buf return buf
} }
// GetAsContainer returns the given amount of bytes in a new container. Data will NOT be copied and IS consumed. // GetAsContainer returns the given amount of bytes in a new container. Data will NOT be copied and IS consumed.
func (c *Container) GetAsContainer(n int) (*Container, error) { func (c *Container) GetAsContainer(n int) (*Container, error) {
newC := c.GatherAsContainer(n) newC := c.PeekContainer(n)
if newC == nil { if newC == nil {
return nil, errors.New("container: not enough data to return") return nil, errors.New("container: not enough data to return")
} }
@ -155,7 +155,7 @@ func (c *Container) GetAsContainer(n int) (*Container, error) {
// GetMax returns as much as possible, but the given amount of bytes at maximum. Data MAY be copied and IS consumed. // GetMax returns as much as possible, but the given amount of bytes at maximum. Data MAY be copied and IS consumed.
func (c *Container) GetMax(n int) []byte { func (c *Container) GetMax(n int) []byte {
buf := c.Gather(n) buf := c.Peek(n)
c.skip(len(buf)) c.skip(len(buf))
return buf return buf
} }
@ -233,8 +233,8 @@ func (c *Container) PrependLength() {
c.Prepend(varint.Pack64(uint64(c.Length()))) c.Prepend(varint.Pack64(uint64(c.Length())))
} }
// Gather returns the given amount of bytes. Data MAY be copied and IS NOT consumed. // Peek returns the given amount of bytes. Data MAY be copied and IS NOT consumed.
func (c *Container) Gather(n int) []byte { func (c *Container) Peek(n int) []byte {
// Check requested length. // Check requested length.
if n <= 0 { if n <= 0 {
return nil return nil
@ -261,8 +261,8 @@ func (c *Container) Gather(n int) []byte {
return slice[:n] return slice[:n]
} }
// GatherAsContainer returns the given amount of bytes in a new container. Data will NOT be copied and IS NOT consumed. // PeekContainer returns the given amount of bytes in a new container. Data will NOT be copied and IS NOT consumed.
func (c *Container) GatherAsContainer(n int) (newC *Container) { func (c *Container) PeekContainer(n int) (newC *Container) {
// Check requested length. // Check requested length.
if n < 0 { if n < 0 {
return nil return nil
@ -325,7 +325,7 @@ func (c *Container) GetNextBlockAsContainer() (*Container, error) {
// GetNextN8 parses and returns a varint of type uint8. // GetNextN8 parses and returns a varint of type uint8.
func (c *Container) GetNextN8() (uint8, error) { func (c *Container) GetNextN8() (uint8, error) {
buf := c.Gather(2) buf := c.Peek(2)
num, n, err := varint.Unpack8(buf) num, n, err := varint.Unpack8(buf)
if err != nil { if err != nil {
return 0, err return 0, err
@ -336,7 +336,7 @@ func (c *Container) GetNextN8() (uint8, error) {
// GetNextN16 parses and returns a varint of type uint16. // GetNextN16 parses and returns a varint of type uint16.
func (c *Container) GetNextN16() (uint16, error) { func (c *Container) GetNextN16() (uint16, error) {
buf := c.Gather(3) buf := c.Peek(3)
num, n, err := varint.Unpack16(buf) num, n, err := varint.Unpack16(buf)
if err != nil { if err != nil {
return 0, err return 0, err
@ -347,7 +347,7 @@ func (c *Container) GetNextN16() (uint16, error) {
// GetNextN32 parses and returns a varint of type uint32. // GetNextN32 parses and returns a varint of type uint32.
func (c *Container) GetNextN32() (uint32, error) { func (c *Container) GetNextN32() (uint32, error) {
buf := c.Gather(5) buf := c.Peek(5)
num, n, err := varint.Unpack32(buf) num, n, err := varint.Unpack32(buf)
if err != nil { if err != nil {
return 0, err return 0, err
@ -358,7 +358,7 @@ func (c *Container) GetNextN32() (uint32, error) {
// GetNextN64 parses and returns a varint of type uint64. // GetNextN64 parses and returns a varint of type uint64.
func (c *Container) GetNextN64() (uint64, error) { func (c *Container) GetNextN64() (uint64, error) {
buf := c.Gather(10) buf := c.Peek(10)
num, n, err := varint.Unpack64(buf) num, n, err := varint.Unpack64(buf)
if err != nil { if err != nil {
return 0, err return 0, err

View file

@ -66,9 +66,9 @@ func TestContainerDataHandling(t *testing.T) {
} }
c8.clean() c8.clean()
c9 := c8.GatherAsContainer(len(testData)) c9 := c8.PeekContainer(len(testData))
c10 := c9.GatherAsContainer(len(testData) - 1) c10 := c9.PeekContainer(len(testData) - 1)
c10.Append(testData[len(testData)-1:]) c10.Append(testData[len(testData)-1:])
compareMany(t, testData, c1.CompileData(), c2.CompileData(), c3.CompileData(), d4, d5, c6.CompileData(), c7.CompileData(), c8.CompileData(), c9.CompileData(), c10.CompileData()) compareMany(t, testData, c1.CompileData(), c2.CompileData(), c3.CompileData(), d4, d5, c6.CompileData(), c7.CompileData(), c8.CompileData(), c9.CompileData(), c10.CompileData())