Skip to content

Commit

Permalink
Change preconditions to assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed May 13, 2022
1 parent ca31b92 commit 8f97f72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
16 changes: 10 additions & 6 deletions Sources/Rearrange/NSRange+ApplyMutation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ extension NSRange {
if location == NSNotFound {
return nil
}

if change.presetLimit != nil {
precondition(change.range.max <= change.limit)

if change.presetLimit != nil && change.range.max > change.limit {
assertionFailure()
return nil
}

// a trivial case
Expand All @@ -31,11 +32,14 @@ extension NSRange {
// in front of us, shift
if change.range.max <= location {
guard let new = shifted(by: change.delta) else {
fatalError("change makes range invalid")
assertionFailure("change makes range invalid")
return nil
}

if change.presetLimit != nil {
precondition(new.max <= change.postApplyLimit)
if change.presetLimit != nil && new.max > change.postApplyLimit {
assertionFailure()

return nil
}

return new
Expand Down
23 changes: 15 additions & 8 deletions Sources/Rearrange/RangeMutation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public struct RangeMutation {
self.presetLimit = limit

if let l = limit {
precondition(l >= 0)
precondition(range.max <= l, "range must not exceed limit")
assert(l >= 0)
assert(range.max <= l, "range must not exceed limit")
}

precondition(range.length + delta >= 0, "delta must not cause range length to go negative")
assert(range.length + delta >= 0, "delta must not cause range length to go negative")
}

public init(stringContents string: String) {
Expand Down Expand Up @@ -83,8 +83,9 @@ extension RangeMutation: Hashable {

extension RangeMutation {
public func transform(location: Int) -> Int? {
if let l = presetLimit {
precondition(location <= l)
if let l = presetLimit, location > l {
assertionFailure()
return nil
}

if range.location > location {
Expand All @@ -103,10 +104,16 @@ extension RangeMutation {

let result = location + delta

precondition(result >= 0)
if result < 0 {
assertionFailure()

return nil
}

if presetLimit != nil && result > postApplyLimit {
assertionFailure()

if presetLimit != nil {
precondition(result <= postApplyLimit)
return nil
}

return result
Expand Down

0 comments on commit 8f97f72

Please sign in to comment.