Skip to content

Commit 08b813d

Browse files
committed
Merge master
2 parents 621583a + ef8e9fa commit 08b813d

File tree

6 files changed

+278
-102
lines changed

6 files changed

+278
-102
lines changed

CoreFoundation/Base.subproj/CFInternal.h

+49-51
Original file line numberDiff line numberDiff line change
@@ -341,50 +341,10 @@ CF_PRIVATE Boolean __CFProcessIsRestricted();
341341

342342

343343
CF_EXPORT void * __CFConstantStringClassReferencePtr;
344+
#if defined(__CONSTANT_CFSTRINGS__)
344345
CF_EXPORT void *__CFConstantStringClassReference[];
345-
346-
#if __CF_BIG_ENDIAN__
347-
#define CFINFO {0x00, 0x00, 0x07, 0xc8}
348-
#elif __CF_LITTLE_ENDIAN__
349-
#define CFINFO {0xc8, 0x07, 0x00, 0x00}
350-
#endif
351-
352-
353-
#ifdef __CONSTANT_CFSTRINGS__
354-
355-
#if DEPLOYMENT_RUNTIME_SWIFT
356-
357-
#if DEPLOYMENT_TARGET_LINUX
358-
#define CONST_STRING_SECTION __attribute__((section(".cfstr.data")))
359-
#else
360-
#define CONST_STRING_SECTION
361-
#endif
362-
363-
364-
// TODO: Pinned retain count for constants?
365-
#define CONST_STRING_DECL(S, V) \
366-
const struct __CFConstStr __##S CONST_STRING_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
367-
const CFStringRef S = (CFStringRef)&__##S;
368-
369-
#define PE_CONST_STRING_DECL(S, V) \
370-
const static struct __CFConstStr __##S CONST_STRING_SECTION = {{(uintptr_t)&__CFConstantStringClassReference, _CF_CONSTANT_OBJECT_STRONG_RC, 0, CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
371-
CF_PRIVATE const CFStringRef S = (CFStringRef)&__##S;
372-
373-
374-
#else
375-
376-
#define CONST_STRING_DECL(S, V) \
377-
const struct __CFConstStr __##S = {{(uintptr_t)&__CFConstantStringClassReference, _CFSWIFT_RC_INIT CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
378-
const CFStringRef S = (CFStringRef)&__##S;
379-
380-
#define PE_CONST_STRING_DECL(S, V) \
381-
const static struct __CFConstStr __##S = {{(uintptr_t)&__CFConstantStringClassReference, _CFSWIFT_RC_INIT CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
382-
CF_PRIVATE const CFStringRef S = (CFStringRef)&__##S;
383-
384-
385-
#endif
386-
387346
#else
347+
CF_EXPORT int __CFConstantStringClassReference[];
388348

389349
struct CF_CONST_STRING {
390350
CFRuntimeBase _base;
@@ -395,19 +355,57 @@ struct CF_CONST_STRING {
395355
uint32_t _length;
396356
#endif
397357
};
358+
#endif
398359

399-
CF_EXPORT int __CFConstantStringClassReference[];
360+
#if __CF_BIG_ENDIAN__
361+
#define __CFSTR_FLAGS {0x00, 0x00, 0x07, 0xc8}
362+
#elif __CF_LITTLE_ENDIAN__
363+
#define __CFSTR_FLAGS {0xc8, 0x07, 0x00, 0x00}
364+
#endif
400365

401-
/* CFNetwork also has a copy of the CONST_STRING_DECL macro (for use on platforms without constant string support in cc); please warn cfnetwork-core@group.apple.com of any necessary changes to this macro. -- REW, 1/28/2002 */
366+
#if defined(__CONSTANT_CFSTRINGS__)
367+
#define __CFSTR_TAG __CFConstStr
368+
#define __CFSTR_CONST const
369+
#else
370+
#define __CFSTR_TAG CF_CONST_STRING
371+
#define __CFSTR_CONST
372+
#endif
402373

403-
#define CONST_STRING_DECL(S, V) \
404-
static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
405-
const CFStringRef S = (CFStringRef) & __ ## S ## __;
406-
#define PE_CONST_STRING_DECL(S, V) \
407-
static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, CFINFO}, (uint8_t *)(V), sizeof(V) - 1}; \
408-
CF_PRIVATE const CFStringRef S = (CFStringRef) & __ ## S ## __;
374+
#if DEPLOYMENT_RUNTIME_SWIFT
375+
// TODO: Pinned retain count for constants?
376+
#define __CFSTR_RC_INIT _CF_CONSTANT_OBJECT_STRONG_RC, 0,
377+
378+
#if DEPLOYMENT_TARGET_LINUX
379+
#define __CFSTR_SECTION __attribute__((section(".cfstr.data")))
380+
#else
381+
#define __CFSTR_SECTION
382+
#endif
383+
#else
384+
#define __CFSTR_RC_INIT
385+
#define __CFSTR_SECTION
386+
#endif
409387

