Skip to content

Commit a53f339

Browse files
committed
Implement basic initializers of NSAttributedString
Start implementing the NSAttributedString as is-a CFAttributedString, this commit contains some basic initializer implementations
1 parent 660193d commit a53f339

File tree

5 files changed

+86
-9
lines changed

5 files changed

+86
-9
lines changed

CoreFoundation/Base.subproj/SwiftRuntime/CoreFoundation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787

8888
#include <CoreFoundation/CFURLPriv.h>
8989
#include <CoreFoundation/CFURLComponents.h>
90+
#include <CoreFoundation/CFRunArray.h>
9091

9192
#include <CoreFoundation/ForSwiftFoundationOnly.h>
9293

Foundation.xcodeproj/project.pbxproj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
294E3C1D1CC5E19300E4F44C /* TestNSAttributedString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */; };
1011
2EBE67A51C77BF0E006583D5 /* TestNSDateFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2EBE67A31C77BF05006583D5 /* TestNSDateFormatter.swift */; };
1112
528776141BF2629700CB0090 /* FoundationErrors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 522C253A1BF16E1600804FC6 /* FoundationErrors.swift */; };
1213
528776191BF27D9500CB0090 /* Test.plist in Resources */ = {isa = PBXBuildFile; fileRef = 528776181BF27D9500CB0090 /* Test.plist */; };
@@ -66,9 +67,9 @@
6667
5B5D89761BBDADD300234F36 /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B5D89751BBDADD300234F36 /* libicucore.dylib */; };
6768
5B5D89781BBDADDB00234F36 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B5D89771BBDADDB00234F36 /* libz.dylib */; };
6869
5B6228BB1C179041009587FE /* CFRunArray.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B6228BA1C179041009587FE /* CFRunArray.c */; };
69-
5B6228BD1C179049009587FE /* CFRunArray.h in Headers */ = {isa = PBXBuildFile; fileRef = 5B6228BC1C179049009587FE /* CFRunArray.h */; };
70+
5B6228BD1C179049009587FE /* CFRunArray.h in Headers */ = {isa = PBXBuildFile; fileRef = 5B6228BC1C179049009587FE /* CFRunArray.h */; settings = {ATTRIBUTES = (Private, ); }; };
7071
5B6228BF1C179052009587FE /* CFAttributedString.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B6228BE1C179052009587FE /* CFAttributedString.c */; };
71-
5B6228C11C17905B009587FE /* CFAttributedString.h in Headers */ = {isa = PBXBuildFile; fileRef = 5B6228C01C17905B009587FE /* CFAttributedString.h */; };
72+
5B6228C11C17905B009587FE /* CFAttributedString.h in Headers */ = {isa = PBXBuildFile; fileRef = 5B6228C01C17905B009587FE /* CFAttributedString.h */; settings = {ATTRIBUTES = (Public, ); }; };
7273
5B7C8A721BEA7FCE00C5B690 /* CFBase.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D895D1BBDABBF00234F36 /* CFBase.c */; };
7374
5B7C8A731BEA7FCE00C5B690 /* CFFileUtilities.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D89851BBDB18D00234F36 /* CFFileUtilities.c */; };
7475
5B7C8A741BEA7FCE00C5B690 /* CFPlatform.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D897B1BBDAE0800234F36 /* CFPlatform.c */; };
@@ -253,9 +254,9 @@
253254
61E0117F1C1B5990000037DD /* CFRunLoop.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88D81BBC9AD800234F36 /* CFRunLoop.c */; };
254255
61E011811C1B5998000037DD /* CFMessagePort.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88DC1BBC9AEC00234F36 /* CFMessagePort.c */; };
255256
61E011821C1B599A000037DD /* CFMachPort.c in Sources */ = {isa = PBXBuildFile; fileRef = 5B5D88D01BBC9AAC00234F36 /* CFMachPort.c */; };
256-
AE35A1861CBAC85E0042DB84 /* SwiftFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = AE35A1851CBAC85E0042DB84 /* SwiftFoundation.h */; settings = {ATTRIBUTES = (Public, ); }; };
257257
7900433B1CACD33E00ECCBF1 /* TestNSCompoundPredicate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 790043391CACD33E00ECCBF1 /* TestNSCompoundPredicate.swift */; };
258258
7900433C1CACD33E00ECCBF1 /* TestNSPredicate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7900433A1CACD33E00ECCBF1 /* TestNSPredicate.swift */; };
259+
AE35A1861CBAC85E0042DB84 /* SwiftFoundation.h in Headers */ = {isa = PBXBuildFile; fileRef = AE35A1851CBAC85E0042DB84 /* SwiftFoundation.h */; settings = {ATTRIBUTES = (Public, ); }; };
259260
CE19A88C1C23AA2300B4CB6A /* NSStringTestData.txt in Resources */ = {isa = PBXBuildFile; fileRef = CE19A88B1C23AA2300B4CB6A /* NSStringTestData.txt */; };
260261
D31302011C30CEA900295652 /* NSConcreteValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D31302001C30CEA900295652 /* NSConcreteValue.swift */; };
261262
D370696E1C394FBF00295652 /* NSKeyedUnarchiver-RangeTest.plist in Resources */ = {isa = PBXBuildFile; fileRef = D370696D1C394FBF00295652 /* NSKeyedUnarchiver-RangeTest.plist */; };
@@ -385,6 +386,7 @@
385386

