diff --git a/stdlib/public/core/StringGuts.swift b/stdlib/public/core/StringGuts.swift index fa4ae7fe2ec..16ae6d950c0 100644 --- a/stdlib/public/core/StringGuts.swift +++ b/stdlib/public/core/StringGuts.swift @@ -500,12 +500,7 @@ extension _StringGuts { return _taggedCocoaObject } _sanityCheck(_object.isUnmanaged) - if _object.isSingleByte { - return _NSContiguousString(_StringGuts(_asUnmanaged(of: UInt8.self))) - } - - return _NSContiguousString( - _StringGuts(_asUnmanaged(of: UTF16.CodeUnit.self))) + return _NSContiguousString(_unmanaged: self) } /// Return an NSString instance containing a slice of this string. diff --git a/stdlib/public/core/StringLegacy.swift b/stdlib/public/core/StringLegacy.swift index 2227903e6f1..f73b9c03545 100644 --- a/stdlib/public/core/StringLegacy.swift +++ b/stdlib/public/core/StringLegacy.swift @@ -48,23 +48,22 @@ extension String { /// string. @_inlineable // FIXME(sil-serialize-all) public init(repeating repeatedValue: String, count: Int) { - if count == 0 { - self = "" - } else if count == 1 { - self = repeatedValue + guard count > 1 else { + self = count == 0 ? "" : repeatedValue + return + } + + precondition(count > 0, "Negative count not allowed") + defer { _fixLifetime(repeatedValue) } + if _slowPath(repeatedValue._guts._isOpaque) { + let opaque = repeatedValue._guts._asOpaque() + self.init(_StringGuts(opaque._repeated(count))) + } else if repeatedValue._guts.isASCII { + let ascii = repeatedValue._guts._unmanagedASCIIView + self.init(_StringGuts(ascii._repeated(count))) } else { - precondition(count > 0, "Negative count not allowed") - defer { _fixLifetime(repeatedValue) } - if _slowPath(repeatedValue._guts._isOpaque) { - let opaque = repeatedValue._guts._asOpaque() - self.init(_StringGuts(opaque._repeated(count))) - } else if repeatedValue._guts.isASCII { - let ascii = repeatedValue._guts._unmanagedASCIIView - self.init(_StringGuts(ascii._repeated(count))) - } else { - let utf16 = repeatedValue._guts._unmanagedUTF16View - self.init(_StringGuts(utf16._repeated(count))) - } + let utf16 = repeatedValue._guts._unmanagedUTF16View + self.init(_StringGuts(utf16._repeated(count))) } } diff --git a/stdlib/public/core/StringObject.swift b/stdlib/public/core/StringObject.swift index 8a027d92bf6..93b6d18b4d4 100644 --- a/stdlib/public/core/StringObject.swift +++ b/stdlib/public/core/StringObject.swift @@ -735,7 +735,7 @@ extension _StringObject { } else { fatalError("Unimplemented string form") } -#endif +#endif // INTERNAL_CHECKS_ENABLED } } @@ -754,34 +754,39 @@ extension _StringObject { isOpaque: Bool, isTwoByte: Bool ) { +#if INTERNAL_CHECKS_ENABLED + defer { + _sanityCheck(isSmall == (isValue && isSmallOrObjC)) + _sanityCheck(isUnmanaged == (isValue && !isSmallOrObjC)) + _sanityCheck(isCocoa == (!isValue && isSmallOrObjC)) + _sanityCheck(isNative == (!isValue && !isSmallOrObjC)) + } +#endif + #if arch(i386) || arch(arm) - var variant: _Variant - var bits: UInt if isValue { if isSmallOrObjC { _sanityCheck(isOpaque) - self.init( - isTwoByte ? .smallDoubleByte : .smallSingleByte, - _payloadBits) + self.init(isTwoByte ? .smallDoubleByte : .smallSingleByte, _payloadBits) } else { _sanityCheck(!isOpaque) self.init( - isTwoByte ? .unmanagedDoubleByte : .unmanagedSingleByte, - _payloadBits) + isTwoByte ? .unmanagedDoubleByte : .unmanagedSingleByte, _payloadBits) } - } else { - var bits: UInt = 0 - if isSmallOrObjC { - bits |= _StringObject._isCocoaBit - } - if isOpaque { - bits |= _StringObject._isOpaqueBit - } - if isTwoByte { - bits |= _StringObject._twoByteBit - } - self.init(.strong(Builtin.reinterpretCast(_payloadBits)), bits) + return } + + var bits: UInt = 0 + if isSmallOrObjC { + bits |= _StringObject._isCocoaBit + } + if isOpaque { + bits |= _StringObject._isOpaqueBit + } + if isTwoByte { + bits |= _StringObject._twoByteBit + } + self.init(.strong(Builtin.reinterpretCast(_payloadBits)), bits) #else _sanityCheck(_payloadBits & ~_StringObject._payloadMask == 0) var rawBits = _payloadBits & _StringObject._payloadMask @@ -815,10 +820,6 @@ extension _StringObject { self.init(nonTaggedRawBits: rawBits) } #endif - _sanityCheck(isSmall == (isValue && isSmallOrObjC)) - _sanityCheck(isUnmanaged == (isValue && !isSmallOrObjC)) - _sanityCheck(isCocoa == (!isValue && isSmallOrObjC)) - _sanityCheck(isNative == (!isValue && !isSmallOrObjC)) } @_versioned