[stdlib] conversions to UTF16View.Index

Swift SVN r24013
This commit is contained in:
Dave Abrahams
2014-12-18 20:47:38 +00:00
parent 386146364b
commit 43ea3db04e
4 changed files with 48 additions and 0 deletions

View File

@@ -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)
}
} }

View File

@@ -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? {

View File

@@ -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)
}
}

View File

@@ -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)
}
} }