386387
/* Begin PBXFileReference section */
387388
22B9C1E01C165D7A00DECFF9 /* TestNSDate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSDate.swift; sourceTree = "<group>"; };
389+
294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSAttributedString.swift; sourceTree = "<group>"; };
388390
2EBE67A31C77BF05006583D5 /* TestNSDateFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSDateFormatter.swift; sourceTree = "<group>"; };
389391
400E22641C1A4E58007C5933 /* TestNSProcessInfo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSProcessInfo.swift; sourceTree = "<group>"; };
390392
4AE109261C17CCBF007367B5 /* TestNSIndexPath.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSIndexPath.swift; sourceTree = "<group>"; };
@@ -1210,6 +1212,7 @@
12101212
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */,
12111213
5B6F17961C48631C00935030 /* TestUtils.swift */,
12121214
E19E17DB1C2225930023AF4D /* TestNSXMLDocument.swift */,
1215+
294E3C1C1CC5E19300E4F44C /* TestNSAttributedString.swift */,
12131216
);
12141217
name = Tests;
12151218
sourceTree = "<group>";
@@ -1517,7 +1520,6 @@
15171520
5B7C8ACC1BEA80FC00C5B690 /* CFDateFormatter.h in Headers */,
15181521
5B7C8AD41BEA80FC00C5B690 /* CFXMLParser.h in Headers */,
15191522
5B7C8AD11BEA80FC00C5B690 /* CFTimeZone.h in Headers */,
1520-
5B6228C11C17905B009587FE /* CFAttributedString.h in Headers */,
15211523
5B7C8AC31BEA80FC00C5B690 /* CFBag.h in Headers */,
15221524
5B7C8AE01BEA80FC00C5B690 /* CFStringEncodingExt.h in Headers */,
15231525
5B7C8AE11BEA80FC00C5B690 /* CFURL.h in Headers */,
@@ -1552,12 +1554,12 @@
15521554
5B7C8AD21BEA80FC00C5B690 /* CFPropertyList.h in Headers */,
15531555
5B7C8AE51BEA81AC00C5B690 /* ForFoundationOnly.h in Headers */,
15541556
5B7C8AD51BEA80FC00C5B690 /* CFBundle.h in Headers */,
1557+
5B6228C11C17905B009587FE /* CFAttributedString.h in Headers */,
15551558
5B7C8AE21BEA80FC00C5B690 /* CFURLAccess.h in Headers */,
15561559
5B7C8ACA1BEA80FC00C5B690 /* CFError.h in Headers */,
15571560
5B7C8AE91BEA81AC00C5B690 /* CFLocaleInternal.h in Headers */,
15581561
5B7C8AE61BEA81AC00C5B690 /* ForSwiftFoundationOnly.h in Headers */,
15591562
5B7C8AC61BEA80FC00C5B690 /* CFData.h in Headers */,
1560-
5B6228BD1C179049009587FE /* CFRunArray.h in Headers */,
15611563
5B7C8AC01BEA807A00C5B690 /* CFUUID.h in Headers */,
15621564
5B7C8AD31BEA80FC00C5B690 /* CFXMLNode.h in Headers */,
15631565
5B7C8AC21BEA80FC00C5B690 /* CFArray.h in Headers */,
@@ -1587,6 +1589,7 @@
15871589
5B7C8AF71BEA81AC00C5B690 /* CFStringEncodingConverterExt.h in Headers */,
15881590
5B7C8AE31BEA81AC00C5B690 /* CFLogUtilities.h in Headers */,
15891591
5B7C8AD01BEA80FC00C5B690 /* CFNumber.h in Headers */,
1592+
5B6228BD1C179049009587FE /* CFRunArray.h in Headers */,
15901593
5B7C8ACE1BEA80FC00C5B690 /* CFNumberFormatter.h in Headers */,
15911594
);
15921595
runOnlyForDeploymentPostprocessing = 0;
@@ -1981,6 +1984,7 @@
19811984
5B13B3321C582D4C00651CE2 /* TestNSIndexSet.swift in Sources */,
19821985
5B13B3511C582D4C00651CE2 /* TestNSByteCountFormatter.swift in Sources */,
19831986
5B13B3501C582D4C00651CE2 /* TestUtils.swift in Sources */,
1987+
294E3C1D1CC5E19300E4F44C /* TestNSAttributedString.swift in Sources */,
19841988
5B13B3431C582D4C00651CE2 /* TestNSScanner.swift in Sources */,
19851989
5B13B3401C582D4C00651CE2 /* TestNSRange.swift in Sources */,
19861990
5B13B3371C582D4C00651CE2 /* TestNSNotificationCenter.swift in Sources */,

