Skip to content

Internally add Collection.endOfPrefix(while:) and BidirectionalCollection.startOfSuffix(while:) #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 9, 2021

Conversation

timvermeulen
Copy link
Contributor

Internally adds the endOfPrefix(while:) method to Collection and the startOfSuffix(while:) method to BidirectionalCollection.

These methods return the same values as prefix(while: predicate).endIndex and suffix(while: predicate).startIndex, respectively. We're adding them as internal methods for now because we're already using this functionality in several places throughout the package, but it's unclear whether these are the names we want to stick with.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

@timvermeulen timvermeulen linked an issue Mar 9, 2021 that may be closed by this pull request
Copy link
Member

@natecook1000 natecook1000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

return self[Range(uncheckedBounds: (sliceStart, sliceStart))]
let start = try endOfPrefix(while: predicate)
let end = try self[start...].startOfSuffix(while: predicate)
return self[start..<end]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is amazing ❤️

@timvermeulen timvermeulen merged commit ab0ec2a into apple:main Mar 9, 2021
@timvermeulen timvermeulen deleted the end-of-prefix branch March 9, 2021 19:07
/// be called again.
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
@usableFromInline
Copy link
Contributor

@karwa karwa Mar 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks really nice, but I think these should be inlinable rather than just usableFromInline. We want the compiler to be able to specialise them for a particular predicate closure (and for generic specialisation for the collection, of course)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add indexBeforeFirst(where:) and indexAfterLast(where:)
3 participants