<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Charles,<div class=""><br class=""></div><div class="">As part of the String redesign in Swift 4, these methods on String are intended to be part of String’s conformance to RangeReplaceableCollection. As such, this proposal should be phrased in terms of a change to that protocol, rather than to String.</div><div class=""><br class=""></div><div class="">Couple of additional things to note regarding this:</div><div class=""> - this would also need to consider the source compatibility consequences for any existing adopters of RRC.</div><div class=""> - RRC is designed such that the minimum you need to implement is a single replaceSubrange call (and an init) and you get the other methods "for free". See "Conforming to the RangeReplaceableCollection Protocol” in the docs for details: <a href="https://developer.apple.com/reference/swift/rangereplaceablecollection" class="">https://developer.apple.com/reference/swift/rangereplaceablecollection</a>, and the implementation for gorier details: <a href="https://github.com/apple/swift/blob/master/stdlib/public/core/RangeReplaceableCollection.swift.gyb" class="">https://github.com/apple/swift/blob/master/stdlib/public/core/RangeReplaceableCollection.swift.gyb</a></div><div class=""><br class=""></div><div class="">Regards,</div><div class="">Ben</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Mar 14, 2017, at 9:21 AM, Charles Srstka via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">MOTIVATION:<br class=""><br class="">Swift strings support a few methods for inserting and replacing things in the middle of them:<br class=""><br class="">mutating func insert(_ newElement: Character, at i: String.Index)<br class=""><br class="">mutating func insert<S : Collection where S.Iterator.Element == Character>(contentsOf newElements: S, at i: String.Index)<br class=""><br class="">mutating func replaceSubrange(_ bounds: ClosedRange<String.Index>, with newElements: String)<br class=""><br class="">mutating func replaceSubrange(_ bounds: Range<String.Index>, with newElements: String)<br class=""><br class="">mutating func replaceSubrange<C where C : Collection, C.Iterator.Element == Character>(_: ClosedRange<String.Index>, with: C)<br class=""><br class="">mutating func replaceSubrange<C where C : Collection, C.Iterator.Element == Character>(_ bounds: Range<String.Index>, with newElements: C)<br class=""><br class="">These work well, but sometimes is advantageous to know the range of the portion of the string taken up by the inserted elements—for example, if one is creating a UI element, one may want to highlight the inserted text. In the old days when strings were defined in terms of UTF-16 code units, this was easy enough—just make a range starting at the insertion point and with the length of the string you just inserted. In Swift it is awkward and verbose:<br class=""><br class="">let insertedRange = replacementRange.lowerBound..<string.index(replacementRange.lowerBound, offsetBy: replacementString.distance(from: replacementString.startIndex, to: replacementString.endIndex))<br class=""><br class="">PROPOSED SOLUTION:<br class=""><br class="">Introduce a return value on the insert and replace methods that contains the range of the inserted characters or string:<br class=""><br class="">@discardableResult mutating func insert(_ newElement: Character, at i: String.Index) -> Range<String.Index><br class=""><br class="">@discardableResult mutating func insert<S : Collection where S.Iterator.Element == Character>(contentsOf newElements: S, at i: String.Index) -> Range<String.Index><br class=""><br class="">@discardableResult mutating func replaceSubrange(_ bounds: ClosedRange<String.Index>, with newElements: String) -> Range<String.Index><br class=""><br class="">@discardableResult mutating func replaceSubrange(_ bounds: Range<String.Index>, with newElements: String) -> Range<String.Index><br class=""><br class="">@discardableResult mutating func replaceSubrange<C where C : Collection, C.Iterator.Element == Character>(_: ClosedRange<String.Index>, with: C) -> Range<String.Index><br class=""><br class="">@discardableResult mutating func replaceSubrange<C where C : Collection, C.Iterator.Element == Character>(_ bounds: Range<String.Index>, with newElements: C) -> Range<String.Index><br class=""><br class="">The return value would contain the range of the portion of the string now occupied by what was inserted.<br class=""><br class="">If this change is considered acceptable, it could also be applied to the equivalent methods on RangeReplaceableCollection.<br class=""><br class="">IMPACT ON EXISTING CODE:<br class=""><br class="">The @discardableResult attribute would ensure that existing code would continue to work as is. The change in the signatures, however, would affect ABI compatibility.<br class=""><br class="">If the change is made to the protocols, however, this would affect source compatibility, as concrete types conforming to the protocols would need to be updated.<br class=""><br class="">ALTERNATIVES CONSIDERED:<br class=""><br class="">If there are performance concerns with generating the ranges, String could simply have two versions of each method, one returning a range and one returning Void.<br class=""><br class="">Charles<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></body></html>