Closed timvermeulen closed 3 years ago
I wonder if we should name these using "prefix" and "suffix" rather than "start" and "end"—trimmingPrefix(while:)
and trimmingSuffix(while:)
Yep, that's what I meant to say 😅
I'd like to work on this!
Could we also have the mutating methods on RangeReplaceableCollection
, by replacing the whole range of self
with the trimmed subsequence? I think we'd probably need tiebreakers for range-replaceable collections that are also self-slicing, but that would cover String
without the type-specific overloads.
Since trimming
and trimmingSuffix
are only available to BidirectionalCollection
because they depend on startOfSuffix
they cannot be used with RangeReplacableCollection
. I came up with a solution to replace the current implementation of startOfSuffix
to make it available also to Collection
and not only to BidirectionalCollection
.
@usableFromInline
internal func startOfSuffix(
while predicate: (Element) throws -> Bool
) rethrows -> Index {
var index = startIndex
var startOfSuffix: Index?
while index != endIndex {
if try predicate(self[index]) {
if startOfSuffix == nil {
startOfSuffix = index
}
} else {
startOfSuffix = nil
}
formIndex(after: &index)
}
return startOfSuffix ?? endIndex
}
It is still quite raw and can be simplified (recursion maybe), but should work. What do you think?
@fedeci Could we, instead of changing startOfSuffix
, require that the collection is both RangeReplaceableCollection
and BidirectionalCollection
? The reason that trimming from the end is only available on bidirectional collections is that it makes the performance of the operation depend only on the size of the part being trimmed, rather than the size of the entire collection.
We currently provide just one way to trim a collection:
We should also provide ways to trim only the start of the collection (
trimmingPrefix(while:)
, available to all collections), or only the end (trimmingSuffix(while:)
).Furthermore, collections for which
Self == SubSequence
should havemutating
variants usingtrim
as the base name. As an example,Collection
'spopFirst()
from the standard library is implemented using the same constraint.And finally, despite
String
not being its ownSubSequence
,String
should also have access to these mutating variants for the sake of convenience.