410-
#endif // __CONSTANT_CFSTRINGS__
388+
/*
389+
* CFNetwork also has a copy of the CONST_STRING_DECL macro (for use on
390+
* platforms without constant string support in cc); please warn
391+
* cfnetwork-core@group.apple.com of any necessary changes to this macro.
392+
* -- REW, 1/28/2002
393+
*/
394+
#define CONST_STRING_DECL(S, V) \
395+
__CFSTR_CONST struct __CFSTR_TAG __##S __CFSTR_SECTION = { \
396+
{(uintptr_t)&__CFConstantStringClassReference, \
397+
__CFSTR_RC_INIT __CFSTR_FLAGS}, \
398+
(uint8_t *)(V), \
399+
sizeof(V) - 1}; \
400+
const CFStringRef S = (CFStringRef)&__##S;
401+
402+
#define PE_CONST_STRING_DECL(S, V) \
403+
static __CFSTR_CONST struct __CFSTR_TAG __##S __CFSTR_SECTION = { \
404+
{(uintptr_t)&__CFConstantStringClassReference, \
405+
__CFSTR_RC_INIT __CFSTR_FLAGS}, \
406+
(uint8_t *)(V), \
407+
sizeof(V) - 1}; \
408+
CF_PRIVATE const CFStringRef S = (CFStringRef)&__##S;
411409

412410
CF_EXPORT bool __CFOASafe;
413411
CF_EXPORT void __CFSetLastAllocationEventName(void *ptr, const char *classname);

Foundation/NSAttributedString.swift

+114-48
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import CoreFoundation
1212
public class AttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
1313

1414
private let _cfinfo = _CFInfo(typeID: CFAttributedStringGetTypeID())
15-
private let _string: NSString
16-
private let _attributeArray: CFRunArrayRef
15+
private var _string: NSString
16+
private var _attributeArray: CFRunArrayRef
1717

1818
public required init?(coder aDecoder: NSCoder) {
1919
NSUnimplemented()
@@ -48,57 +48,42 @@ public class AttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
4848
}
4949

5050
public func attributesAtIndex(_ location: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] {
51-
var cfRange = CFRange()
52-
return withUnsafeMutablePointer(&cfRange) { (rangePointer: UnsafeMutablePointer<CFRange>) -> [String : AnyObject] in
53-
// Get attributes value using CoreFoundation function
54-
let value = CFAttributedStringGetAttributes(_cfObject, location, rangePointer)
55-
56-
// Convert the value to [String : AnyObject]
57-
let dictionary = unsafeBitCast(value, to: NSDictionary.self)
58-
var results = [String : AnyObject]()
59-
for (key, value) in dictionary {
60-
guard let stringKey = (key as? NSString)?._swiftObject else {
61-
continue
62-
}
63-
results[stringKey] = value
64-
}
65-
66-
// Update effective range
67-
let hasAttrs = results.count > 0
68-
range.pointee.location = hasAttrs ? rangePointer.pointee.location : NSNotFound
69-
range.pointee.length = hasAttrs ? rangePointer.pointee.length : 0
70-
71-
return results
72-
}
51+
let rangeInfo = RangeInfo(
52+
rangePointer: range,
53+
shouldFetchLongestEffectiveRange: false,
54+
longestEffectiveRangeSearchRange: nil)
55+
return _attributesAtIndex(location, rangeInfo: rangeInfo)
7356
}
7457

7558
public var length: Int {
7659
return CFAttributedStringGetLength(_cfObject)
7760
}
7861

7962
public func attribute(_ attrName: String, atIndex location: Int, effectiveRange range: NSRangePointer) -> AnyObject? {
80-
var cfRange = CFRange()
81-
return withUnsafeMutablePointer(&cfRange) { (rangePointer: UnsafeMutablePointer<CFRange>) -> AnyObject? in
82-
// Get attribute value using CoreFoundation function
83-
let attribute = CFAttributedStringGetAttribute(_cfObject, location, attrName._cfObject, rangePointer)
84-
85-
// Update effective range and return the result
86-
if let attribute = attribute {
87-
range.pointee.location = rangePointer.pointee.location
88-
range.pointee.length = rangePointer.pointee.length
89-
return attribute
90-
} else {
91-
range.pointee.location = NSNotFound
92-
range.pointee.length = 0
93-
return nil
94-
}
95-
}
63+
let rangeInfo = RangeInfo(
64+
rangePointer: range,
65+
shouldFetchLongestEffectiveRange: false,
66+
longestEffectiveRangeSearchRange: nil)
67+
return _attribute(attrName, atIndex: location, rangeInfo: rangeInfo)
9668
}
9769

