Skip to content

Commit 099c6a8

Browse files
authored
Fix issue #13 (#15)
* `stream.IsNextSequence` or `stream.GoTo` froze when they meets end of the stream (#13)
1 parent 0e7b738 commit 099c6a8

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

stream.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ func (s *Stream) GoPrev() *Stream {
138138
// GoTo moves pointer of stream to specific token.
139139
// The search is done by token ID.
140140
func (s *Stream) GoTo(id int) *Stream {
141+
if s.current == undefToken {
142+
if s.prev != nil && id <= s.prev.id { // we at the end of the stream
143+
s.GoPrev() // now current is available
144+
for s.current != nil && id != s.current.id {
145+
s.GoPrev()
146+
}
147+
} else if s.next != nil && id >= s.prev.id { // we at the beginning of the stream
148+
s.GoNext() // now current is available
149+
for s.current != nil && id != s.current.id {
150+
s.GoNext()
151+
}
152+
}
153+
return s
154+
}
141155
if id > s.current.id {
142156
for s.current != nil && id != s.current.id {
143157
s.GoNext()

stream_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ func TestInfStream(t *testing.T) {
232232

233233
}
234234

235+
func TestIssues13_SequenceLongerThenStream(t *testing.T) {
236+
const TOK_CMD = 100
237+
var parser = New()
238+
parser.DefineTokens(TOK_CMD, []string{"CMD"})
239+
240+
stream := parser.ParseString("CMD CMD")
241+
242+
ok := stream.IsNextSequence(TOK_CMD, TOK_CMD, TOK_CMD, TOK_CMD)
243+
require.False(t, ok)
244+
}
245+
235246
var pattern = []byte(`<item count=10 valid id="n9762"> Носки <![CDATA[ socks ]]></item>`)
236247

237248
type dataGenerator struct {

0 commit comments

Comments
 (0)