Skip to content

Commit 60b6789

Browse files
authored
Merge pull request swiftlang#16578 from milseman/never_forgiv_never_inlin
[String] Drop many @inlinable annotations
2 parents b6e637e + 6003261 commit 60b6789

20 files changed

+149
-177
lines changed

stdlib/private/SwiftPrivate/IO.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ public struct _FDInputStream {
2626
public mutating func getline() -> String? {
2727
if let newlineIndex =
2828
_buffer[0..<_bufferUsed].index(of: UInt8(Unicode.Scalar("\n").value)) {
29-
let result = String._fromWellFormedUTF8CodeUnitSequence(
30-
_buffer[0..<newlineIndex])
29+
let result = String(decoding: _buffer[0..<newlineIndex], as: UTF8.self)
3130
_buffer.removeSubrange(0...newlineIndex)
3231
_bufferUsed -= newlineIndex + 1
3332
return result
3433
}
3534
if isEOF && _bufferUsed > 0 {
36-
let result = String._fromWellFormedUTF8CodeUnitSequence(
37-
_buffer[0..<_bufferUsed])
35+
let result = String(decoding: _buffer[0..<_bufferUsed], as: UTF8.self)
3836
_buffer.removeAll()
3937
_bufferUsed = 0
4038
return result

stdlib/public/core/CString.swift

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ extension String {
4343
/// // Prints "Caf�"
4444
///
4545
/// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
46-
@inlinable // FIXME(sil-serialize-all)
4746
public init(cString: UnsafePointer<CChar>) {
4847
self = _decodeValidCString(cString, repair: true)
4948
}
@@ -53,7 +52,6 @@ extension String {
5352
///
5453
/// This is identical to init(cString: UnsafePointer<CChar> but operates on an
5554
/// unsigned sequence of bytes.
56-
@inlinable // FIXME(sil-serialize-all)
5755
public init(cString: UnsafePointer<UInt8>) {
5856
self = _decodeValidCString(cString, repair: true)
5957
}
@@ -84,7 +82,6 @@ extension String {
8482
/// // Prints "nil"
8583
///
8684
/// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
87-
@inlinable // FIXME(sil-serialize-all)
8885
public init?(validatingUTF8 cString: UnsafePointer<CChar>) {
8986
guard let str = _decodeCString(cString, repair: false) else {
9087
return nil
@@ -134,7 +131,8 @@ extension String {
134131
/// - Returns: A tuple with the new string and a Boolean value that indicates
135132
/// whether any repairs were made. If `isRepairing` is `false` and an
136133
/// ill-formed sequence is detected, this method returns `nil`.
137-
@inlinable // FIXME(sil-serialize-all)
134+
@_specialize(where Encoding == Unicode.UTF8)
135+
@_specialize(where Encoding == Unicode.UTF16)
138136
public static func decodeCString<Encoding : _UnicodeEncoding>(
139137
_ cString: UnsafePointer<Encoding.CodeUnit>?,
140138
as encoding: Encoding.Type,
@@ -157,7 +155,6 @@ extension String {
157155
/// From a non-`nil` `UnsafePointer` to a null-terminated string
158156
/// with possibly-transient lifetime, create a null-terminated array of 'C' char.
159157
/// Returns `nil` if passed a null pointer.
160-
@inlinable // FIXME(sil-serialize-all)
161158
public func _persistCString(_ p: UnsafePointer<CChar>?) -> [CChar]? {
162159
guard let s = p else {
163160
return nil
@@ -170,7 +167,6 @@ public func _persistCString(_ p: UnsafePointer<CChar>?) -> [CChar]? {
170167
return result
171168
}
172169

173-
@inlinable
174170
internal func _decodeValidCString(
175171
_ cString: UnsafePointer<Int8>, repair: Bool
176172
) -> String {
@@ -182,7 +178,6 @@ internal func _decodeValidCString(
182178
}
183179
}
184180

185-
@inlinable
186181
internal func _decodeValidCString(
187182
_ cString: UnsafePointer<UInt8>, repair: Bool
188183
) -> String {
@@ -191,7 +186,6 @@ internal func _decodeValidCString(
191186
return String._fromWellFormedUTF8CodeUnitSequence(bufPtr, repair: repair)
192187
}
193188

194-
@inlinable
195189
internal func _decodeCString(
196190
_ cString: UnsafePointer<Int8>, repair: Bool
197191
) -> String? {
@@ -203,7 +197,6 @@ internal func _decodeCString(
203197
}
204198
}
205199

206-
@inlinable
207200
internal func _decodeCString(
208201
_ cString: UnsafePointer<UInt8>, repair: Bool
209202
) -> String? {
@@ -216,7 +209,6 @@ internal func _decodeCString(
216209
/// the given pointer using the specified encoding.
217210
///
218211
/// This internal helper takes the string length as an argument.
219-
@inlinable // FIXME(sil-serialize-all)
220212
internal func _decodeCString<Encoding : _UnicodeEncoding>(
221213
_ cString: UnsafePointer<Encoding.CodeUnit>,
222214
as encoding: Encoding.Type, length: Int,

stdlib/public/core/InputStream.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import SwiftShims
2626
/// or character combinations are preserved. The default is `true`.
2727
/// - Returns: The string of characters read from standard input. If EOF has
2828
/// already been reached when `readLine()` is called, the result is `nil`.
29-
@inlinable // FIXME(sil-serialize-all)
3029
public func readLine(strippingNewline: Bool = true) -> String? {
3130
var linePtrVar: UnsafeMutablePointer<UInt8>?
3231
var readBytes = swift_stdlib_readLine_stdin(&linePtrVar)
@@ -66,7 +65,7 @@ public func readLine(strippingNewline: Bool = true) -> String? {
6665
}
6766
}
6867
let result = String._fromUTF8CodeUnitSequence(
69-
UnsafeMutableBufferPointer(start: linePtr, count: readBytes),
68+
UnsafeBufferPointer(start: linePtr, count: readBytes),
7069
repair: true)!
7170
_stdlib_free(linePtr)
7271
return result

stdlib/public/core/Integers.swift.gyb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,10 @@ extension BinaryInteger {
18271827

18281828
let isNegative = Self.isSigned && self < (0 as Self)
18291829
var value = magnitude
1830+
1831+
// TODO(FIXME JIRA): All current stdlib types fit in small. Use a stack
1832+
// buffer instead of an array on the heap.
1833+
18301834
var result: [UInt8] = []
18311835
while value != 0 {
18321836
let (quotient, remainder) = _quotientAndRemainder(value)
@@ -1837,7 +1841,11 @@ extension BinaryInteger {
18371841
if isNegative {
18381842
result.append(UInt8(("-" as Unicode.Scalar).value))
18391843
}
1840-
return String._fromASCII(result.reversed())
1844+
1845+
result.reverse()
1846+
return result.withUnsafeBufferPointer {
1847+
return String._fromASCII($0)
1848+
}
18411849
}
18421850

18431851
/// A textual representation of this value.

stdlib/public/core/SmallString.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,20 @@ extension _SmallUTF8String {
129129

130130
@inlinable
131131
public // @testable
132-
init?<C: RandomAccessCollection>(_ codeUnits: C) where C.Element == UInt8 {
132+
init?(_ codeUnits: UnsafeBufferPointer<UInt8>) {
133133
#if arch(i386) || arch(arm)
134134
return nil // Never form small strings on 32-bit
135135
#else
136136
let count = codeUnits.count
137137
guard count <= _SmallUTF8String.capacity else { return nil }
138138
self.init()
139139
self._withAllUnsafeMutableBytes { rawBufPtr in
140-
let bufPtr = UnsafeMutableBufferPointer(
141-
start: rawBufPtr.baseAddress.unsafelyUnwrapped.assumingMemoryBound(
142-
to: UInt8.self),
143-
count: rawBufPtr.count)
144-
var (itr, written) = codeUnits._copyContents(initializing: bufPtr)
145-
_sanityCheck(itr.next() == nil)
146-
_sanityCheck(count == written)
140+
let rawDst = rawBufPtr.baseAddress._unsafelyUnwrappedUnchecked
141+
memcpy_(
142+
dst: rawDst.assumingMemoryBound(to: UInt8.self),
143+
src: codeUnits.baseAddress._unsafelyUnwrappedUnchecked,
144+
count: count
145+
)
147146
}
148147
_sanityCheck(self.count == 0, "overwrote count early?")
149148
self.count = count
@@ -152,6 +151,20 @@ extension _SmallUTF8String {
152151
if !self.isASCII { return nil }
153152

154153
_invariantCheck()
154+
#endif
155+
}
156+
157+
@inlinable
158+
public // @testable
159+
init?(_ scalar: Unicode.Scalar) {
160+
#if arch(i386) || arch(arm)
161+
return nil // Never form small strings on 32-bit
162+
#else
163+
// FIXME: support transcoding
164+
guard scalar.value <= 0x7F else { return nil }
165+
self.init()
166+
self.count = 1
167+
self[0] = UInt8(truncatingIfNeeded: scalar.value)
155168
#endif
156169
}
157170
}

stdlib/public/core/String.swift

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -625,38 +625,46 @@ extension String {
625625
}
626626
}
627627

628+
internal func _isAllASCII(_ input: UnsafeBufferPointer<UInt8>) -> Bool {
629+
for byte in input {
630+
guard byte <= 0x7F else { return false }
631+
}
632+
return true
633+
}
634+
628635
extension String {
629-
@inlinable
630-
static func _fromUTF8CodeUnitSequence<C : RandomAccessCollection>(
631-
_ input: C, repair: Bool
632-
) -> String? where C.Element == UInt8 {
636+
static func _fromUTF8CodeUnitSequence(
637+
_ input: UnsafeBufferPointer<UInt8>, repair: Bool
638+
) -> String? {
639+
if _isAllASCII(input) {
640+
return _fromASCII(input)
641+
}
642+
633643
if let smol = _SmallUTF8String(input) {
634644
return String(_StringGuts(smol))
635645
}
646+
636647
return String._fromCodeUnits(
637648
input, encoding: UTF8.self, repairIllFormedSequences: repair)
638649
}
639650

640-
@inlinable
641-
static func _fromASCII<C : RandomAccessCollection>(
642-
_ input: C
643-
) -> String where C.Element == UInt8 {
651+
@usableFromInline
652+
static func _fromASCII(_ input: UnsafeBufferPointer<UInt8>) -> String {
644653
if let smol = _SmallUTF8String(input) {
645654
return String(_StringGuts(smol))
646655
}
647656
let storage = _SwiftStringStorage<UInt8>.create(
648657
capacity: input.count, count: input.count)
649-
var (itr, end) = input._copyContents(initializing: storage.usedBuffer)
650-
_sanityCheck(itr.next() == nil)
651-
_sanityCheck(end == storage.usedBuffer.endIndex)
658+
_sanityCheck(storage.count == input.count)
659+
storage.start.initialize(
660+
from: input.baseAddress._unsafelyUnwrappedUnchecked, count: input.count)
652661
return String(_StringGuts(_large: storage))
653662
}
654663

655-
@inlinable // FIXME(sil-serialize-all)
656-
public // FIXME: @usableFromInline, currently public because testing...
657-
static func _fromWellFormedUTF8CodeUnitSequence<C : RandomAccessCollection>(
658-
_ input: C, repair: Bool = false
659-
) -> String where C.Element == UTF8.CodeUnit {
664+
@usableFromInline
665+
static func _fromWellFormedUTF8CodeUnitSequence(
666+
_ input: UnsafeBufferPointer<UInt8>, repair: Bool = false
667+
) -> String {
660668
return String._fromUTF8CodeUnitSequence(input, repair: repair)!
661669
}
662670
}
@@ -674,9 +682,7 @@ extension String : _ExpressibleByBuiltinUnicodeScalarLiteral {
674682
//
675683
// TODO: All scalars are small
676684
if scalar.value <= 0x7f {
677-
if let small = _SmallUTF8String(
678-
Unicode.UTF8.encode(scalar)._unsafelyUnwrappedUnchecked
679-
) {
685+
if let small = _SmallUTF8String(scalar) {
680686
self = String(_StringGuts(small))
681687
return
682688
} else {
@@ -893,7 +899,6 @@ extension String {
893899
/// // Prints "Hello, friend"
894900
///
895901
/// - Parameter other: Another string.
896-
@inlinable // FIXME(sil-serialize-all)
897902
public mutating func append(_ other: String) {
898903
self._guts.append(other._guts)
899904
}
@@ -969,12 +974,12 @@ extension Sequence where Element: StringProtocol {
969974
/// - Parameter separator: A string to insert between each of the elements
970975
/// in this sequence. The default separator is an empty string.
971976
/// - Returns: A single, concatenated string.
972-
@inlinable // FIXME(sil-serialize-all)
977+
@_specialize(where Self == Array<Substring>)
978+
@_specialize(where Self == Array<String>)
973979
public func joined(separator: String = "") -> String {
974980
return _joined(separator: separator)
975981
}
976982

977-
@inlinable // FIXME(sil-serialize-all)
978983
internal func _joined(separator: String = "") -> String {
979984
let separatorSize = separator._guts.count
980985
var width = separator._guts.byteWidth
@@ -1030,7 +1035,7 @@ extension BidirectionalCollection where Iterator.Element == String {
10301035
/// - Parameter separator: A string to insert between each of the elements
10311036
/// in this sequence. The default separator is an empty string.
10321037
/// - Returns: A single, concatenated string.
1033-
@inlinable // FIXME(sil-serialize-all)
1038+
@_specialize(where Self == Array<String>)
10341039
public func joined(separator: String = "") -> String {
10351040
return _joined(separator: separator)
10361041
}
@@ -1045,7 +1050,6 @@ internal func _stdlib_NSStringLowercaseString(_ str: AnyObject) -> _CocoaString
10451050
@_silgen_name("swift_stdlib_NSStringUppercaseString")
10461051
internal func _stdlib_NSStringUppercaseString(_ str: AnyObject) -> _CocoaString
10471052
#else
1048-
@inlinable // FIXME(sil-serialize-all)
10491053
internal func _nativeUnicodeLowercaseString(_ str: String) -> String {
10501054

10511055
// TODO (TODO: JIRA): check for small
@@ -1076,7 +1080,6 @@ internal func _nativeUnicodeLowercaseString(_ str: String) -> String {
10761080
return String(_largeStorage: storage)
10771081
}
10781082

1079-
@inlinable // FIXME(sil-serialize-all)
10801083
@usableFromInline // FIXME(sil-serialize-all)
10811084
internal func _nativeUnicodeUppercaseString(_ str: String) -> String {
10821085

@@ -1146,7 +1149,6 @@ extension String {
11461149
/// - Returns: A lowercase copy of the string.
11471150
///
11481151
/// - Complexity: O(*n*)
1149-
@inlinable // FIXME(sil-serialize-all)
11501152
public func lowercased() -> String {
11511153
if _guts.isASCII {
11521154
var guts = _guts
@@ -1194,7 +1196,6 @@ extension String {
11941196
/// - Returns: An uppercase copy of the string.
11951197
///
11961198
/// - Complexity: O(*n*)
1197-
@inlinable // FIXME(sil-serialize-all)
11981199
public func uppercased() -> String {
11991200
if _guts.isASCII {
12001201
var guts = _guts

stdlib/public/core/StringCharacterView.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,15 +330,13 @@ extension String._CharacterView : RangeReplaceableCollection {
330330
/// to allocate.
331331
///
332332
/// - Complexity: O(*n*), where *n* is the capacity being reserved.
333-
@inlinable // FIXME(sil-serialize-all)
334333
public mutating func reserveCapacity(_ n: Int) {
335334
_base.reserveCapacity(n)
336335
}
337336

338337
/// Appends the given character to the character view.
339338
///
340339
/// - Parameter c: The character to append to the character view.
341-
@inlinable // FIXME(sil-serialize-all)
342340
public mutating func append(_ c: Character) {
343341
_base.append(c)
344342
}
@@ -368,7 +366,6 @@ extension String._CharacterView {
368366
///
369367
/// - Complexity: O(*n*) if the underlying string is bridged from
370368
/// Objective-C, where *n* is the length of the string; otherwise, O(1).
371-
@inlinable // FIXME(sil-serialize-all)
372369
@available(swift, deprecated: 3.2, message:
373370
"Please use String or Substring directly")
374371
public subscript(bounds: Range<Index>) -> String.CharacterView {

0 commit comments

Comments
 (0)