You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fx/search.go

55 lines
1.5 KiB
Go

9 months ago
package main
type match struct {
start, end int
index int
}
9 months ago
type кусочек struct {
b []byte
index int
}
func splitBytesByIndexes(b []byte, indexes []match) []кусочек {
out := make([]кусочек, 0, 1)
9 months ago
pos := 0
for _, pair := range indexes {
9 months ago
out = append(out, кусочек{safeSlice(b, pos, pair.start), -1})
out = append(out, кусочек{safeSlice(b, pair.start, pair.end), pair.index})
pos = pair.end
9 months ago
}
9 months ago
out = append(out, кусочек{safeSlice(b, pos, len(b)), -1})
9 months ago
return out
}
9 months ago
func splitIndexesToChunks(chunks [][]byte, indexes [][]int, searchIndex int) (chunkIndexes [][]match) {
chunkIndexes = make([][]match, len(chunks))
9 months ago
9 months ago
for index, idx := range indexes {
9 months ago
position := 0
for i, chunk := range chunks {
// If start index lies in this chunk
if idx[0] < position+len(chunk) {
// Calculate local start and end for this chunk
localStart := idx[0] - position
localEnd := idx[1] - position
// If the end index also lies in this chunk
if idx[1] <= position+len(chunk) {
9 months ago
chunkIndexes[i] = append(chunkIndexes[i], match{start: localStart, end: localEnd, index: searchIndex + index})
9 months ago
break
} else {
// If the end index is outside this chunk, split the index
9 months ago
chunkIndexes[i] = append(chunkIndexes[i], match{start: localStart, end: len(chunk), index: searchIndex + index})
9 months ago
// Adjust the starting index for the next chunk
idx[0] = position + len(chunk)
}
}
position += len(chunk)
}
}
return
}