swift-algorithms
swift-algorithms copied to clipboard
Add `commonPrefix(with:)`, `commonSuffix(with:)`
Find the common prefix/suffix of two sequences or collections.
commonPrefix(with:)commonSuffix(with:)endOfCommonPrefix(with:): get a pair of indices that mark the end of the common prefix of two collectionsstartOfCommonSuffix(with:): get a pair of indices that mark the start of the common suffix of two collections
extension Sequence {
public func commonPrefix<Other: Sequence>(
with other: Other,
by areEquivalent: (Element, Other.Element) throws -> Bool
) rethrows -> [Element]
}
extension Sequence where Element: Equatable {
public func commonPrefix<Other: Sequence>(
with other: Other
) -> CommonPrefix<Self, Other> where Other.Element == Element
}
extension LazySequenceProtocol {
public func commonPrefix<Other: Sequence>(
with other: Other,
by areEquivalent: @escaping (Element, Other.Element) -> Bool
) -> CommonPrefix<Self, Other>
}
extension Collection {
public func commonPrefix<Other: Sequence>(
with other: Other,
by areEquivalent: (Element, Other.Element) throws -> Bool
) rethrows -> SubSequence
}
extension Collection where Element: Equatable {
public func commonPrefix<Other: Sequence>(
with other: Other
) -> SubSequence where Other.Element == Element
}
extension LazyCollectionProtocol where Element: Equatable {
public func commonPrefix<Other: Sequence>(
with other: Other
) -> CommonPrefix<Self, Other> where Other.Element == Element
}
extension BidirectionalCollection {
public func commonSuffix<Other: BidirectionalCollection>(
with other: Other,
by areEquivalent: (Element, Other.Element) throws -> Bool
) rethrows -> SubSequence
}
extension BidirectionalCollection where Element: Equatable {
public func commonSuffix<Other: BidirectionalCollection>(
with other: Other
) -> SubSequence where Other.Element == Element
}
extension Collection {
public func endOfCommonPrefix<Other: Collection>(
with other: Other,
by areEquivalent: (Element, Other.Element) throws -> Bool
) rethrows -> (Index, Other.Index)
}
extension Collection where Element: Equatable {
public func endOfCommonPrefix<Other: Collection>(
with other: Other
) -> (Index, Other.Index) where Other.Element == Element
}
extension BidirectionalCollection {
public func startOfCommonSuffix<Other: BidirectionalCollection>(
with other: Other,
by areEquivalent: (Element, Other.Element) throws -> Bool
) rethrows -> (Index, Other.Index)
}
extension BidirectionalCollection where Element: Equatable {
public func startOfCommonSuffix<Other: BidirectionalCollection>(
with other: Other
) -> (Index, Other.Index) where Other.Element == Element
}
Checklist
- [x] I've added at least one test that validates that my change is working, if appropriate
- [x] I've followed the code style of the rest of the project
- [x] I've read the Contribution Guidelines
- [x] I've updated the documentation if necessary
I think it's more natural to not pluralize of start and end, i.e. just have endOfCommonPrefix, startOfCommonSuffix
I think it's more natural to not pluralize of
startandend, i.e. just haveendOfCommonPrefix,startOfCommonSuffix
Fair point. I think I went with this because we already have endOfPrefix(while:) and startOfSuffix(while:) which both return a single index, but that's probably fine.
Don't endsOfCommonPrefix(with:) and startssOfCommonSuffix(with:) overlap with diverges(from:) and converges(with:) from #37 ?