Skip to content

Commit

Permalink
Refine safe access routine
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle.brown committed Dec 6, 2024
1 parent 598e864 commit ec2c34b
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/helper/arrays.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package helper

func SafeAccess(i, j int, data [][]byte) byte {
func SafeAccess[T any](i, j int, data [][]T) T {
res, _ := SafeAccessOption(i, j, data)
return res
}

func SafeAccessOption[T any](i, j int, data [][]T) (T, bool) {
var zeroValue T
if i < 0 || i >= len(data) {
return 0
return zeroValue, false
}
if j < 0 || j >= len(data[i]) {
return 0
return zeroValue, false
}
return data[i][j]
return data[i][j], true
}

0 comments on commit ec2c34b

Please sign in to comment.