File: BigString%2BContents.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 (534 lines) | stat: -rw-r--r-- 17,780 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
//===----------------------------------------------------------------------===//
//
// 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, *)
extension BigString {
  /// The estimated maximum number of UTF-8 code units that `BigString` is guaranteed to be able
  /// to hold without encountering an overflow in its operations. This corresponds to the capacity
  /// of the deepest tree where every node is the minimum possible size.
  public static var _minimumCapacity: Int {
    let c = _Rope._minimumCapacity
    let (r, overflow) = _Chunk.minUTF8Count.multipliedReportingOverflow(by: c)
    guard !overflow else { return Int.max }
    return r
  }

  /// The maximum number of UTF-8 code units that `BigString` may be able to store in the best
  /// possible case, when every node in the underlying tree is fully filled with data.
  public static var _maximumCapacity: Int {
    let c = _Rope._maximumCapacity
    let (r, overflow) = _Chunk.maxUTF8Count.multipliedReportingOverflow(by: c)
    guard !overflow else { return Int.max }
    return r
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  var _characterCount: Int { _rope.summary.characters }
  var _unicodeScalarCount: Int { _rope.summary.unicodeScalars }
  var _utf16Count: Int { _rope.summary.utf16 }
  var _utf8Count: Int { _rope.summary.utf8 }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  func _distance(
    from start: Index,
    to end: Index,
    in metric: some _StringMetric
  ) -> Int {
    precondition(start <= endIndex && end <= endIndex, "Invalid index")
    guard start != end else { return 0 }
    assert(!isEmpty)
    let (lesser, greater) = (start <= end ? (start, end) : (end, start))
    let a = resolve(lesser, preferEnd: false)
    let b = resolve(greater, preferEnd: true)
    var d = 0

    let ropeIndexA = a._rope!
    let ropeIndexB = b._rope!
    let chunkIndexA = a._chunkIndex
    let chunkIndexB = b._chunkIndex

    if ropeIndexA == ropeIndexB {
      d = metric.distance(from: chunkIndexA, to: chunkIndexB, in: _rope[ropeIndexA])
    } else {
      let chunkA = _rope[ropeIndexA]
      let chunkB = _rope[ropeIndexB]
      d += _rope.distance(from: ropeIndexA, to: ropeIndexB, in: metric)
      d -= metric.distance(from: chunkA.string.startIndex, to: chunkIndexA, in: chunkA)
      d += metric.distance(from: chunkB.string.startIndex, to: chunkIndexB, in: chunkB)
    }
    return start <= end ? d : -d
  }
  
  func _characterDistance(from start: Index, to end: Index) -> Int {
    _distance(from: start, to: end, in: _CharacterMetric())
  }
  
  func _unicodeScalarDistance(from start: Index, to end: Index) -> Int {
    _distance(from: start, to: end, in: _UnicodeScalarMetric())
  }
  
  func _utf16Distance(from start: Index, to end: Index) -> Int {
    _distance(from: start, to: end, in: _UTF16Metric())
  }
  
  func _utf8Distance(from start: Index, to end: Index) -> Int {
    end.utf8Offset - start.utf8Offset
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  // FIXME: See if we need direct implementations for these.

  func _characterOffset(of index: Index) -> Int {
    _characterDistance(from: startIndex, to: index)
  }
  
  func _unicodeScalarOffset(of index: Index) -> Int {
    _unicodeScalarDistance(from: startIndex, to: index)
  }
  
  func _utf16Offset(of index: Index) -> Int {
    _utf16Distance(from: startIndex, to: index)
  }

  func _utf8Offset(of index: Index) -> Int {
    _utf8Distance(from: startIndex, to: index)
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  // FIXME: See if we need direct implementations for these.

  func _characterIndex(at offset: Int) -> Index {
    _characterIndex(startIndex, offsetBy: offset)
  }

  func _unicodeScalarIndex(at offset: Int) -> Index {
    _unicodeScalarIndex(startIndex, offsetBy: offset)
  }

  func _utf16Index(at offset: Int) -> Index {
    _utf16Index(startIndex, offsetBy: offset)
  }

  func _utf8Index(at offset: Int) -> Index {
    _utf8Index(startIndex, offsetBy: offset)
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  func _index(
    _ i: Index,
    offsetBy distance: Int,
    in metric: some _StringMetric
  ) -> Index {
    precondition(i <= endIndex, "Index out of bounds")
    if isEmpty {
      precondition(distance == 0, "Index out of bounds")
      return startIndex
    }
    if i == endIndex, distance == 0 { return i }
    let i = resolve(i, preferEnd: i == endIndex || distance < 0)
    var ri = i._rope!
    var ci = i._chunkIndex
    var d = distance
    var chunk = _rope[ri]
    let r = metric.formIndex(&ci, offsetBy: &d, in: chunk)
    if r.found {
      return Index(baseUTF8Offset: i._utf8BaseOffset, _rope: ri, chunk: ci)
    }

    if r.forward {
      assert(distance >= 0)
      assert(ci == chunk.string.endIndex)
      d += metric._nonnegativeSize(of: chunk.summary)
      let start = ri
      _rope.formIndex(&ri, offsetBy: &d, in: metric, preferEnd: false)
      if ri == _rope.endIndex {
        return endIndex
      }
      chunk = _rope[ri]
      ci = metric.index(at: d, in: chunk)
      let base = i._utf8BaseOffset + _rope.distance(from: start, to: ri, in: _UTF8Metric())
      return Index(baseUTF8Offset: base, _rope: ri, chunk: ci)
    }

    assert(distance <= 0)
    assert(ci == chunk.string.startIndex)
    let start = ri
    _rope.formIndex(&ri, offsetBy: &d, in: metric, preferEnd: false)
    chunk = _rope[ri]
    ci = metric.index(at: d, in: chunk)
    let base = i._utf8BaseOffset + _rope.distance(from: start, to: ri, in: _UTF8Metric())
    return Index(baseUTF8Offset: base, _rope: ri, chunk: ci)
  }
  
  func _characterIndex(_ i: Index, offsetBy distance: Int) -> Index {
    _index(i, offsetBy: distance, in: _CharacterMetric())._knownCharacterAligned()
  }
  
  func _unicodeScalarIndex(_ i: Index, offsetBy distance: Int) -> Index {
    _index(i, offsetBy: distance, in: _UnicodeScalarMetric())._knownScalarAligned()
  }
  
  func _utf16Index(_ i: Index, offsetBy distance: Int) -> Index {
    _index(i, offsetBy: distance, in: _UTF16Metric())
  }
  
  func _utf8Index(_ i: Index, offsetBy distance: Int) -> Index {
    _index(i, offsetBy: distance, in: _UTF8Metric())
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  func _index(
    _ i: Index,
    offsetBy distance: Int,
    limitedBy limit: Index,
    in metric: some _StringMetric
  ) -> Index? {
    // FIXME: Do we need a direct implementation?
    if distance >= 0 {
      if limit >= i {
        let d = self._distance(from: i, to: limit, in: metric)
        if d < distance { return nil }
      }
    } else {
      if limit <= i {
        let d = self._distance(from: i, to: limit, in: metric)
        if d > distance { return nil }
      }
    }
    return self._index(i, offsetBy: distance, in: metric)
  }

  func _characterIndex(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
    guard let j = _index(i, offsetBy: distance, limitedBy: limit, in: _CharacterMetric()) else {
      return nil
    }
    return j._knownCharacterAligned()
  }

  func _unicodeScalarIndex(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
    guard let j = _index(i, offsetBy: distance, limitedBy: limit, in: _UnicodeScalarMetric()) else {
      return nil
    }
    return j._knownScalarAligned()
  }

  func _utf16Index(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
    _index(i, offsetBy: distance, limitedBy: limit, in: _UTF16Metric())
  }

  func _utf8Index(_ i: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
    _index(i, offsetBy: distance, limitedBy: limit, in: _UTF8Metric())
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  func _characterIndex(after i: Index) -> Index {
    _index(i, offsetBy: 1, in: _CharacterMetric())._knownCharacterAligned()
  }
  
  func _unicodeScalarIndex(after i: Index) -> Index {
    _index(i, offsetBy: 1, in: _UnicodeScalarMetric())._knownScalarAligned()
  }
  
  func _utf16Index(after i: Index) -> Index {
    _index(i, offsetBy: 1, in: _UTF16Metric())
  }
  
  func _utf8Index(after i: Index) -> Index {
    precondition(i < endIndex, "Can't advance above end index")
    let i = resolve(i, preferEnd: false)
    let ri = i._rope!
    var ci = i._chunkIndex
    let chunk = _rope[ri]
    chunk.string.utf8.formIndex(after: &ci)
    if ci == chunk.string.endIndex {
      return Index(
        baseUTF8Offset: i._utf8BaseOffset + chunk.utf8Count,
        _rope: _rope.index(after: ri),
        chunk: String.Index(_utf8Offset: 0))
    }
    return Index(_utf8Offset: i.utf8Offset + 1, _rope: ri, chunkOffset: ci._utf8Offset)
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  func _characterIndex(before i: Index) -> Index {
    _index(i, offsetBy: -1, in: _CharacterMetric())._knownCharacterAligned()
  }
  
  func _unicodeScalarIndex(before i: Index) -> Index {
    _index(i, offsetBy: -1, in: _UnicodeScalarMetric())._knownScalarAligned()
  }
  
  func _utf16Index(before i: Index) -> Index {
    _index(i, offsetBy: -1, in: _UTF16Metric())
  }
  
  func _utf8Index(before i: Index) -> Index {
    precondition(i > startIndex, "Can't advance below start index")
    let i = resolve(i, preferEnd: true)
    var ri = i._rope!
    let ci = i._chunkIndex
    if ci._utf8Offset > 0 {
      return Index(
        _utf8Offset: i.utf8Offset &- 1,
        _rope: ri,
        chunkOffset: ci._utf8Offset &- 1)
    }
    _rope.formIndex(before: &ri)
    let chunk = _rope[ri]
    return Index(
      baseUTF8Offset: i._utf8BaseOffset - chunk.utf8Count,
      _rope: ri,
      chunk: String.Index(_utf8Offset: chunk.utf8Count - 1))
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  func _characterIndex(roundingDown i: Index) -> Index {
    let offset = i.utf8Offset
    precondition(offset >= 0 && offset <= _utf8Count, "Index out of bounds")
    guard offset > 0 else { return resolve(i, preferEnd: false)._knownCharacterAligned() }
    guard offset < _utf8Count else { return resolve(i, preferEnd: true)._knownCharacterAligned() }

    let i = resolve(i, preferEnd: false)
    guard !i._isKnownCharacterAligned else { return resolve(i, preferEnd: false) }

    var ri = i._rope!
    let ci = i._chunkIndex
    var chunk = _rope[ri]
    if chunk.hasBreaks {
      let first = chunk.firstBreak
      let last = chunk.lastBreak
      if ci == first || ci == last { return i }
      if ci > last {
        return Index(
          baseUTF8Offset: i._utf8BaseOffset, _rope: ri, chunk: last
        )._knownCharacterAligned()
      }
      if ci > first {
        let j = chunk.wholeCharacters._index(roundingDown: ci)
        return Index(baseUTF8Offset: i._utf8BaseOffset, _rope: ri, chunk: j)._knownCharacterAligned()
      }
    }

    var baseOffset = i._utf8BaseOffset
    while ri > self._rope.startIndex {
      self._rope.formIndex(before: &ri)
      chunk = self._rope[ri]
      baseOffset -= chunk.utf8Count
      if chunk.hasBreaks { break }
    }
    return Index(
      baseUTF8Offset: baseOffset, _rope: ri, chunk: chunk.lastBreak
    )._knownCharacterAligned()
  }

  func _unicodeScalarIndex(roundingDown i: Index) -> Index {
    precondition(i <= endIndex, "Index out of bounds")
    guard i > startIndex else { return resolve(i, preferEnd: false)._knownCharacterAligned() }
    guard i < endIndex else { return resolve(i, preferEnd: true)._knownCharacterAligned() }

    let start = self.resolve(i, preferEnd: false)
    guard !i._isKnownScalarAligned else { return resolve(i, preferEnd: false) }
    let ri = start._rope!
    let chunk = self._rope[ri]
    let ci = chunk.string.unicodeScalars._index(roundingDown: start._chunkIndex)
    return Index(baseUTF8Offset: start._utf8BaseOffset, _rope: ri, chunk: ci)._knownScalarAligned()
  }

  func _utf8Index(roundingDown i: Index) -> Index {
    precondition(i <= endIndex, "Index out of bounds")
    guard i < endIndex else { return endIndex }
    var r = i
    if i._isUTF16TrailingSurrogate {
      r._clearUTF16TrailingSurrogate()
    }
    return resolve(r, preferEnd: false)
  }

  func _utf16Index(roundingDown i: Index) -> Index {
    if i._isUTF16TrailingSurrogate {
      precondition(i < endIndex, "Index out of bounds")
      // (We know i can't be the endIndex -- it addresses a trailing surrogate.)
      return self.resolve(i, preferEnd: false)
    }
    return _unicodeScalarIndex(roundingDown: i)
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  func _characterIndex(roundingUp i: Index) -> Index {
    let j = _characterIndex(roundingDown: i)
    if i == j { return j }
    return _characterIndex(after: j)
  }

  func _unicodeScalarIndex(roundingUp i: Index) -> Index {
    let j = _unicodeScalarIndex(roundingDown: i)
    if i == j { return j }
    return _unicodeScalarIndex(after: j)
  }

  func _utf8Index(roundingUp i: Index) -> Index {
    // Note: this orders UTF-16 trailing surrogate indices in between the first and second byte
    // of the UTF-8 encoding.
    let j = _utf8Index(roundingDown: i)
    if i == j { return j }
    return _utf8Index(after: j)
  }

  func _utf16Index(roundingUp i: Index) -> Index {
    // Note: if `i` addresses some byte in the middle of a non-BMP scalar then the result will
    // point to the trailing surrogate.
    let j = _utf16Index(roundingDown: i)
    if i == j { return j }
    return _utf16Index(after: j)
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  func _character(at start: Index) -> (character: Character, end: Index) {
    let start = _characterIndex(roundingDown: start)
    precondition(start.utf8Offset < _utf8Count, "Index out of bounds")

    var ri = start._rope!
    var ci = start._chunkIndex
    var chunk = _rope[ri]
    let char = chunk.wholeCharacters[ci]
    let endOffset = start._utf8ChunkOffset + char.utf8.count
    if endOffset < chunk.utf8Count {
      let endStringIndex = chunk.string._utf8Index(at: endOffset)
      let endIndex = Index(
        baseUTF8Offset: start._utf8BaseOffset, _rope: ri, chunk: endStringIndex
      )._knownCharacterAligned()
      return (char, endIndex)
    }
    var s = String(char)
    var base = start._utf8BaseOffset + chunk.utf8Count
    while true {
      _rope.formIndex(after: &ri)
      guard ri < _rope.endIndex else {
        ci = "".endIndex
        break
      }
      chunk = _rope[ri]
      s.append(contentsOf: chunk.prefix)
      if chunk.hasBreaks {
        ci = chunk.firstBreak
        break
      }
      base += chunk.utf8Count
    }
    return (Character(s), Index(baseUTF8Offset: base, _rope: ri, chunk: ci)._knownCharacterAligned())
  }

  subscript(_utf8 index: Index) -> UInt8 {
    precondition(index < endIndex, "Index out of bounds")
    let index = resolve(index, preferEnd: false)
    return _rope[index._rope!].string.utf8[index._chunkIndex]
  }

  subscript(_utf8 offset: Int) -> UInt8 {
    precondition(offset >= 0 && offset < _utf8Count, "Offset out of bounds")
    let index = _utf8Index(at: offset)
    return self[_utf8: index]
  }

  subscript(_utf16 index: Index) -> UInt16 {
    precondition(index < endIndex, "Index out of bounds")
    let index = resolve(index, preferEnd: false)
    return _rope[index._rope!].string.utf16[index._chunkIndex]
  }

  subscript(_utf16 offset: Int) -> UInt16 {
    precondition(offset >= 0 && offset < _utf16Count, "Offset out of bounds")
    let index = _utf16Index(at: offset)
    return self[_utf16: index]
  }

  subscript(_character index: Index) -> Character {
    _character(at: index).character
  }

  subscript(_character offset: Int) -> Character {
    precondition(offset >= 0 && offset < _utf8Count, "Offset out of bounds")
    return _character(at: Index(_utf8Offset: offset)).character
  }

  subscript(_unicodeScalar index: Index) -> Unicode.Scalar {
    precondition(index < endIndex, "Index out of bounds")
    let index = resolve(index, preferEnd: false)
    return _rope[index._rope!].string.unicodeScalars[index._chunkIndex]
  }

  subscript(_unicodeScalar offset: Int) -> Unicode.Scalar {
    precondition(offset >= 0 && offset < _unicodeScalarCount, "Offset out of bounds")
    let index = _unicodeScalarIndex(at: offset)
    return self[_unicodeScalar: index]
  }
}

@available(macOS 13.3, iOS 16.4, watchOS 9.4, tvOS 16.4, *)
extension BigString {
  func _foreachChunk(
    from start: Index,
    to end: Index,
    _ body: (Substring) -> Void
  ) {
    precondition(start <= end)
    guard start < end else { return }
    let start = resolve(start, preferEnd: false)
    let end = resolve(end, preferEnd: true)

    var ri = start._rope!
    let endRopeIndex = end._rope!

    if ri == endRopeIndex {
      let str = self._rope[ri].string
      body(str[start._chunkIndex ..< end._chunkIndex])
      return
    }

    let firstChunk = self._rope[ri].string
    body(firstChunk[start._chunkIndex...])

    _rope.formIndex(after: &ri)
    while ri < endRopeIndex {
      let string = _rope[ri].string
      body(string[...])
    }

    let lastChunk = self._rope[ri].string
    body(lastChunk[..<end._chunkIndex])
  }
}

#endif