Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: captures unsafe casting from TSQueryMatch #24

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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