Skip to content

Commit 69a2c84

Browse files
committed
_StringGuts: Slighty relax _isEmptyLiteral
The empty string literal doesn’t use the empty singleton yet. This change enables String.append's empty shortcut to work in more cases.
1 parent 3b15809 commit 69a2c84

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

stdlib/public/core/StringGuts.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ extension _StringGuts {
267267
@_versioned
268268
internal
269269
var _isEmptyLiteral: Bool {
270-
return _isUnmanaged && _unmanagedRawStart == _emptyStringBase
270+
return _isUnmanaged && _unmanagedCount == 0
271271
}
272272
}
273273

validation-test/stdlib/String.swift

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,34 @@ StringTests.test("substringDoesNotCopy/Swift4") {
493493
}
494494
}
495495

496+
StringTests.test("appendToEmptyString") {
497+
let x = "Bumfuzzle"
498+
expectNil(x.bufferID)
499+
500+
// Appending to empty string literal should replace it.
501+
var a1 = ""
502+
a1 += x
503+
expectNil(a1.bufferID)
504+
505+
// Appending to native string should keep the existing buffer.
506+
var b1 = ""
507+
b1.reserveCapacity(20)
508+
let b1ID = b1.bufferID
509+
b1 += x
510+
expectEqual(b1.bufferID, b1ID)
511+
512+
// .append(_:) should have the same behavior as +=
513+
var a2 = ""
514+
a2.append(x)
515+
expectNil(a2.bufferID)
516+
517+
var b2 = ""
518+
b2.reserveCapacity(20)
519+
let b2ID = b2.bufferID
520+
b2.append(x)
521+
expectEqual(b2.bufferID, b2ID)
522+
}
523+
496524
StringTests.test("appendToSubstring") {
497525
for initialSize in 1..<16 {
498526
for sliceStart in [0, 2, 8, initialSize] {
@@ -504,9 +532,6 @@ StringTests.test("appendToSubstring") {
504532
var s0 = String(repeating: "x", count: initialSize)
505533
s0 = s0[s0.index(_nth: sliceStart)..<s0.index(_nth: sliceEnd)]
506534
s0 += "x"
507-
if sliceStart == sliceEnd {
508-
expectNil(s0.bufferID) // Empty string gets replaced on append
509-
}
510535
expectEqual(
511536
String(
512537
repeating: "x",

0 commit comments

Comments
 (0)