Skip to content

Commit 2f0dafb

Browse files
authored
Merge pull request #11191 from milseman/very_persistent
[stdlib] Fix bug in Substring's _persistentContent
2 parents c36cb57 + 10e9e33 commit 2f0dafb

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

stdlib/public/core/String.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,6 @@ extension StringProtocol {
150150
}
151151
return String(String.CharacterView(self))
152152
}
153-
154-
internal var _persistentString : String {
155-
if _fastPath(self is _SwiftStringView) {
156-
return (self as! _SwiftStringView)._persistentContent
157-
}
158-
return String(String.CharacterView(self))
159-
}
160153
}
161154

162155
extension String : _SwiftStringView {

stdlib/public/core/Substring.swift.gyb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,16 @@ extension Substring : _SwiftStringView {
266266
var _persistentContent : String {
267267
let wholeCore = _slice._base._core
268268
let native = wholeCore.nativeBuffer
269-
if _fastPath(native != nil), let n = native {
269+
if _fastPath(native != nil) {
270+
let wholeString = String(wholeCore)
271+
let n = native._unsafelyUnwrappedUnchecked
270272
if _fastPath(
271-
n.start == wholeCore._baseAddress && n.usedCount == wholeCore.count) {
272-
return String(wholeCore)
273+
n.start == wholeCore._baseAddress
274+
&& n.usedCount == wholeCore.count
275+
&& _slice.startIndex == wholeString.startIndex
276+
&& _slice.endIndex == wholeString.endIndex
277+
) {
278+
return wholeString
273279
}
274280
}
275281
var r = String()

test/stdlib/subString.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,11 @@ SubstringTests.test("UTF8View") {
190190
expectEqual("", String(u.dropLast(10))!)
191191
}
192192

193+
SubstringTests.test("Persistent Content") {
194+
var str = "abc"
195+
str += "def"
196+
expectEqual("bcdefg", str.dropFirst(1) + "g")
197+
expectEqual("bcdefg", (str.dropFirst(1) + "g") as String)
198+
}
199+
193200
runAllTests()

0 commit comments

Comments
 (0)