Skip to content

Commit 5f2bbd9

Browse files
authored
Merge pull request #1599 from swiftlang/future
Merge `future` into `main`
2 parents d8533a2 + 4dcbbcc commit 5f2bbd9

38 files changed

+9710
-2284
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ list(APPEND _SwiftFoundation_versions
102102
"6.1"
103103
"6.2"
104104
"6.3"
105+
"6.4"
105106
)
106107

107108
# Each availability name to define

Package.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import CompilerPluginSupport
99
let availabilityTags: [_Availability] = [
1010
_Availability("FoundationPreview"), // Default FoundationPreview availability
1111
]
12-
let versionNumbers = ["6.0.2", "6.1", "6.2", "6.3"]
12+
let versionNumbers = ["6.0.2", "6.1", "6.2", "6.3", "6.4"]
1313

1414
// Availability Macro Utilities
1515

@@ -140,7 +140,8 @@ let package = Package(
140140
"ProcessInfo/CMakeLists.txt",
141141
"FileManager/CMakeLists.txt",
142142
"URL/CMakeLists.txt",
143-
"NotificationCenter/CMakeLists.txt"
143+
"NotificationCenter/CMakeLists.txt",
144+
"ProgressManager/CMakeLists.txt",
144145
],
145146
cSettings: [
146147
.define("_GNU_SOURCE", .when(platforms: [.linux]))
@@ -185,7 +186,7 @@ let package = Package(
185186
"Locale/CMakeLists.txt",
186187
"Calendar/CMakeLists.txt",
187188
"CMakeLists.txt",
188-
"Predicate/CMakeLists.txt"
189+
"Predicate/CMakeLists.txt",
189190
],
190191
cSettings: wasiLibcCSettings,
191192
swiftSettings: [

Sources/FoundationEssentials/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_library(FoundationEssentials
2727
Logging.swift
2828
OutputBuffer.swift
2929
Platform.swift
30+
Progress+Stub.swift
3031
SortComparator.swift
3132
UUID_Wrappers.swift
3233
UUID.swift
@@ -45,6 +46,7 @@ add_subdirectory(Locale)
4546
add_subdirectory(NotificationCenter)
4647
add_subdirectory(Predicate)
4748
add_subdirectory(ProcessInfo)
49+
add_subdirectory(ProgressManager)
4850
add_subdirectory(PropertyList)
4951
add_subdirectory(String)
5052
add_subdirectory(TimeZone)

Sources/FoundationEssentials/Data/CMakeLists.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
##
33
## This source file is part of the Swift open source project
44
##
5-
## Copyright (c) 2024 Apple Inc. and the Swift project authors
5+
## Copyright (c) 2024-2025 Apple Inc. and the Swift project authors
66
## Licensed under Apache License v2.0
77
##
88
## See LICENSE.txt for license information
@@ -13,13 +13,23 @@
1313
##===----------------------------------------------------------------------===##
1414

1515
target_sources(FoundationEssentials PRIVATE
16+
Representations/Data+Inline.swift
17+
Representations/Data+InlineSlice.swift
18+
Representations/Data+LargeSlice.swift
19+
Representations/Data+Representation.swift
20+
Representations/DataStorage.swift
21+
1622
Collections+DataProtocol.swift
1723
ContiguousBytes.swift
24+
Data.swift
1825
Data+Base64.swift
26+
Data+Bridging.swift
27+
Data+Deprecated.swift
1928
Data+Error.swift
29+
Data+Iterator.swift
2030
Data+Reading.swift
21-
Data+Stub.swift
31+
Data+Searching.swift
2232
Data+Writing.swift
23-
Data.swift
2433
DataProtocol.swift
34+
PathOrURL.swift
2535
Pointers+DataProtocol.swift)

Sources/FoundationEssentials/Data/ContiguousBytes.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2018-2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -39,6 +39,9 @@ extension ArraySlice : ContiguousBytes where Element == UInt8 { }
3939
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
4040
extension ContiguousArray : ContiguousBytes where Element == UInt8 { }
4141

42+
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
43+
extension Data : ContiguousBytes { }
44+
4245
//===--- Pointer Conformances ---------------------------------------------===//
4346

4447
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)

Sources/FoundationEssentials/Data/Data+Base64.swift

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2023-2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023-2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -25,6 +25,56 @@ import WinSDK
2525
import WASILibc
2626
#endif
2727

28+
#if !FOUNDATION_FRAMEWORK
29+
extension Data {
30+
31+
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
32+
public struct Base64EncodingOptions : OptionSet, Sendable {
33+
public let rawValue: UInt
34+
35+
public init(rawValue: UInt) {
36+
self.rawValue = rawValue
37+
}
38+
/// Set the maximum line length to 64 characters, after which a line ending is inserted.
39+
public static let lineLength64Characters = Base64EncodingOptions(rawValue: 1 << 0)
40+
/// Set the maximum line length to 76 characters, after which a line ending is inserted.
41+
public static let lineLength76Characters = Base64EncodingOptions(rawValue: 1 << 1)
42+
/// When a maximum line length is set, specify that the line ending to insert should include a carriage return.
43+
public static let endLineWithCarriageReturn = Base64EncodingOptions(rawValue: 1 << 4)
44+
/// When a maximum line length is set, specify that the line ending to insert should include a line feed.
45+
public static let endLineWithLineFeed = Base64EncodingOptions(rawValue: 1 << 5)
46+
}
47+
48+
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
49+
public struct Base64DecodingOptions : OptionSet, Sendable {
50+
public let rawValue: UInt
51+
52+
public init(rawValue: UInt) {
53+
self.rawValue = rawValue
54+
}
55+
/// Modify the decoding algorithm so that it ignores unknown non-Base-64 bytes, including line ending characters.
56+
public static let ignoreUnknownCharacters = Base64DecodingOptions(rawValue: 1 << 0)
57+
}
58+
}
59+
#else
60+
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
61+
extension Data {
62+
// These types are typealiased to the `NSData` options for framework builds only.
63+
public typealias Base64EncodingOptions = NSData.Base64EncodingOptions
64+
public typealias Base64DecodingOptions = NSData.Base64DecodingOptions
65+
}
66+
#endif //!FOUNDATION_FRAMEWORK
67+
68+
extension Data.Base64EncodingOptions {
69+
/// Use the base64url alphabet to encode the data
70+
@available(FoundationPreview 6.3, *)
71+
public static let base64URLAlphabet = Self(rawValue: 1 << 6)
72+
73+
/// Omit the `=` padding characters in the end of the base64 encoded result
74+
@available(FoundationPreview 6.3, *)
75+
public static let omitPaddingCharacter = Self(rawValue: 1 << 7)
76+
}
77+
2878
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
2979
extension Data {
3080

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022-2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
#if FOUNDATION_FRAMEWORK
13+
14+
internal import _ForSwiftFoundation
15+
16+
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
17+
extension __DataStorage {
18+
@inline(never) // This is not @inlinable to avoid emission of the private `__NSSwiftData` class name into clients.
19+
@usableFromInline
20+
func bridgedReference(_ range: Range<Int>) -> AnyObject {
21+
if range.isEmpty {
22+
return NSData() // zero length data can be optimized as a singleton
23+
}
24+
25+
return __NSSwiftData(backing: self, range: range)
26+
}
27+
}
28+
29+
// NOTE: older overlays called this _NSSwiftData. The two must
30+
// coexist, so it was renamed. The old name must not be used in the new
31+
// runtime.
32+
internal final class __NSSwiftData : NSData {
33+
var _backing: __DataStorage!
34+
var _range: Range<Data.Index>!
35+
36+
convenience init(backing: __DataStorage, range: Range<Data.Index>) {
37+
self.init()
38+
_backing = backing
39+
_range = range
40+
}
41+
@objc override var length: Int {
42+
return _range.upperBound - _range.lowerBound
43+
}
44+
45+
@objc override var bytes: UnsafeRawPointer {
46+
// NSData's byte pointer methods are not annotated for nullability correctly
47+
// (but assume non-null by the wrapping macro guards). This placeholder value
48+
// is to work-around this bug. Any indirection to the underlying bytes of an NSData
49+
// with a length of zero would have been a programmer error anyhow so the actual
50+
// return value here is not needed to be an allocated value. This is specifically
51+
// needed to live like this to be source compatible with Swift3. Beyond that point
52+
// this API may be subject to correction.
53+
guard let bytes = _backing.bytes else {
54+
return UnsafeRawPointer(bitPattern: 0xBAD0)!
55+
}
56+
57+
return bytes.advanced(by: _range.lowerBound)
58+
}
59+
60+
@objc override func copy(with zone: NSZone? = nil) -> Any {
61+
if _backing._copyWillRetain {
62+
return self
63+
} else {
64+
return NSData(bytes: bytes, length: length)
65+
}
66+
67+
}
68+
69+
@objc override func mutableCopy(with zone: NSZone? = nil) -> Any {
70+
return NSMutableData(bytes: bytes, length: length)
71+
}
72+
73+
@objc override
74+
func _isCompact() -> Bool {
75+
return true
76+
}
77+
78+
@objc override
79+
func _bridgingCopy(_ bytes: UnsafeMutablePointer<UnsafeRawPointer?>, length: UnsafeMutablePointer<Int>) -> Data? {
80+
fatalError("Unexpected call to __NSSwiftData._bridgingCopy(_:length:)")
81+
}
82+
}
83+
84+
extension Data {
85+
internal func _bridgeToObjectiveCImpl() -> AnyObject {
86+
switch _representation {
87+
case .empty: return NSData()
88+
case .inline(let inline):
89+
return inline.withUnsafeBytes {
90+
return NSData(bytes: $0.baseAddress, length: $0.count)
91+
}
92+
case .slice(let slice):
93+
return slice.storage.bridgedReference(slice.range)
94+
case .large(let slice):
95+
return slice.storage.bridgedReference(slice.range)
96+
}
97+
}
98+
99+
internal static func _bridgeFromObjectiveCAdoptingNativeStorageOf(_ source: AnyObject) -> Data? {
100+
guard object_getClass(source) == __NSSwiftData.self else { return nil }
101+
102+
let swiftData = unsafeDowncast(source, to: __NSSwiftData.self)
103+
let range = swiftData._range!
104+
let originalBacking = swiftData._backing!
105+
106+
// (rdar://162776451) Some clients assume that the double-bridged Data's start index is 0 due to historical behavior. We need to make sure the created Data's indices begin at 0 rather than preserving the original offset/slice range here. This requires creating a new __DataStorage instead of using the existing one.
107+
// (rdar://121865256) We also need to make sure that we don't create a new __DataStorage that holds on to the original via the deallocator. If a value is double bridged repeatedly (as is the case in some clients), unwinding in the dealloc can cause a stack overflow. This requires either using the existing __DataStorage, or creating a new one with a copy of the bytes to avoid a deallocator chain.
108+
// Based on the two constraints above, we perform a copy here. Ideally in the future if we remove the first constraint we could re-use the existing originalBacking to avoid the copy.
109+
let newBacking = __DataStorage(bytes: originalBacking.mutableBytes?.advanced(by: range.lowerBound), length: range.count)
110+
111+
if InlineSlice.canStore(count: newBacking.length) {
112+
return Data(representation: .slice(InlineSlice(newBacking, count: newBacking.length)))
113+
} else {
114+
return Data(representation: .large(LargeSlice(newBacking, count: newBacking.length)))
115+
}
116+
}
117+
}
118+
119+
#endif
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
14+
extension Data {
15+
@available(swift, introduced: 4.2)
16+
@available(swift, deprecated: 5, message: "use `init(_:)` instead")
17+
public init<S: Sequence>(bytes elements: S) where S.Iterator.Element == UInt8 {
18+
self.init(elements)
19+
}
20+
21+
@available(swift, obsoleted: 4.2)
22+
public init(bytes: Array<UInt8>) {
23+
self.init(bytes)
24+
}
25+
26+
@available(swift, obsoleted: 4.2)
27+
public init(bytes: ArraySlice<UInt8>) {
28+
self.init(bytes)
29+
}
30+
31+
/// Access the bytes in the data.
32+
///
33+
/// - warning: The byte pointer argument should not be stored and used outside of the lifetime of the call to the closure.
34+
@available(swift, deprecated: 5, message: "use `withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R` instead")
35+
public func withUnsafeBytes<ResultType, ContentType>(_ body: (UnsafePointer<ContentType>) throws -> ResultType) rethrows -> ResultType {
36+
return try self.withUnsafeBytes {
37+
return try body($0.baseAddress?.assumingMemoryBound(to: ContentType.self) ?? UnsafePointer<ContentType>(bitPattern: 0xBAD0)!)
38+
}
39+
}
40+
41+
/// Mutate the bytes in the data.
42+
///
43+
/// This function assumes that you are mutating the contents.
44+
/// - warning: The byte pointer argument should not be stored and used outside of the lifetime of the call to the closure.
45+
@available(swift, deprecated: 5, message: "use `withUnsafeMutableBytes<R>(_: (UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R` instead")
46+
public mutating func withUnsafeMutableBytes<ResultType, ContentType>(_ body: (UnsafeMutablePointer<ContentType>) throws -> ResultType) rethrows -> ResultType {
47+
return try self.withUnsafeMutableBytes {
48+
return try body($0.baseAddress?.assumingMemoryBound(to: ContentType.self) ?? UnsafeMutablePointer<ContentType>(bitPattern: 0xBAD0)!)
49+
}
50+
}
51+
52+
/// Enumerate the contents of the data.
53+
///
54+
/// In some cases, (for example, a `Data` backed by a `dispatch_data_t`, the bytes may be stored discontinuously. In those cases, this function invokes the closure for each contiguous region of bytes.
55+
/// - parameter block: The closure to invoke for each region of data. You may stop the enumeration by setting the `stop` parameter to `true`.
56+
@available(swift, deprecated: 5, message: "use `regions` or `for-in` instead")
57+
public func enumerateBytes(_ block: (_ buffer: UnsafeBufferPointer<UInt8>, _ byteIndex: Index, _ stop: inout Bool) -> Void) {
58+
self.withUnsafeBytes {
59+
var stop = false
60+
block($0.assumingMemoryBound(to: UInt8.self), 0, &stop)
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)