Skip to content

Commit 67f4ae0

Browse files
authored
Merge pull request #5723 from natecook1000/nc-fixes-05
2 parents 0773731 + 534bea8 commit 67f4ae0

23 files changed

+758
-202
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 114 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public struct _DependenceToken {}
4141
%{
4242
if True:
4343
contiguousCaveat = (
44-
' If no such storage exists, it is first created.' if Self == 'Array'
44+
' If no such storage exists, it is created.' if Self == 'Array'
4545
else '')
4646

4747
if Self == 'ContiguousArray':
@@ -397,7 +397,7 @@ if True:
397397
/// Bridging Between Array and NSArray
398398
/// ==================================
399399
///
400-
/// When you need to access APIs that expect data in an `NSArray` instance
400+
/// When you need to access APIs that require data in an `NSArray` instance
401401
/// instead of `Array`, use the type-cast operator (`as`) to bridge your
402402
/// instance. For bridging to be possible, the `Element` type of your array
403403
/// must be a class, an `@objc` protocol (a protocol imported from Objective-C
@@ -406,9 +406,9 @@ if True:
406406
///
407407
/// The following example shows how you can bridge an `Array` instance to
408408
/// `NSArray` to use the `write(to:atomically:)` method. In this example, the
409-
/// `colors` array can be bridged to `NSArray` because its `String` elements
410-
/// bridge to `NSString`. The compiler prevents bridging the `moreColors`
411-
/// array, on the other hand, because its `Element` type is
409+
/// `colors` array can be bridged to `NSArray` because the `colors` array's
410+
/// `String` elements bridge to `NSString`. The compiler prevents bridging the
411+
/// `moreColors` array, on the other hand, because its `Element` type is
412412
/// `Optional<String>`, which does *not* bridge to a Foundation type.
413413
///
414414
/// let colors = ["periwinkle", "rose", "moss"]
@@ -425,14 +425,23 @@ if True:
425425
/// array's elements are already instances of a class or an `@objc` protocol;
426426
/// otherwise, it takes O(*n*) time and space.
427427
///
428-
/// Bridging from `NSArray` to `Array` first calls the `copy(with:)`
428+
/// When the destination array's element type is a class or an `@objc`
429+
/// protocol, bridging from `NSArray` to `Array` first calls the `copy(with:)`
429430
/// (`- copyWithZone:` in Objective-C) method on the array to get an immutable
430431
/// copy and then performs additional Swift bookkeeping work that takes O(1)
431432
/// time. For instances of `NSArray` that are already immutable, `copy(with:)`
432433
/// usually returns the same array in O(1) time; otherwise, the copying
433-
/// performance is unspecified. The instances of `NSArray` and `Array` share
434-
/// storage using the same copy-on-write optimization that is used when two
435-
/// instances of `Array` share storage.
434+
/// performance is unspecified. If `copy(with:)` returns the same array, the
435+
/// instances of `NSArray` and `Array` share storage using the same
436+
/// copy-on-write optimization that is used when two instances of `Array`
437+
/// share storage.
438+
///
439+
/// When the destination array's element type is a nonclass type that bridges
440+
/// to a Foundation type, bridging from `NSArray` to `Array` performs a
441+
/// bridging copy of the elements to contiguous storage in O(*n*) time. For
442+
/// example, bridging from `NSArray` to `Array<Int>` performs such a copy. No
443+
/// further bridging is required when accessing elements of the `Array`
444+
/// instance.
436445
///
437446
/// - Note: The `ContiguousArray` and `ArraySlice` types are not bridged;
438447
/// instances of those types always have a contiguous block of memory as
@@ -1135,24 +1144,31 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
11351144
return _getCount()
11361145
}
11371146

