//===--- StringUTF16.swift ------------------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// // FIXME(ABI)#71 : The UTF-16 string view should have a custom iterator type to // allow performance optimizations of linear traversals. extension String { /// A view of a string's contents as a collection of UTF-16 code units. /// /// You can access a string's view of UTF-16 code units by using its `utf16` /// property. A string's UTF-16 view encodes the string's Unicode scalar /// values as 16-bit integers. /// /// let flowers = "Flowers 💐" /// for v in flowers.utf16 { /// print(v) /// } /// // 70 /// // 108 /// // 111 /// // 119 /// // 101 /// // 114 /// // 115 /// // 32 /// // 55357 /// // 56464 /// /// Unicode scalar values that make up a string's contents can be up to 21 /// bits long. The longer scalar values may need two `UInt16` values for /// storage. Those "pairs" of code units are called *surrogate pairs*. /// /// let flowermoji = "💐" /// for v in flowermoji.unicodeScalars { /// print(v, v.value) /// } /// // 💐 128144 /// /// for v in flowermoji.utf16 { /// print(v) /// } /// // 55357 /// // 56464 /// /// To convert a `String.UTF16View` instance back into a string, use the /// `String` type's `init(_:)` initializer. /// /// let favemoji = "My favorite emoji is 🎉" /// if let i = favemoji.utf16.index(where: { $0 >= 128 }) { /// let asciiPrefix = String(favemoji.utf16[.. Index { // FIXME: swift-3-indexing-model: range check i? return Index(encodedOffset: _unsafePlus(i.encodedOffset, 1)) } // TODO: swift-3-indexing-model - add docs @_inlineable // FIXME(sil-serialize-all) public func index(before i: Index) -> Index { // FIXME: swift-3-indexing-model: range check i? return Index(encodedOffset: _unsafeMinus(i.encodedOffset, 1)) } // TODO: swift-3-indexing-model - add docs @_inlineable // FIXME(sil-serialize-all) public func index(_ i: Index, offsetBy n: Int) -> Index { // FIXME: swift-3-indexing-model: range check i? return Index(encodedOffset: i.encodedOffset.advanced(by: n)) } // TODO: swift-3-indexing-model - add docs @_inlineable // FIXME(sil-serialize-all) public func index( _ i: Index, offsetBy n: Int, limitedBy limit: Index ) -> Index? { // FIXME: swift-3-indexing-model: range check i? let d = i.encodedOffset.distance(to: limit.encodedOffset) if (d >= 0) ? (d < n) : (d > n) { return nil } return Index(encodedOffset: i.encodedOffset.advanced(by: n)) } // TODO: swift-3-indexing-model - add docs @_inlineable // FIXME(sil-serialize-all) public func distance(from start: Index, to end: Index) -> Int { // FIXME: swift-3-indexing-model: range check start and end? return start.encodedOffset.distance(to: end.encodedOffset) } @_inlineable // FIXME(sil-serialize-all) @_versioned // FIXME(sil-serialize-all) internal func _internalIndex(at i: Int) -> Int { return _core.startIndex + i } /// Accesses the code unit at the given position. /// /// The following example uses the subscript to print the value of a /// string's first UTF-16 code unit. /// /// let greeting = "Hello, friend!" /// let i = greeting.utf16.startIndex /// print("First character's UTF-16 code unit: \(greeting.utf16[i])") /// // Prints "First character's UTF-16 code unit: 72" /// /// - Parameter position: A valid index of the view. `position` must be /// less than the view's end index. @_inlineable // FIXME(sil-serialize-all) public subscript(i: Index) -> UTF16.CodeUnit { _precondition(i >= startIndex && i < endIndex, "out-of-range access on a UTF16View") let index = _internalIndex(at: i.encodedOffset) let u = _core[index] if _fastPath((u &>> 11) != 0b1101_1) { // Neither high-surrogate, nor low-surrogate -- well-formed sequence // of 1 code unit. return u } if (u &>> 10) == 0b1101_10 { // `u` is a high-surrogate. Sequence is well-formed if it // is followed by a low-surrogate. if _fastPath( index + 1 < _core.count && (_core[index + 1] &>> 10) == 0b1101_11) { return u } return 0xfffd } // `u` is a low-surrogate. Sequence is well-formed if // previous code unit is a high-surrogate. if _fastPath(index != 0 && (_core[index - 1] &>> 10) == 0b1101_10) { return u } return 0xfffd } #if _runtime(_ObjC) // These may become less important once is addressed. @available( *, unavailable, message: "Indexing a String's UTF16View requires a String.UTF16View.Index, which can be constructed from Int when Foundation is imported") public subscript(i: Int) -> UTF16.CodeUnit { Builtin.unreachable() } @available( *, unavailable, message: "Slicing a String's UTF16View requires a Range, String.UTF16View.Index can be constructed from Int when Foundation is imported") public subscript(bounds: Range) -> UTF16View { Builtin.unreachable() } #endif @_inlineable // FIXME(sil-serialize-all) @_versioned // FIXME(sil-serialize-all) internal init(_ _core: _StringCore) { self.init(_core, offset: 0, length: _core.count) } @_inlineable // FIXME(sil-serialize-all) @_versioned // FIXME(sil-serialize-all) internal init(_ _core: _StringCore, offset: Int, length: Int) { self._offset = offset self._length = length self._core = _core } @_inlineable // FIXME(sil-serialize-all) public var description: String { let start = _internalIndex(at: _offset) let end = _internalIndex(at: _offset + _length) return String(_core[start.. String.UnicodeScalarIndex? { return String.UnicodeScalarIndex(self, within: unicodeScalars) } } // Reflection extension String.UTF16View : CustomReflectable { /// Returns a mirror that reflects the UTF-16 view of a string. @_inlineable // FIXME(sil-serialize-all) public var customMirror: Mirror { return Mirror(self, unlabeledChildren: self) } } extension String.UTF16View : CustomPlaygroundQuickLookable { @_inlineable // FIXME(sil-serialize-all) public var customPlaygroundQuickLook: PlaygroundQuickLook { return .text(description) } } extension String.UTF16View.Indices : BidirectionalCollection { public typealias Index = String.UTF16View.Index public typealias Indices = String.UTF16View.Indices public typealias SubSequence = String.UTF16View.Indices @_inlineable // FIXME(sil-serialize-all) @_versioned // FIXME(sil-serialize-all) internal init( _elements: String.UTF16View, startIndex: Index, endIndex: Index ) { self._elements = _elements self._startIndex = startIndex self._endIndex = endIndex } @_inlineable // FIXME(sil-serialize-all) public var startIndex: Index { return _startIndex } @_inlineable // FIXME(sil-serialize-all) public var endIndex: Index { return _endIndex } @_inlineable // FIXME(sil-serialize-all) public var indices: Indices { return self } @_inlineable // FIXME(sil-serialize-all) public subscript(i: Index) -> Index { // FIXME: swift-3-indexing-model: range check. return i } @_inlineable // FIXME(sil-serialize-all) public subscript(bounds: Range) -> String.UTF16View.Indices { // FIXME: swift-3-indexing-model: range check. return String.UTF16View.Indices( _elements: _elements, startIndex: bounds.lowerBound, endIndex: bounds.upperBound) } @_inlineable // FIXME(sil-serialize-all) public func index(after i: Index) -> Index { // FIXME: swift-3-indexing-model: range check. return _elements.index(after: i) } @_inlineable // FIXME(sil-serialize-all) public func formIndex(after i: inout Index) { // FIXME: swift-3-indexing-model: range check. _elements.formIndex(after: &i) } @_inlineable // FIXME(sil-serialize-all) public func index(before i: Index) -> Index { // FIXME: swift-3-indexing-model: range check. return _elements.index(before: i) } @_inlineable // FIXME(sil-serialize-all) public func formIndex(before i: inout Index) { // FIXME: swift-3-indexing-model: range check. _elements.formIndex(before: &i) } @_inlineable // FIXME(sil-serialize-all) public func index(_ i: Index, offsetBy n: Int) -> Index { // FIXME: swift-3-indexing-model: range check i? return _elements.index(i, offsetBy: n) } @_inlineable // FIXME(sil-serialize-all) public func index( _ i: Index, offsetBy n: Int, limitedBy limit: Index ) -> Index? { // FIXME: swift-3-indexing-model: range check i? return _elements.index(i, offsetBy: n, limitedBy: limit) } // TODO: swift-3-indexing-model - add docs @_inlineable // FIXME(sil-serialize-all) public func distance(from start: Index, to end: Index) -> Int { // FIXME: swift-3-indexing-model: range check start and end? return _elements.distance(from: start, to: end) } } // backward compatibility for index interchange. extension String.UTF16View { @_inlineable // FIXME(sil-serialize-all) @available( swift, obsoleted: 4.0, message: "Any String view index conversion can fail in Swift 4; please unwrap the optional index") public func index(after i: Index?) -> Index { return index(after: i!) } @_inlineable // FIXME(sil-serialize-all) @available( swift, obsoleted: 4.0, message: "Any String view index conversion can fail in Swift 4; please unwrap the optional index") public func index( _ i: Index?, offsetBy n: Int) -> Index { return index(i!, offsetBy: n) } @_inlineable // FIXME(sil-serialize-all) @available( swift, obsoleted: 4.0, message: "Any String view index conversion can fail in Swift 4; please unwrap the optional indices") public func distance(from i: Index?, to j: Index?) -> Int { return distance(from: i!, to: j!) } @_inlineable // FIXME(sil-serialize-all) @available( swift, obsoleted: 4.0, message: "Any String view index conversion can fail in Swift 4; please unwrap the optional index") public subscript(i: Index?) -> Unicode.UTF16.CodeUnit { return self[i!] } } //===--- Slicing Support --------------------------------------------------===// /// In Swift 3.2, in the absence of type context, /// /// someString.utf16[someString.utf16.startIndex..) -> String.UTF16View.SubSequence { return String.UTF16View.SubSequence(self, _bounds: r) } @_inlineable // FIXME(sil-serialize-all) @available(swift, obsoleted: 4) public subscript(bounds: Range) -> String.UTF16View { return String.UTF16View( _core, offset: _internalIndex(at: bounds.lowerBound.encodedOffset), length: bounds.upperBound.encodedOffset - bounds.lowerBound.encodedOffset) } @_inlineable // FIXME(sil-serialize-all) @available(swift, obsoleted: 4) public subscript(bounds: ClosedRange) -> String.UTF16View { return self[bounds.relative(to: self)] } }