Skip to content

Commit 9336821

Browse files
committed
[stdlib] Don’t use unbound ranges (foo[…]) in String → Substring conversions
The UnboundRange → Range conversion path is complicated and it involves at least one unnecessary _precondition check for startIndex ..< endIndex.
1 parent d1ef4c4 commit 9336821

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

stdlib/public/core/Substring.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ extension Substring: CustomDebugStringConvertible {
322322

323323
extension Substring: LosslessStringConvertible {
324324
public init(_ content: String) {
325-
self = content[...]
325+
let range = Range(_uncheckedBounds: (content.startIndex, content.endIndex))
326+
self.init(Slice(base: content, bounds: range))
326327
}
327328
}
328329

@@ -727,14 +728,14 @@ extension Substring: RangeReplaceableCollection {
727728
public init<S: Sequence>(_ elements: S)
728729
where S.Element == Character {
729730
if let str = elements as? String {
730-
self = str[...]
731+
self.init(str)
731732
return
732733
}
733734
if let subStr = elements as? Substring {
734735
self = subStr
735736
return
736737
}
737-
self = String(elements)[...]
738+
self.init(String(elements))
738739
}
739740

740741
@inlinable // specialize

0 commit comments

Comments
 (0)