mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
86d60400c1
Over the years, SwiftLintFramework had become a fairly massive monolith, containing over 400 source files with both core infrastructure and rules. Architecturally, the rules should rely on the core infrastructure but not the other way around. There are two exceptions to this: `custom_rules` and `superfluous_disable_command` which need special integration with the linter infrastructure. Now the time has come to formalize this architecture and one way to do that is to move the core SwiftLint functionality out of SwiftLintFramework and into a new SwiftLintCore module that the rules can depend on. Beyond enforcing architectural patterns, this also has the advantage of speeding up incremental compilation by skipping rebuilding the core functionality when iterating on rules. Because the core functionality is always useful when building rules, I'm opting to import SwiftLintCore in SwiftLintFramework as `@_exported` so that it's implicitly available to all files in SwiftLintFramework without needing to import it directly. In a follow-up I'll also split the built-in rules and the extra rules into their own modules. More modularization is possible from there, but not planned. The bulk of this PR just moves files from `Source/SwiftLintFramework/*` to `Source/SwiftLintCore/*`. There are some other changes that can't be split up into their own PRs: * Change jazzy to document the SwiftLintCore module instead of SwiftLintFramework. * Change imports in unit tests to reflect where code was moved to. * Update `sourcery` make rule to reflect where code was moved to. * Create a new `coreRules` array and register those rules with the registry. This allows the `custom_rules` and `superfluous_disable_command` rule implementations to remain internal to the SwiftLintCore module, preventing more implementation details from leaking across architectural layers. * Move `RuleRegistry.registerAllRulesOnce()` out of the type declaration and up one level so it can access rules defined downstream from SwiftLintCore.
59 lines
2.3 KiB
Swift
59 lines
2.3 KiB
Swift
public extension RandomAccessCollection where Index == Int {
|
|
/// Returns the first index in which an element of the collection satisfies the given predicate.
|
|
/// The collection assumed to be sorted. If collection is not have sorted values the result is undefined.
|
|
///
|
|
/// The idea is to get first index of a function for which the given predicate evaluates to true.
|
|
///
|
|
/// let values = [1,2,3,4,5]
|
|
/// let idx = values.firstIndexAssumingSorted(where: { $0 > 3 })
|
|
///
|
|
/// // false, false, false, true, true
|
|
/// // ^
|
|
/// // therefore idx == 3
|
|
///
|
|
/// - parameter predicate: A closure that takes an element as its argument
|
|
/// and returns a Boolean value that indicates whether the passed element
|
|
/// represents a match.
|
|
///
|
|
/// - returns: The index of the first element for which `predicate` returns `true`.
|
|
/// If no elements in the collection satisfy the given predicate, returns `nil`.
|
|
///
|
|
/// - complexity: O(log(*n*)), where *n* is the length of the collection.
|
|
///
|
|
/// - throws: Rethrows errors thrown by the predicate.
|
|
@inlinable
|
|
func firstIndexAssumingSorted(where predicate: (Self.Element) throws -> Bool) rethrows -> Int? {
|
|
// Predicate should divide a collection to two pairs of values
|
|
// "bad" values for which predicate returns `false``
|
|
// "good" values for which predicate return `true`
|
|
|
|
// false false false false false true true true
|
|
// ^
|
|
// The idea is to get _first_ index which for which the predicate returns `true`
|
|
|
|
let lastIndex = count
|
|
|
|
// The index that represents where bad values start
|
|
var badIndex = -1
|
|
|
|
// The index that represents where good values start
|
|
var goodIndex = lastIndex
|
|
var midIndex = (badIndex + goodIndex) / 2
|
|
|
|
while badIndex + 1 < goodIndex {
|
|
if try predicate(self[midIndex]) {
|
|
goodIndex = midIndex
|
|
} else {
|
|
badIndex = midIndex
|
|
}
|
|
midIndex = (badIndex + goodIndex) / 2
|
|
}
|
|
|
|
// We're out of bounds, no good items in array
|
|
if midIndex == lastIndex {
|
|
return nil
|
|
}
|
|
return goodIndex
|
|
}
|
|
}
|