9870
public func attributedSubstringFromRange(_ range: NSRange) -> AttributedString { NSUnimplemented() }
9971

100-
public func attributesAtIndex(_ location: Int, longestEffectiveRange range: NSRangePointer, inRange rangeLimit: NSRange) -> [String : AnyObject] { NSUnimplemented() }
101-
public func attribute(_ attrName: String, atIndex location: Int, longestEffectiveRange range: NSRangePointer, inRange rangeLimit: NSRange) -> AnyObject? { NSUnimplemented() }
72+
public func attributesAtIndex(_ location: Int, longestEffectiveRange range: NSRangePointer, inRange rangeLimit: NSRange) -> [String : AnyObject] {
73+
let rangeInfo = RangeInfo(
74+
rangePointer: range,
75+
shouldFetchLongestEffectiveRange: true,
76+
longestEffectiveRangeSearchRange: rangeLimit)
77+
return _attributesAtIndex(location, rangeInfo: rangeInfo)
78+
}
79+
80+
public func attribute(_ attrName: String, atIndex location: Int, longestEffectiveRange range: NSRangePointer, inRange rangeLimit: NSRange) -> AnyObject? {
81+
let rangeInfo = RangeInfo(
82+
rangePointer: range,
83+
shouldFetchLongestEffectiveRange: true,
84+
longestEffectiveRangeSearchRange: rangeLimit)
85+
return _attribute(attrName, atIndex: location, rangeInfo: rangeInfo)
86+
}
10287

10388
public func isEqualToAttributedString(_ other: AttributedString) -> Bool { NSUnimplemented() }
10489

@@ -119,6 +104,72 @@ public class AttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
119104
}
120105

