-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Basic implementations of NSAttributedString #334
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a53f339
Implement basic initializers of NSAttributedString
eyeplum 0d21ba9
Implement attributesAtIndex(:effectiveRange:)
eyeplum 864d2bb
Handle effective range when no attributes found at location
eyeplum 3659545
Use CoreFoundation methods when possible
eyeplum 7e0ef38
Tweak comment
eyeplum 4cd5eb7
Implement attribute(atIndex:effectiveRange:)
eyeplum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See http://swift.org/LICENSE.txt for license information | ||
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
|
||
|
||
|
||
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux) | ||
import Foundation | ||
import XCTest | ||
#else | ||
import SwiftFoundation | ||
import SwiftXCTest | ||
#endif | ||
|
||
|
||
|
||
class TestNSAttributedString : XCTestCase { | ||
|
||
static var allTests: [(String, TestNSAttributedString -> () throws -> Void)] { | ||
return [ | ||
("test_initWithString", test_initWithString), | ||
("test_initWithStringAndAttributes", test_initWithStringAndAttributes) | ||
] | ||
} | ||
|
||
func test_initWithString() { | ||
let string = "Lorem 😀 ipsum dolor sit amet, consectetur adipiscing elit. ⌘ Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit. ಠ_ರೃ" | ||
let attrString = NSAttributedString(string: string) | ||
XCTAssertEqual(attrString.string, string) | ||
XCTAssertEqual(attrString.length, string.utf16Count) | ||
|
||
var range = NSRange() | ||
let attrs = attrString.attributesAtIndex(0, effectiveRange: &range) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that this will cause a compile error with the current swift toolchain:
But the darwin counterpart can actually accept |
||
XCTAssertEqual(range.location, NSNotFound) | ||
XCTAssertEqual(range.length, 0) | ||
XCTAssertEqual(attrs.count, 0) | ||
|
||
let attribute = attrString.attribute("invalid", atIndex: 0, effectiveRange: &range) | ||
XCTAssertNil(attribute) | ||
XCTAssertEqual(range.location, NSNotFound) | ||
XCTAssertEqual(range.length, 0) | ||
} | ||
|
||
func test_initWithStringAndAttributes() { | ||
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur et sem vitae consectetur. Nam venenatis lectus a laoreet blandit." | ||
let attributes: [String : AnyObject] = ["attribute.placeholder.key" : "attribute.placeholder.value" as NSString] | ||
|
||
let attrString = NSAttributedString(string: string, attributes: attributes) | ||
XCTAssertEqual(attrString.string, string) | ||
XCTAssertEqual(attrString.length, string.utf16Count) | ||
|
||
var range = NSRange() | ||
let attrs = attrString.attributesAtIndex(0, effectiveRange: &range) | ||
guard let value = attrs["attribute.placeholder.key"] as? NSString else { | ||
XCTAssert(false, "attribute value not found") | ||
return | ||
} | ||
XCTAssertEqual(range.location, 0) | ||
XCTAssertEqual(range.length, attrString.length) | ||
XCTAssertEqual(value, "attribute.placeholder.value") | ||
|
||
let invalidAttribute = attrString.attribute("invalid", atIndex: 0, effectiveRange: &range) | ||
XCTAssertNil(invalidAttribute) | ||
XCTAssertEqual(range.location, NSNotFound) | ||
XCTAssertEqual(range.length, 0) | ||
|
||
let attribute = attrString.attribute("attribute.placeholder.key", atIndex: 0, effectiveRange: &range) | ||
XCTAssertEqual(range.location, 0) | ||
XCTAssertEqual(range.length, attrString.length) | ||
guard let validAttribute = attribute as? NSString else { | ||
XCTAssert(false, "attribuet not found") | ||
return | ||
} | ||
XCTAssertEqual(validAttribute, "attribute.placeholder.value") | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to use
private let _string: String
here, but encountered EXC_BAD_ACCESS at CFString function calls likeCFStringGetLength
( inside theCF_SWIFT_FUNCDISPATCHV
macro ).I couldn't figure out why, but found out that with a
NSString
, everything seemed to be fine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think https://bugs.swift.org/browse/SR-1129 could be related?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SR-1129 looks like a compile time error, what I encountered is a runtime error.
So, maybe they are not related.
My guess is that this is a memory related issue.