Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from ZevEisenberg/patch-1
Browse files Browse the repository at this point in the history
Simpler array bounds checking
  • Loading branch information
david-swift authored Mar 26, 2024
2 parents 75847de + e22f528 commit b17ec73
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
1 change: 1 addition & 0 deletions Contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

- [david-swift](https://github.com/david-swift)
- [Greg Cotten](https://github.com/gregcotten)
- [Zev Eisenberg](https://github.com/ZevEisenberg)
11 changes: 2 additions & 9 deletions Sources/Adwaita/Model/Extensions/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,16 @@ extension Array {
/// ```
public subscript(safe index: Int?) -> Element? {
get {
if let index, checkIndex(index) {
if let index, indices.contains(index) {
return self[index]
}
return nil
}
set {
if let index, let value = newValue, checkIndex(index) {
if let index, let value = newValue, indices.contains(index) {
self[index] = value
}
}
}

/// Check if a given index is valid for the array.
/// - Parameter index: The index to test.
/// - Returns: Return whether the index is valid or not.
private func checkIndex(_ index: Int) -> Bool {
index < count && index >= 0
}

}

0 comments on commit b17ec73

Please sign in to comment.