File: BigString%2BMetrics.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (209 lines) | stat: -rw-r--r-- 6,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2023 - 2024 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
//
//===----------------------------------------------------------------------===//

#if swift(>=5.8)

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
internal protocol _StringMetric: RopeMetric where Element == BigString._Chunk {
  func distance(
    from start: String.Index,
    to end: String.Index,
    in chunk: BigString._Chunk
  ) -> Int
  
  func formIndex(
    _ i: inout String.Index,
    offsetBy distance: inout Int,
    in chunk: BigString._Chunk
  ) -> (found: Bool, forward: Bool)
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  internal struct _CharacterMetric: _StringMetric {
    typealias Element = BigString._Chunk
    typealias Summary = BigString.Summary
    
    @inline(__always)
    func size(of summary: Summary) -> Int {
      summary.characters
    }
    
    func distance(
      from start: String.Index,
      to end: String.Index,
      in chunk: BigString._Chunk
    ) -> Int {
      chunk.characterDistance(from: start, to: end)
    }
    
    func formIndex(
      _ i: inout String.Index,
      offsetBy distance: inout Int,
      in chunk: BigString._Chunk
    ) -> (found: Bool, forward: Bool) {
      chunk.formCharacterIndex(&i, offsetBy: &distance)
    }
    
    func index(at offset: Int, in chunk: BigString._Chunk) -> String.Index {
      precondition(offset < chunk.characterCount)
      return chunk.wholeCharacters._index(at: offset)
    }
  }
  
  internal struct _UnicodeScalarMetric: _StringMetric {
    @inline(__always)
    func size(of summary: Summary) -> Int {
      summary.unicodeScalars
    }
    
    func distance(
      from start: String.Index,
      to end: String.Index,
      in chunk: BigString._Chunk
    ) -> Int {
      chunk.string.unicodeScalars.distance(from: start, to: end)
    }
    
    func formIndex(
      _ i: inout String.Index,
      offsetBy distance: inout Int,
      in chunk: BigString._Chunk
    ) -> (found: Bool, forward: Bool) {
      guard distance != 0 else {
        i = chunk.string.unicodeScalars._index(roundingDown: i)
        return (true, false)
      }
      if distance > 0 {
        let end = chunk.string.endIndex
        while distance > 0, i < end {
          chunk.string.unicodeScalars.formIndex(after: &i)
          distance &-= 1
        }
        return (distance == 0, true)
      }
      let start = chunk.string.startIndex
      while distance < 0, i > start {
        chunk.string.unicodeScalars.formIndex(before: &i)
        distance &+= 1
      }
      return (distance == 0, false)
    }
    
    func index(at offset: Int, in chunk: BigString._Chunk) -> String.Index {
      chunk.string.unicodeScalars.index(chunk.string.startIndex, offsetBy: offset)
    }
  }
  
  internal struct _UTF8Metric: _StringMetric {
    @inline(__always)
    func size(of summary: Summary) -> Int {
      summary.utf8
    }
    
    func distance(
      from start: String.Index,
      to end: String.Index,
      in chunk: BigString._Chunk
    ) -> Int {
      chunk.string.utf8.distance(from: start, to: end)
    }
    
    func formIndex(
      _ i: inout String.Index,
      offsetBy distance: inout Int,
      in chunk: BigString._Chunk
    ) -> (found: Bool, forward: Bool) {
      // Here we make use of the fact that the UTF-8 view of native Swift strings
      // have O(1) index distance & offset calculations.
      let offset = chunk.string.utf8.distance(from: chunk.string.startIndex, to: i)
      if distance >= 0 {
        let rest = chunk.utf8Count - offset
        if distance > rest {
          i = chunk.string.endIndex
          distance -= rest
          return (false, true)
        }
        i = chunk.string.utf8.index(i, offsetBy: distance)
        distance = 0
        return (true, true)
      }
      
      if offset + distance < 0 {
        i = chunk.string.startIndex
        distance += offset
        return (false, false)
      }
      i = chunk.string.utf8.index(i, offsetBy: distance)
      distance = 0
      return (true, false)
    }
    
    func index(at offset: Int, in chunk: BigString._Chunk) -> String.Index {
      chunk.string.utf8.index(chunk.string.startIndex, offsetBy: offset)
    }
  }
  
  internal struct _UTF16Metric: _StringMetric {
    @inline(__always)
    func size(of summary: Summary) -> Int {
      summary.utf16
    }
    
    func distance(
      from start: String.Index,
      to end: String.Index,
      in chunk: BigString._Chunk
    ) -> Int {
      chunk.string.utf16.distance(from: start, to: end)
    }
    
    func formIndex(
      _ i: inout String.Index,
      offsetBy distance: inout Int,
      in chunk: BigString._Chunk
    ) -> (found: Bool, forward: Bool) {
      if distance >= 0 {
        if
          distance <= chunk.utf16Count,
          let r = chunk.string.utf16.index(
            i, offsetBy: distance, limitedBy: chunk.string.endIndex
          ) {
          i = r
          distance = 0
          return (true, true)
        }
        distance -= chunk.string.utf16.distance(from: i, to: chunk.string.endIndex)
        i = chunk.string.endIndex
        return (false, true)
      }
      
      if
        distance.magnitude <= chunk.utf16Count,
        let r = chunk.string.utf16.index(
          i, offsetBy: distance, limitedBy: chunk.string.endIndex
        ) {
        i = r
        distance = 0
        return (true, true)
      }
      distance += chunk.string.utf16.distance(from: chunk.string.startIndex, to: i)
      i = chunk.string.startIndex
      return (false, false)
    }
    
    func index(at offset: Int, in chunk: BigString._Chunk) -> String.Index {
      chunk.string.utf16.index(chunk.string.startIndex, offsetBy: offset)
    }
  }
}

#endif