|
| 1 | +extension String { |
| 2 | + /// Print out a full list of indices in every view of this string. |
| 3 | + /// This is useful while debugging string indexing issues. |
| 4 | + public func dumpIndices() { |
| 5 | + print("-------------------------------------------------------------------") |
| 6 | + print("String: \(String(reflecting: self))") |
| 7 | + print("Characters:") |
| 8 | + self.indices.forEach { i in |
| 9 | + let char = self[i] |
| 10 | + print(" \(i) -> \(String(reflecting: char))") |
| 11 | + } |
| 12 | + print("Scalars:") |
| 13 | + self.unicodeScalars.indices.forEach { i in |
| 14 | + let scalar = self.unicodeScalars[i] |
| 15 | + let value = String(scalar.value, radix: 16, uppercase: true) |
| 16 | + let padding = String(repeating: "0", count: max(0, 4 - value.count)) |
| 17 | + let name = scalar.properties.name ?? "\(scalar.debugDescription)" |
| 18 | + print(" \(i) -> U+\(padding)\(value) \(name)") |
| 19 | + } |
| 20 | + print("UTF-8:") |
| 21 | + self.utf8.indices.forEach { i in |
| 22 | + let code = self.utf8[i] |
| 23 | + let value = String(code, radix: 16, uppercase: true) |
| 24 | + let padding = value.count < 2 ? "0" : "" |
| 25 | + print(" \(i) -> \(padding)\(value)") |
| 26 | + } |
| 27 | + print("UTF-16:") |
| 28 | + self.utf16.indices.forEach { i in |
| 29 | + let code = self.utf16[i] |
| 30 | + let value = String(code, radix: 16, uppercase: true) |
| 31 | + let padding = String(repeating: "0", count: 4 - value.count) |
| 32 | + print(" \(i) -> \(padding)\(value)") |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + // Returns a list of every valid index in every string view, optionally |
| 37 | + // including end indices. We keep equal indices originating from different |
| 38 | + // views because they may have different grapheme size caches or flags etc. |
| 39 | + public func allIndices(includingEnd: Bool = true) -> [String.Index] { |
| 40 | + var r = Array(self.indices) |
| 41 | + if includingEnd { r.append(self.endIndex) } |
| 42 | + r += Array(self.unicodeScalars.indices) |
| 43 | + if includingEnd { r.append(self.unicodeScalars.endIndex) } |
| 44 | + r += Array(self.utf8.indices) |
| 45 | + if includingEnd { r.append(self.utf8.endIndex) } |
| 46 | + r += Array(self.utf16.indices) |
| 47 | + if includingEnd { r.append(self.utf16.endIndex) } |
| 48 | + return r |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +extension Substring { |
| 53 | + // Returns a list of every valid index in every substring view, optionally |
| 54 | + // including end indices. We keep equal indices originating from different |
| 55 | + // views because they may have different grapheme size caches or flags etc. |
| 56 | + public func allIndices(includingEnd: Bool = true) -> [String.Index] { |
| 57 | + var r = Array(self.indices) |
| 58 | + if includingEnd { r.append(self.endIndex) } |
| 59 | + r += Array(self.unicodeScalars.indices) |
| 60 | + if includingEnd { r.append(self.unicodeScalars.endIndex) } |
| 61 | + r += Array(self.utf8.indices) |
| 62 | + if includingEnd { r.append(self.utf8.endIndex) } |
| 63 | + r += Array(self.utf16.indices) |
| 64 | + if includingEnd { r.append(self.utf16.endIndex) } |
| 65 | + return r |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +extension Collection { |
| 70 | + // Assuming both `self` and `other` use the same index space, call `body` for |
| 71 | + // each index `i` in `other`, along with the slice in `self` that begins at |
| 72 | + // `i` and ends at the index following it in `other`. |
| 73 | + // |
| 74 | + // `other` must start with an item that is less than or equal to the first |
| 75 | + // item in `self`. |
| 76 | + func forEachIndexGroup<G: Collection>( |
| 77 | + by other: G, |
| 78 | + body: (G.Index, Self.SubSequence, Int) throws -> Void |
| 79 | + ) rethrows |
| 80 | + where G.Index == Self.Index |
| 81 | + { |
| 82 | + if other.isEmpty { |
| 83 | + assert(self.isEmpty) |
| 84 | + return |
| 85 | + } |
| 86 | + var i = other.startIndex |
| 87 | + var j = self.startIndex |
| 88 | + var offset = 0 |
| 89 | + while i != other.endIndex { |
| 90 | + let current = i |
| 91 | + other.formIndex(after: &i) |
| 92 | + let start = j |
| 93 | + while j < i, j < self.endIndex { |
| 94 | + self.formIndex(after: &j) |
| 95 | + } |
| 96 | + let end = j |
| 97 | + try body(current, self[start ..< end], offset) |
| 98 | + offset += 1 |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +extension String { |
| 104 | + /// Returns a dictionary mapping each valid index to the index that addresses |
| 105 | + /// the nearest scalar boundary, rounding down. |
| 106 | + public func scalarMap() -> [Index: (index: Index, offset: Int)] { |
| 107 | + var map: [Index: (index: Index, offset: Int)] = [:] |
| 108 | + |
| 109 | + utf8.forEachIndexGroup(by: unicodeScalars) { scalar, slice, offset in |
| 110 | + for i in slice.indices { map[i] = (scalar, offset) } |
| 111 | + } |
| 112 | + utf16.forEachIndexGroup(by: unicodeScalars) { scalar, slice, offset in |
| 113 | + for i in slice.indices { map[i] = (scalar, offset) } |
| 114 | + } |
| 115 | + self.forEachIndexGroup(by: unicodeScalars) { scalar, slice, offset in |
| 116 | + for i in slice.indices { map[i] = (scalar, offset) } |
| 117 | + } |
| 118 | + map[endIndex] = (endIndex, unicodeScalars.count) |
| 119 | + return map |
| 120 | + } |
| 121 | + |
| 122 | + /// Returns a dictionary mapping each valid index to the index that addresses |
| 123 | + /// the nearest character boundary, rounding down. |
| 124 | + public func characterMap() -> [Index: (index: Index, offset: Int)] { |
| 125 | + var map: [Index: (index: Index, offset: Int)] = [:] |
| 126 | + utf8.forEachIndexGroup(by: self) { char, slice, offset in |
| 127 | + for i in slice.indices { map[i] = (char, offset) } |
| 128 | + } |
| 129 | + utf16.forEachIndexGroup(by: self) { char, slice, offset in |
| 130 | + for i in slice.indices { map[i] = (char, offset) } |
| 131 | + } |
| 132 | + unicodeScalars.forEachIndexGroup(by: self) { char, slice, offset in |
| 133 | + for i in slice.indices { map[i] = (char, offset) } |
| 134 | + } |
| 135 | + map[endIndex] = (endIndex, count) |
| 136 | + return map |
| 137 | + } |
| 138 | +} |
| 139 | + |
0 commit comments