diff --git a/Sources/FoundationEssentials/Calendar/Calendar_Cache.swift b/Sources/FoundationEssentials/Calendar/Calendar_Cache.swift index e7f8c43c0..b866e7ab9 100644 --- a/Sources/FoundationEssentials/Calendar/Calendar_Cache.swift +++ b/Sources/FoundationEssentials/Calendar/Calendar_Cache.swift @@ -10,11 +10,9 @@ // //===----------------------------------------------------------------------===// - #if FOUNDATION_FRAMEWORK @_implementationOnly import _ForSwiftFoundation import CoreFoundation -#else #endif /// Singleton which listens for notifications about preference changes for Calendar and holds cached singletons for the current locale, calendar, and time zone. @@ -24,7 +22,7 @@ struct CalendarCache : Sendable { // _CalendarICU, if present static var calendarICUClass: _CalendarProtocol.Type = { -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && canImport(FoundationICU) _CalendarICU.self #else if let name = _typeByName("FoundationInternationalization._CalendarICU"), let t = name as? _CalendarProtocol.Type { diff --git a/Sources/FoundationEssentials/Data/Data.swift b/Sources/FoundationEssentials/Data/Data.swift index 3b7310c0e..fec6aea2b 100644 --- a/Sources/FoundationEssentials/Data/Data.swift +++ b/Sources/FoundationEssentials/Data/Data.swift @@ -37,6 +37,12 @@ private func malloc_good_size(_ size: Int) -> Int { return size } +#elseif canImport(C) + +private func malloc_good_size(_ size: Int) -> Int { + return size +} + #endif #if os(Windows) @@ -46,6 +52,8 @@ import func WinSDK.UnmapViewOfFile internal func __DataInvokeDeallocatorUnmap(_ mem: UnsafeMutableRawPointer, _ length: Int) { #if os(Windows) _ = UnmapViewOfFile(mem) +#elseif canImport(C) + free(mem) #else munmap(mem, length) #endif diff --git a/Sources/FoundationEssentials/Data/DataProtocol.swift b/Sources/FoundationEssentials/Data/DataProtocol.swift index ba77e1133..253fc52d7 100644 --- a/Sources/FoundationEssentials/Data/DataProtocol.swift +++ b/Sources/FoundationEssentials/Data/DataProtocol.swift @@ -210,7 +210,7 @@ extension DataProtocol { extension DataProtocol where Self : ContiguousBytes { public func copyBytes(to ptr: UnsafeMutableBufferPointer, from range: R) where R.Bound == Index { precondition(ptr.baseAddress != nil) - + let concreteRange = range.relative(to: self) withUnsafeBytes { fullBuffer in let adv = distance(from: startIndex, to: concreteRange.lowerBound) diff --git a/Sources/FoundationEssentials/Date.swift b/Sources/FoundationEssentials/Date.swift index ae254cb93..2500ae191 100644 --- a/Sources/FoundationEssentials/Date.swift +++ b/Sources/FoundationEssentials/Date.swift @@ -358,7 +358,7 @@ extension NSDate : _HasCustomAnyHashableRepresentation { #endif // FOUNDATION_FRAMEWORK // MARK: - Playground Support -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS @available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *) extension Date : _CustomPlaygroundQuickLookable { var summary: String { diff --git a/Sources/FoundationEssentials/ErrorCodes+POSIX.swift b/Sources/FoundationEssentials/ErrorCodes+POSIX.swift index 76a3b682c..3a5b32416 100644 --- a/Sources/FoundationEssentials/ErrorCodes+POSIX.swift +++ b/Sources/FoundationEssentials/ErrorCodes+POSIX.swift @@ -9,27 +9,7 @@ // //===----------------------------------------------------------------------===// - -#if FOUNDATION_FRAMEWORK -/// Describes an error in the POSIX error domain. -@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *) -public struct POSIXError : _BridgedStoredNSError { - public let _nsError: NSError - - public init(_nsError error: NSError) { - precondition(error.domain == NSPOSIXErrorDomain) - self._nsError = error - } - - public static var errorDomain: String { return NSPOSIXErrorDomain } - - public var hashValue: Int { - return _nsError.hashValue - } - - public typealias Code = POSIXErrorCode -} -#else +#if !FOUNDATION_FRAMEWORK || canImport(C) // Define the POSIXErrorCode for all platforms here. public enum POSIXErrorCode : Int32 { /// Operation not permitted. @@ -366,6 +346,28 @@ public enum POSIXErrorCode : Int32 { extension POSIXErrorCode : Equatable, Hashable, RawRepresentable { } +#endif + +#if FOUNDATION_FRAMEWORK +/// Describes an error in the POSIX error domain. +@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *) +public struct POSIXError : _BridgedStoredNSError { + public let _nsError: NSError + + public init(_nsError error: NSError) { + precondition(error.domain == NSPOSIXErrorDomain) + self._nsError = error + } + + public static var errorDomain: String { return NSPOSIXErrorDomain } + + public var hashValue: Int { + return _nsError.hashValue + } + + public typealias Code = POSIXErrorCode +} +#else /// Describes an error in the POSIX error domain. @available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *) diff --git a/Sources/FoundationEssentials/JSON/JSONDecoder.swift b/Sources/FoundationEssentials/JSON/JSONDecoder.swift index 59a5c4a98..c4306400a 100644 --- a/Sources/FoundationEssentials/JSON/JSONDecoder.swift +++ b/Sources/FoundationEssentials/JSON/JSONDecoder.swift @@ -62,7 +62,7 @@ open class JSONDecoder { /// Decode the `Date` as UNIX millisecond timestamp from a JSON number. case millisecondsSince1970 -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS // TODO: Reenable once DateFormatStyle has been moved: https://github.com/apple/swift-foundation/issues/46 /// Decode the `Date` as an ISO-8601-formatted string (in RFC 3339 format). @available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) @@ -647,7 +647,8 @@ extension JSONDecoderImpl: Decoder { case .millisecondsSince1970: let double = try self.unwrapFloatingPoint(from: mapValue, as: Double.self, for: codingPathNode, additionalKey) return Date(timeIntervalSince1970: double / 1000.0) -#if FOUNDATION_FRAMEWORK // TODO: Reenable once DateFormatStyle has been moved +#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS + // TODO: Reenable once DateFormatStyle has been moved case .iso8601: let string = try self.unwrapString(from: mapValue, for: codingPathNode, additionalKey) guard let date = try? Date.ISO8601FormatStyle().parse(string) else { diff --git a/Sources/FoundationEssentials/JSON/JSONEncoder.swift b/Sources/FoundationEssentials/JSON/JSONEncoder.swift index 0594d635e..8eab376cd 100644 --- a/Sources/FoundationEssentials/JSON/JSONEncoder.swift +++ b/Sources/FoundationEssentials/JSON/JSONEncoder.swift @@ -66,7 +66,7 @@ open class JSONEncoder { /// Encode the `Date` as UNIX millisecond timestamp (as a JSON number). case millisecondsSince1970 -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS // TODO: Reenable once DateFormatStyle has been ported: https://github.com/apple/swift-foundation/issues/46 /// Encode the `Date` as an ISO-8601-formatted string (in RFC 3339 format). @available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) @@ -998,7 +998,7 @@ private extension __JSONEncoder { case .millisecondsSince1970: return try .number(from: 1000.0 * date.timeIntervalSince1970, with: .throw, for: codingPathNode, additionalKey) -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS case .iso8601: return self.wrap(date.formatted(.iso8601)) diff --git a/Sources/FoundationEssentials/JSON/JSONWriter.swift b/Sources/FoundationEssentials/JSON/JSONWriter.swift index 78f54469e..4816e8367 100644 --- a/Sources/FoundationEssentials/JSON/JSONWriter.swift +++ b/Sources/FoundationEssentials/JSON/JSONWriter.swift @@ -349,7 +349,7 @@ internal struct JSONWriter { pretty ? writer(" : ") : writer(":") try serializeJSON(value, depth: depth) } -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_LOCALIZATION if sortedKeys { // TODO: Until we have a solution for sorting like Locale.system in FoundationEssentials or with the help of FoundationLocalization, this comparison requires bridging back to NSString. To avoid the extreme overhead of bridging the strings on every comparison, we'll do it up front instead. let nsKeysAndValues = dict.map { diff --git a/Sources/FoundationEssentials/Locale/Locale+Language.swift b/Sources/FoundationEssentials/Locale/Locale+Language.swift index 3bab2eb31..9aae15440 100644 --- a/Sources/FoundationEssentials/Locale/Locale+Language.swift +++ b/Sources/FoundationEssentials/Locale/Locale+Language.swift @@ -59,7 +59,7 @@ extension Locale { /// Returns a list of system languages, includes the languages of all product localization for the current platform public static var systemLanguages: [Language] { -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && canImport(FoundationICU) NSLocale.systemLanguages().map { let comp = Components(identifier: $0 as! String) return Language(components: comp) diff --git a/Sources/FoundationEssentials/Locale/Locale.swift b/Sources/FoundationEssentials/Locale/Locale.swift index 2c8fe2703..30cc2e7a8 100644 --- a/Sources/FoundationEssentials/Locale/Locale.swift +++ b/Sources/FoundationEssentials/Locale/Locale.swift @@ -78,7 +78,7 @@ public struct Locale : Hashable, Equatable, Sendable { Locale(inner: LocaleCache.cache.system) } -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && canImport(FoundationICU) /// This returns an instance of `Locale` that's set up exactly like it would be if the user changed the current locale to that identifier, set the preferences keys in the overrides dictionary, then called `current`. internal static func localeAsIfCurrent(name: String?, cfOverrides: CFDictionary? = nil, disableBundleMatching: Bool = false) -> Locale { return LocaleCache.cache.localeAsIfCurrent(name: name, cfOverrides: cfOverrides, disableBundleMatching: disableBundleMatching) @@ -483,7 +483,7 @@ public struct Locale : Hashable, Equatable, Sendable { _locale.forceMinDaysInFirstWeek(calendar) } -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS // This is framework-only because Date.FormatStyle.DateStyle is Internationalization-only. package func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String? { _locale.customDateFormat(style) diff --git a/Sources/FoundationEssentials/Locale/Locale_Autoupdating.swift b/Sources/FoundationEssentials/Locale/Locale_Autoupdating.swift index 55d458271..b28fd6b83 100644 --- a/Sources/FoundationEssentials/Locale/Locale_Autoupdating.swift +++ b/Sources/FoundationEssentials/Locale/Locale_Autoupdating.swift @@ -231,7 +231,7 @@ internal final class _LocaleAutoupdating : _LocaleProtocol, @unchecked Sendable LocaleCache.cache.current.forceTemperatureUnit } -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String? { LocaleCache.cache.current.customDateFormat(style) } diff --git a/Sources/FoundationEssentials/Locale/Locale_Cache.swift b/Sources/FoundationEssentials/Locale/Locale_Cache.swift index 2d6f6b690..b16a5294f 100644 --- a/Sources/FoundationEssentials/Locale/Locale_Cache.swift +++ b/Sources/FoundationEssentials/Locale/Locale_Cache.swift @@ -26,7 +26,7 @@ struct LocaleCache : Sendable { // _LocaleICU, if present. Otherwise we use _LocaleUnlocalized. The `Locale` initializers are not failable, so we just fall back to the unlocalized type when needed without failure. static var localeICUClass: _LocaleProtocol.Type = { -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && canImport(FoundationICU) return _LocaleICU.self #else if let name = _typeByName("FoundationInternationalization._LocaleICU"), let t = name as? _LocaleProtocol.Type { @@ -140,6 +140,7 @@ struct LocaleCache : Sendable { } } +#if canImport(FoundationICU) mutating func fixedNSLocale(_ locale: _LocaleICU) -> _NSSwiftLocale { let id = IdentifierAndPrefs(identifier: locale.identifier, prefs: locale.prefs) if let locale = cachedFixedLocaleToNSLocales[id] { @@ -152,6 +153,7 @@ struct LocaleCache : Sendable { return nsLocale } } +#endif mutating func currentNSLocale(preferences: LocalePreferences?, cache: Bool) -> _NSSwiftLocale? { resetCurrentIfNeeded() @@ -172,9 +174,14 @@ struct LocaleCache : Sendable { return nil } +#if canImport(FoundationICU) // We have neither a Swift Locale nor an NSLocale. Recalculate and set both. let locale = _LocaleICU(name: nil, prefs: preferences, disableBundleMatching: false) +#else + let locale = _LocaleUnlocalized(name: nil, prefs: preferences, disableBundleMatching: false) +#endif let nsLocale = _NSSwiftLocale(Locale(inner: locale)) + if cache { // It's possible this was an 'incomplete locale', in which case we will want to calculate it again later. self.cachedCurrentLocale = locale @@ -281,9 +288,11 @@ struct LocaleCache : Sendable { lock.withLock { $0.fixedNSLocale(identifier: id) } } +#if canImport(FoundationICU) func fixedNSLocale(_ locale: _LocaleICU) -> _NSSwiftLocale { lock.withLock { $0.fixedNSLocale(locale) } } +#endif func autoupdatingCurrentNSLocale() -> _NSSwiftLocale { lock.withLock { $0.autoupdatingNSLocale() } @@ -319,7 +328,7 @@ struct LocaleCache : Sendable { lock.withLock { $0.fixedComponents(comps) } } -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_CFPREFERENCES func preferences() -> (LocalePreferences, Bool) { // On Darwin, we check the current user preferences for Locale values var wouldDeadlock: DarwinBoolean = false @@ -372,7 +381,7 @@ struct LocaleCache : Sendable { } #endif -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_CFPREFERENCES /// This returns an instance of `Locale` that's set up exactly like it would be if the user changed the current locale to that identifier, set the preferences keys in the overrides dictionary, then called `current`. func localeAsIfCurrent(name: String?, cfOverrides: CFDictionary? = nil, disableBundleMatching: Bool = false) -> Locale { @@ -399,7 +408,7 @@ struct LocaleCache : Sendable { } func localeAsIfCurrentWithBundleLocalizations(_ availableLocalizations: [String], allowsMixedLocalizations: Bool) -> Locale? { -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && canImport(FoundationICU) guard !allowsMixedLocalizations else { let (prefs, _) = preferences() let inner = _LocaleICU(name: nil, prefs: prefs, disableBundleMatching: true) diff --git a/Sources/FoundationEssentials/Locale/Locale_Preferences.swift b/Sources/FoundationEssentials/Locale/Locale_Preferences.swift index 8ce907c4b..c2cd5b786 100644 --- a/Sources/FoundationEssentials/Locale/Locale_Preferences.swift +++ b/Sources/FoundationEssentials/Locale/Locale_Preferences.swift @@ -75,7 +75,9 @@ package struct LocalePreferences: Hashable { // The OS no longer writes out this preference, but we keep it here for compatibility with CFDateFormatter behavior. package var icuNumberFormatStrings: CFDictionary? package var icuNumberSymbols: CFDictionary? +#if !NO_FORMATTERS package var dateFormats: [Date.FormatStyle.DateStyle: String]? // Bridged version of `icuDateFormatStrings` +#endif #endif package var numberSymbols: [UInt32 : String]? // Bridged version of `icuNumberSymbols` @@ -87,7 +89,7 @@ package struct LocalePreferences: Hashable { package init() { } -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && canImport(FoundationICU) // The framework init supports customized dateFormats package init(metricUnits: Bool? = nil, languages: [String]? = nil, @@ -153,7 +155,7 @@ package struct LocalePreferences: Hashable { } #endif -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_CFPREFERENCES /// Interpret a dictionary (from user defaults) according to a predefined set of strings and convert it into the more strongly-typed `LocalePreferences` values. /// Several dictionaries may need to be applied to the same instance, which is why this is structured as a mutating setter rather than an initializer. /// Why use a `CFDictionary` instead of a Swift dictionary here? The input prefs may be a complete copy of the user's prefs, and we don't want to bridge a ton of unrelated data into Swift just to extract a few keys. Keeping it as a `CFDictionary` avoids that overhead, and we call into small CF helper functions to get the data we need, if it is there. @@ -284,15 +286,17 @@ package struct LocalePreferences: Hashable { if let other = prefs.collationOrder { self.collationOrder = other } if let other = prefs.firstWeekday { self.firstWeekday = other } if let other = prefs.minDaysInFirstWeek { self.minDaysInFirstWeek = other } + if let other = prefs.numberSymbols { self.numberSymbols = other } #if FOUNDATION_FRAMEWORK if let other = prefs.icuDateTimeSymbols { self.icuDateTimeSymbols = other } if let other = prefs.icuDateFormatStrings { self.icuDateFormatStrings = other } if let other = prefs.icuTimeFormatStrings { self.icuTimeFormatStrings = other } if let other = prefs.icuNumberFormatStrings { self.icuNumberFormatStrings = other } if let other = prefs.icuNumberSymbols { self.icuNumberSymbols = other } +#if !NO_FORMATTERS if let other = prefs.dateFormats { self.dateFormats = other } -#endif - if let other = prefs.numberSymbols { self.numberSymbols = other } +#endif // !NO_FORMATTERS +#endif // FOUNDATION_FRAMEWORK if let other = prefs.country { self.country = other } if let other = prefs.measurementUnits { self.measurementUnits = other } if let other = prefs.temperatureUnit { self.temperatureUnit = other } diff --git a/Sources/FoundationEssentials/Locale/Locale_Protocol.swift b/Sources/FoundationEssentials/Locale/Locale_Protocol.swift index b11857de6..590033ef5 100644 --- a/Sources/FoundationEssentials/Locale/Locale_Protocol.swift +++ b/Sources/FoundationEssentials/Locale/Locale_Protocol.swift @@ -91,9 +91,11 @@ package protocol _LocaleProtocol : AnyObject, Sendable, CustomDebugStringConvert func bridgeToNSLocale() -> NSLocale +#if !NO_FORMATTERS // This is framework-only because Date.FormatStyle.DateStlye is Internationalization-only func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String? #endif +#endif } extension _LocaleProtocol { diff --git a/Sources/FoundationEssentials/Locale/Locale_Unlocalized.swift b/Sources/FoundationEssentials/Locale/Locale_Unlocalized.swift index a984639f3..1040b4d9b 100644 --- a/Sources/FoundationEssentials/Locale/Locale_Unlocalized.swift +++ b/Sources/FoundationEssentials/Locale/Locale_Unlocalized.swift @@ -233,7 +233,7 @@ internal final class _LocaleUnlocalized : _LocaleProtocol, @unchecked Sendable { nil } -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_FORMATTERS func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String? { nil } diff --git a/Sources/FoundationEssentials/Platform.swift b/Sources/FoundationEssentials/Platform.swift index 2f36ce3c3..1fff087e7 100644 --- a/Sources/FoundationEssentials/Platform.swift +++ b/Sources/FoundationEssentials/Platform.swift @@ -12,7 +12,9 @@ #if canImport(Darwin) import Darwin -fileprivate let _pageSize = Int(vm_page_size) +fileprivate var _pageSize: Int { + Int(vm_page_size) +} #elseif canImport(WinSDK) import WinSDK fileprivate var _pageSize: Int { @@ -23,13 +25,17 @@ fileprivate var _pageSize: Int { #elseif os(WASI) // WebAssembly defines a fixed page size fileprivate let _pageSize: Int = 65_536 -#else +#elseif canImport(Glibc) import Glibc fileprivate let _pageSize: Int = Int(getpagesize()) +#elseif canImport(C) +fileprivate let _pageSize: Int = Int(getpagesize()) #endif // canImport(Darwin) internal struct Platform { - static var pageSize: Int = _pageSize + static var pageSize: Int { + _pageSize + } static func roundDownToMultipleOfPageSize(_ size: Int) -> Int { return size & ~(self.pageSize - 1) diff --git a/Sources/FoundationEssentials/Predicate/Archiving/PredicateCodableConfiguration.swift b/Sources/FoundationEssentials/Predicate/Archiving/PredicateCodableConfiguration.swift index a6f0bb7f6..a8c8a5a85 100644 --- a/Sources/FoundationEssentials/Predicate/Archiving/PredicateCodableConfiguration.swift +++ b/Sources/FoundationEssentials/Predicate/Archiving/PredicateCodableConfiguration.swift @@ -12,6 +12,7 @@ #if FOUNDATION_FRAMEWORK +#if canImport(ReflectionInternal) @_implementationOnly import ReflectionInternal @@ -499,4 +500,5 @@ extension PredicateCodableConfiguration { }() } +#endif // canImport(ReflectionInternal) #endif // FOUNDATION_FRAMEWORK diff --git a/Sources/FoundationEssentials/Predicate/NSPredicateConversion.swift b/Sources/FoundationEssentials/Predicate/NSPredicateConversion.swift index 3d89ef50f..4dcfd4f6e 100644 --- a/Sources/FoundationEssentials/Predicate/NSPredicateConversion.swift +++ b/Sources/FoundationEssentials/Predicate/NSPredicateConversion.swift @@ -12,6 +12,7 @@ #if FOUNDATION_FRAMEWORK +#if canImport(Foundation_Private.NSExpression) @_implementationOnly import Foundation_Private.NSExpression @_implementationOnly import Foundation_Private.NSPredicate @@ -531,4 +532,5 @@ extension NSPredicate { } } +#endif //canImport(Foundation_Private.NSExpression) #endif diff --git a/Sources/FoundationEssentials/String/String+Comparison.swift b/Sources/FoundationEssentials/String/String+Comparison.swift index 972c53543..183f4c8cd 100644 --- a/Sources/FoundationEssentials/String/String+Comparison.swift +++ b/Sources/FoundationEssentials/String/String+Comparison.swift @@ -600,6 +600,7 @@ extension Substring { } func _range(of strToFind: Substring, options: String.CompareOptions) throws -> Range? { + #if canImport(FoundationICU) if options.contains(.regularExpression) { guard let regex = try RegexPatternCache.cache.regex(for: String(strToFind), caseInsensitive: options.contains(.caseInsensitive)) else { return nil @@ -613,7 +614,8 @@ extension Substring { return match.range } } - + #endif + guard !isEmpty, !strToFind.isEmpty else { return nil } diff --git a/Sources/FoundationEssentials/String/String+Encoding.swift b/Sources/FoundationEssentials/String/String+Encoding.swift index b505666e6..c0d1d6bfc 100644 --- a/Sources/FoundationEssentials/String/String+Encoding.swift +++ b/Sources/FoundationEssentials/String/String+Encoding.swift @@ -97,7 +97,7 @@ extension String.Encoding : Hashable { @available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *) extension String.Encoding : CustomStringConvertible { public var description: String { -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && !NO_LOCALIZATION return String.localizedName(of: self) #else return "\(self)" diff --git a/Sources/FoundationEssentials/TimeZone/TimeZone_Cache.swift b/Sources/FoundationEssentials/TimeZone/TimeZone_Cache.swift index f71c6300e..5084aae49 100644 --- a/Sources/FoundationEssentials/TimeZone/TimeZone_Cache.swift +++ b/Sources/FoundationEssentials/TimeZone/TimeZone_Cache.swift @@ -34,7 +34,7 @@ struct TimeZoneCache : Sendable { // _TimeZoneICU, if present static var timeZoneICUClass: _TimeZoneProtocol.Type? = { -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && canImport(FoundationICU) _TimeZoneICU.self #else if let name = _typeByName("FoundationInternationalization._TimeZoneICU"), let t = name as? _TimeZoneProtocol.Type { @@ -47,7 +47,7 @@ struct TimeZoneCache : Sendable { // _TimeZoneGMTICU or _TimeZoneGMT static var timeZoneGMTClass: _TimeZoneProtocol.Type = { -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && canImport(FoundationICU) _TimeZoneGMTICU.self #else if let name = _typeByName("FoundationInternationalization._TimeZoneGMTICU"), let t = name as? _TimeZoneProtocol.Type { @@ -118,6 +118,7 @@ struct TimeZoneCache : Sendable { /// Reads from environment variables `TZFILE`, `TZ` and finally the symlink pointed at by the C macro `TZDEFAULT` to figure out what the current (aka "system") time zone is. mutating func findCurrentTimeZone() -> TimeZone { +#if !NO_TZFILE if let tzenv = getenv("TZFILE") { if let name = String(validatingUTF8: tzenv) { if let result = fixed(name) { @@ -183,7 +184,7 @@ struct TimeZoneCache : Sendable { } } #endif - +#endif //!NO_TZFILE // Last option as a default is the GMT value (again, using the cached version directly to avoid recursive lock) return TimeZone(inner: offsetFixed(0)!) } @@ -355,34 +356,44 @@ struct TimeZoneCache : Sendable { mutating func bridgedFixed(_ identifier: String) -> _NSSwiftTimeZone? { if let cached = bridgedFixedTimeZones[identifier] { return cached - } else if let swiftCached = fixedTimeZones[identifier] { + } + if let swiftCached = fixedTimeZones[identifier] { // If we don't have a bridged instance yet, check to see if we have a Swift one and re-use that let bridged = _NSSwiftTimeZone(timeZone: TimeZone(inner: swiftCached)) bridgedFixedTimeZones[identifier] = bridged return bridged - } else if let innerTz = _TimeZoneICU(identifier: identifier) { + } +#if canImport(FoundationICU) + if let innerTz = _TimeZoneICU(identifier: identifier) { // In this case, the identifier is unique and we need to cache it (in two places) fixedTimeZones[identifier] = innerTz let bridgedTz = _NSSwiftTimeZone(timeZone: TimeZone(inner: innerTz)) bridgedFixedTimeZones[identifier] = bridgedTz return bridgedTz - } else { - return nil } +#endif + return nil } mutating func bridgedOffsetFixed(_ offset: Int) -> _NSSwiftTimeZone? { if let cached = bridgedOffsetTimeZones[offset] { return cached - } else if let swiftCached = offsetTimeZones[offset] { + } + if let swiftCached = offsetTimeZones[offset] { // If we don't have a bridged instance yet, check to see if we have a Swift one and re-use that let bridged = _NSSwiftTimeZone(timeZone: TimeZone(inner: swiftCached)) bridgedOffsetTimeZones[offset] = bridged return bridged - } else if let innerTz = _TimeZoneGMTICU(secondsFromGMT: offset) { + } +#if canImport(FoundationICU) + let maybeInnerTz = _TimeZoneGMTICU(secondsFromGMT: offset) +#else + let maybeInnerTz = _TimeZoneGMT(secondsFromGMT: offset) +#endif + if let innerTz = maybeInnerTz { // In order to avoid bloating a cache with weird time zones, only cache values that are 30min offsets (including 1hr offsets). let doCache = abs(offset) % 1800 == 0 - + // In this case, the offset is unique and we need to cache it (in two places) let bridgedTz = _NSSwiftTimeZone(timeZone: TimeZone(inner: innerTz)) if doCache { @@ -390,9 +401,9 @@ struct TimeZoneCache : Sendable { bridgedOffsetTimeZones[offset] = bridgedTz } return bridgedTz - } else { - return nil } + + return nil } #endif // FOUNDATION_FRAMEWORK } diff --git a/Sources/FoundationEssentials/UUID_Wrappers.swift b/Sources/FoundationEssentials/UUID_Wrappers.swift index 0671eda73..375d2d91f 100644 --- a/Sources/FoundationEssentials/UUID_Wrappers.swift +++ b/Sources/FoundationEssentials/UUID_Wrappers.swift @@ -13,8 +13,10 @@ @_implementationOnly import _ForSwiftFoundation +#if canImport(Darwin.uuid) // Needed this for backward compatibility even though we don't use it. import Darwin.uuid +#endif @available(macOS 10.10, iOS 8.0, tvOS 9.0, watchOS 2.0, *) extension UUID : ReferenceConvertible { diff --git a/Sources/FoundationEssentials/_ThreadLocal.swift b/Sources/FoundationEssentials/_ThreadLocal.swift index 14f58a4e2..682f89357 100644 --- a/Sources/FoundationEssentials/_ThreadLocal.swift +++ b/Sources/FoundationEssentials/_ThreadLocal.swift @@ -15,11 +15,25 @@ import Darwin import Glibc #elseif canImport(WinSDK) import WinSDK +#elseif canImport(threads_h) +#if FOUNDATION_FRAMEWORK +@_implementationOnly import threads_h +#else +package import threads_h +#endif +#elseif canImport(threads) +#if FOUNDATION_FRAMEWORK +@_implementationOnly import threads +#else +package import threads +#endif #endif struct _ThreadLocal { #if canImport(Darwin) || canImport(Glibc) fileprivate typealias PlatformKey = pthread_key_t +#elseif USE_TSS + fileprivate typealias PlatformKey = tss_t #elseif canImport(WinSDK) fileprivate typealias PlatformKey = DWORD #endif @@ -32,6 +46,10 @@ struct _ThreadLocal { var key = PlatformKey() pthread_key_create(&key, nil) self.key = key +#elseif USE_TSS + var key = PlatformKey() + tss_create(&key, nil) + self.key = key #elseif canImport(WinSDK) key = FlsAlloc(nil) #endif @@ -42,6 +60,8 @@ struct _ThreadLocal { get { #if canImport(Darwin) || canImport(Glibc) pthread_getspecific(key) +#elseif USE_TSS + tss_get(key) #elseif canImport(WinSDK) FlsGetValue(key) #endif @@ -50,6 +70,8 @@ struct _ThreadLocal { set { #if canImport(Darwin) || canImport(Glibc) pthread_setspecific(key, newValue) +#elseif USE_TSS + tss_set(key, newValue) #elseif canImport(WinSDK) FlsSetValue(key, newValue) #endif diff --git a/Sources/FoundationInternationalization/Formatting/ICUListFormatter.swift b/Sources/FoundationInternationalization/Formatting/ICUListFormatter.swift index 473fad11a..6a06250fd 100644 --- a/Sources/FoundationInternationalization/Formatting/ICUListFormatter.swift +++ b/Sources/FoundationInternationalization/Formatting/ICUListFormatter.swift @@ -76,6 +76,3 @@ internal final class ICUListFormatter { } } - - - diff --git a/Sources/FoundationInternationalization/Locale/Locale_Bridge.swift b/Sources/FoundationInternationalization/Locale/Locale_Bridge.swift index bc9e066c8..653199dbb 100644 --- a/Sources/FoundationInternationalization/Locale/Locale_Bridge.swift +++ b/Sources/FoundationInternationalization/Locale/Locale_Bridge.swift @@ -50,10 +50,14 @@ internal final class _LocaleBridged: _LocaleProtocol, @unchecked Sendable { } func copy(newCalendarIdentifier identifier: Calendar.Identifier) -> any _LocaleProtocol { +#if canImport(FoundationICU) // Round trip through Locale.Components var comps = Locale.Components(identifier: self.identifier) comps.calendar = identifier return Locale(components: comps)._locale +#else + return _LocaleUnlocalized(identifier: identifier.cfCalendarIdentifier) +#endif } var isBridged: Bool { @@ -287,9 +291,11 @@ internal final class _LocaleBridged: _LocaleProtocol, @unchecked Sendable { nil } +#if !NO_FORMATTERS func customDateFormat(_ style: Date.FormatStyle.DateStyle) -> String? { nil } +#endif var prefs: LocalePreferences? { nil diff --git a/Sources/FoundationInternationalization/Locale/Locale_ObjC.swift b/Sources/FoundationInternationalization/Locale/Locale_ObjC.swift index c29689629..188a48bab 100644 --- a/Sources/FoundationInternationalization/Locale/Locale_ObjC.swift +++ b/Sources/FoundationInternationalization/Locale/Locale_ObjC.swift @@ -15,8 +15,12 @@ @_implementationOnly import _ForSwiftFoundation import CoreFoundation @_implementationOnly import os +#if canImport(CoreFoundation_Private.CFLocale) @_implementationOnly import CoreFoundation_Private.CFLocale +#endif +#if canImport(Foundation_Private.NSLocale) @_implementationOnly import Foundation_Private.NSLocale +#endif /// Entry points for the ObjC and C code to call into the common Swift implementation. @objc @@ -43,8 +47,12 @@ extension NSLocale { @objc private class func _newLocaleAsIfCurrent(_ name: String?, overrides: CFDictionary?, disableBundleMatching: Bool) -> NSLocale? { +#if canImport(FoundationICU) let inner = LocaleCache.cache.localeAsIfCurrent(name: name, cfOverrides: overrides, disableBundleMatching: disableBundleMatching) return _NSSwiftLocale(inner) +#else + return nil +#endif } @objc(_currentLocaleWithBundleLocalizations:disableBundleMatching:) @@ -65,10 +73,12 @@ extension NSLocale { LocaleCache.cache.preferredLanguages(forCurrentUser: forCurrentUser) } +#if canImport(FoundationICU) @objc class var _availableLocaleIdentifiers: [String] { Locale.availableIdentifiers } +#endif // This is internal, but silence the compiler's warnings about deprecation by deprecating this, too. @available(macOS, deprecated: 13) @available(iOS, deprecated: 16) @available(tvOS, deprecated: 16) @available(watchOS, deprecated: 9) @@ -89,10 +99,12 @@ extension NSLocale { Locale.isoCurrencyCodes } +#if canImport(FoundationICU) @objc class var _commonISOCurrencyCodes: [String] { Locale.commonISOCurrencyCodes } +#endif @objc class var _preferredLanguages: [String] { @@ -122,6 +134,7 @@ extension NSLocale { Locale.canonicalLanguageIdentifier(from: string) } +#if canImport(FoundationICU) @objc(_localeIdentifierFromWindowsLocaleCode:) class func _localeIdentifier(fromWindowsLocaleCode: UInt32) -> String? { guard let code = Int(exactly: fromWindowsLocaleCode) else { @@ -137,7 +150,8 @@ extension NSLocale { } return 0 } - +#endif + @available(macOS, deprecated: 13) @available(iOS, deprecated: 16) @available(tvOS, deprecated: 16) @available(watchOS, deprecated: 9) @objc(_characterDirectionForLanguage:) class func _characterDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection { @@ -152,6 +166,7 @@ extension NSLocale { @objc(_numberingSystemForLocaleIdentifier:) class func _numberingSystem(forLocaleIdentifier identifier: String) -> String? { +#if canImport(FoundationICU) let components = Locale.Components(identifier: identifier) if let system = components.numberingSystem { return system.identifier @@ -159,22 +174,35 @@ extension NSLocale { if let defaultSystem = Locale.NumberingSystem.defaultNumberingSystem(for: identifier) { return defaultSystem.identifier } +#endif return nil } @objc(_validNumberingSystemsForLocaleIdentifier:) class func _validNumberingSystems(forLocaleIdentifier identifier: String) -> [String] { +#if canImport(FoundationICU) Locale.NumberingSystem.validNumberingSystems(for: identifier).map { $0.identifier } +#else + [] +#endif } @objc(_localeIdentifierByReplacingLanguageCodeAndScriptCodeForLangCode:desiredComponents:) class func _localeIdentifierByReplacingLanguageCodeAndScriptCode(_ localeIDWithDesiredLangCode: String, desiredComponents localeIDWithDesiredComponents: String) -> String? { +#if canImport(FoundationICU) Locale.localeIdentifierByReplacingLanguageCodeAndScriptCode(localeIDWithDesiredLangCode: localeIDWithDesiredLangCode, localeIDWithDesiredComponents: localeIDWithDesiredComponents) +#else + nil +#endif } @objc(_localeIdentifierByAddingLikelySubtags:) class func _localeIdentifierByAddingLikelySubtags(_ localeID: String) -> String { +#if canImport(FoundationICU) Locale.localeIdentifierWithLikelySubtags(localeID) +#else + "" +#endif } @objc(_localeWithNewCalendarIdentifier:) @@ -232,7 +260,7 @@ internal class _NSSwiftLocale: _NSLocaleBridge { return nil } - guard let ident = coder.decodeObject(of: NSString.self, forKey: "NS.identifier") as? String else { + guard let ident = coder.decodeObject(forKey: "NS.identifier") as? String else { coder.failWithError(CocoaError(CocoaError.coderReadCorrupt, userInfo: [NSDebugDescriptionErrorKey : "Identifier has been corrupted"])) return nil } @@ -550,7 +578,11 @@ extension Locale { @available(tvOS, deprecated: 16, message: "Use `Locale.LanguageCode.isoLanguageCodes` instead") @available(watchOS, deprecated: 9, message: "Use `Locale.LanguageCode.isoLanguageCodes` instead") public static var isoLanguageCodes: [String] { +#if canImport(FoundationICU) Locale.LanguageCode._isoLanguageCodeStrings +#else + [] +#endif } /// Returns a dictionary that splits an identifier into its component pieces. @@ -574,7 +606,11 @@ extension Locale { @available(watchOS, deprecated: 9, message: "Use `Locale.Region.isoRegions` instead") public static var isoRegionCodes: [String] { // This was renamed from Obj-C +#if canImport(FoundationICU) Locale.Region.isoCountries +#else + [] +#endif } /// Returns a list of available `Locale` currency codes. @@ -583,7 +619,11 @@ extension Locale { @available(tvOS, deprecated: 16, message: "Use `Locale.Currency.isoCurrencies` instead") @available(watchOS, deprecated: 9, message: "Use `Locale.Currency.isoCurrencies` instead") public static var isoCurrencyCodes: [String] { +#if canImport(FoundationICU) Locale.Currency.isoCurrencies.map { $0.identifier } +#else + [] +#endif } /// Returns the character direction for a specified language code. @@ -592,8 +632,12 @@ extension Locale { @available(tvOS, deprecated: 16, message: "Use `Locale.Language(identifier:).characterDirection`") @available(watchOS, deprecated: 9, message: "Use `Locale.Language(identifier:).characterDirection`") public static func characterDirection(forLanguage isoLangCode: String) -> Locale.LanguageDirection { +#if canImport(FoundationICU) let language = Locale.Language(components: .init(identifier: isoLangCode)) return language.characterDirection +#else + return .unknown +#endif } /// Returns the line direction for a specified language code. @@ -602,8 +646,12 @@ extension Locale { @available(tvOS, deprecated: 16, message: "Use `Locale.Language(identifier:).lineLayoutDirection`") @available(watchOS, deprecated: 9, message: "Use `Locale.Language(identifier:).lineLayoutDirection`") public static func lineDirection(forLanguage isoLangCode: String) -> Locale.LanguageDirection { +#if canImport(FoundationICU) let language = Locale.Language(components: .init(identifier: isoLangCode)) return language.lineLayoutDirection +#else + return .unknown +#endif } } @@ -614,4 +662,4 @@ extension NSLocale.Key { static var languageIdentifier = NSLocale.Key(rawValue: "locale:languageIdentifier") } -#endif +#endif // FOUNDATION_FRAMEWORK diff --git a/Sources/FoundationInternationalization/String/StringProtocol+Locale.swift b/Sources/FoundationInternationalization/String/StringProtocol+Locale.swift index 30a8e687b..f9ac2519c 100644 --- a/Sources/FoundationInternationalization/String/StringProtocol+Locale.swift +++ b/Sources/FoundationInternationalization/String/StringProtocol+Locale.swift @@ -24,11 +24,12 @@ extension StringProtocol { @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *) public var localizedCapitalized: String { #if FOUNDATION_FRAMEWORK +#if canImport(FoundationICU) if _foundation_essentials_feature_enabled() { return String(self)._capitalized(with: .current) - } else { - return _ns.localizedCapitalized } +#endif + return _ns.localizedCapitalized #else return String(self)._capitalized(with: .current) #endif @@ -39,11 +40,12 @@ extension StringProtocol { @available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *) public func capitalized(with locale: Locale?) -> String { #if FOUNDATION_FRAMEWORK +#if canImport(FoundationICU) if _foundation_essentials_feature_enabled() { return String(self)._capitalized(with: locale) - } else { - return _ns.capitalized(with: locale) } +#endif + return _ns.capitalized(with: locale) #else return String(self)._capitalized(with: locale) #endif @@ -54,9 +56,11 @@ extension StringProtocol { @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *) public var localizedLowercase: String { #if FOUNDATION_FRAMEWORK +#if canImport(FoundationICU) if _foundation_essentials_feature_enabled() { return String(self)._lowercased(with: .current) } +#endif return _ns.localizedLowercase #else @@ -71,9 +75,11 @@ extension StringProtocol { @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *) public func lowercased(with locale: Locale?) -> String { #if FOUNDATION_FRAMEWORK +#if canImport(FoundationICU) if _foundation_essentials_feature_enabled() { return String(self)._lowercased(with: locale) } +#endif return _ns.lowercased(with: locale) #else return String(self)._lowercased(with: locale) @@ -85,9 +91,11 @@ extension StringProtocol { @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *) public var localizedUppercase: String { #if FOUNDATION_FRAMEWORK +#if canImport(FoundationICU) if _foundation_essentials_feature_enabled() { return String(self)._uppercased(with: .current) } +#endif return _ns.localizedUppercase #else @@ -101,9 +109,11 @@ extension StringProtocol { @available(macOS 10.11, iOS 9.0, watchOS 2.0, tvOS 9.0, *) public func uppercased(with locale: Locale?) -> String { #if FOUNDATION_FRAMEWORK +#if canImport(FoundationICU) if _foundation_essentials_feature_enabled() { return String(self)._uppercased(with: locale) } +#endif return _ns.uppercased(with: locale) #else diff --git a/Sources/FoundationInternationalization/TimeZone/TimeZone_ICU.swift b/Sources/FoundationInternationalization/TimeZone/TimeZone_ICU.swift index de9b3f46c..390af16c3 100644 --- a/Sources/FoundationInternationalization/TimeZone/TimeZone_ICU.swift +++ b/Sources/FoundationInternationalization/TimeZone/TimeZone_ICU.swift @@ -22,6 +22,7 @@ import Glibc import ucrt #endif +#if canImport(FoundationICU) #if FOUNDATION_FRAMEWORK @_implementationOnly import FoundationICU #else @@ -446,6 +447,7 @@ internal final class _TimeZoneICU: _TimeZoneProtocol, Sendable { return result } } +#endif //canImport(FoundationICU) // MARK: - @@ -460,7 +462,7 @@ extension TimeZone { icuTZIdentifiers } -#if FOUNDATION_FRAMEWORK +#if FOUNDATION_FRAMEWORK && canImport(FoundationICU) /// Returns the time zone data version. public static var timeZoneDataVersion : String { // At this time only available in Framework build because of dependency on ICU. When TimeZone sinks to FoundationEssentials, we can make this available everywhere as an extension on TimeZone from FoundationInternationalization. diff --git a/Sources/FoundationInternationalization/TimeZone/TimeZone_ObjC.swift b/Sources/FoundationInternationalization/TimeZone/TimeZone_ObjC.swift index 1c9517e45..db70de803 100644 --- a/Sources/FoundationInternationalization/TimeZone/TimeZone_ObjC.swift +++ b/Sources/FoundationInternationalization/TimeZone/TimeZone_ObjC.swift @@ -111,12 +111,20 @@ extension NSTimeZone { @objc static func _knownTimeZoneIdentifiers() -> [String] { +#if canImport(FoundationICU) TimeZone.knownTimeZoneIdentifiers +#else + [] +#endif } @objc static func _timeZoneDataVersion() -> String { +#if canImport(FoundationICU) TimeZone.timeZoneDataVersion +#else + "" +#endif } } @@ -229,13 +237,16 @@ final class _NSSwiftTimeZone: _NSTimeZoneBridge { } private static func dataFromTZFile(_ name: String) -> Data { +#if NO_TZFILE + return Data() +#else let path = TimeZone.TZDIR + "/" + name guard !path.contains("..") else { // No good reason for .. to be present anywhere in the path return Data() } - #if os(Windows) +#if os(Windows) let fd: CInt = path.withCString(encodedAs: UTF16.self) { var fd: CInt = -1 let errno: errno_t = @@ -243,9 +254,9 @@ final class _NSSwiftTimeZone: _NSTimeZoneBridge { guard errno == 0 else { return -1 } return fd } - #else +#else let fd = open(path, O_RDONLY, 0666) - #endif +#endif guard fd >= 0 else { return Data() } defer { close(fd) } @@ -280,6 +291,7 @@ final class _NSSwiftTimeZone: _NSTimeZoneBridge { guard ret >= sz else { return Data() } return Data(bytes: bytes.baseAddress!, count: sz) +#endif } } diff --git a/Sources/_CShims/include/string_shims.h b/Sources/_CShims/include/string_shims.h index b809d224d..f63cd8f62 100644 --- a/Sources/_CShims/include/string_shims.h +++ b/Sources/_CShims/include/string_shims.h @@ -13,7 +13,9 @@ #ifndef CSHIMS_STRING_H #define CSHIMS_STRING_H +#if __has_include() #include +#endif #include #if __has_include() @@ -24,6 +26,10 @@ #define locale_t _locale_t #endif +#if defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT +#define locale_t void * +#endif + int _cshims_strncasecmp_l(const char * _Nullable s1, const char * _Nullable s2, size_t n, locale_t _Nullable loc); double _cshims_strtod_l(const char * _Nullable restrict nptr, char * _Nullable * _Nullable restrict endptr, locale_t _Nullable loc); diff --git a/Sources/_CShims/string_shims.c b/Sources/_CShims/string_shims.c index 3d321f244..bfbf2da5a 100644 --- a/Sources/_CShims/string_shims.c +++ b/Sources/_CShims/string_shims.c @@ -10,16 +10,24 @@ // //===----------------------------------------------------------------------===// -#include "include/string_shims.h" #include "include/_CShimsTargetConditionals.h" +#include "include/string_shims.h" #include #include #include #include +#include + +#if !defined(CF_PRIVATE) +#define CF_PRIVATE __attribute__((__visibility__("hidden"))) extern +#endif -int -_cshims_strncasecmp_l(const char * _Nullable s1, +#if defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT +#include +#endif + +CF_PRIVATE int _cshims_strncasecmp_l(const char * _Nullable s1, const char * _Nullable s2, size_t n, locale_t _Nullable loc) @@ -34,11 +42,17 @@ _cshims_strncasecmp_l(const char * _Nullable s1, return _strnicmp_l(s1, s2, n, loc ? loc : *cloc); #else if (loc != NULL) { +#if defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT + abort(); +#else return strncasecmp_l(s1, s2, n, loc); +#endif } // On Darwin, NULL loc means unlocalized compare. // Uses the standard C locale for Linux in this case -#if TARGET_OS_MAC +#if defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT + return strncasecmp(s1, s2, n); +#elif TARGET_OS_MAC return strncasecmp_l(s1, s2, n, NULL); #else locale_t clocale = newlocale(LC_ALL_MASK, "C", (locale_t)0); @@ -47,12 +61,14 @@ _cshims_strncasecmp_l(const char * _Nullable s1, #endif // TARGET_OS_WINDOWS } -double -_cshims_strtod_l(const char * _Nullable restrict nptr, +CF_PRIVATE double _cshims_strtod_l(const char * _Nullable restrict nptr, char * _Nullable * _Nullable restrict endptr, locale_t _Nullable loc) { -#if TARGET_OS_MAC +#if defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT + assert(loc == NULL); + return strtod_l(nptr, endptr, NULL); +#elif TARGET_OS_MAC return strtod_l(nptr, endptr, loc); #elif TARGET_OS_WINDOWS return _strtod_l(nptr, endptr, loc); @@ -67,12 +83,14 @@ _cshims_strtod_l(const char * _Nullable restrict nptr, #endif } -float -_cshims_strtof_l(const char * _Nullable restrict nptr, +CF_PRIVATE float _cshims_strtof_l(const char * _Nullable restrict nptr, char * _Nullable * _Nullable restrict endptr, locale_t _Nullable loc) { -#if TARGET_OS_MAC +#if defined(TARGET_OS_EXCLAVEKIT) && TARGET_OS_EXCLAVEKIT + assert(loc == NULL); + return strtof_l(nptr, endptr, NULL); +#elif TARGET_OS_MAC return strtof_l(nptr, endptr, loc); #elif TARGET_OS_WINDOWS return _strtof_l(nptr, endptr, loc); @@ -87,8 +105,7 @@ _cshims_strtof_l(const char * _Nullable restrict nptr, #endif } -int -_cshims_get_formatted_str_length(double value) +CF_PRIVATE int _cshims_get_formatted_str_length(double value) { char empty[1]; return snprintf(empty, 0, "%0.*g", DBL_DECIMAL_DIG, value); diff --git a/Sources/_FoundationInternals/LockedState.swift b/Sources/_FoundationInternals/LockedState.swift index b2ca545ee..dfbaa1e08 100644 --- a/Sources/_FoundationInternals/LockedState.swift +++ b/Sources/_FoundationInternals/LockedState.swift @@ -10,9 +10,12 @@ // //===----------------------------------------------------------------------===// -#if canImport(Darwin) +#if canImport(os) #if FOUNDATION_FRAMEWORK @_implementationOnly import os +#if canImport(C.os.lock) +@_implementationOnly import C.os.lock +#endif #else package import os #endif @@ -26,7 +29,7 @@ internal struct LockedState { // Internal implementation for a cheap lock to aid sharing code across platforms private struct _Lock { -#if canImport(Darwin) +#if canImport(os) typealias Primitive = os_unfair_lock #elseif canImport(Glibc) typealias Primitive = pthread_mutex_t @@ -38,7 +41,7 @@ internal struct LockedState { var _platformLock: PlatformLock fileprivate static func initialize(_ platformLock: PlatformLock) { -#if canImport(Darwin) +#if canImport(os) platformLock.initialize(to: os_unfair_lock()) #elseif canImport(Glibc) pthread_mutex_init(platformLock, nil) @@ -55,7 +58,7 @@ internal struct LockedState { } static fileprivate func lock(_ platformLock: PlatformLock) { -#if canImport(Darwin) +#if canImport(os) os_unfair_lock_lock(platformLock) #elseif canImport(Glibc) pthread_mutex_lock(platformLock) @@ -65,7 +68,7 @@ internal struct LockedState { } static fileprivate func unlock(_ platformLock: PlatformLock) { -#if canImport(Darwin) +#if canImport(os) os_unfair_lock_unlock(platformLock) #elseif canImport(Glibc) pthread_mutex_unlock(platformLock)