Merge pull request #20952 from xwu/stdlib-style-fixes
diff --git a/stdlib/public/SwiftShims/CoreFoundationShims.h b/stdlib/public/SwiftShims/CoreFoundationShims.h
index 72e1b2b..e71a0c7 100644
--- a/stdlib/public/SwiftShims/CoreFoundationShims.h
+++ b/stdlib/public/SwiftShims/CoreFoundationShims.h
@@ -134,9 +134,9 @@
SWIFT_RUNTIME_STDLIB_API
__swift_uint8_t
_swift_stdlib_NSStringGetCStringTrampoline(id _Nonnull obj,
- _swift_shims_UInt8 *_Nonnull buffer,
- _swift_shims_CFIndex maxLength,
- unsigned long encoding);
+ _swift_shims_UInt8 *_Nonnull buffer,
+ _swift_shims_CFIndex maxLength,
+ unsigned long encoding);
SWIFT_RUNTIME_STDLIB_API
__swift_uintptr_t
diff --git a/stdlib/public/core/StringBridge.swift b/stdlib/public/core/StringBridge.swift
index 99bc25f..c23f57f 100644
--- a/stdlib/public/core/StringBridge.swift
+++ b/stdlib/public/core/StringBridge.swift
@@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
-// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
+// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
@@ -47,8 +47,8 @@
mutating: _swift_stdlib_CFStringGetCharactersPtr(source))
}
-/// Copies a slice of a _CocoaString into contiguous storage of
-/// sufficient capacity.
+/// Copies a slice of a _CocoaString into contiguous storage of sufficient
+/// capacity.
@_effects(releasenone)
internal func _cocoaStringCopyCharacters(
from source: _CocoaString,
@@ -81,54 +81,46 @@
@_effects(readonly)
internal func _cocoaHashString(
_ string: _CocoaString
- ) -> UInt {
+) -> UInt {
return _swift_stdlib_CFStringHashNSString(string)
}
@_effects(readonly)
internal func _cocoaHashASCIIBytes(
- _ bytes: UnsafePointer<UInt8>,
- length: Int
- ) -> UInt {
+ _ bytes: UnsafePointer<UInt8>, length: Int
+) -> UInt {
return _swift_stdlib_CFStringHashCString(bytes, length)
}
// These "trampolines" are effectively objc_msgSend_super.
-// They bypass our implementations to use NSString's
+// They bypass our implementations to use NSString's.
@_effects(readonly)
internal func _cocoaCStringUsingEncodingTrampoline(
- _ string: _CocoaString,
- _ encoding: UInt)
- -> UnsafePointer<UInt8>? {
- return _swift_stdlib_NSStringCStringUsingEncodingTrampoline(
- string,
- encoding)
+ _ string: _CocoaString, _ encoding: UInt
+) -> UnsafePointer<UInt8>? {
+ return _swift_stdlib_NSStringCStringUsingEncodingTrampoline(string, encoding)
}
-
-
-
@_effects(releasenone)
internal func _cocoaGetCStringTrampoline(
- _ string: _CocoaString,
- _ buffer: UnsafeMutablePointer<UInt8>,
- _ maxLength: Int,
- _ encoding: UInt)
- -> Int8 {
- return Int8(_swift_stdlib_NSStringGetCStringTrampoline(string,
- buffer,
- maxLength,
- encoding))
+ _ string: _CocoaString,
+ _ buffer: UnsafeMutablePointer<UInt8>,
+ _ maxLength: Int,
+ _ encoding: UInt
+) -> Int8 {
+ return Int8(_swift_stdlib_NSStringGetCStringTrampoline(
+ string, buffer, maxLength, encoding))
}
//
-// Conversion from NSString to Swift's native representation
+// Conversion from NSString to Swift's native representation.
//
private var kCFStringEncodingASCII : _swift_shims_CFStringEncoding {
@inline(__always) get { return 0x0600 }
}
+
private var kCFStringEncodingUTF8 : _swift_shims_CFStringEncoding {
@inline(__always) get { return 0x8000100 }
}
@@ -149,12 +141,12 @@
@inline(__always)
init(_ str: _CocoaString) {
- #if !(arch(i386) || arch(arm))
+#if !(arch(i386) || arch(arm))
if _isObjCTaggedPointer(str) {
self = .tagged
return
}
- #endif
+#endif
switch _unsafeAddressOfCocoaStringClass(str) {
case unsafeBitCast(_StringStorage.self, to: UInt.self):
@@ -168,7 +160,7 @@
}
#if !(arch(i386) || arch(arm))
-// Resiliently write a tagged cocoa string's contents into a buffer
+// Resiliently write a tagged _CocoaString's contents into a buffer.
@_effects(releasenone) // @opaque
internal func _bridgeTagged(
_ cocoa: _CocoaString,
@@ -223,16 +215,18 @@
internal func _bridgeCocoaString(_ cocoaString: _CocoaString) -> _StringGuts {
switch _KnownCocoaString(cocoaString) {
case .storage:
- return _unsafeUncheckedDowncast(cocoaString, to: _StringStorage.self).asString._guts
+ return _unsafeUncheckedDowncast(
+ cocoaString, to: _StringStorage.self).asString._guts
case .shared:
- return _unsafeUncheckedDowncast(cocoaString, to: _SharedStringStorage.self).asString._guts
+ return _unsafeUncheckedDowncast(
+ cocoaString, to: _SharedStringStorage.self).asString._guts
#if !(arch(i386) || arch(arm))
case .tagged:
- return _StringGuts(_SmallString(taggedCocoa: cocoaString))
+ return _StringGuts(_SmallString(taggedCocoa: cocoaString))
#endif
case .cocoa:
- // "copy" it into a value to be sure nobody will modify behind
- // our backs. In practice, when value is already immutable, this
+ // "Copy" it into a value to be sure nobody will modify behind
+ // our backs. In practice, when value is already immutable, this
// just does a retain.
//
// TODO: Only in certain circumstances should we emit this call:
@@ -240,15 +234,14 @@
// 2) If it's mutable with no associated information, then a copy must
// happen; might as well eagerly bridge it in.
// 3) If it's mutable with associated information, must make the call
- //
let immutableCopy
= _stdlib_binary_CFStringCreateCopy(cocoaString) as AnyObject
- #if !(arch(i386) || arch(arm))
+#if !(arch(i386) || arch(arm))
if _isObjCTaggedPointer(immutableCopy) {
return _StringGuts(_SmallString(taggedCocoa: immutableCopy))
}
- #endif
+#endif
let (fastUTF8, isASCII): (Bool, Bool)
switch _getCocoaStringPointer(immutableCopy) {
diff --git a/stdlib/public/core/StringStorage.swift b/stdlib/public/core/StringStorage.swift
index babb40a..f454ca4 100644
--- a/stdlib/public/core/StringStorage.swift
+++ b/stdlib/public/core/StringStorage.swift
@@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
-// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
+// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
@@ -12,7 +12,8 @@
import SwiftShims
-//Having @objc stuff in an extension creates an ObjC category, which we don't want
+// Having @objc stuff in an extension creates an ObjC category, which we don't
+// want.
#if _runtime(_ObjC)
internal protocol _AbstractStringStorage : _NSCopying {
@@ -20,8 +21,7 @@
var count: Int { get }
var isASCII: Bool { get }
var start: UnsafePointer<UInt8> { get }
- //in UTF16 code units
- var length: Int { get }
+ var length: Int { get } // In UTF16 code units.
}
internal let _cocoaASCIIEncoding:UInt = 1 /* NSASCIIStringEncoding */
@@ -45,15 +45,14 @@
extension _AbstractStringStorage {
- // ObjC interfaces
- #if _runtime(_ObjC)
+// ObjC interfaces.
+#if _runtime(_ObjC)
@inline(__always)
@_effects(releasenone)
internal func _getCharacters(
- _ buffer: UnsafeMutablePointer<UInt16>,
- _ aRange: _SwiftNSRange
- ) {
+ _ buffer: UnsafeMutablePointer<UInt16>, _ aRange: _SwiftNSRange
+ ) {
_precondition(aRange.location >= 0 && aRange.length >= 0,
"Range out of bounds")
_precondition(aRange.location + aRange.length <= Int(count),
@@ -70,25 +69,21 @@
@inline(__always)
@_effects(releasenone)
internal func _getCString(
- _ outputPtr: UnsafeMutablePointer<UInt8>,
- _ maxLength: Int,
- _ encoding: UInt)
- -> Int8 {
- switch (encoding, isASCII) {
- case (_cocoaASCIIEncoding, true):
- fallthrough
- case (_cocoaUTF8Encoding, _):
- guard maxLength >= count + 1 else { return 0 }
- let buffer = UnsafeMutableBufferPointer(start: outputPtr, count: maxLength)
- buffer.initialize(from: UnsafeBufferPointer(start: start, count: count))
- buffer[count] = 0
- return 1
- default:
- return _cocoaGetCStringTrampoline(self,
- outputPtr,
- maxLength,
- encoding)
- }
+ _ outputPtr: UnsafeMutablePointer<UInt8>, _ maxLength: Int, _ encoding: UInt
+ ) -> Int8 {
+ switch (encoding, isASCII) {
+ case (_cocoaASCIIEncoding, true):
+ fallthrough
+ case (_cocoaUTF8Encoding, _):
+ guard maxLength >= count + 1 else { return 0 }
+ let buffer =
+ UnsafeMutableBufferPointer(start: outputPtr, count: maxLength)
+ buffer.initialize(from: UnsafeBufferPointer(start: start, count: count))
+ buffer[count] = 0
+ return 1
+ default:
+ return _cocoaGetCStringTrampoline(self, outputPtr, maxLength, encoding)
+ }
}
@inline(__always)
@@ -105,66 +100,69 @@
}
@_effects(readonly)
- internal func _nativeIsEqual<T:_AbstractStringStorage>(_ nativeOther: T) -> Int8 {
+ internal func _nativeIsEqual<T:_AbstractStringStorage>(
+ _ nativeOther: T
+ ) -> Int8 {
if count != nativeOther.count {
return 0
}
- return (start == nativeOther.start || (memcmp(start, nativeOther.start, count) == 0)) ? 1 : 0
+ return (start == nativeOther.start ||
+ (memcmp(start, nativeOther.start, count) == 0)) ? 1 : 0
}
@inline(__always)
@_effects(readonly)
- internal func _isEqual(_ other:AnyObject?)
- -> Int8 {
- guard let other = other else {
+ internal func _isEqual(_ other: AnyObject?) -> Int8 {
+ guard let other = other else {
+ return 0
+ }
+
+ if self === other {
+ return 1
+ }
+
+ // Handle the case where both strings were bridged from Swift.
+ // We can't use String.== because it doesn't match NSString semantics.
+ let knownOther = _KnownCocoaString(other)
+ switch knownOther {
+ case .storage:
+ return _nativeIsEqual(
+ _unsafeUncheckedDowncast(other, to: _StringStorage.self))
+ case .shared:
+ return _nativeIsEqual(
+ _unsafeUncheckedDowncast(other, to: _SharedStringStorage.self))
+#if !(arch(i386) || arch(arm))
+ case .tagged:
+ fallthrough
+#endif
+ case .cocoa:
+ // We're allowed to crash, but for compatibility reasons NSCFString allows
+ // non-strings here.
+ if _isNSString(other) != 1 {
return 0
}
-
- if self === other {
- return 1
+ // At this point we've proven that it is an NSString of some sort, but not
+ // one of ours.
+ if length != _stdlib_binary_CFStringGetLength(other) {
+ return 0
}
-
- // Handle the case where both strings were bridged from Swift.
- // We can't use String.== because it doesn't match NSString semantics.
- let knownOther = _KnownCocoaString(other)
- switch knownOther {
- case .storage:
- return _nativeIsEqual(_unsafeUncheckedDowncast(other, to: _StringStorage.self))
- case .shared:
- return _nativeIsEqual(_unsafeUncheckedDowncast(other, to: _SharedStringStorage.self))
-#if !(arch(i386) || arch(arm))
- case .tagged:
- fallthrough
-#endif
- case .cocoa:
- //we're allowed to crash, but for compatibility reasons NSCFString allows non-strings here
- if _isNSString(other) != 1 {
- return 0
- }
-
- //At this point we've proven that it is an NSString of some sort, but not one of ours
- if length != _stdlib_binary_CFStringGetLength(other) {
- return 0
- }
-
- defer { _fixLifetime(other) }
-
- //CFString will only give us ASCII bytes here, but that's fine
- //We already handled non-ASCII UTF8 strings earlier since they're Swift
- if let otherStart = _cocoaUTF8Pointer(other) {
- return (start == otherStart || (memcmp(start, otherStart, count) == 0)) ? 1 : 0
- }
-
- /*
- The abstract implementation of -isEqualToString: falls back to -compare:
- immediately, so when we run out of fast options to try, do the same.
- We can likely be more clever here if need be
- */
- return _cocoaStringCompare(self, other) == 0 ? 1 : 0
+ defer { _fixLifetime(other) }
+ // CFString will only give us ASCII bytes here, but that's fine.
+ // We already handled non-ASCII UTF8 strings earlier since they're Swift.
+ if let otherStart = _cocoaUTF8Pointer(other) {
+ return (start == otherStart ||
+ (memcmp(start, otherStart, count) == 0)) ? 1 : 0
}
+ /*
+ The abstract implementation of -isEqualToString: falls back to -compare:
+ immediately, so when we run out of fast options to try, do the same.
+ We can likely be more clever here if need be
+ */
+ return _cocoaStringCompare(self, other) == 0 ? 1 : 0
+ }
}
- #endif //_runtime(_ObjC)
+#endif //_runtime(_ObjC)
}
#if arch(i386) || arch(arm)
@@ -178,16 +176,14 @@
// Optional<_StringBreadcrumbs>.
//
-final internal class _StringStorage: __SwiftNativeNSString, _AbstractStringStorage {
+final internal class _StringStorage
+ : __SwiftNativeNSString, _AbstractStringStorage {
#if arch(i386) || arch(arm)
// The total allocated storage capacity. Note that this includes the required
- // nul-terminator
+ // nul-terminator.
internal var _realCapacity: Int
-
internal var _count: Int
-
internal var _flags: _StringObject.Flags
-
internal var _reserved: UInt16
internal var count: Int {
@@ -196,9 +192,8 @@
}
#else
// The capacity of our allocation. Note that this includes the nul-terminator,
- // which is not available for overridding.
+ // which is not available for overriding.
internal var _realCapacityAndFlags: UInt64
-
internal var _countAndFlags: _StringObject.CountAndFlags
internal var count: Int {
@@ -207,7 +202,7 @@
}
// The total allocated storage capacity. Note that this includes the required
- // nul-terminator
+ // nul-terminator.
internal var _realCapacity: Int {
@inline(__always) get {
return Int(truncatingIfNeeded:
@@ -216,13 +211,13 @@
}
#endif
- internal final var isASCII: Bool {
+ final internal var isASCII: Bool {
@inline(__always) get {
- #if arch(i386) || arch(arm)
+#if arch(i386) || arch(arm)
return _flags.isASCII
- #else
+#else
return _countAndFlags.isASCII
- #endif
+#endif
}
}
@@ -237,11 +232,12 @@
@objc(length)
final internal var length: Int {
@_effects(readonly) @inline(__always) get {
- return asString.utf16.count //UTF16View special-cases ASCII for us
+ return asString.utf16.count // UTF16View special-cases ASCII for us.
}
}
- @objc final internal var hash: UInt {
+ @objc
+ final internal var hash: UInt {
@_effects(readonly) get {
if isASCII {
return _cocoaHashASCIIBytes(start, length: count)
@@ -260,18 +256,19 @@
@objc(getCharacters:range:)
@_effects(releasenone)
final internal func getCharacters(
- _ buffer: UnsafeMutablePointer<UInt16>,
- range aRange: _SwiftNSRange) {
+ _ buffer: UnsafeMutablePointer<UInt16>, range aRange: _SwiftNSRange
+ ) {
_getCharacters(buffer, aRange)
}
@objc(_fastCStringContents:)
@_effects(readonly)
- final internal func _fastCStringContents(_ requiresNulTermination:Int8) -> UnsafePointer<CChar>? {
+ final internal func _fastCStringContents(
+ _ requiresNulTermination: Int8
+ ) -> UnsafePointer<CChar>? {
if isASCII {
return start._asCChar
}
-
return nil
}
@@ -289,10 +286,9 @@
@objc(getCString:maxLength:encoding:)
@_effects(releasenone)
- final internal func getCString(_ outputPtr: UnsafeMutablePointer<UInt8>,
- maxLength: Int,
- encoding: UInt)
- -> Int8 {
+ final internal func getCString(
+ _ outputPtr: UnsafeMutablePointer<UInt8>, maxLength: Int, encoding: UInt
+ ) -> Int8 {
return _getCString(outputPtr, maxLength, encoding)
}
@@ -308,7 +304,7 @@
@objc(isEqualToString:)
@_effects(readonly)
- final internal func isEqual(to other:AnyObject?) -> Int8 {
+ final internal func isEqual(to other: AnyObject?) -> Int8 {
return _isEqual(other)
}
@@ -352,11 +348,11 @@
_internalInvariant(capacity > desiredCapacity)
return capacity
#else
- // Bigger than _SmallString, and we need 1 extra for nul-terminator
+ // Bigger than _SmallString, and we need 1 extra for nul-terminator.
let minCap = 1 + Swift.max(desiredCapacity, _SmallString.capacity)
_internalInvariant(minCap < 0x1_0000_0000_0000, "max 48-bit length")
- // Round up to the nearest multiple of 8 that isn't also a multiple of 16
+ // Round up to the nearest multiple of 8 that isn't also a multiple of 16.
let capacity = ((minCap + 7) & -16) + 8
_internalInvariant(
capacity > desiredCapacity && capacity % 8 == 0 && capacity % 16 != 0)
@@ -455,7 +451,7 @@
@inline(__always) get { return UnsafePointer(mutableEnd) }
}
- // Point to the nul-terminator
+ // Point to the nul-terminator.
private final var terminator: UnsafeMutablePointer<UInt8> {
@inline(__always) get { return mutableEnd }
}
@@ -477,7 +473,7 @@
}
// The total capacity available for code units. Note that this excludes the
- // required nul-terminator
+ // required nul-terminator.
internal var capacity: Int {
return _realCapacity &- 1
}
@@ -494,7 +490,7 @@
}
// The capacity available for appending. Note that this excludes the required
- // nul-terminator
+ // nul-terminator.
internal var unusedCapacity: Int {
get { return _realCapacity &- count &- 1 }
}
@@ -528,7 +524,7 @@
// Appending
extension _StringStorage {
- // Perform common post-RRC adjustments and invariant enforcement
+ // Perform common post-RRC adjustments and invariant enforcement.
@_effects(releasenone)
private func _postRRCAdjust(newCount: Int, newIsASCII: Bool) {
#if arch(i386) || arch(arm)
@@ -540,12 +536,12 @@
#endif
self.terminator.pointee = 0
- // TODO(String performance): Consider updating breadcrumbs when feasible
+ // TODO(String performance): Consider updating breadcrumbs when feasible.
self._breadcrumbsAddress.pointee = nil
_invariantCheck()
}
- // Perform common post-append adjustments and invariant enforcement
+ // Perform common post-append adjustments and invariant enforcement.
@_effects(releasenone)
private func _postAppendAdjust(
appendedCount: Int, appendedIsASCII isASCII: Bool
@@ -621,12 +617,12 @@
let replCount = replacement.count
_internalInvariant(replCount - (upper - lower) <= unusedCapacity)
- // Position the tail
+ // Position the tail.
let lowerPtr = mutableStart + lower
let tailCount = _slideTail(
src: mutableStart + upper, dst: lowerPtr + replCount)
- // Copy in the contents
+ // Copy in the contents.
lowerPtr.moveInitialize(
from: UnsafeMutablePointer(
mutating: replacement.baseAddress._unsafelyUnwrappedUnchecked),
@@ -647,12 +643,12 @@
_internalInvariant(lower <= upper)
_internalInvariant(replCount - (upper - lower) <= unusedCapacity)
- // Position the tail
+ // Position the tail.
let lowerPtr = mutableStart + lower
let tailCount = _slideTail(
src: mutableStart + upper, dst: lowerPtr + replCount)
- // Copy in the contents
+ // Copy in the contents.
var isASCII = self.isASCII
var srcCount = 0
for cu in replacement {
@@ -668,14 +664,13 @@
}
// For shared storage and bridging literals
-final internal class _SharedStringStorage: __SwiftNativeNSString, _AbstractStringStorage {
+final internal class _SharedStringStorage
+ : __SwiftNativeNSString, _AbstractStringStorage {
internal var _owner: AnyObject?
-
internal var start: UnsafePointer<UInt8>
#if arch(i386) || arch(arm)
internal var _count: Int
-
internal var _flags: _StringObject.Flags
@inlinable
@@ -692,11 +687,11 @@
internal var count: Int {
@_effects(readonly) @inline(__always) get {
- #if arch(i386) || arch(arm)
- return _count
- #else
- return _countAndFlags.count
- #endif
+#if arch(i386) || arch(arm)
+ return _count
+#else
+ return _countAndFlags.count
+#endif
}
}
@@ -716,13 +711,13 @@
self._invariantCheck()
}
- internal final var isASCII: Bool {
+ final internal var isASCII: Bool {
@inline(__always) get {
- #if arch(i386) || arch(arm)
+#if arch(i386) || arch(arm)
return _flags.isASCII
- #else
+#else
return _countAndFlags.isASCII
- #endif
+#endif
}
}
@@ -737,11 +732,12 @@
@objc(length)
final internal var length: Int {
@_effects(readonly) get {
- return asString.utf16.count //UTF16View special-cases ASCII for us
+ return asString.utf16.count // UTF16View special-cases ASCII for us.
}
}
- @objc final internal var hash: UInt {
+ @objc
+ final internal var hash: UInt {
@_effects(readonly) get {
if isASCII {
return _cocoaHashASCIIBytes(start, length: count)
@@ -760,8 +756,8 @@
@objc(getCharacters:range:)
@_effects(releasenone)
final internal func getCharacters(
- _ buffer: UnsafeMutablePointer<UInt16>,
- range aRange: _SwiftNSRange) {
+ _ buffer: UnsafeMutablePointer<UInt16>, range aRange: _SwiftNSRange
+ ) {
_getCharacters(buffer, aRange)
}
@@ -777,11 +773,12 @@
@objc(_fastCStringContents:)
@_effects(readonly)
- final internal func _fastCStringContents(_ requiresNulTermination:Int8) -> UnsafePointer<CChar>? {
+ final internal func _fastCStringContents(
+ _ requiresNulTermination: Int8
+ ) -> UnsafePointer<CChar>? {
if isASCII {
return start._asCChar
}
-
return nil
}
@@ -799,10 +796,9 @@
@objc(getCString:maxLength:encoding:)
@_effects(releasenone)
- final internal func getCString(_ outputPtr: UnsafeMutablePointer<UInt8>,
- maxLength: Int,
- encoding: UInt)
- -> Int8 {
+ final internal func getCString(
+ _ outputPtr: UnsafeMutablePointer<UInt8>, maxLength: Int, encoding: UInt
+ ) -> Int8 {
return _getCString(outputPtr, maxLength, encoding)
}
@@ -826,16 +822,15 @@
}
extension _SharedStringStorage {
- #if !INTERNAL_CHECKS_ENABLED
- @inline(__always) internal func _invariantCheck() {}
- #else
+#if !INTERNAL_CHECKS_ENABLED
+ @inline(__always)
+ internal func _invariantCheck() {}
+#else
internal func _invariantCheck() {
if let crumbs = _breadcrumbs {
crumbs._invariantCheck(for: self.asString)
}
_countAndFlags._invariantCheck()
}
- #endif // INTERNAL_CHECKS_ENABLED
+#endif // INTERNAL_CHECKS_ENABLED
}
-
-