121106
public init(attributedString attrStr: AttributedString) { NSUnimplemented() }
107+
108+
public func enumerateAttributesInRange(_ enumerationRange: NSRange, options opts: EnumerationOptions, usingBlock block: ([String : AnyObject], NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) { NSUnimplemented() }
109+
public func enumerateAttribute(_ attrName: String, inRange enumerationRange: NSRange, options opts: EnumerationOptions, usingBlock block: (AnyObject?, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) { NSUnimplemented() }
110+
111+
}
112+
113+
private extension AttributedString {
114+
private struct RangeInfo {
115+
let rangePointer: NSRangePointer
116+
let shouldFetchLongestEffectiveRange: Bool
117+
let longestEffectiveRangeSearchRange: NSRange?
118+
}
119+
120+
private func _attributesAtIndex(_ location: Int, rangeInfo: RangeInfo) -> [String : AnyObject] {
121+
var cfRange = CFRange()
122+
return withUnsafeMutablePointer(&cfRange) { (cfRangePointer: UnsafeMutablePointer<CFRange>) -> [String : AnyObject] in
123+
// Get attributes value using CoreFoundation function
124+
let value: CFDictionary
125+
if rangeInfo.shouldFetchLongestEffectiveRange, let searchRange = rangeInfo.longestEffectiveRangeSearchRange {
126+
value = CFAttributedStringGetAttributesAndLongestEffectiveRange(_cfObject, location, CFRange(searchRange), cfRangePointer)
127+
} else {
128+
value = CFAttributedStringGetAttributes(_cfObject, location, cfRangePointer)
129+
}
130+
131+
// Convert the value to [String : AnyObject]
132+
let dictionary = unsafeBitCast(value, to: NSDictionary.self)
133+
var results = [String : AnyObject]()
134+
for (key, value) in dictionary {
135+
guard let stringKey = (key as? NSString)?._swiftObject else {
136+
continue
137+
}
138+
results[stringKey] = value
139+
}
140+
141+
// Update effective range
142+
let hasAttrs = results.count > 0
143+
rangeInfo.rangePointer.pointee.location = hasAttrs ? cfRangePointer.pointee.location : NSNotFound
144+
rangeInfo.rangePointer.pointee.length = hasAttrs ? cfRangePointer.pointee.length : 0
145+
146+
return results
147+
}
148+
}
149+
150+
private func _attribute(_ attrName: String, atIndex location: Int, rangeInfo: RangeInfo) -> AnyObject? {
151+
var cfRange = CFRange()
152+
return withUnsafeMutablePointer(&cfRange) { (cfRangePointer: UnsafeMutablePointer<CFRange>) -> AnyObject? in
153+
// Get attribute value using CoreFoundation function
154+
let attribute: AnyObject?
155+
if rangeInfo.shouldFetchLongestEffectiveRange, let searchRange = rangeInfo.longestEffectiveRangeSearchRange {
156+
attribute = CFAttributedStringGetAttributeAndLongestEffectiveRange(_cfObject, location, attrName._cfObject, CFRange(searchRange), cfRangePointer)
157+
} else {
158+
attribute = CFAttributedStringGetAttribute(_cfObject, location, attrName._cfObject, cfRangePointer)
159+
}
160+
161+
// Update effective range and return the result
162+
if let attribute = attribute {
163+
rangeInfo.rangePointer.pointee.location = cfRangePointer.pointee.location
164+
rangeInfo.rangePointer.pointee.length = cfRangePointer.pointee.length
165+
return attribute
166+
} else {
167+
rangeInfo.rangePointer.pointee.location = NSNotFound
168+
rangeInfo.rangePointer.pointee.length = 0
169+
return nil
170+
}
171+
}
172+
}
122173

123174
private func addAttributesToAttributeArray(attrs: [String : AnyObject]?) {
124175
guard _string.length > 0 else {
@@ -133,9 +184,6 @@ public class AttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
133184
CFRunArrayInsert(_attributeArray, range, emptyAttrs._cfObject)
134185
}
135186
}
136-
137-
public func enumerateAttributesInRange(_ enumerationRange: NSRange, options opts: EnumerationOptions, usingBlock block: ([String : AnyObject], NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) { NSUnimplemented() }
138-
public func enumerateAttribute(_ attrName: String, inRange enumerationRange: NSRange, options opts: EnumerationOptions, usingBlock block: (AnyObject?, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) { NSUnimplemented() }
139187
}
140188

141189
extension AttributedString: _CFBridgable {
@@ -160,12 +208,17 @@ public class NSMutableAttributedString : AttributedString {
160208

161209
public func replaceCharactersInRange(_ range: NSRange, withString str: String) { NSUnimplemented() }
162210
public func setAttributes(_ attrs: [String : AnyObject]?, range: NSRange) { NSUnimplemented() }
163-
164211

165-
public var mutableString: NSMutableString { NSUnimplemented() }
212+
public var mutableString: NSMutableString {
213+
return _string as! NSMutableString
214+
}
215+
216+
public func addAttribute(_ name: String, value: AnyObject, range: NSRange) {
217+
CFAttributedStringSetAttribute(_cfMutableObject, CFRange(range), name._cfObject, value)
218+
}
166219

167-
public func addAttribute(_ name: String, value: AnyObject, range: NSRange) { NSUnimplemented() }
168220
public func addAttributes(_ attrs: [String : AnyObject], range: NSRange) { NSUnimplemented() }
221+
169222
public func removeAttribute(_ name: String, range: NSRange) { NSUnimplemented() }
170223

171224
public func replaceCharactersInRange(_ range: NSRange, withAttributedString attrString: AttributedString) { NSUnimplemented() }
@@ -176,5 +229,18 @@ public class NSMutableAttributedString : AttributedString {
176229

177230
public func beginEditing() { NSUnimplemented() }
178231
public func endEditing() { NSUnimplemented() }
232+
233+
public override init(string str: String) {
234+
super.init(string: str)
235+
_string = NSMutableString(string: str)
236+
}
237+
238+
public required init?(coder aDecoder: NSCoder) {
239+
NSUnimplemented()
240+
}
241+
179242
}
180243

244+
extension NSMutableAttributedString {
245+
internal var _cfMutableObject: CFMutableAttributedString { return unsafeBitCast(self, to: CFMutableAttributedString.self) }
246+
}

Foundation/NSTextCheckingResult.swift

+17-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,21 @@ internal class _NSRegularExpressionTextCheckingResultResult : TextCheckingResult
7878

7979
extension TextCheckingResult {
8080

81-
82-
83-
public func resultByAdjustingRangesWithOffset(_ offset: Int) -> TextCheckingResult { NSUnimplemented() }
81+
public func resultByAdjustingRangesWithOffset(_ offset: Int) -> TextCheckingResult {
82+
let count = self.numberOfRanges
83+
var newRanges = [NSRange]()
84+
for idx in 0..<count {
85+
let currentRange = self.range(at: idx)
86+
if (currentRange.location == NSNotFound) {
87+
newRanges.append(currentRange)
88+
} else if ((offset > 0 && NSNotFound - currentRange.location <= offset) || (offset < 0 && currentRange.location < -offset)) {
89+
NSInvalidArgument(" \(offset) invalid offset for range {\(currentRange.location), \(currentRange.length)}")
90+
} else {
91+
newRanges.append(NSRange(location: currentRange.location + offset,length: currentRange.length))
92+
}
93+
}
94+
let result = TextCheckingResult.regularExpressionCheckingResultWithRanges(&newRanges, count: count, regularExpression: self.regularExpression!)
95+
return result
96+
}
8497
}
98+

0 commit comments

Comments
 (0)