Skip to content

[String.Index] Obsolete encodedOffset var/init #22108

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
Feb 19, 2019
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
10 changes: 1 addition & 9 deletions stdlib/public/Darwin/Foundation/URLComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,8 @@ public struct URLComponents : ReferenceConvertible, Hashable, Equatable, _Mutabl

@available(macOS 10.11, iOS 9.0, *)
private func _toStringRange(_ r : NSRange) -> Range<String.Index>? {
guard r.location != NSNotFound else { return nil }

let utf16Start = String.UTF16View.Index(encodedOffset: r.location)
let utf16End = String.UTF16View.Index(encodedOffset: r.location + r.length)

guard let s = self.string else { return nil }
guard let start = String.Index(utf16Start, within: s) else { return nil }
guard let end = String.Index(utf16End, within: s) else { return nil }

return start..<end
return Range(r, in: s)
}

/// Returns the character range of the scheme in the string returned by `var string`.
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Darwin/NaturalLanguage/NLTagger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ extension NLTagger {
@nonobjc
public func tokenRange(at index: String.Index, unit: NLTokenUnit) -> Range<String.Index> {
let str = self.string ?? ""
let characterIndex = index.encodedOffset
let characterIndex = index.utf16Offset(in: str)
let nsrange = self.__tokenRange(at: characterIndex, unit: unit)
return Range(nsrange, in: str)!
}

@nonobjc
public func tag(at index: String.Index, unit: NLTokenUnit, scheme: NLTagScheme) -> (NLTag?, Range<String.Index>) {
let str = self.string ?? ""
let characterIndex = index.encodedOffset
let characterIndex = index.utf16Offset(in: str)
let rangePointer = NSRangePointer.allocate(capacity: 1)
rangePointer.initialize(to: NSMakeRange(0, 0))
let tag = self.__tag(at: characterIndex, unit: unit, scheme: scheme, tokenRange: rangePointer)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Darwin/NaturalLanguage/NLTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension NLTokenizer {
@nonobjc
public func tokenRange(at index: String.Index) -> Range<String.Index> {
let str = self.string ?? ""
let characterIndex = index.encodedOffset
let characterIndex = index.utf16Offset(in: str)
let nsrange = self.__tokenRange(at:characterIndex)
return Range(nsrange, in: str)!
}
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,8 @@ internal func _fastWithNormalizedCodeUnitsImpl(
var icuInputBuffer = icuInputBuffer
var icuOutputBuffer = icuOutputBuffer

var index = String.Index(encodedOffset: 0)
let cachedEndIndex = String.Index(encodedOffset: sourceBuffer.count)
var index = String.Index(_encodedOffset: 0)
let cachedEndIndex = String.Index(_encodedOffset: sourceBuffer.count)

var hasBufferOwnership = false

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/StringBreadcrumbs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ extension _StringBreadcrumbs {
internal func getBreadcrumb(
forIndex idx: String.Index
) -> (lowerBound: String.Index, offset: Int) {
var lowerBound = idx.encodedOffset / 3 / stride
var upperBound = Swift.min(1 + (idx.encodedOffset / stride), crumbs.count)
var lowerBound = idx._encodedOffset / 3 / stride
var upperBound = Swift.min(1 + (idx._encodedOffset / stride), crumbs.count)
_internalInvariant(crumbs[lowerBound] <= idx)
_internalInvariant(upperBound == crumbs.count || crumbs[upperBound] >= idx)

Expand Down
12 changes: 6 additions & 6 deletions stdlib/public/core/StringCharacterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ extension String: BidirectionalCollection {

// TODO: known-ASCII fast path, single-scalar-grapheme fast path, etc.
let stride = _characterStride(startingAt: i)
let nextOffset = i.encodedOffset &+ stride
let nextOffset = i._encodedOffset &+ stride
let nextStride = _characterStride(
startingAt: Index(encodedOffset: nextOffset))
startingAt: Index(_encodedOffset: nextOffset))

return Index(
encodedOffset: nextOffset, characterStride: nextStride)
Expand All @@ -84,7 +84,7 @@ extension String: BidirectionalCollection {

// TODO: known-ASCII fast path, single-scalar-grapheme fast path, etc.
let stride = _characterStride(endingAt: i)
let priorOffset = i.encodedOffset &- stride
let priorOffset = i._encodedOffset &- stride
return Index(encodedOffset: priorOffset, characterStride: stride)
}
/// Returns an index that is the specified distance from the given index.
Expand Down Expand Up @@ -198,7 +198,7 @@ extension String: BidirectionalCollection {
let i = _guts.scalarAlign(i)
let distance = _characterStride(startingAt: i)
return _guts.errorCorrectedCharacter(
startingAt: i.encodedOffset, endingAt: i.encodedOffset &+ distance)
startingAt: i._encodedOffset, endingAt: i._encodedOffset &+ distance)
}
}

Expand All @@ -209,14 +209,14 @@ extension String: BidirectionalCollection {

if i == endIndex { return 0 }

return _guts._opaqueCharacterStride(startingAt: i.encodedOffset)
return _guts._opaqueCharacterStride(startingAt: i._encodedOffset)
}

@inlinable @inline(__always)
internal func _characterStride(endingAt i: Index) -> Int {
if i == startIndex { return 0 }

return _guts._opaqueCharacterStride(endingAt: i.encodedOffset)
return _guts._opaqueCharacterStride(endingAt: i._encodedOffset)
}
}

Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/core/StringComparison.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ extension _StringGutsSlice {
if _fastPath(self.isFastUTF8 && other.isFastUTF8) {
return self.withFastUTF8 { leftUTF8 in
other.withFastUTF8 { rightUTF8 in
let leftStartIndex = String.Index(encodedOffset: 0)
let rightStartIndex = String.Index(encodedOffset: 0)
let leftEndIndex = String.Index(encodedOffset: leftUTF8.count)
let rightEndIndex = String.Index(encodedOffset: rightUTF8.count)
let leftStartIndex = String.Index(_encodedOffset: 0)
let rightStartIndex = String.Index(_encodedOffset: 0)
let leftEndIndex = String.Index(_encodedOffset: leftUTF8.count)
let rightEndIndex = String.Index(_encodedOffset: rightUTF8.count)
return _normalizedCompareImpl(
left_outputBuffer: _castOutputBuffer(&left_output),
left_icuInputBuffer: _castOutputBuffer(&left_icuInput),
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/StringGraphemeBreaking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ extension _StringGuts {
internal func isOnGraphemeClusterBoundary(_ i: String.Index) -> Bool {
guard i.transcodedOffset == 0 else { return false }

let offset = i.encodedOffset
let offset = i._encodedOffset
if offset == 0 || offset == self.count { return true }

guard isOnUnicodeScalarBoundary(i) else { return false }
Expand Down Expand Up @@ -197,7 +197,7 @@ extension _StringGuts {
let count = _object.largeCount
let cocoa = _object.cocoaObject

let startIdx = String.Index(encodedOffset: i)
let startIdx = String.Index(_encodedOffset: i)
let (sc1, len) = foreignErrorCorrectedScalar(startingAt: startIdx)
if i &+ len == count {
// Last scalar is last grapheme
Expand Down Expand Up @@ -263,7 +263,7 @@ extension _StringGuts {
let count = _object.largeCount
let cocoa = _object.cocoaObject

let endIdx = String.Index(encodedOffset: i)
let endIdx = String.Index(_encodedOffset: i)
let (sc2, len) = foreignErrorCorrectedScalar(endingAt: endIdx)
if i &- len == 0 {
// First scalar is first grapheme
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/StringGuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,11 @@ extension _StringGuts {

@inlinable
internal var startIndex: String.Index {
@inline(__always) get { return Index(encodedOffset: 0) }
@inline(__always) get { return Index(_encodedOffset: 0) }
}
@inlinable
internal var endIndex: String.Index {
@inline(__always) get { return Index(encodedOffset: self.count) }
@inline(__always) get { return Index(_encodedOffset: self.count) }
}
}

Expand Down
20 changes: 10 additions & 10 deletions stdlib/public/core/StringGutsRangeReplaceable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ extension _StringGuts {
}

internal mutating func remove(from lower: Index, to upper: Index) {
let lowerOffset = lower.encodedOffset
let upperOffset = upper.encodedOffset
let lowerOffset = lower._encodedOffset
let upperOffset = upper._encodedOffset
_internalInvariant(lower.transcodedOffset == 0 && upper.transcodedOffset == 0)
_internalInvariant(lowerOffset <= upperOffset && upperOffset <= self.count)

Expand Down Expand Up @@ -279,16 +279,16 @@ extension _StringGuts {
isASCII: Bool
) {
let neededCapacity =
bounds.lowerBound.encodedOffset
+ codeUnits.count + (self.count - bounds.upperBound.encodedOffset)
bounds.lowerBound._encodedOffset
+ codeUnits.count + (self.count - bounds.upperBound._encodedOffset)
reserveCapacity(neededCapacity)

_internalInvariant(bounds.lowerBound.transcodedOffset == 0)
_internalInvariant(bounds.upperBound.transcodedOffset == 0)

_object.nativeStorage.replace(
from: bounds.lowerBound.encodedOffset,
to: bounds.upperBound.encodedOffset,
from: bounds.lowerBound._encodedOffset,
to: bounds.upperBound._encodedOffset,
with: codeUnits)
self = _StringGuts(_object.nativeStorage)
}
Expand All @@ -300,16 +300,16 @@ extension _StringGuts {
let replCount = codeUnits.count

let neededCapacity =
bounds.lowerBound.encodedOffset
+ replCount + (self.count - bounds.upperBound.encodedOffset)
bounds.lowerBound._encodedOffset
+ replCount + (self.count - bounds.upperBound._encodedOffset)
reserveCapacity(neededCapacity)

_internalInvariant(bounds.lowerBound.transcodedOffset == 0)
_internalInvariant(bounds.upperBound.transcodedOffset == 0)

_object.nativeStorage.replace(
from: bounds.lowerBound.encodedOffset,
to: bounds.upperBound.encodedOffset,
from: bounds.lowerBound._encodedOffset,
to: bounds.upperBound._encodedOffset,
with: codeUnits,
replacementCount: replCount)
self = _StringGuts(_object.nativeStorage)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/StringGutsSlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ internal struct _StringGutsSlice {
@inlinable
internal var range: Range<String.Index> {
@inline(__always) get {
return String.Index(encodedOffset: _offsetRange.lowerBound)
..< String.Index(encodedOffset: _offsetRange.upperBound)
return String.Index(_encodedOffset: _offsetRange.lowerBound)
..< String.Index(_encodedOffset: _offsetRange.upperBound)
}
}

Expand Down
59 changes: 47 additions & 12 deletions stdlib/public/core/StringIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,22 @@ extension String.Index {
@inline(__always) get { return orderingValue == 0 }
}

/// The UTF-16 code unit offset corresponding to this Index
public func utf16Offset<S: StringProtocol>(in s: S) -> Int {
return s.utf16.distance(from: s.utf16.startIndex, to: self)
}

/// The offset into a string's code units for this index.
@available(swift, deprecated: 4.2, message: """
encodedOffset has been deprecated as most common usage is incorrect. \
Use utf16Offset(in:) to achieve the same behavior.
""")
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Should the initializer and property be deprecated: 4.2 or deprecated: 5.0?

  • Should the deprecated APIs be moved to MigrationSupport.swift if possible?

  • A multi-line message (even with escaped line breaks) won't display properly in Xcode 10.1 (and perhaps in Xcode 10.2 beta).

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. I believe that availability matches the language version and not deployment target. This should be deprecated regardless of what is passed in -swift-version. @jrose-apple / @moiseev what do you think?

  2. I think MigrationSupport is done for obsoleted. We should probably obsolete this around 5.1 or so and move it then.

  3. I'll have to try that then.

Copy link
Contributor

Choose a reason for hiding this comment

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

MigrationSupport has some non-obsoleted APIs:

@inlinable
public var encodedOffset: Int {
@inline(__always) get { return Int(truncatingIfNeeded: _rawBits &>> 16) }
public var encodedOffset: Int { return _encodedOffset }

@inlinable @inline(__always)
internal var _encodedOffset: Int {
return Int(truncatingIfNeeded: _rawBits &>> 16)
}

@inlinable
Expand All @@ -91,12 +103,35 @@ extension String.Index {
self.init((pos &<< 16) | (trans &<< 14))
}

/// Creates a new index at the specified UTF-16 code unit offset
///
/// - Parameter offset: An offset in UTF-16 code units.
public init<S: StringProtocol>(utf16Offset offset: Int, in s: S) {
let (start, end) = (s.utf16.startIndex, s.utf16.endIndex)
guard offset >= 0,
let idx = s.utf16.index(start, offsetBy: offset, limitedBy: end)
else {
self = end.nextEncoded
return
}
self = idx
}

/// Creates a new index at the specified code unit offset.
///
/// - Parameter offset: An offset in code units.
@available(swift, deprecated: 4.2, message: """
encodedOffset has been deprecated as most common usage is incorrect. \
Use String.Index(utf16Offset:in:) to achieve the same behavior.
""")
@inlinable
public init(encodedOffset offset: Int) {
self.init(_encodedOffset: offset)
}

@inlinable @inline(__always)
public init(encodedOffset: Int) {
self.init(encodedOffset: encodedOffset, transcodedOffset: 0)
internal init(_encodedOffset offset: Int) {
self.init(encodedOffset: offset, transcodedOffset: 0)
}

@usableFromInline
Expand All @@ -121,7 +156,7 @@ extension String.Index {
#else
@usableFromInline @inline(never) @_effects(releasenone)
internal func _invariantCheck() {
_internalInvariant(encodedOffset >= 0)
_internalInvariant(_encodedOffset >= 0)
}
#endif // INTERNAL_CHECKS_ENABLED
}
Expand All @@ -132,31 +167,31 @@ extension String.Index {
@inlinable
internal var strippingTranscoding: String.Index {
@inline(__always) get {
return String.Index(encodedOffset: self.encodedOffset)
return String.Index(_encodedOffset: self._encodedOffset)
}
}

@inlinable
internal var nextEncoded: String.Index {
@inline(__always) get {
_internalInvariant(self.transcodedOffset == 0)
return String.Index(encodedOffset: self.encodedOffset &+ 1)
return String.Index(_encodedOffset: self._encodedOffset &+ 1)
}
}

@inlinable
internal var priorEncoded: String.Index {
@inline(__always) get {
_internalInvariant(self.transcodedOffset == 0)
return String.Index(encodedOffset: self.encodedOffset &- 1)
return String.Index(_encodedOffset: self._encodedOffset &- 1)
}
}

@inlinable
internal var nextTranscoded: String.Index {
@inline(__always) get {
return String.Index(
encodedOffset: self.encodedOffset,
encodedOffset: self._encodedOffset,
transcodedOffset: self.transcodedOffset &+ 1)
}
}
Expand All @@ -165,7 +200,7 @@ extension String.Index {
internal var priorTranscoded: String.Index {
@inline(__always) get {
return String.Index(
encodedOffset: self.encodedOffset,
encodedOffset: self._encodedOffset,
transcodedOffset: self.transcodedOffset &- 1)
}
}
Expand All @@ -174,13 +209,13 @@ extension String.Index {
// Note: strips any transcoded offset.
@inlinable @inline(__always)
internal func encoded(offsetBy n: Int) -> String.Index {
return String.Index(encodedOffset: self.encodedOffset &+ n)
return String.Index(_encodedOffset: self._encodedOffset &+ n)
}

@inlinable @inline(__always)
internal func transcoded(withOffset n: Int) -> String.Index {
_internalInvariant(self.transcodedOffset == 0)
return String.Index(encodedOffset: self.encodedOffset, transcodedOffset: n)
return String.Index(encodedOffset: self._encodedOffset, transcodedOffset: n)
}

}
Expand Down
Loading