Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ymz-ncnk committed Jan 9, 2025
1 parent 0ee1b18 commit 417f487
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 32 deletions.
10 changes: 5 additions & 5 deletions ord/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func UnmarshalValidString(lenU mus.Unmarshaller[int],
err = com.ErrNegativeLength
return
}
l := n + length
if len(bs) < l {
err = mus.ErrTooSmallByteSlice
return
}
if lenVl != nil {
if err = lenVl.Validate(length); err != nil {
if skip {
Expand All @@ -74,11 +79,6 @@ func UnmarshalValidString(lenU mus.Unmarshaller[int],
if length == 0 {
return
}
l := n + length
if len(bs) < l {
err = mus.ErrTooSmallByteSlice
return
}
return string(bs[n:l]), l, nil
}

Expand Down
24 changes: 2 additions & 22 deletions unsafe/byte_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,7 @@ func UnmarshalValidByteSlice(lenU mus.Unmarshaller[int],
// The lenS argument specifies the Sizer for the length of the slice, if nil,
// varint.SizePositiveInt() is used.
func SizeByteSlice(v []byte, lenS mus.Sizer[int]) (size int) {
length := len(v)
if lenS == nil {
size = varint.SizePositiveInt(length)
} else {
size = lenS.Size(length)
}
return size + length
return ord.SizeByteSlice(v, lenS)
}

// SkipByteSlice skips an encoded slice value.
Expand All @@ -101,19 +95,5 @@ func SizeByteSlice(v []byte, lenS mus.Sizer[int]) (size int) {
// In addition to the number of skipped bytes, it may also return
// mus.ErrTooSmallByteSlice, com.ErrOverflow or com.ErrNegativeLength.
func SkipByteSlice(lenU mus.Unmarshaller[int], bs []byte) (n int, err error) {
var length int
if lenU == nil {
length, n, err = varint.UnmarshalPositiveInt(bs)
} else {
length, n, err = lenU.Unmarshal(bs)
}
if err != nil {
return
}
if length < 0 {
err = com.ErrNegativeLength
return
}
n += length
return
return ord.SkipByteSlice(lenU, bs)
}
10 changes: 5 additions & 5 deletions unsafe/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func UnmarshalValidString(lenU mus.Unmarshaller[int], lenVl com.Validator[int],
err = com.ErrNegativeLength
return
}
l := n + length
if len(bs) < l {
err = mus.ErrTooSmallByteSlice
return
}
if lenVl != nil {
if err = lenVl.Validate(length); err != nil {
if skip {
Expand All @@ -75,11 +80,6 @@ func UnmarshalValidString(lenU mus.Unmarshaller[int], lenVl com.Validator[int],
if length == 0 {
return
}
l := n + length
if len(bs) < l {
err = mus.ErrTooSmallByteSlice
return
}
return unsafe_mod.String(&bs[n], length), l, nil
}

Expand Down

0 comments on commit 417f487

Please sign in to comment.