-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
StringProtocol.swift
285 lines (257 loc) · 10.8 KB
/
StringProtocol.swift
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
/// A type that can represent a string as a collection of characters.
///
/// Do not declare new conformances to `StringProtocol`. Only the `String` and
/// `Substring` types in the standard library are valid conforming types.
public protocol StringProtocol
: BidirectionalCollection,
TextOutputStream, TextOutputStreamable,
LosslessStringConvertible, ExpressibleByStringInterpolation,
Hashable, Comparable
where Iterator.Element == Character,
Index == String.Index,
SubSequence: StringProtocol,
StringInterpolation == DefaultStringInterpolation
{
associatedtype UTF8View: /*Bidirectional*/Collection
where UTF8View.Element == UInt8, // Unicode.UTF8.CodeUnit
UTF8View.Index == Index
associatedtype UTF16View: BidirectionalCollection
where UTF16View.Element == UInt16, // Unicode.UTF16.CodeUnit
UTF16View.Index == Index
associatedtype UnicodeScalarView: BidirectionalCollection
where UnicodeScalarView.Element == Unicode.Scalar,
UnicodeScalarView.Index == Index
associatedtype SubSequence = Substring
var utf8: UTF8View { get }
var utf16: UTF16View { get }
var unicodeScalars: UnicodeScalarView { get }
func hasPrefix(_ prefix: String) -> Bool
func hasSuffix(_ suffix: String) -> Bool
func lowercased() -> String
func uppercased() -> String
/// Creates a string from the given Unicode code units in the specified
/// encoding.
///
/// - Parameters:
/// - codeUnits: A collection of code units encoded in the encoding
/// specified in `sourceEncoding`.
/// - sourceEncoding: The encoding in which `codeUnits` should be
/// interpreted.
init<C: Collection, Encoding: Unicode.Encoding>(
decoding codeUnits: C, as sourceEncoding: Encoding.Type
)
where C.Iterator.Element == Encoding.CodeUnit
/// Creates a string from the null-terminated, UTF-8 encoded sequence of
/// bytes at the given pointer.
///
/// - Parameter nullTerminatedUTF8: A pointer to a sequence of contiguous,
/// UTF-8 encoded bytes ending just before the first zero byte.
init(cString nullTerminatedUTF8: UnsafePointer<CChar>)
/// Creates a string from the null-terminated sequence of bytes at the given
/// pointer.
///
/// - Parameters:
/// - nullTerminatedCodeUnits: A pointer to a sequence of contiguous code
/// units in the encoding specified in `sourceEncoding`, ending just
/// before the first zero code unit.
/// - sourceEncoding: The encoding in which the code units should be
/// interpreted.
init<Encoding: Unicode.Encoding>(
decodingCString nullTerminatedCodeUnits: UnsafePointer<Encoding.CodeUnit>,
as sourceEncoding: Encoding.Type)
/// Calls the given closure with a pointer to the contents of the string,
/// represented as a null-terminated sequence of UTF-8 code units.
///
/// The pointer passed as an argument to `body` is valid only during the
/// execution of `withCString(_:)`. Do not store or return the pointer for
/// later use.
///
/// - Parameter body: A closure with a pointer parameter that points to a
/// null-terminated sequence of UTF-8 code units. If `body` has a return
/// value, that value is also used as the return value for the
/// `withCString(_:)` method. The pointer argument is valid only for the
/// duration of the method's execution.
/// - Returns: The return value, if any, of the `body` closure parameter.
func withCString<Result>(
_ body: (UnsafePointer<CChar>) throws -> Result) rethrows -> Result
/// Calls the given closure with a pointer to the contents of the string,
/// represented as a null-terminated sequence of code units.
///
/// The pointer passed as an argument to `body` is valid only during the
/// execution of `withCString(encodedAs:_:)`. Do not store or return the
/// pointer for later use.
///
/// - Parameters:
/// - body: A closure with a pointer parameter that points to a
/// null-terminated sequence of code units. If `body` has a return
/// value, that value is also used as the return value for the
/// `withCString(encodedAs:_:)` method. The pointer argument is valid
/// only for the duration of the method's execution.
/// - targetEncoding: The encoding in which the code units should be
/// interpreted.
/// - Returns: The return value, if any, of the `body` closure parameter.
func withCString<Result, Encoding: Unicode.Encoding>(
encodedAs targetEncoding: Encoding.Type,
_ body: (UnsafePointer<Encoding.CodeUnit>) throws -> Result
) rethrows -> Result
}
extension StringProtocol {
// TODO(String performance): Make a _SharedString for non-smol Substrings
//
// TODO(String performance): Provide a closure-based call with stack-allocated
// _SharedString for non-smol Substrings
//
public // @SPI(NSStringAPI.swift)
var _ephemeralString: String {
@_specialize(where Self == String)
@_specialize(where Self == Substring)
get { return String(self) }
}
internal var _gutsSlice: _StringGutsSlice {
@_specialize(where Self == String)
@_specialize(where Self == Substring)
@inline(__always) get {
if let str = self as? String {
return _StringGutsSlice(str._guts)
}
if let subStr = self as? Substring {
return _StringGutsSlice(subStr._wholeGuts, subStr._offsetRange)
}
return _StringGutsSlice(String(self)._guts)
}
}
@inlinable
internal var _offsetRange: Range<Int> {
@inline(__always) get {
let start = startIndex
let end = endIndex
_internalInvariant(
start.transcodedOffset == 0 && end.transcodedOffset == 0)
return Range(_uncheckedBounds: (start._encodedOffset, end._encodedOffset))
}
}
@inlinable
internal var _wholeGuts: _StringGuts {
@_specialize(where Self == String)
@_specialize(where Self == Substring)
@inline(__always) get {
if let str = self as? String {
return str._guts
}
if let subStr = self as? Substring {
return subStr._wholeGuts
}
return String(self)._guts
}
}
}
// Contiguous UTF-8 strings
extension String {
/// Returns whether this string is capable of providing access to
/// validly-encoded UTF-8 contents in contiguous memory in O(1) time.
///
/// Contiguous strings always operate in O(1) time for withUTF8 and always
/// give a result for String.UTF8View.withContiguousStorageIfAvailable.
/// Contiguous strings also benefit from fast-paths and better optimizations.
///
@_alwaysEmitIntoClient
public var isContiguousUTF8: Bool { return _guts.isFastUTF8 }
/// If this string is not contiguous, make it so. If this mutates the string,
/// it will invalidate any pre-existing indices.
///
/// Complexity: O(n) if non-contiguous, O(1) if already contiguous
///
@_alwaysEmitIntoClient
public mutating func makeContiguousUTF8() {
if _fastPath(isContiguousUTF8) { return }
self = String._copying(self)
}
/// Runs `body` over the content of this string in contiguous memory. If this
/// string is not contiguous, this will first make it contiguous, which will
/// also speed up subsequent access. If this mutates the string,
/// it will invalidate any pre-existing indices.
///
/// Note that it is unsafe to escape the pointer provided to `body`. For
/// example, strings of up to 15 UTF-8 code units in length may be represented
/// in a small-string representation, and thus will be spilled into
/// temporary stack space which is invalid after `withUTF8` finishes
/// execution.
///
/// Complexity: O(n) if non-contiguous, O(1) if already contiguous
///
@_alwaysEmitIntoClient
public mutating func withUTF8<R>(
_ body: (UnsafeBufferPointer<UInt8>) throws -> R
) rethrows -> R {
makeContiguousUTF8()
return try _guts.withFastUTF8(body)
}
}
// Contiguous UTF-8 strings
extension Substring {
/// Returns whether this string is capable of providing access to
/// validly-encoded UTF-8 contents in contiguous memory in O(1) time.
///
/// Contiguous strings always operate in O(1) time for withUTF8 and always
/// give a result for String.UTF8View.withContiguousStorageIfAvailable.
/// Contiguous strings also benefit from fast-paths and better optimizations.
///
@_alwaysEmitIntoClient
public var isContiguousUTF8: Bool { return self.base.isContiguousUTF8 }
/// If this string is not contiguous, make it so. If this mutates the
/// substring, it will invalidate any pre-existing indices.
///
/// Complexity: O(n) if non-contiguous, O(1) if already contiguous
///
@_alwaysEmitIntoClient @inline(__always)
public mutating func makeContiguousUTF8() {
if isContiguousUTF8 { return }
return _slowMakeContiguousUTF8()
}
@_alwaysEmitIntoClient // Swift 5.7
@inline(never)
internal mutating func _slowMakeContiguousUTF8() {
_internalInvariant(!isContiguousUTF8)
let scalarOffset = base.unicodeScalars.distance(
from: base.startIndex, to: startIndex)
let scalarCount = base.unicodeScalars.distance(
from: startIndex, to: endIndex)
let scalars = String._copying(base).unicodeScalars
var newStart = scalars.index(scalars.startIndex, offsetBy: scalarOffset)
var newEnd = scalars.index(newStart, offsetBy: scalarCount)
if startIndex._isCharacterAligned { newStart = newStart._characterAligned }
if endIndex._isCharacterAligned { newEnd = newEnd._characterAligned }
self = Substring(_unchecked: scalars._guts, bounds: newStart ..< newEnd)
}
/// Runs `body` over the content of this substring in contiguous memory. If
/// this substring is not contiguous, this will first make it contiguous,
/// which will also speed up subsequent access. If this mutates the substring,
/// it will invalidate any pre-existing indices.
///
/// Note that it is unsafe to escape the pointer provided to `body`. For
/// example, strings of up to 15 UTF-8 code units in length may be represented
/// in a small-string representation, and thus will be spilled into
/// temporary stack space which is invalid after `withUTF8` finishes
/// execution.
///
/// Complexity: O(n) if non-contiguous, O(1) if already contiguous
///
@_alwaysEmitIntoClient
public mutating func withUTF8<R>(
_ body: (UnsafeBufferPointer<UInt8>) throws -> R
) rethrows -> R {
makeContiguousUTF8()
return try _wholeGuts.withFastUTF8(range: _offsetRange, body)
}
}