File tree Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -267,7 +267,7 @@ extension _StringGuts {
267
267
@_versioned
268
268
internal
269
269
var _isEmptyLiteral : Bool {
270
- return _isUnmanaged && _unmanagedRawStart == _emptyStringBase
270
+ return _isUnmanaged && _unmanagedCount == 0
271
271
}
272
272
}
273
273
Original file line number Diff line number Diff line change @@ -493,6 +493,34 @@ StringTests.test("substringDoesNotCopy/Swift4") {
493
493
}
494
494
}
495
495
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
+
496
524
StringTests . test ( " appendToSubstring " ) {
497
525
for initialSize in 1 ..< 16 {
498
526
for sliceStart in [ 0 , 2 , 8 , initialSize] {
@@ -504,9 +532,6 @@ StringTests.test("appendToSubstring") {
504
532
var s0 = String ( repeating: " x " , count: initialSize)
505
533
s0 = s0 [ s0. index ( _nth: sliceStart) ..< s0. index ( _nth: sliceEnd) ]
506
534
s0 += " x "
507
- if sliceStart == sliceEnd {
508
- expectNil ( s0. bufferID) // Empty string gets replaced on append
509
- }
510
535
expectEqual (
511
536
String (
512
537
repeating: " x " ,
You can’t perform that action at this time.
0 commit comments