@@ -241,22 +241,29 @@ extension _SmallString {
241241 fileprivate mutating func withMutableCapacity(
242242 _ f: ( UnsafeMutableRawBufferPointer ) throws -> Int
243243 ) rethrows {
244- let len = try withUnsafeMutableBytes ( of: & self . _storage) {
245- ( rawBufPtr: UnsafeMutableRawBufferPointer ) -> Int in
246- let len = try f ( rawBufPtr)
247- UnsafeMutableRawBufferPointer (
248- rebasing: rawBufPtr [ len... ]
249- ) . initializeMemory ( as: UInt8 . self, repeating: 0 )
250- return len
251- }
252- if len == 0 {
244+ let len = try withUnsafeMutableBytes ( of: & _storage, f)
245+
246+ if len <= 0 {
247+ _debugPrecondition ( len == 0 )
253248 self = _SmallString ( )
254249 return
255250 }
256- _internalInvariant ( len <= _SmallString. capacity)
251+ _SmallString. zeroTrailingBytes ( of: & _storage, from: len)
252+ self = _SmallString ( leading: _storage. 0 , trailing: _storage. 1 , count: len)
253+ }
257254
258- let ( leading, trailing) = self . zeroTerminatedRawCodeUnits
259- self = _SmallString ( leading: leading, trailing: trailing, count: len)
255+ @inlinable
256+ @_alwaysEmitIntoClient
257+ internal static func zeroTrailingBytes(
258+ of storage: inout RawBitPattern , from index: Int
259+ ) {
260+ _internalInvariant ( index > 0 )
261+ _internalInvariant ( index <= _SmallString. capacity)
262+ //FIXME: Verify this on big-endian architecture
263+ let mask0 = ( UInt64 ( bitPattern: ~ 0 ) &>> ( 8 &* ( 8 &- Swift . min ( index, 8 ) ) ) )
264+ let mask1 = ( UInt64 ( bitPattern: ~ 0 ) &>> ( 8 &* ( 16 &- Swift . max ( index, 8 ) ) ) )
265+ storage. 0 &= ( index <= 0 ) ? 0 : mask0. littleEndian
266+ storage. 1 &= ( index <= 8 ) ? 0 : mask1. littleEndian
260267 }
261268}
262269
0 commit comments