# startOfSuffix() can be much simplified by using \`.lastIndex\`?

**URL:** <https://forums.swift.org/t/startofsuffix-can-be-much-simplified-by-using-lastindex/48087>\
**Category:** Algorithms\
**Created:** [May 11, 2021, 12:25am UTC](https://forums.swift.org/t/startofsuffix-can-be-much-simplified-by-using-lastindex/48087 "2021-05-11T00:25:45Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [May 12, 2021, 6:59pm UTC](https://forums.swift.org/t/startofsuffix-can-be-much-simplified-by-using-lastindex/48087/9 "2021-05-12T18:59:34Z")

</div>

> [@young](#):
>
> Maybe add a comment stating this code is written for maximum performance is the reason why `.lastIndex` is not used. Or stating this "re-write for max performance" choice in the doc.
> 
> And it seems this makes the case that `.lastIndex` is insufficient for this use case, as such, the `startOfSuffix()` is generally useful, maybe it should be `public` ?
> 
> And should the stdlib address this apparent shortcoming of `.lastIndex` ? Perhaps provide a variant that does what the `startOfSuffix()` does? Or one the returns both (lastIndex, after)

I think these questions are addressed adequately by @lorentey's discussion on the thread linked above. I quote some salient parts below:

> [@Add \`SortedArray\`, \`SortedSet\` and \`SortedDictionary\` with binary search](https://forums.swift.org/t/add-sortedarray-sortedset-and-sorteddictionary-with-binary-search/47303/8):
>
> - I think we can build out a rich library of standard algorithms that prevents the need to code raw loops in the vast majority of cases, but I expect there will always be niche problems that are best solved by rolling your own code from scratch. The idea is that these should be rare, because they are so much more difficult to implement correctly.  
> . . .
> - It is okay to reinvent the wheel when your job is to design wheels in a wheel factory. The standard library codebase is an algorithms factory; as engineers working on it, it is our business is to write raw algorithms, so that our customers don't have to.

It is the goal of the standard library (and algorithms library, which is a proving ground for the standard library) to provide a rich array of useful algorithms for general use. It's a non-goal to remove the need for raw loops to be used in its own implementations, and by the same token, the need for raw loops in the standard library or algorithms library doesn't demonstrate that there's a missing building block that's generally useful. Nor is it even really remarkable enough to warrant a comment in the code, since doing this legwork in the standard library and algorithms library so that users don't have to is the _entire point_ of these libraries.

---

_[View the full topic](https://forums.swift.org/t/startofsuffix-can-be-much-simplified-by-using-lastindex/48087)._
