Skip to content

Apply fix for SR-2988 from overlay. #707

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 1 commit into from
Nov 3, 2016
Merged
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
13 changes: 7 additions & 6 deletions Foundation/CharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
private func _utfRangeToNSRange(_ inRange : Range<UnicodeScalar>) -> NSRange {
return NSMakeRange(Int(inRange.lowerBound.value), Int(inRange.upperBound.value - inRange.lowerBound.value))
}

private func _utfRangeToNSRange(_ inRange : ClosedRange<UnicodeScalar>) -> NSRange {
return NSMakeRange(Int(inRange.lowerBound.value), Int(inRange.upperBound.value - inRange.lowerBound.value + 1))
}

internal final class _SwiftNSCharacterSet : NSCharacterSet, _SwiftNativeFoundationType {
internal typealias ImmutableType = NSCharacterSet
Expand Down Expand Up @@ -129,8 +133,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
///
/// It is the caller's responsibility to ensure that the values represent valid `UnicodeScalar` values, if that is what is desired.
public init(charactersIn range: ClosedRange<UnicodeScalar>) {
let halfOpenRange = range.lowerBound..<UnicodeScalar(range.upperBound.value + 1)!
_wrapped = _SwiftNSCharacterSet(immutableObject: NSCharacterSet(range: _utfRangeToNSRange(halfOpenRange)))
_wrapped = _SwiftNSCharacterSet(immutableObject: NSCharacterSet(range: _utfRangeToNSRange(range)))
}

/// Initialize with the characters in the given string.
Expand Down Expand Up @@ -320,8 +323,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
///
/// It is the caller's responsibility to ensure that the values represent valid `UnicodeScalar` values, if that is what is desired.
public mutating func insert(charactersIn range: ClosedRange<UnicodeScalar>) {
let halfOpenRange = range.lowerBound..<UnicodeScalar(range.upperBound.value + 1)!
let nsRange = _utfRangeToNSRange(halfOpenRange)
let nsRange = _utfRangeToNSRange(range)
_applyUnmanagedMutation {
$0.addCharacters(in: nsRange)
}
Expand All @@ -337,8 +339,7 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb

/// Remove a closed range of integer values from the `CharacterSet`.
public mutating func remove(charactersIn range: ClosedRange<UnicodeScalar>) {
let halfOpenRange = range.lowerBound..<UnicodeScalar(range.upperBound.value + 1)!
let nsRange = _utfRangeToNSRange(halfOpenRange)
let nsRange = _utfRangeToNSRange(range)
_applyUnmanagedMutation {
$0.removeCharacters(in: nsRange)
}
Expand Down
14 changes: 13 additions & 1 deletion TestFoundation/TestNSCharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TestNSCharacterSet : XCTestCase {
("testRanges", testRanges),
("testInsertAndRemove", testInsertAndRemove),
("testBasics", testBasics),

("testClosedRanges_SR_2988", testClosedRanges_SR_2988),
("test_Predefines", test_Predefines),
("test_Range", test_Range),
("test_String", test_String),
Expand Down Expand Up @@ -223,6 +223,18 @@ class TestNSCharacterSet : XCTestCase {
}
}

func testClosedRanges_SR_2988() {
// "CharacterSet.insert(charactersIn: ClosedRange) crashes on a closed ClosedRange<UnicodeScalar> containing U+D7FF"
let problematicChar = UnicodeScalar(0xD7FF)!
let range = capitalA...problematicChar
var characters = CharacterSet(charactersIn: range) // this should not crash
XCTAssertTrue(characters.contains(problematicChar))
characters.remove(charactersIn: range) // this should not crash
XCTAssertTrue(!characters.contains(problematicChar))
characters.insert(charactersIn: range) // this should not crash
XCTAssertTrue(characters.contains(problematicChar))
}

func test_Bitmap() {

}
Expand Down