mirror of
https://github.com/projectdiscovery/nuclei.git
synced 2025-12-17 19:45:28 +00:00
17 lines
389 B
Go
17 lines
389 B
Go
|
|
package utils
|
||
|
|
|
||
|
|
// TransformIndex transforms user given index (start from 1) to array index (start from 0)
|
||
|
|
// in safe way without panic i.e negative index or index out of range
|
||
|
|
func TransformIndex[T any](arr []T, index int) int {
|
||
|
|
if index <= 1 {
|
||
|
|
// negative index
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
if index >= len(arr) {
|
||
|
|
// index out of range
|
||
|
|
return len(arr) - 1
|
||
|
|
}
|
||
|
|
// valid index
|
||
|
|
return index - 1
|
||
|
|
}
|