Closed
Description
Previous ID | SR-15615 |
Radar | None |
Original Reporter | @benrimmington |
Type | Bug |
Environment
-
macOS 12.0.1 (21A559)
-
Xcode version 13.2 (13C90)
Additional Detail from JIRA
Votes | 0 |
Component/s | Standard Library |
Labels | Bug |
Assignee | None |
Priority | Medium |
md5: 1a2f74d992daac9b9dd8f4d6500c6f1b
Issue Description:
String.init(unsafeUninitializedCapacity:initializingUTF8With:)
has different behavior for a small vs large capacity.
// Example 1. Small strings (capacity <= _SmallString.capacity)
for capacity in 0...15 {
String.init(unsafeUninitializedCapacity: capacity) { buffer in
// Always prints "false 16"
print(capacity == buffer.count, buffer.count)
return 0
}
}
// Example 2. Large strings (capacity > _SmallString.capacity)
for capacity in 16...255 {
String.init(unsafeUninitializedCapacity: capacity) { buffer in
// Always prints "true ..."
print(capacity == buffer.count, buffer.count)
return 0
}
}
The first example is surprising because:
-
The actual
buffer.count
of 16 exceeds the_SmallString.capacity
of 15. -
The API documentation suggests the buffer will only have "room for `capacity` UTF-8 code units".
I noticed this issue because I was using buffer.endIndex
when initializing in reverse.