Skip to content

Foundation: synchronize macOS/iOS and Linux paths #1729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions Foundation/DateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ open class DateFormatter : Formatter {
private var __cfObject: CFType?
private var _cfObject: CFType {
guard let obj = __cfObject else {
#if os(macOS) || os(iOS)
let dateStyle = CFDateFormatterStyle(rawValue: CFIndex(self.dateStyle.rawValue))!
let timeStyle = CFDateFormatterStyle(rawValue: CFIndex(self.timeStyle.rawValue))!
#else
let dateStyle = CFDateFormatterStyle(self.dateStyle.rawValue)
let timeStyle = CFDateFormatterStyle(self.timeStyle.rawValue)
#endif

let dateStyle = CFDateFormatterStyle(rawValue: CFIndex(self.dateStyle.rawValue))!
let timeStyle = CFDateFormatterStyle(rawValue: CFIndex(self.timeStyle.rawValue))!

let obj = CFDateFormatterCreate(kCFAllocatorSystemDefault, locale._cfObject, dateStyle, timeStyle)!
_setFormatterAttributes(obj)
if let dateFormat = _dateFormat {
Expand Down
15 changes: 2 additions & 13 deletions Foundation/ISO8601DateFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ open class ISO8601DateFormatter : Formatter, NSSecureCoding {
private var __cfObject: CFType?
private var _cfObject: CFType {
guard let obj = __cfObject else {
#if os(macOS) || os(iOS)
let format = CFISO8601DateFormatOptions(rawValue: formatOptions.rawValue)
#else
let format = CFISO8601DateFormatOptions(self.formatOptions.rawValue)
#endif
let format = CFISO8601DateFormatOptions(rawValue: formatOptions.rawValue)
let obj = CFDateFormatterCreateISO8601Formatter(kCFAllocatorSystemDefault, format)!
CFDateFormatterSetProperty(obj, kCFDateFormatterTimeZone, timeZone._cfObject)
__cfObject = obj
Expand Down Expand Up @@ -101,17 +97,10 @@ open class ISO8601DateFormatter : Formatter, NSSecureCoding {
}

open class func string(from date: Date, timeZone: TimeZone, formatOptions: ISO8601DateFormatter.Options = []) -> String {

#if os(macOS) || os(iOS)
let format = CFISO8601DateFormatOptions(rawValue: formatOptions.rawValue)
#else
let format = CFISO8601DateFormatOptions(formatOptions.rawValue)
#endif

let format = CFISO8601DateFormatOptions(rawValue: formatOptions.rawValue)
let obj = CFDateFormatterCreateISO8601Formatter(kCFAllocatorSystemDefault, format)
CFDateFormatterSetProperty(obj, kCFDateFormatterTimeZone, timeZone._cfObject)
return CFDateFormatterCreateStringWithDate(kCFAllocatorSystemDefault, obj, date._cfObject)._swiftObject

}

private func _reset() {
Expand Down
6 changes: 0 additions & 6 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import CoreFoundation

#if os(macOS) || os(iOS)
internal let kCFCalendarUnitEra = CFCalendarUnit.era.rawValue
internal let kCFCalendarUnitYear = CFCalendarUnit.year.rawValue
internal let kCFCalendarUnitMonth = CFCalendarUnit.month.rawValue
Expand All @@ -29,7 +28,6 @@ internal let kCFDateFormatterShortStyle = CFDateFormatterStyle.shortStyle
internal let kCFDateFormatterMediumStyle = CFDateFormatterStyle.mediumStyle
internal let kCFDateFormatterLongStyle = CFDateFormatterStyle.longStyle
internal let kCFDateFormatterFullStyle = CFDateFormatterStyle.fullStyle
#endif

extension NSCalendar {
public struct Identifier : RawRepresentable, Equatable, Hashable, Comparable {
Expand Down Expand Up @@ -89,11 +87,7 @@ extension NSCalendar {
public static let timeZone = Unit(rawValue: UInt(1 << 21))

internal var _cfValue: CFCalendarUnit {
#if os(macOS) || os(iOS)
return CFCalendarUnit(rawValue: self.rawValue)
#else
return CFCalendarUnit(self.rawValue)
#endif
}
}

Expand Down
3 changes: 0 additions & 3 deletions Foundation/NSCharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import CoreFoundation

#if os(macOS) || os(iOS)
let kCFCharacterSetControl = CFCharacterSetPredefinedSet.control
let kCFCharacterSetWhitespace = CFCharacterSetPredefinedSet.whitespace
let kCFCharacterSetWhitespaceAndNewline = CFCharacterSetPredefinedSet.whitespaceAndNewline
Expand All @@ -26,8 +25,6 @@ let kCFCharacterSetCapitalizedLetter = CFCharacterSetPredefinedSet.capitalizedLe
let kCFCharacterSetSymbol = CFCharacterSetPredefinedSet.symbol
let kCFCharacterSetNewline = CFCharacterSetPredefinedSet.newline
let kCFCharacterSetIllegal = CFCharacterSetPredefinedSet.illegal
#endif


open class NSCharacterSet : NSObject, NSCopying, NSMutableCopying, NSCoding {
typealias CFType = CFCharacterSet
Expand Down
8 changes: 0 additions & 8 deletions Foundation/NSLocale.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,12 @@ extension NSLocale {

open class func characterDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection {
let dir = CFLocaleGetLanguageCharacterDirection(isoLangCode._cfObject)
#if os(macOS) || os(iOS)
return NSLocale.LanguageDirection(rawValue: UInt(dir.rawValue))!
#else
return NSLocale.LanguageDirection(rawValue: UInt(dir))!
#endif
}

open class func lineDirection(forLanguage isoLangCode: String) -> NSLocale.LanguageDirection {
let dir = CFLocaleGetLanguageLineDirection(isoLangCode._cfObject)
#if os(macOS) || os(iOS)
return NSLocale.LanguageDirection(rawValue: UInt(dir.rawValue))!
#else
return NSLocale.LanguageDirection(rawValue: UInt(dir))!
#endif
}
}

Expand Down
4 changes: 0 additions & 4 deletions Foundation/NSNumber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import CoreFoundation

#if os(macOS) || os(iOS)
internal let kCFNumberSInt8Type = CFNumberType.sInt8Type
internal let kCFNumberSInt16Type = CFNumberType.sInt16Type
internal let kCFNumberSInt32Type = CFNumberType.sInt32Type
Expand All @@ -28,9 +27,6 @@ internal let kCFNumberCFIndexType = CFNumberType.cfIndexType
internal let kCFNumberNSIntegerType = CFNumberType.nsIntegerType
internal let kCFNumberCGFloatType = CFNumberType.cgFloatType
internal let kCFNumberSInt128Type = CFNumberType(rawValue: 17)!
#else
internal let kCFNumberSInt128Type: CFNumberType = 17
#endif

extension Int8 : _ObjectiveCBridgeable {
@available(swift, deprecated: 4, renamed: "init(truncating:)")
Expand Down
2 changes: 0 additions & 2 deletions Foundation/NSObjCRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@

import CoreFoundation

#if os(macOS) || os(iOS)
internal let kCFCompareLessThan = CFComparisonResult.compareLessThan
internal let kCFCompareEqualTo = CFComparisonResult.compareEqualTo
internal let kCFCompareGreaterThan = CFComparisonResult.compareGreaterThan
#endif

internal enum _NSSimpleObjCType : UnicodeScalar {
case ID = "@"
Expand Down
20 changes: 0 additions & 20 deletions Foundation/NSRegularExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ open class NSRegularExpression: NSObject, NSCopying, NSCoding {

public init(pattern: String, options: Options = []) throws {
var error: Unmanaged<CFError>?
#if os(macOS) || os(iOS)
let opt = _CFRegularExpressionOptions(rawValue: options.rawValue)
#else
let opt = _CFRegularExpressionOptions(options.rawValue)
#endif
if let regex = _CFRegularExpressionCreate(kCFAllocatorSystemDefault, pattern._cfObject, opt, &error) {
_internal = regex
} else {
Expand All @@ -94,11 +90,7 @@ open class NSRegularExpression: NSObject, NSCopying, NSCoding {
}

open var options: Options {
#if os(macOS) || os(iOS)
let opt = _CFRegularExpressionGetOptions(_internal).rawValue
#else
let opt = _CFRegularExpressionGetOptions(_internal)
#endif

return Options(rawValue: opt)
}
Expand Down Expand Up @@ -152,23 +144,15 @@ internal class _NSRegularExpressionMatcher {
internal func _NSRegularExpressionMatch(_ context: UnsafeMutableRawPointer?, ranges: UnsafeMutablePointer<CFRange>?, count: CFIndex, options: _CFRegularExpressionMatchingOptions, stop: UnsafeMutablePointer<_DarwinCompatibleBoolean>) -> Void {
let matcher = unsafeBitCast(context, to: _NSRegularExpressionMatcher.self)
if ranges == nil {
#if os(macOS) || os(iOS)
let opts = options.rawValue
#else
let opts = options
#endif
stop.withMemoryRebound(to: ObjCBool.self, capacity: 1, {
matcher.block(nil, NSRegularExpression.MatchingFlags(rawValue: opts), $0)
})
} else {
let result = ranges!.withMemoryRebound(to: NSRange.self, capacity: count) { rangePtr in
NSTextCheckingResult.regularExpressionCheckingResultWithRanges(rangePtr, count: count, regularExpression: matcher.regex)
}
#if os(macOS) || os(iOS)
let flags = NSRegularExpression.MatchingFlags(rawValue: options.rawValue)
#else
let flags = NSRegularExpression.MatchingFlags(rawValue: options)
#endif
stop.withMemoryRebound(to: ObjCBool.self, capacity: 1, {
matcher.block(result, flags, $0)
})
Expand All @@ -183,11 +167,7 @@ extension NSRegularExpression {
public func enumerateMatches(in string: String, options: NSRegularExpression.MatchingOptions = [], range: NSRange, using block: @escaping (NSTextCheckingResult?, NSRegularExpression.MatchingFlags, UnsafeMutablePointer<ObjCBool>) -> Swift.Void) {
let matcher = _NSRegularExpressionMatcher(regex: self, block: block)
withExtendedLifetime(matcher) { (m: _NSRegularExpressionMatcher) -> Void in
#if os(macOS) || os(iOS)
let opts = _CFRegularExpressionMatchingOptions(rawValue: options.rawValue)
#else
let opts = _CFRegularExpressionMatchingOptions(options.rawValue)
#endif
_CFRegularExpressionEnumerateMatchesInString(_internal, string._cfObject, opts, CFRange(range), unsafeBitCast(matcher, to: UnsafeMutableRawPointer.self), _NSRegularExpressionMatch)
}
}
Expand Down
7 changes: 0 additions & 7 deletions Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func NSLocalizedString(_ key: String,
return bundle.localizedString(forKey: key, value: value, table: tableName)
}

#if os(macOS) || os(iOS)
internal let kCFStringEncodingMacRoman = CFStringBuiltInEncodings.macRoman.rawValue
internal let kCFStringEncodingWindowsLatin1 = CFStringBuiltInEncodings.windowsLatin1.rawValue
internal let kCFStringEncodingISOLatin1 = CFStringBuiltInEncodings.isoLatin1.rawValue
Expand All @@ -53,8 +52,6 @@ internal let kCFStringNormalizationFormD = CFStringNormalizationForm.D
internal let kCFStringNormalizationFormKD = CFStringNormalizationForm.KD
internal let kCFStringNormalizationFormC = CFStringNormalizationForm.C
internal let kCFStringNormalizationFormKC = CFStringNormalizationForm.KC

#endif

extension NSString {

Expand Down Expand Up @@ -100,11 +97,7 @@ extension NSString {
public static let regularExpression = CompareOptions(rawValue: 1024)

internal func _cfValue(_ fixLiteral: Bool = false) -> CFStringCompareFlags {
#if os(macOS) || os(iOS)
return contains(.literal) || !fixLiteral ? CFStringCompareFlags(rawValue: rawValue) : CFStringCompareFlags(rawValue: rawValue).union(.compareNonliteral)
#else
return contains(.literal) || !fixLiteral ? CFStringCompareFlags(rawValue) : CFStringCompareFlags(rawValue) | UInt(kCFCompareNonliteral)
#endif
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions Foundation/NSTimeZone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,7 @@ extension NSTimeZone {
}

open func localizedName(_ style: NameStyle, locale: Locale?) -> String? {
#if os(macOS) || os(iOS)
let cfStyle = CFTimeZoneNameStyle(rawValue: style.rawValue)!
#else
let cfStyle = CFTimeZoneNameStyle(style.rawValue)
#endif
let cfStyle = CFTimeZoneNameStyle(rawValue: style.rawValue)!
return CFTimeZoneCopyLocalizedName(self._cfObject, cfStyle, locale?._cfObject ?? CFLocaleCopyCurrent())._swiftObject
}

Expand Down
6 changes: 0 additions & 6 deletions Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import Darwin
import Glibc
#endif

#if os(macOS) || os(iOS)
internal let kCFURLPOSIXPathStyle = CFURLPathStyle.cfurlposixPathStyle
internal let kCFURLWindowsPathStyle = CFURLPathStyle.cfurlWindowsPathStyle
#endif

private func _standardizedPath(_ path: String) -> String {
if !path.absolutePath {
Expand Down Expand Up @@ -499,11 +497,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {

open var password: String? {
let absoluteURL = CFURLCopyAbsoluteURL(_cfObject)
#if os(Linux) || os(Android) || CYGWIN
let passwordRange = CFURLGetByteRangeForComponent(absoluteURL, kCFURLComponentPassword, nil)
#else
let passwordRange = CFURLGetByteRangeForComponent(absoluteURL, .password, nil)
#endif
guard passwordRange.location != kCFNotFound else {
return nil
}
Expand Down
16 changes: 3 additions & 13 deletions Foundation/NumberFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import CoreFoundation

#if os(macOS) || os(iOS)
internal let kCFNumberFormatterNoStyle = CFNumberFormatterStyle.noStyle
internal let kCFNumberFormatterDecimalStyle = CFNumberFormatterStyle.decimalStyle
internal let kCFNumberFormatterCurrencyStyle = CFNumberFormatterStyle.currencyStyle
Expand All @@ -20,7 +19,6 @@ internal let kCFNumberFormatterOrdinalStyle = CFNumberFormatterStyle.ordinalStyl
internal let kCFNumberFormatterCurrencyISOCodeStyle = CFNumberFormatterStyle.currencyISOCodeStyle
internal let kCFNumberFormatterCurrencyPluralStyle = CFNumberFormatterStyle.currencyPluralStyle
internal let kCFNumberFormatterCurrencyAccountingStyle = CFNumberFormatterStyle.currencyAccountingStyle
#endif

extension NumberFormatter {
public enum Style : UInt {
Expand Down Expand Up @@ -62,12 +60,8 @@ open class NumberFormatter : Formatter {
if let obj = _currentCfFormatter {
return obj
} else {
#if os(macOS) || os(iOS)
let numberStyle = CFNumberFormatterStyle(rawValue: CFIndex(self.numberStyle.rawValue))!
#else
let numberStyle = CFNumberFormatterStyle(self.numberStyle.rawValue)
#endif

let numberStyle = CFNumberFormatterStyle(rawValue: CFIndex(self.numberStyle.rawValue))!

let obj = CFNumberFormatterCreate(kCFAllocatorSystemDefault, locale._cfObject, numberStyle)!
_setFormatterAttributes(obj)
if let format = _format {
Expand Down Expand Up @@ -103,11 +97,7 @@ open class NumberFormatter : Formatter {
var range = CFRange(location: 0, length: string.length)
let number = withUnsafeMutablePointer(to: &range) { (rangePointer: UnsafeMutablePointer<CFRange>) -> NSNumber? in

#if os(macOS) || os(iOS)
let parseOption = allowsFloats ? 0 : CFNumberFormatterOptionFlags.parseIntegersOnly.rawValue
#else
let parseOption = allowsFloats ? 0 : CFOptionFlags(kCFNumberFormatterParseIntegersOnly)
#endif
let parseOption = allowsFloats ? 0 : CFNumberFormatterOptionFlags.parseIntegersOnly.rawValue
let result = CFNumberFormatterCreateNumberFromString(kCFAllocatorSystemDefault, _cfFormatter, string._cfObject, rangePointer, parseOption)

return result?._nsObject
Expand Down
2 changes: 0 additions & 2 deletions Foundation/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ private var managerThreadRunLoop : RunLoop? = nil
private var managerThreadRunLoopIsRunning = false
private var managerThreadRunLoopIsRunningCondition = NSCondition()

#if os(macOS) || os(iOS)
internal let kCFSocketDataCallBack = CFSocketCallBackType.dataCallBack.rawValue
#endif

private func emptyRunLoopCallback(_ context : UnsafeMutableRawPointer?) -> Void {}

Expand Down
18 changes: 0 additions & 18 deletions Foundation/PropertyListSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@

import CoreFoundation

#if os(macOS) || os(iOS)
let kCFPropertyListOpenStepFormat = CFPropertyListFormat.openStepFormat
let kCFPropertyListXMLFormat_v1_0 = CFPropertyListFormat.xmlFormat_v1_0
let kCFPropertyListBinaryFormat_v1_0 = CFPropertyListFormat.binaryFormat_v1_0
#endif

extension PropertyListSerialization {

Expand All @@ -39,23 +37,15 @@ extension PropertyListSerialization {
open class PropertyListSerialization : NSObject {

open class func propertyList(_ plist: Any, isValidFor format: PropertyListFormat) -> Bool {
#if os(macOS) || os(iOS)
let fmt = CFPropertyListFormat(rawValue: CFIndex(format.rawValue))!
#else
let fmt = CFPropertyListFormat(format.rawValue)
#endif
let plistObj = __SwiftValue.store(plist)
return CFPropertyListIsValid(plistObj, fmt)
}

open class func data(fromPropertyList plist: Any, format: PropertyListFormat, options opt: WriteOptions) throws -> Data {
var error: Unmanaged<CFError>? = nil
let result = withUnsafeMutablePointer(to: &error) { (outErr: UnsafeMutablePointer<Unmanaged<CFError>?>) -> CFData? in
#if os(macOS) || os(iOS)
let fmt = CFPropertyListFormat(rawValue: CFIndex(format.rawValue))!
#else
let fmt = CFPropertyListFormat(format.rawValue)
#endif
let options = CFOptionFlags(opt)
let plistObj = __SwiftValue.store(plist)
let d = CFPropertyListCreateData(kCFAllocatorSystemDefault, plistObj, fmt, options, outErr)
Expand All @@ -76,11 +66,7 @@ open class PropertyListSerialization : NSObject {
return unsafeBitCast(CFPropertyListCreateWithData(kCFAllocatorSystemDefault, data._cfObject, CFOptionFlags(CFIndex(opt.rawValue)), outFmt, outErr), to: NSObject.self)
}
}
#if os(macOS) || os(iOS)
format?.pointee = PropertyListFormat(rawValue: UInt(fmt.rawValue))!
#else
format?.pointee = PropertyListFormat(rawValue: UInt(fmt))!
#endif
if let err = error {
throw err.takeUnretainedValue()._nsObject
} else {
Expand All @@ -96,11 +82,7 @@ open class PropertyListSerialization : NSObject {
return unsafeBitCast(CFPropertyListCreateWithStream(kCFAllocatorSystemDefault, stream, 0, CFOptionFlags(CFIndex(opt.rawValue)), outFmt, outErr), to: NSObject.self)
}
}
#if os(macOS) || os(iOS)
format?.pointee = PropertyListFormat(rawValue: UInt(fmt.rawValue))!
#else
format?.pointee = PropertyListFormat(rawValue: UInt(fmt))!
#endif
if let err = error {
throw err.takeUnretainedValue()._nsObject
} else {
Expand Down
Loading