@@ -379,7 +379,7 @@ extension String.UnicodeScalarView: RangeReplaceableCollection {
379
379
/// string.
380
380
///
381
381
/// - Parameters:
382
- /// - bounds : The range of elements to replace. The bounds of the range
382
+ /// - subrange : The range of elements to replace. The bounds of the range
383
383
/// must be valid indices of the view.
384
384
/// - newElements: The new Unicode scalar values to add to the string.
385
385
///
@@ -388,18 +388,12 @@ extension String.UnicodeScalarView: RangeReplaceableCollection {
388
388
/// removes elements at the end of the string, the complexity is O(*n*),
389
389
/// where *n* is equal to `bounds.count`.
390
390
public mutating func replaceSubrange< C> (
391
- _ bounds : Range < Index > ,
391
+ _ subrange : Range < Index > ,
392
392
with newElements: C
393
393
) where C: Collection , C. Element == Unicode . Scalar {
394
- // TODO(String performance): Skip extra String and Array allocation
395
- let bounds = _guts. validateScalarRange ( bounds)
396
- let utf8Replacement = newElements. flatMap { String ( $0) . utf8 }
397
- let replacement = utf8Replacement. withUnsafeBufferPointer {
398
- return String . _uncheckedFromUTF8 ( $0)
399
- }
400
- var copy = String ( _guts)
401
- copy. replaceSubrange ( bounds, with: replacement)
402
- self = copy. unicodeScalars
394
+ let subrange = _guts. validateScalarRange ( subrange)
395
+ _guts. replaceSubrange ( subrange, with: newElements)
396
+ _invariantCheck ( )
403
397
}
404
398
}
405
399
0 commit comments