[string] Minor simplifications

This commit is contained in:
Michael Ilseman
2018-02-02 12:04:15 -08:00
parent 444796a66b
commit a6e79efbbe
3 changed files with 41 additions and 46 deletions

View File

@@ -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.

View File

@@ -48,11 +48,11 @@ 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
} else {
guard count > 1 else {
self = count == 0 ? "" : repeatedValue
return
}
precondition(count > 0, "Negative count not allowed")
defer { _fixLifetime(repeatedValue) }
if _slowPath(repeatedValue._guts._isOpaque) {
@@ -66,7 +66,6 @@ extension String {
self.init(_StringGuts(utf16._repeated(count)))
}
}
}
/// A Boolean value indicating whether a string has no characters.
@_inlineable // FIXME(sil-serialize-all)

View File

@@ -735,7 +735,7 @@ extension _StringObject {
} else {
fatalError("Unimplemented string form")
}
#endif
#endif // INTERNAL_CHECKS_ENABLED
}
}
@@ -754,22 +754,28 @@ 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 {
return
}
var bits: UInt = 0
if isSmallOrObjC {
bits |= _StringObject._isCocoaBit
@@ -781,7 +787,6 @@ extension _StringObject {
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