1138-
/// The total number of elements that the array can contain using its current
1139-
/// storage.
1147+
/// The total number of elements that the array can contain without
1148+
/// allocating new storage.
11401149
///
1141-
/// If the array grows larger than its capacity, it discards its current
1142-
/// storage and allocates a larger one.
1150+
/// Every array reserves a specific amount of memory to hold its contents.
1151+
/// When you add elements to an array and that array begins to exceed its
1152+
/// reserved capacity, the array allocates a larger region of memory and
1153+
/// copies its elements into the new storage. The new storage is a multiple
1154+
/// of the old storage's size. This exponential growth strategy means that
1155+
/// appending an element happens in constant time, averaging the performance
1156+
/// of many append operations. Append operations that trigger reallocation
1157+
/// have a performance cost, but they occur less and less often as the array
1158+
/// grows larger.
11431159
///
11441160
/// The following example creates an array of integers from an array literal,
11451161
/// then appends the elements of another collection. Before appending, the
11461162
/// array allocates new storage that is large enough store the resulting
11471163
/// elements.
11481164
///
11491165
/// var numbers = [10, 20, 30, 40, 50]
1150-
/// print("Count: \(numbers.count), capacity: \(numbers.capacity)")
1151-
/// // Prints "Count: 5, capacity: 5"
1166+
/// // numbers.count == 5
1167+
/// // numbers.capacity == 5
11521168
///
11531169
/// numbers.append(contentsOf: stride(from: 60, through: 100, by: 10))
1154-
/// print("Count: \(numbers.count), capacity: \(numbers.capacity)")
1155-
/// // Prints "Count: 10, capacity: 12"
1170+
/// // numbers.count == 10
1171+
/// // numbers.capacity == 12
11561172
public var capacity: Int {
11571173
return _getCapacity()
11581174
}
@@ -1184,13 +1200,19 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
11841200
/// unique, mutable, contiguous storage, with space allocated for at least
11851201
/// the requested number of elements.
11861202
///
1187-
/// For performance reasons, the newly allocated storage may be larger than
1188-
/// the requested capacity. Use the array's `capacity` property to determine
1189-
/// the size of the new storage.
1203+
% if Self != 'ContiguousArray':
1204+
/// Calling the `reserveCapacity(_:)` method on an array with bridged storage
1205+
/// triggers a copy to contiguous storage even if the existing storage
1206+
/// has room to store `minimumCapacity` elements.
1207+
///
1208+
% end
1209+
/// For performance reasons, the size of the newly allocated storage might be
1210+
/// greater than the requested capacity. Use the array's `capacity` property
1211+
/// to determine the size of the new storage.
11901212
///
11911213
/// - Parameter minimumCapacity: The requested number of elements to store.
11921214
///
1193-
/// - Complexity: O(*n*), where *n* is the count of the array.
1215+
/// - Complexity: O(*n*), where *n* is the number of elements in the array.
11941216
@_semantics("array.mutate_unknown")
11951217
public mutating func reserveCapacity(_ minimumCapacity: Int) {
11961218
if _buffer.requestUniqueMutableBackingBuffer(
@@ -1515,7 +1537,6 @@ extension ${Self} {
15151537

15161538
extension ${Self} {
15171539
/// Calls a closure with a pointer to the array's contiguous storage.
1518-
/// ${contiguousCaveat}
15191540
///
15201541
/// Often, the optimizer can eliminate bounds checks within an array
15211542
/// algorithm, but when that fails, invoking the same algorithm on the
@@ -1534,9 +1555,13 @@ extension ${Self} {
15341555
/// }
15351556
/// // 'sum' == 9
15361557
///
1558+
/// The pointer passed as an argument to `body` is valid only for the
1559+
/// lifetime of the closure. Do not escape it from the closure for later
1560+
/// use.
1561+
///
15371562
/// - Parameter body: A closure with an `UnsafeBufferPointer` parameter that
1538-
/// points to the contiguous storage for the array. If `body` has a return
1539-
/// value, it is used as the return value for the
1563+
/// points to the contiguous storage for the array. ${contiguousCaveat} If
1564+
/// `body` has a return value, it is used as the return value for the
15401565
/// `withUnsafeBufferPointer(_:)` method. The pointer argument is valid
15411566
/// only for the duration of the closure's execution.
15421567
/// - Returns: The return value of the `body` closure parameter, if any.
@@ -1549,13 +1574,13 @@ extension ${Self} {
15491574
}
15501575

15511576
/// Calls the given closure with a pointer to the array's mutable contiguous
1552-
/// storage.${contiguousCaveat}
1577+
/// storage.
15531578
///
15541579
/// Often, the optimizer can eliminate bounds checks within an array
15551580
/// algorithm, but when that fails, invoking the same algorithm on the
15561581
/// buffer pointer passed into your closure lets you trade safety for speed.
15571582
///
1558-
/// The following example shows modifying the contents of the
1583+
/// The following example shows how modifying the contents of the
15591584
/// `UnsafeMutableBufferPointer` argument to `body` alters the contents of
15601585
/// the array:
15611586
///
@@ -1568,16 +1593,21 @@ extension ${Self} {
15681593
/// print(numbers)
15691594
/// // Prints "[2, 1, 4, 3, 5]"
15701595
///
1571-
/// - Warning: Do not rely on anything about `self` (the array that is the
1572-
/// target of this method) during the execution of the `body` closure: It
1573-
/// may not appear to have its correct value. Instead, use only the
1596+
/// The pointer passed as an argument to `body` is valid only for the
1597+
/// lifetime of the closure. Do not escape it from the closure for later
1598+
/// use.
1599+
///
1600+
/// - Warning: Do not rely on anything about the array that is the target of
1601+
/// this method during execution of the `body` closure; it might not
1602+
/// appear to have its correct value. Instead, use only the
15741603
/// `UnsafeMutableBufferPointer` argument to `body`.
15751604
///
15761605
/// - Parameter body: A closure with an `UnsafeMutableBufferPointer`
1577-
/// parameter that points to the contiguous storage for the array. If
1578-
/// `body` has a return value, it is used as the return value for the
1579-
/// `withUnsafeMutableBufferPointer(_:)` method. The pointer argument is
1580-
/// valid only for the duration of the closure's execution.
1606+
/// parameter that points to the contiguous storage for the array.
1607+
/// ${contiguousCaveat} If `body` has a return value, it is used as the
1608+
/// return value for the `withUnsafeMutableBufferPointer(_:)` method. The
1609+
/// pointer argument is valid only for the duration of the closure's
1610+
/// execution.
15811611
/// - Returns: The return value of the `body` closure parameter, if any.
15821612
///
15831613
/// - SeeAlso: `withUnsafeBufferPointer`, `UnsafeMutableBufferPointer`
@@ -2099,28 +2129,42 @@ public func != <Element : Equatable>(
20992129
}
21002130

21012131
extension ${Self} {
2102-
/// Calls a closure with a view of the array's underlying bytes of memory as a
2103-
/// Collection of `UInt8`.
2104-
///
2105-
/// ${contiguousCaveat}
2132+
/// Calls the given closure with a pointer to the underlying bytes of the
2133+
/// array's mutable contiguous storage.
21062134
///
2107-
/// - Precondition: `Pointee` is a trivial type.
2135+
/// The array's `Element` type must be a *trivial type*, which can be copied
2136+
/// with just a bit-for-bit copy without any indirection or
2137+
/// reference-counting operations. Generally, native Swift types that do not
2138+
/// contain strong or weak references are trivial, as are imported C structs
2139+
/// and enums.
21082140
///
2109-
/// The following example shows how you copy bytes into an array:
2141+
/// The following example copies bytes from the `byteValues` array into
2142+
/// `numbers`, an array of `Int`:
21102143
///
2111-
/// var numbers = [Int32](repeating: 0, count: 2)
2112-
/// var byteValues: [UInt8] = [0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]
2113-
/// numbers.withUnsafeMutableBytes { destBytes in
2114-
/// byteValues.withUnsafeBytes { srcBytes in
2115-
/// destBytes.copyBytes(from: srcBytes)
2116-
/// }
2117-
/// }
2144+
/// var numbers: [Int32] = [0, 0]
2145+
/// var byteValues: [UInt8] = [0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00]
21182146
///
2119-
/// - Parameter body: A closure with an `UnsafeRawBufferPointer`
2120-
/// parameter that points to the contiguous storage for the array. If `body`
2121-
/// has a return value, it is used as the return value for the
2122-
/// `withUnsafeMutableBytes(_:)` method. The argument is valid only for the
2123-
/// duration of the closure's execution.
2147+
/// numbers.withUnsafeMutableBytes { destBytes in
2148+
/// byteValues.withUnsafeBytes { srcBytes in
2149+
/// destBytes.copyBytes(from: srcBytes)
2150+
/// }
2151+
/// }
2152+
/// // numbers == [1, 2]
2153+
///
2154+
/// The pointer passed as an argument to `body` is valid only for the
2155+
/// lifetime of the closure. Do not escape it from the closure for later
2156+
/// use.
2157+
///
2158+
/// - Warning: Do not rely on anything about the array that is the target of
2159+
/// this method during execution of the `body` closure; it might not
2160+
/// appear to have its correct value. Instead, use only the
2161+
/// `UnsafeMutableRawBufferPointer` argument to `body`.
2162+
///
2163+
/// - Parameter body: A closure with an `UnsafeMutableRawBufferPointer`
2164+
/// parameter that points to the contiguous storage for the array.
2165+
/// ${contiguousCaveat} If `body` has a return value, it is used as the
2166+
/// return value for the `withUnsafeMutableBytes(_:)` method. The argument
2167+
/// is valid only for the duration of the closure's execution.
21242168
/// - Returns: The return value of the `body` closure parameter, if any.
21252169
///
21262170
/// - SeeAlso: `withUnsafeBytes`, `UnsafeMutableRawBufferPointer`
@@ -2132,30 +2176,33 @@ extension ${Self} {
21322176
}
21332177
}
21342178

2135-
/// Calls a closure with a view of the array's underlying bytes of memory
2136-
/// as a Collection of `UInt8`.
2137-
///
2138-
/// ${contiguousCaveat}
2179+
/// Calls the given closure with a pointer to the underlying bytes of the
2180+
/// array's contiguous storage.
21392181
///
2140-
/// - Precondition: `Pointee` is a trivial type.
2182+
/// The array's `Element` type must be a *trivial type*, which can be copied
2183+
/// with just a bit-for-bit copy without any indirection or
2184+
/// reference-counting operations. Generally, native Swift types that do not
2185+
/// contain strong or weak references are trivial, as are imported C structs
2186+
/// and enums.
21412187
///
2142-
/// The following example shows how you copy the contents of an array into a
2188+
/// The following example copies the bytes of the `numbers` array into a
21432189
/// buffer of `UInt8`:
21442190
///
2145-
/// let numbers = [1, 2, 3]
2146-
/// var byteBuffer = [UInt8]()
2147-
/// numbers.withUnsafeBytes {
2148-
/// byteBuffer += $0
2149-
/// }
2191+
/// var numbers = [1, 2, 3]
2192+
/// var byteBuffer: [UInt8] = []
2193+
/// numbers.withUnsafeBytes {
2194+
/// byteBuffer.append(contentsOf: $0)
2195+
/// }
2196+
/// // byteBuffer == [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, ...]
21502197
///
21512198
/// - Parameter body: A closure with an `UnsafeRawBufferPointer` parameter
2152-
/// that points to the contiguous storage for the array. If `body` has a
2153-
/// return value, it is used as the return value for the
2154-
/// `withUnsafeBytes(_:)` method. The argument is valid only for the
2155-
/// duration of the closure's execution.
2199+
/// that points to the contiguous storage for the array.
2200+
/// ${contiguousCaveat} If `body` has a return value, it is used as the
2201+
/// return value for the `withUnsafeBytes(_:)` method. The argument is
2202+
/// valid only for the duration of the closure's execution.
21562203
/// - Returns: The return value of the `body` closure parameter, if any.
21572204
///
2158-
/// - SeeAlso: `withUnsafeBytes`, `UnsafeRawBufferPointer`
2205+
/// - SeeAlso: `withUnsafeMutableBytes`, `UnsafeRawBufferPointer`
21592206
public func withUnsafeBytes<R>(
21602207
_ body: (UnsafeRawBufferPointer) throws -> R
21612208
) rethrows -> R {

stdlib/public/core/Bool.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414

1515
/// A value type whose instances are either `true` or `false`.
1616
///
17-
/// `Bool` represents Boolean values in Swift. Create instances of
18-
/// `Bool` by using one of the Boolean literals `true` and `false` or by
19-
/// assigning the result of a Boolean method or operation to a variable or
20-
/// constant.
17+
/// `Bool` represents Boolean values in Swift. Create instances of `Bool` by
18+
/// using one of the Boolean literals `true` or `false`, or by assigning the
19+
/// result of a Boolean method or operation to a variable or constant.
2120
///
2221
/// var godotHasArrived = false
2322
///
@@ -33,8 +32,8 @@
3332
///
3433
/// Swift uses only simple Boolean values in conditional contexts to help avoid
3534
/// accidental programming errors and to help maintain the clarity of each
36-
/// control statement. Unlike other programming languages, in Swift integers
37-
/// and strings cannot be used where a Boolean value is expected.
35+
/// control statement. Unlike in other programming languages, in Swift, integers
36+
/// and strings cannot be used where a Boolean value is required.
3837
///
3938
/// For example, the following code sample does not compile, because it
4039
/// attempts to use the integer `i` in a logical context:
@@ -52,13 +51,21 @@
5251
/// print(i)
5352
/// i -= 1
5453
/// }
54+
///
55+
/// Using Imported Boolean values
56+
/// =============================
57+
///
58+
/// The C `bool` and `Boolean` types and the Objective-C `BOOL` type are all
59+
/// bridged into Swift as `Bool`. The single `Bool` type in Swift guarantees
60+
/// that functions, methods, and properties imported from C and Objective-C
61+
/// have a consistent type interface.
5562
@_fixed_layout
5663
public struct Bool {
5764
internal var _value: Builtin.Int1
5865

5966
/// Creates an instance initialized to `false`.
6067
///
61-
/// Don't call this initializer directly. Instead, use the Boolean literal
68+
/// Do not call this initializer directly. Instead, use the Boolean literal
6269
/// `false` to create a new `Bool` instance.
6370
@_transparent
6471
public init() {
@@ -85,7 +92,7 @@ extension Bool : _ExpressibleByBuiltinBooleanLiteral, ExpressibleByBooleanLitera
8592
///
8693
/// Do not call this initializer directly. It is used by the compiler when
8794
/// you use a Boolean literal. Instead, create a new `Bool` instance by
88-
/// using one of the Boolean literals `true` and `false`.
95+
/// using one of the Boolean literals `true` or `false`.
8996
///
9097
/// var printedMessage = false
9198
///

stdlib/public/core/Character.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public struct Character :
106106

107107
/// Creates a character with the specified value.
108108
///
109-
/// Don't call this initializer directly. It is used by the compiler when you
109+
/// Do not call this initializer directly. It is used by the compiler when you
110110
/// use a string literal to initialize a `Character` instance. For example:
111111
///
112112
/// let snowflake: Character = "❄︎"
@@ -134,8 +134,9 @@ public struct Character :
134134

135135
/// Creates a character with the specified value.
136136
///
137-
/// Don't call this initializer directly. It is used by the compiler when you
138-
/// use a string literal to initialize a `Character` instance. For example:
137+
/// Do not call this initializer directly. It is used by the compiler when
138+
/// you use a string literal to initialize a `Character` instance. For
139+
/// example:
139140
///
140141
/// let oBreve: Character = "o\u{306}"
141142
/// print(oBreve)

0 commit comments

Comments
 (0)