Skip to content

Commit

Permalink
fix: avoid unsafe rule violation & clarify statefulness of Query iter…
Browse files Browse the repository at this point in the history
…ators

Co-authored-by: Branden J Brown <[email protected]>
Co-authored-by: Amaan Qureshi <[email protected]>
  • Loading branch information
3 people authored Nov 24, 2024
1 parent 4d96fa6 commit 5e5682f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,8 @@ func (qm *QueryMatch) Id() uint {
func newQueryMatch(m *C.TSQueryMatch, cursor *C.TSQueryCursor) QueryMatch {
var captures []QueryCapture
if m.capture_count > 0 {
captures = (*[1 << 16]QueryCapture)(unsafe.Pointer(m.captures))[:m.capture_count:m.capture_count]
cCaptures := unsafe.Slice(m.captures, m.capture_count)
captures = *(*[]QueryCapture)(unsafe.Pointer(&cCaptures))
}
return QueryMatch{
cursor: cursor,
Expand Down Expand Up @@ -917,6 +918,9 @@ func NewQueryProperty(key string, value *string, captureId *uint) QueryProperty

// Next will return the next match in the sequence of matches.
//
// Subsequent calls to [QueryMatches.Next] will overwrite the memory at the same location as prior matches, since the memory is reused. You can think of this as a stateful iterator.
// If you need to keep the data of a prior match without it being overwritten, you should copy what you need before calling [QueryMatches.Next] again.
//
// If there are no more matches, it will return nil.
func (qm *QueryMatches) Next() *QueryMatch {
for {
Expand All @@ -940,6 +944,9 @@ func (qm *QueryMatches) Next() *QueryMatch {

// Next will return the next match in the sequence of matches, as well as the index of the capture.
//
// Subsequent calls to [QueryCaptures.Next] will overwrite the memory at the same location as prior matches, since the memory is reused. You can think of this as a stateful iterator.
// If you need to keep the data of a prior match without it being overwritten, you should copy what you need before calling [QueryCaptures.Next] again.
//
// If there are no more matches, it will return nil.
func (qc *QueryCaptures) Next() (*QueryMatch, uint) {
for {
Expand Down

0 comments on commit 5e5682f

Please sign in to comment.