Skip to content

Commit

Permalink
Fix issue #13 (#15)
Browse files Browse the repository at this point in the history
* `stream.IsNextSequence` or `stream.GoTo` froze when they meets end of the stream (#13)
  • Loading branch information
bzick authored Apr 3, 2024
1 parent 0e7b738 commit 099c6a8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ func (s *Stream) GoPrev() *Stream {
// GoTo moves pointer of stream to specific token.
// The search is done by token ID.
func (s *Stream) GoTo(id int) *Stream {
if s.current == undefToken {
if s.prev != nil && id <= s.prev.id { // we at the end of the stream
s.GoPrev() // now current is available
for s.current != nil && id != s.current.id {
s.GoPrev()
}
} else if s.next != nil && id >= s.prev.id { // we at the beginning of the stream
s.GoNext() // now current is available
for s.current != nil && id != s.current.id {
s.GoNext()
}
}
return s
}
if id > s.current.id {
for s.current != nil && id != s.current.id {
s.GoNext()
Expand Down
11 changes: 11 additions & 0 deletions stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,17 @@ func TestInfStream(t *testing.T) {

}

func TestIssues13_SequenceLongerThenStream(t *testing.T) {
const TOK_CMD = 100
var parser = New()
parser.DefineTokens(TOK_CMD, []string{"CMD"})

stream := parser.ParseString("CMD CMD")

ok := stream.IsNextSequence(TOK_CMD, TOK_CMD, TOK_CMD, TOK_CMD)
require.False(t, ok)
}

var pattern = []byte(`<item count=10 valid id="n9762"> Носки <![CDATA[ socks ]]></item>`)

type dataGenerator struct {
Expand Down

0 comments on commit 099c6a8

Please sign in to comment.