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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// Declarations to enable ease-of-testing
public // @testable
struct _StringRepresentation {
public var _isASCII: Bool
public var _count: Int
public var _capacity: Int
public enum _Form {
case _small
case _cocoa(object: AnyObject)
case _native(object: AnyObject)
case _immortal(address: UInt)
// TODO: shared native
}
public var _form: _Form
public var _objectIdentifier: ObjectIdentifier? {
switch _form {
case ._cocoa(let object): return ObjectIdentifier(object)
case ._native(let object): return ObjectIdentifier(object)
default: return nil
}
}
}
@available(*, unavailable)
extension _StringRepresentation: Sendable {}
@available(*, unavailable)
extension _StringRepresentation._Form: Sendable {}
extension String {
public // @testable
func _classify() -> _StringRepresentation { return _guts._classify() }
@_alwaysEmitIntoClient
public // @testable
func _deconstructUTF8<ToPointer: _Pointer>(
scratch: UnsafeMutableRawBufferPointer?
) -> (
owner: AnyObject?,
ToPointer,
length: Int,
usesScratch: Bool,
allocatedMemory: Bool
) {
_guts._deconstructUTF8(scratch: scratch)
}
}
extension _StringGuts {
internal func _classify() -> _StringRepresentation {
var result = _StringRepresentation(
_isASCII: self.isASCII,
_count: self.count,
_capacity: nativeCapacity ?? 0,
_form: ._small
)
if self.isSmall {
result._capacity = _SmallString.capacity
return result
}
if _object.largeIsCocoa {
result._form = ._cocoa(object: _object.cocoaObject)
return result
}
// TODO: shared native
_internalInvariant(_object.providesFastUTF8)
if _object.isImmortal {
result._form = ._immortal(
address: UInt(bitPattern: _object.nativeUTF8Start))
return result
}
if _object.hasNativeStorage {
_internalInvariant(_object.largeFastIsTailAllocated)
result._form = ._native(object: _object.nativeStorage)
return result
}
fatalError()
}
/*
Deconstruct the string into contiguous UTF-8, allocating memory if necessary
┌────────────────────╥───────────────────────┬─────────────────────┬─────────────┬─────────────────┐
│ Form ║ owner │ pointer+length │ usesScratch │ allocatedMemory │
├────────────────────╫───────────────────────┼─────────────────────┼─────────────┼─────────────────┤
│ small with scratch ║ nil │ `scratch` │ true │ false │
├────────────────────╫───────────────────────┼─────────────────────┼─────────────┼─────────────────┤
│ small w/o scratch ║ extra allocation │ `owner` pointer │ false │ true │
╞════════════════════╬═══════════════════════╪═════════════════════╪═════════════╪═════════════════╡
│ immortal, large ║ nil │ literal pointer │ false │ false │
├────────────────────╫───────────────────────┼─────────────────────┼─────────────┼─────────────────┤
│ native ║ __StringStorage │ tail alloc pointer │ false │ false │
╞════════════════════╬═══════════════════════╪═════════════════════╪═════════════╪═════════════════╡
│ shared ║ __SharedStringStorage │ shared pointer │ false │ false │
├────────────────────╫───────────────────────┼─────────────────────┼─────────────┼─────────────────┤
│ shared, bridged ║ _CocoaString │ cocoa ASCII pointer │ false │ false │
╞════════════════════╬═══════════════════════╪═════════════════════╪═════════════╪═════════════════╡
│ foreign ║ extra allocation │ `owner` pointer │ false │ true │
└────────────────────╨───────────────────────┴─────────────────────┴─────────────┴─────────────────┘
*/
@_alwaysEmitIntoClient
internal // TODO: figure out if this works as a compiler intrinsic
func _deconstructUTF8<ToPointer: _Pointer>(
scratch: UnsafeMutableRawBufferPointer?
) -> (
owner: AnyObject?,
ToPointer,
length: Int,
usesScratch: Bool,
allocatedMemory: Bool
) {
// If we're small, try to copy into the scratch space provided
if self.isSmall {
let smol = self.asSmall
if let scratch = scratch, scratch.count > smol.count {
let scratchStart =
scratch.baseAddress!
smol.withUTF8 { smolUTF8 -> () in
scratchStart.initializeMemory(
as: UInt8.self, from: smolUTF8.baseAddress!, count: smolUTF8.count)
}
scratch[smol.count] = 0
return (
owner: nil,
_convertPointerToPointerArgument(scratchStart),
length: smol.count,
usesScratch: true, allocatedMemory: false)
}
} else if _fastPath(self.isFastUTF8) {
let ptr: ToPointer =
_convertPointerToPointerArgument(self._object.fastUTF8.baseAddress!)
return (
owner: self._object.owner,
ptr,
length: self._object.count,
usesScratch: false, allocatedMemory: false)
}
let (object, ptr, len) = self._allocateForDeconstruct()
return (
owner: object,
_convertPointerToPointerArgument(ptr),
length: len,
usesScratch: false,
allocatedMemory: true)
}
@_alwaysEmitIntoClient
@inline(never) // slow path
internal
func _allocateForDeconstruct() -> (
owner: AnyObject,
UnsafeRawPointer,
length: Int
) {
let utf8 = Array(String(self).utf8) + [0]
let (owner, ptr): (AnyObject?, UnsafeRawPointer) =
_convertConstArrayToPointerArgument(utf8)
// Array's owner cannot be nil, even though it is declared optional...
return (owner: owner!, ptr, length: utf8.count - 1)
}
}
|