Skip to content
This repository has been archived by the owner on Sep 23, 2023. It is now read-only.

Commit

Permalink
compress.Next: return empty byte slice instead of nil when wordLen ==…
Browse files Browse the repository at this point in the history
… 0 (#1085)
  • Loading branch information
AskAlexSharov authored Sep 6, 2023
1 parent a6ad145 commit cf6e038
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
6 changes: 4 additions & 2 deletions compress/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ func TestCompressDict1(t *testing.T) {
require.Equal(t, 0, g.MatchPrefixCmp([]byte("")))
require.Equal(t, 0, g.MatchPrefixCmp([]byte{}))
word, _ := g.Next(nil)
require.Nil(t, word)
require.NotNil(t, word)
require.Zero(t, len(word))

// next word is `long`
require.True(t, g.MatchPrefix([]byte("long")))
Expand Down Expand Up @@ -215,7 +216,8 @@ func TestCompressDictCmp(t *testing.T) {
g.Reset(savePos)

word, _ := g.Next(nil)
require.Nil(t, word)
require.NotNil(t, word)
require.Zero(t, len(word))

// next word is `long`
savePos = g.dataP
Expand Down
31 changes: 10 additions & 21 deletions compress/decompress.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,20 +411,16 @@ type Getter struct {
func (g *Getter) Trace(t bool) { g.trace = t }
func (g *Getter) FileName() string { return g.fName }

func (g *Getter) nextPos(clean bool) uint64 {
if clean {
if g.dataBit > 0 {
g.dataP++
g.dataBit = 0
}
func (g *Getter) nextPos(clean bool) (pos uint64) {
if clean && g.dataBit > 0 {
g.dataP++
g.dataBit = 0
}
table := g.posDict
if table.bitLen == 0 {
return table.pos[0]
}
var l byte
var pos uint64
for l == 0 {
for l := byte(0); l == 0; {
code := uint16(g.data[g.dataP]) >> g.dataBit
if 8-g.dataBit < table.bitLen && int(g.dataP)+1 < len(g.data) {
code |= uint16(g.data[g.dataP+1]) << (8 - g.dataBit)
Expand All @@ -439,7 +435,7 @@ func (g *Getter) nextPos(clean bool) uint64 {
pos = table.pos[code]
}
g.dataP += uint64(g.dataBit / 8)
g.dataBit = g.dataBit % 8
g.dataBit %= 8
}
return pos
}
Expand Down Expand Up @@ -470,7 +466,7 @@ func (g *Getter) nextPattern() []byte {
pattern = *cw.pattern
}
g.dataP += uint64(g.dataBit / 8)
g.dataBit = g.dataBit % 8
g.dataBit %= 8
}
return pattern
}
Expand Down Expand Up @@ -530,11 +526,6 @@ func (g *Getter) HasNext() bool {
// and appends it to the given buf, returning the result of appending
// After extracting next word, it moves to the beginning of the next one
func (g *Getter) Next(buf []byte) ([]byte, uint64) {
defer func() {
if rec := recover(); rec != nil {
panic(fmt.Sprintf("file: %s, %s, %s", g.fName, rec, dbg.Stack()))
}
}()
savePos := g.dataP
wordLen := g.nextPos(true)
wordLen-- // because when create huffman tree we do ++ , because 0 is terminator
Expand All @@ -543,6 +534,9 @@ func (g *Getter) Next(buf []byte) ([]byte, uint64) {
g.dataP++
g.dataBit = 0
}
if buf == nil { // wordLen == 0, means we have valid record of 0 size. nil - is the marker of "something not found"
buf = []byte{}
}
return buf, g.dataP
}
bufPos := len(buf) // Tracking position in buf where to insert part of the word
Expand Down Expand Up @@ -591,11 +585,6 @@ func (g *Getter) Next(buf []byte) ([]byte, uint64) {
}

func (g *Getter) NextUncompressed() ([]byte, uint64) {
defer func() {
if rec := recover(); rec != nil {
panic(fmt.Sprintf("file: %s, %s, %s", g.fName, rec, dbg.Stack()))
}
}()
wordLen := g.nextPos(true)
wordLen-- // because when create huffman tree we do ++ , because 0 is terminator
if wordLen == 0 {
Expand Down

0 comments on commit cf6e038

Please sign in to comment.