|
| 1 | +//===--- StringIndex.swift ------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +extension String { |
| 13 | + public struct Index { |
| 14 | + internal var _compoundOffset : UInt64 |
| 15 | + internal var _cache: _Cache |
| 16 | + |
| 17 | + internal enum _Cache { |
| 18 | + case utf16 |
| 19 | + case utf8(encodedScalar: Unicode.UTF8.EncodedScalar) |
| 20 | + case character(stride: UInt16) |
| 21 | + case unicodeScalar(value: Unicode.Scalar) |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +extension String.Index : Equatable { |
| 27 | + public static func == (lhs: String.Index, rhs: String.Index) -> Bool { |
| 28 | + return lhs._compoundOffset == rhs._compoundOffset |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +extension String.Index : Comparable { |
| 33 | + public static func < (lhs: String.Index, rhs: String.Index) -> Bool { |
| 34 | + return lhs._compoundOffset < rhs._compoundOffset |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +extension String.Index { |
| 39 | + internal init(_ c: Cache, atEncodedOffset o: Int) { |
| 40 | + self._compoundOffset = UInt64(o << _strideBits) |
| 41 | + self.cache = c |
| 42 | + } |
| 43 | + |
| 44 | + internal var _strideBits : Int { return 16 } |
| 45 | + |
| 46 | + internal mutating func _setEncodedOffset(_ x: Int) { |
| 47 | + _compoundOffset = UInt64(x << _strideBits) |
| 48 | + } |
| 49 | + |
| 50 | + public var encodedOffset : Int { |
| 51 | + return Int(_compoundOffset >> _strideBits) |
| 52 | + } |
| 53 | +} |
0 commit comments