Skip to content

ExtraStringAPI: Sync with the overlay version #1406

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
Jan 25, 2018
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
27 changes: 17 additions & 10 deletions Foundation/ExtraStringAPIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2017 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
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// Random access for String.UTF16View, only when Foundation is
// imported. Making this API dependent on Foundation decouples the
// Swift core from a UTF16 representation.
extension String.UTF16View.Index {
public func advanced(by n: Int) -> String.UTF16View.Index {
return String.UTF16View.Index(encodedOffset: encodedOffset.advanced(by: n))
}
}
/// Construct from an integer offset.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way we can change this from an obsoleted to a renamed with instead, so that callers have the opportunity of a fixit to change? Alternatively, instead of just using "Unavailable" can we have a more descriptive message as to what they need to replace this with instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For advanced(by:) would this suffice (I don't think a rename can be used in its place. ) ?
@available(swift, obsoleted: 4.0, message: "Use index(_,offsetBy:) instead.")

Im not sure what the alternatives are for the other 2 although I could remove them entirely since adding obsoleted functions does not really make sense anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to be moving towards keeping them in for a release or two because then the compile error is clearer than 'can't find it'.

@available(swift, obsoleted: 4.0)
public init(_ offset: Int) {
fatalError("Unavailable")
}

@available(swift, obsoleted: 4.0)
public func distance(to other: String.UTF16View.Index?) -> Int {
fatalError("Unavailable")
}

@available(swift, obsoleted: 4.0)
public func advanced(by n: Int) -> String.UTF16View.Index {
fatalError("Unavailable")
}
}
2 changes: 1 addition & 1 deletion Foundation/NSCFString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ internal func _CFSwiftStringGetBytes(_ str: AnyObject, encoding: CFStringEncodin
if let buffer = buffer {
for idx in 0..<range.length {
// Since character is 2 bytes but the buffer is in term of 1 byte values, we have to split it up
let character = encodingView[start.advanced(by: idx + range.location)]
let character = encodingView[encodingView.index(start, offsetBy: idx + range.location)]
#if _endian(big)
let byte0 = UInt8((character >> 8) & 0x00ff)
let byte1 = UInt8(character & 0x00ff)
Expand Down
12 changes: 6 additions & 6 deletions Foundation/NSRegularExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,17 @@ extension NSRegularExpression {
let currentRange = result.range
let replacement = replacementString(for: result, in: string, offset: 0, template: templ)
if currentRange.location > NSMaxRange(previousRange) {
let min = start.advanced(by: NSMaxRange(previousRange))
let max = start.advanced(by: currentRange.location)
let min = string.utf16.index(start, offsetBy: NSMaxRange(previousRange))
let max = string.utf16.index(start, offsetBy: currentRange.location)
str += String(string.utf16[min..<max])!
}
str += replacement
previousRange = currentRange
}

if length > NSMaxRange(previousRange) {
let min = start.advanced(by: NSMaxRange(previousRange))
let max = start.advanced(by: length)
let min = string.utf16.index(start, offsetBy: NSMaxRange(previousRange))
let max = string.utf16.index(start, offsetBy: length)
str += String(string.utf16[min..<max])!
}

Expand Down Expand Up @@ -344,8 +344,8 @@ extension NSRegularExpression {
}
if substringRange.location != NSNotFound && substringRange.length > 0 {
let start = string.utf16.startIndex
let min = start.advanced(by: substringRange.location)
let max = start.advanced(by: substringRange.location + substringRange.length)
let min = string.utf16.index(start, offsetBy: substringRange.location)
let max = string.utf16.index(start, offsetBy: substringRange.location + substringRange.length)
substring = String(string.utf16[min..<max])!
}
str.replaceCharacters(in: rangeToReplace, with: substring)
Expand Down
11 changes: 5 additions & 6 deletions Foundation/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ open class NSString : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSC
NSRequiresConcreteImplementation()
}
let start = _storage.utf16.startIndex
return _storage.utf16[start.advanced(by: index)]
return _storage.utf16[_storage.utf16.index(start, offsetBy: index)]
}

public override convenience init() {
Expand Down Expand Up @@ -347,16 +347,15 @@ extension NSString {

public func substring(from: Int) -> String {
if type(of: self) == NSString.self || type(of: self) == NSMutableString.self {
return String(_storage.utf16.suffix(from: _storage.utf16.startIndex.advanced(by: from)))!
return String(_storage.utf16.suffix(from: _storage.utf16.index(_storage.utf16.startIndex, offsetBy: from)))!
} else {
return substring(with: NSRange(location: from, length: length - from))
}
}

public func substring(to: Int) -> String {
if type(of: self) == NSString.self || type(of: self) == NSMutableString.self {
return String(_storage.utf16.prefix(upTo: _storage.utf16.startIndex
.advanced(by: to)))!
return String(_storage.utf16.prefix(upTo: _storage.utf16.index(_storage.utf16.startIndex, offsetBy: to)))!
} else {
return substring(with: NSRange(location: 0, length: to))
}
Expand All @@ -365,8 +364,8 @@ extension NSString {
public func substring(with range: NSRange) -> String {
if type(of: self) == NSString.self || type(of: self) == NSMutableString.self {
let start = _storage.utf16.startIndex
let min = start.advanced(by: range.location)
let max = start.advanced(by: range.location + range.length)
let min = _storage.utf16.index(start, offsetBy: range.location)
let max = _storage.utf16.index(start, offsetBy: range.location + range.length)
return String(decoding: _storage.utf16[min..<max], as: UTF16.self)
} else {
let buff = UnsafeMutablePointer<unichar>.allocate(capacity: range.length)
Expand Down
2 changes: 1 addition & 1 deletion TestFoundation/TestNSAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ class TestNSMutableAttributedString : XCTestCase {
let deleteRange = NSRange(location: 0, length: 10)
mutableAttrString.deleteCharacters(in: deleteRange)

let expectedString = String(string[string.startIndex.advanced(by: 10)...])
let expectedString = String(string[string.index(string.startIndex, offsetBy: 10)...])
XCTAssertEqual(mutableAttrString.string, expectedString)

let expectedLongestEffectiveRange = NSRange(expectedString.startIndex..., in: expectedString)
Expand Down