Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/utils/index.go
2070 views
1
package utils
2
3
// TransformIndex transforms user given index (start from 1) to array index (start from 0)
4
// in safe way without panic i.e negative index or index out of range
5
func TransformIndex[T any](arr []T, index int) int {
6
if index <= 1 {
7
// negative index
8
return 0
9
}
10
if index >= len(arr) {
11
// index out of range
12
return len(arr) - 1
13
}
14
// valid index
15
return index - 1
16
}
17
18