Performance penalties for overloading "+" have been an important argument when something similar has been discussed :-(swift-evolution/0265-offset-indexing-and-slicing.md at master · apple/swift-evolution · GitHub)
This shouldn't be an issue. I checked with a number of large OS projects using an @_exported import StringIndex and it didn't seem to make a measurable difference to compile times. The types the operators are defined on are not in common use.
I can't think of a more appropriate operator than + though.
Another possible approach might be to use a result builder (aka function builder) to make this a little DSL. That might end up with less cruft than you're forced to use when adding stuff to Swift's normal expression grammar.
Edit: And to clarify that this isn't about Int-based indexing directly into String values.
Just a side note, I can't remember the last time I needed to access characters in a string by an int index. I think fundamentally strings are more sequences than collections, because they are supposed to contain text that can almost always be approached with the standard iterators, prefix(), suffix(), and sequential parsers. I'm curious to see real world examples that absolutely require direct random access to string elements.
What I've come to believe is that Swift strings are much easier to work with if I embrace Substrings and the Collection operations that create them, and avoid indexes as much as possible. To illustrate using your situation, here's how I'd get the substring starting at the second "e" in "Undeterred":
let string = "Undeterred"
let fromSecondE = string.drop { $0 != "e" }.dropFirst().drop { $0 != "e" }
If I really need the index, I can use fromSecondE.startIndex, because the Substring's indices work in the String it was derived from.
I might make it a little nicer with a helper method:
extension Collection where Element: Equatable {
func dropUntil(_ e: Element) -> SubSequence {
return drop { $0 != e }
}
}
let fromSecondE2 = string.dropUntil("e").dropFirst().dropUntil("e")
These two in particular are very hard to understand.