mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[stdlib] conversions to UTF16View.Index
Swift SVN r24013
This commit is contained in:
@@ -941,4 +941,9 @@ extension String.Index {
|
|||||||
) -> String.UTF8View.Index {
|
) -> String.UTF8View.Index {
|
||||||
return String.UTF8View.Index(self, within: otherView)
|
return String.UTF8View.Index(self, within: otherView)
|
||||||
}
|
}
|
||||||
|
public func samePositionIn(
|
||||||
|
otherView: String.UTF16View
|
||||||
|
) -> String.UTF16View.Index {
|
||||||
|
return String.UTF16View.Index(self, within: otherView)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -221,6 +221,35 @@ public func ~> (
|
|||||||
|
|
||||||
// Index conversions
|
// Index conversions
|
||||||
extension String.UTF16View.Index {
|
extension String.UTF16View.Index {
|
||||||
|
public init?(
|
||||||
|
_ sourceIndex: String.UTF8Index, within utf16: String.UTF16View
|
||||||
|
) {
|
||||||
|
let core = utf16._core
|
||||||
|
let sourceView = String.UTF8View(core)
|
||||||
|
|
||||||
|
_precondition(
|
||||||
|
sourceIndex._coreIndex >= 0 && (
|
||||||
|
sourceIndex._coreIndex < core.endIndex
|
||||||
|
|| sourceIndex._coreIndex == core.endIndex
|
||||||
|
&& sourceIndex._isOnUnicodeScalarBoundary
|
||||||
|
), "Invalid String.UTF8Index for this UTF-16 view")
|
||||||
|
|
||||||
|
// Detect positions that have no corresponding index.
|
||||||
|
if !sourceIndex._isOnUnicodeScalarBoundary {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
self.init(sourceIndex._coreIndex)
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(
|
||||||
|
_ sourceIndex: String.UnicodeScalarIndex, within utf16: String.UTF16View) {
|
||||||
|
self.init(sourceIndex._position)
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(_ sourceIndex: String.Index, within utf16: String.UTF16View) {
|
||||||
|
self.init(sourceIndex._utf16Index)
|
||||||
|
}
|
||||||
|
|
||||||
public func samePositionIn(
|
public func samePositionIn(
|
||||||
otherView: String.UTF8View
|
otherView: String.UTF8View
|
||||||
) -> String.UTF8View.Index? {
|
) -> String.UTF8View.Index? {
|
||||||
|
|||||||
@@ -314,3 +314,12 @@ extension String.UTF8Index {
|
|||||||
self.init(utf8._core, _utf16Offset: sourceIndex._base._position)
|
self.init(utf8._core, _utf16Offset: sourceIndex._base._position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Index conversions
|
||||||
|
extension String.UTF8View.Index {
|
||||||
|
public func samePositionIn(
|
||||||
|
otherView: String.UTF16View
|
||||||
|
) -> String.UTF16View.Index? {
|
||||||
|
return String.UTF16View.Index(self, within: otherView)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -343,4 +343,9 @@ extension String.UnicodeScalarView.Index {
|
|||||||
) -> String.UTF8View.Index {
|
) -> String.UTF8View.Index {
|
||||||
return String.UTF8View.Index(self, within: otherView)
|
return String.UTF8View.Index(self, within: otherView)
|
||||||
}
|
}
|
||||||
|
public func samePositionIn(
|
||||||
|
otherView: String.UTF16View
|
||||||
|
) -> String.UTF16View.Index {
|
||||||
|
return String.UTF16View.Index(self, within: otherView)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user