Foundation/NSAttributedString.swift

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
import CoreFoundation
1011

1112
public class NSAttributedString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
1213

14+
private let _cfinfo = _CFInfo(typeID: CFAttributedStringGetTypeID())
15+
private let _string: String
16+
private let _attributeArray: CFRunArrayRef
17+
1318
public required init?(coder aDecoder: NSCoder) {
1419
NSUnimplemented()
1520
}
@@ -38,10 +43,16 @@ public class NSAttributedString : NSObject, NSCopying, NSMutableCopying, NSSecur
3843
NSUnimplemented()
3944
}
4045

41-
public var string: String { NSUnimplemented() }
46+
public var string: String {
47+
return _string
48+
}
49+
4250
public func attributesAtIndex(_ location: Int, effectiveRange range: NSRangePointer) -> [String : AnyObject] { NSUnimplemented() }
4351

44-
public var length: Int { NSUnimplemented() }
52+
public var length: Int {
53+
return _string.length
54+
}
55+
4556
public func attribute(_ attrName: String, atIndex location: Int, effectiveRange range: NSRangePointer) -> AnyObject? { NSUnimplemented() }
4657
public func attributedSubstringFromRange(_ range: NSRange) -> NSAttributedString { NSUnimplemented() }
4758

@@ -50,8 +61,21 @@ public class NSAttributedString : NSObject, NSCopying, NSMutableCopying, NSSecur
5061

5162
public func isEqualToAttributedString(_ other: NSAttributedString) -> Bool { NSUnimplemented() }
5263

53-
public init(string str: String) { NSUnimplemented() }
54-
public init(string str: String, attributes attrs: [String : AnyObject]?) { NSUnimplemented() }
64+
public init(string str: String) {
65+
_string = str
66+
_attributeArray = CFRunArrayCreate(kCFAllocatorDefault)
67+
}
68+
69+
public init(string str: String, attributes attrs: [String : AnyObject]?) {
70+
_string = str
71+
_attributeArray = CFRunArrayCreate(kCFAllocatorDefault)
72+
73+
let length = _string.length
74+
if (length > 0) {
75+
CFRunArrayInsert(_attributeArray, CFRange(location: 0, length: length), attrs?._cfObject)
76+
}
77+
}
78+
5579
public init(attributedString attrStr: NSAttributedString) { NSUnimplemented() }
5680

5781
public func enumerateAttributesInRange(_ enumerationRange: NSRange, options opts: NSAttributedStringEnumerationOptions, usingBlock block: ([String : AnyObject], NSRange, UnsafeMutablePointer<ObjCBool>) -> Void) { NSUnimplemented() }
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
12+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
13+
import Foundation
14+
import XCTest
15+
#else
16+
import SwiftFoundation
17+
import SwiftXCTest
18+
#endif
19+
20+
21+
22+
class TestNSAttributedString : XCTestCase {
23+
24+
static var allTests: [(String, TestNSAttributedString -> () throws -> Void)] {
25+
return [
26+
("test_initWithString", test_initWithString),
27+
("test_initWithStringAndAttributes", test_initWithStringAndAttributes)
28+
]
29+
}
30+
31+
func test_initWithString() {
32+
let string = "Lorem 😀 ipsum dolor sit amet, consectetur adipiscing elit. ⌘ Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. ಠ_ರೃ"
33+
let attrString = NSAttributedString(string: string)
34+
XCTAssertEqual(attrString.string, string)
35+
XCTAssertEqual(attrString.length, string.utf16Count)
36+
}
37+
38+
func test_initWithStringAndAttributes() {
39+
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit."
40+
let attributes: [String : AnyObject] = ["attribute.placeholder.key" : "attribute.placeholder.value" as NSString]
41+
42+
let attrString = NSAttributedString(string: string, attributes: attributes)
43+
XCTAssertEqual(attrString.string, string)
44+
XCTAssertEqual(attrString.length, string.utf16Count)
45+
}
46+
47+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ XCTMain([
7272
testCase(TestNSUserDefaults.allTests),
7373
testCase(TestNSXMLParser.allTests),
7474
testCase(TestNSXMLDocument.allTests),
75+
testCase(TestNSAttributedString.allTests),
7576
])

0 commit comments

Comments
 (0)