Skip to content

Instantly share code, notes, and snippets.

@lukaszo
Created January 7, 2023 01:45
Show Gist options
  • Save lukaszo/e76cd01caec3cf5482899fe32a287016 to your computer and use it in GitHub Desktop.
Save lukaszo/e76cd01caec3cf5482899fe32a287016 to your computer and use it in GitHub Desktop.
func blockPosition(datastream []byte, size int) (int, error) {
block := make([]byte, size)
start := 0
count := 1
var duplicateIndex int
for i := 1; i < len(datastream); {
block = datastream[start : start+count]
if count == size {
return i, nil
}
duplicateIndex = bytes.IndexByte(block, datastream[i])
if duplicateIndex == -1 {
count += 1
} else {
count = count - duplicateIndex
start = start + duplicateIndex + 1
}
i += 1
}
return 0, errors.New("not found")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment