File: String.Index%2BABI.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 (113 lines) | stat: -rw-r--r-- 2,951 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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// Bits of String.Index that are ABI but aren't exposed by public API.
extension String.Index {
  @inline(__always)
  var _abi_rawBits: UInt64 {
    unsafeBitCast(self, to: UInt64.self)
  }
  
  @inline(__always)
  var _abi_encodedOffset: Int {
    Int(truncatingIfNeeded: _abi_rawBits &>> 16)
  }

  @inline(__always)
  var _abi_transcodedOffset: Int {
    Int(truncatingIfNeeded: (_abi_rawBits &>> 14) & 0x3)
  }

  @inline(__always)
  static var _abi_scalarAlignmentBit: UInt64 { 0x1 }

  @inline(__always)
  static var _abi_characterAlignmentBit: UInt64 { 0x2 }

  @inline(__always)
  static var _abi_utf8Bit: UInt64 { 0x4 }

  @inline(__always)
  static var _abi_utf16Bit: UInt64 { 0x8 }

  
  @inline(__always)
  var _encodingBits: UInt64 {
    _abi_rawBits & (Self._abi_utf8Bit | Self._abi_utf16Bit)
  }
  
  @inline(__always)
  var _canBeUTF8: Bool {
    // The only way an index cannot be UTF-8 is it has only the UTF-16 flag set.
    _encodingBits != Self._abi_utf16Bit
  }
  
  var _isKnownScalarAligned: Bool {
    0 != _abi_rawBits & Self._abi_scalarAlignmentBit
  }
  
  var _isKnownCharacterAligned: Bool {
    0 != _abi_rawBits & Self._abi_characterAlignmentBit
  }
  
  var _knownCharacterAligned: String.Index {
    let r = _abi_rawBits | Self._abi_characterAlignmentBit | Self._abi_scalarAlignmentBit
    return unsafeBitCast(r, to: String.Index.self)
  }

  var _knownScalarAligned: String.Index {
    let r = _abi_rawBits | Self._abi_scalarAlignmentBit
    return unsafeBitCast(r, to: String.Index.self)
  }
}

extension String.Index {
  @inline(__always)
  var _utf8Offset: Int {
    assert(_canBeUTF8)
    return _abi_encodedOffset
  }

  @inline(__always)
  var _isUTF16TrailingSurrogate: Bool {
    assert(_canBeUTF8)
    let r = _abi_transcodedOffset
    assert(r <= 1)
    return r > 0
  }

  @inline(__always)
  init(_rawBits: UInt64) {
    self = unsafeBitCast(_rawBits, to: String.Index.self)
  }

  @inline(__always)
  init(_utf8Offset: Int) {
    self.init(_rawBits: (UInt64(_utf8Offset) &<< 16) | Self._abi_utf8Bit)
  }
  
  init(_utf8Offset: Int, utf16TrailingSurrogate: Bool) {
    let transcodedOffset: UInt64 = (utf16TrailingSurrogate ? 1 &<< 14 : 0)
    self.init(_rawBits: (UInt64(_utf8Offset) &<< 16) | transcodedOffset | Self._abi_utf8Bit)
  }
}

extension String.Index {
  @inline(__always)
  var _chunkData: UInt16 {
    UInt16(_abi_rawBits &>> 14)
  }

  @inline(__always)
  init(_chunkData: UInt16) {
    self.init(_rawBits: UInt64(_chunkData) &<< 14)
  }
}