Skip to content

[String] Drop many @inlinable annotations #16578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions stdlib/private/SwiftPrivate/IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ public struct _FDInputStream {
public mutating func getline() -> String? {
if let newlineIndex =
_buffer[0..<_bufferUsed].index(of: UInt8(Unicode.Scalar("\n").value)) {
let result = String._fromWellFormedUTF8CodeUnitSequence(
_buffer[0..<newlineIndex])
let result = String(decoding: _buffer[0..<newlineIndex], as: UTF8.self)
_buffer.removeSubrange(0...newlineIndex)
_bufferUsed -= newlineIndex + 1
return result
}
if isEOF && _bufferUsed > 0 {
let result = String._fromWellFormedUTF8CodeUnitSequence(
_buffer[0..<_bufferUsed])
let result = String(decoding: _buffer[0..<_bufferUsed], as: UTF8.self)
_buffer.removeAll()
_bufferUsed = 0
return result
Expand Down
12 changes: 2 additions & 10 deletions stdlib/public/core/CString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ extension String {
/// // Prints "Caf�"
///
/// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
@inlinable // FIXME(sil-serialize-all)
public init(cString: UnsafePointer<CChar>) {
self = _decodeValidCString(cString, repair: true)
}
Expand All @@ -53,7 +52,6 @@ extension String {
///
/// This is identical to init(cString: UnsafePointer<CChar> but operates on an
/// unsigned sequence of bytes.
@inlinable // FIXME(sil-serialize-all)
public init(cString: UnsafePointer<UInt8>) {
self = _decodeValidCString(cString, repair: true)
}
Expand Down Expand Up @@ -84,7 +82,6 @@ extension String {
/// // Prints "nil"
///
/// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
@inlinable // FIXME(sil-serialize-all)
public init?(validatingUTF8 cString: UnsafePointer<CChar>) {
guard let str = _decodeCString(cString, repair: false) else {
return nil
Expand Down Expand Up @@ -134,7 +131,8 @@ extension String {
/// - Returns: A tuple with the new string and a Boolean value that indicates
/// whether any repairs were made. If `isRepairing` is `false` and an
/// ill-formed sequence is detected, this method returns `nil`.
@inlinable // FIXME(sil-serialize-all)
@_specialize(where Encoding == Unicode.UTF8)
@_specialize(where Encoding == Unicode.UTF16)
public static func decodeCString<Encoding : _UnicodeEncoding>(
_ cString: UnsafePointer<Encoding.CodeUnit>?,
as encoding: Encoding.Type,
Expand All @@ -157,7 +155,6 @@ extension String {
/// From a non-`nil` `UnsafePointer` to a null-terminated string
/// with possibly-transient lifetime, create a null-terminated array of 'C' char.
/// Returns `nil` if passed a null pointer.
@inlinable // FIXME(sil-serialize-all)
public func _persistCString(_ p: UnsafePointer<CChar>?) -> [CChar]? {
guard let s = p else {
return nil
Expand All @@ -170,7 +167,6 @@ public func _persistCString(_ p: UnsafePointer<CChar>?) -> [CChar]? {
return result
}

@inlinable
internal func _decodeValidCString(
_ cString: UnsafePointer<Int8>, repair: Bool
) -> String {
Expand All @@ -182,7 +178,6 @@ internal func _decodeValidCString(
}
}

@inlinable
internal func _decodeValidCString(
_ cString: UnsafePointer<UInt8>, repair: Bool
) -> String {
Expand All @@ -191,7 +186,6 @@ internal func _decodeValidCString(
return String._fromWellFormedUTF8CodeUnitSequence(bufPtr, repair: repair)
}

@inlinable
internal func _decodeCString(
_ cString: UnsafePointer<Int8>, repair: Bool
) -> String? {
Expand All @@ -203,7 +197,6 @@ internal func _decodeCString(
}
}

@inlinable
internal func _decodeCString(
_ cString: UnsafePointer<UInt8>, repair: Bool
) -> String? {
Expand All @@ -216,7 +209,6 @@ internal func _decodeCString(
/// the given pointer using the specified encoding.
///
/// This internal helper takes the string length as an argument.
@inlinable // FIXME(sil-serialize-all)
internal func _decodeCString<Encoding : _UnicodeEncoding>(
_ cString: UnsafePointer<Encoding.CodeUnit>,
as encoding: Encoding.Type, length: Int,
Expand Down
3 changes: 1 addition & 2 deletions stdlib/public/core/InputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import SwiftShims
/// or character combinations are preserved. The default is `true`.
/// - Returns: The string of characters read from standard input. If EOF has
/// already been reached when `readLine()` is called, the result is `nil`.
@inlinable // FIXME(sil-serialize-all)
public func readLine(strippingNewline: Bool = true) -> String? {
var linePtrVar: UnsafeMutablePointer<UInt8>?
var readBytes = swift_stdlib_readLine_stdin(&linePtrVar)
Expand Down Expand Up @@ -66,7 +65,7 @@ public func readLine(strippingNewline: Bool = true) -> String? {
}
}
let result = String._fromUTF8CodeUnitSequence(
UnsafeMutableBufferPointer(start: linePtr, count: readBytes),
UnsafeBufferPointer(start: linePtr, count: readBytes),
repair: true)!
_stdlib_free(linePtr)
return result
Expand Down
10 changes: 9 additions & 1 deletion stdlib/public/core/Integers.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,10 @@ extension BinaryInteger {

let isNegative = Self.isSigned && self < (0 as Self)
var value = magnitude

// TODO(FIXME JIRA): All current stdlib types fit in small. Use a stack
// buffer instead of an array on the heap.

var result: [UInt8] = []
while value != 0 {
let (quotient, remainder) = _quotientAndRemainder(value)
Expand All @@ -1837,7 +1841,11 @@ extension BinaryInteger {
if isNegative {
result.append(UInt8(("-" as Unicode.Scalar).value))
}
return String._fromASCII(result.reversed())

result.reverse()
return result.withUnsafeBufferPointer {
return String._fromASCII($0)
}
}

/// A textual representation of this value.
Expand Down
29 changes: 21 additions & 8 deletions stdlib/public/core/SmallString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,20 @@ extension _SmallUTF8String {

@inlinable
public // @testable
init?<C: RandomAccessCollection>(_ codeUnits: C) where C.Element == UInt8 {
init?(_ codeUnits: UnsafeBufferPointer<UInt8>) {
#if arch(i386) || arch(arm)
return nil // Never form small strings on 32-bit
#else
let count = codeUnits.count
guard count <= _SmallUTF8String.capacity else { return nil }
self.init()
self._withAllUnsafeMutableBytes { rawBufPtr in
let bufPtr = UnsafeMutableBufferPointer(
start: rawBufPtr.baseAddress.unsafelyUnwrapped.assumingMemoryBound(
to: UInt8.self),
count: rawBufPtr.count)
var (itr, written) = codeUnits._copyContents(initializing: bufPtr)
_sanityCheck(itr.next() == nil)
_sanityCheck(count == written)
let rawDst = rawBufPtr.baseAddress._unsafelyUnwrappedUnchecked
memcpy_(
dst: rawDst.assumingMemoryBound(to: UInt8.self),
src: codeUnits.baseAddress._unsafelyUnwrappedUnchecked,
count: count
)
}
_sanityCheck(self.count == 0, "overwrote count early?")
self.count = count
Expand All @@ -152,6 +151,20 @@ extension _SmallUTF8String {
if !self.isASCII { return nil }

_invariantCheck()
#endif
}

@inlinable
public // @testable
init?(_ scalar: Unicode.Scalar) {
#if arch(i386) || arch(arm)
return nil // Never form small strings on 32-bit
#else
// FIXME: support transcoding
guard scalar.value <= 0x7F else { return nil }
self.init()
self.count = 1
self[0] = UInt8(truncatingIfNeeded: scalar.value)
#endif
}
}
Expand Down
55 changes: 28 additions & 27 deletions stdlib/public/core/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -625,38 +625,46 @@ extension String {
}
}

internal func _isAllASCII(_ input: UnsafeBufferPointer<UInt8>) -> Bool {
for byte in input {
guard byte <= 0x7F else { return false }
}
return true
}

extension String {
@inlinable
static func _fromUTF8CodeUnitSequence<C : RandomAccessCollection>(
_ input: C, repair: Bool
) -> String? where C.Element == UInt8 {
static func _fromUTF8CodeUnitSequence(
_ input: UnsafeBufferPointer<UInt8>, repair: Bool
) -> String? {
if _isAllASCII(input) {
return _fromASCII(input)
}

if let smol = _SmallUTF8String(input) {
return String(_StringGuts(smol))
}

return String._fromCodeUnits(
input, encoding: UTF8.self, repairIllFormedSequences: repair)
}

@inlinable
static func _fromASCII<C : RandomAccessCollection>(
_ input: C
) -> String where C.Element == UInt8 {
@usableFromInline
static func _fromASCII(_ input: UnsafeBufferPointer<UInt8>) -> String {
if let smol = _SmallUTF8String(input) {
return String(_StringGuts(smol))
}
let storage = _SwiftStringStorage<UInt8>.create(
capacity: input.count, count: input.count)
var (itr, end) = input._copyContents(initializing: storage.usedBuffer)
_sanityCheck(itr.next() == nil)
_sanityCheck(end == storage.usedBuffer.endIndex)
_sanityCheck(storage.count == input.count)
storage.start.initialize(
from: input.baseAddress._unsafelyUnwrappedUnchecked, count: input.count)
return String(_StringGuts(_large: storage))
}

@inlinable // FIXME(sil-serialize-all)
public // FIXME: @usableFromInline, currently public because testing...
static func _fromWellFormedUTF8CodeUnitSequence<C : RandomAccessCollection>(
_ input: C, repair: Bool = false
) -> String where C.Element == UTF8.CodeUnit {
@usableFromInline
static func _fromWellFormedUTF8CodeUnitSequence(
_ input: UnsafeBufferPointer<UInt8>, repair: Bool = false
) -> String {
return String._fromUTF8CodeUnitSequence(input, repair: repair)!
}
}
Expand All @@ -674,9 +682,7 @@ extension String : _ExpressibleByBuiltinUnicodeScalarLiteral {
//
// TODO: All scalars are small
if scalar.value <= 0x7f {
if let small = _SmallUTF8String(
Unicode.UTF8.encode(scalar)._unsafelyUnwrappedUnchecked
) {
if let small = _SmallUTF8String(scalar) {
self = String(_StringGuts(small))
return
} else {
Expand Down Expand Up @@ -893,7 +899,6 @@ extension String {
/// // Prints "Hello, friend"
///
/// - Parameter other: Another string.
@inlinable // FIXME(sil-serialize-all)
public mutating func append(_ other: String) {
self._guts.append(other._guts)
}
Expand Down Expand Up @@ -969,12 +974,12 @@ extension Sequence where Element: StringProtocol {
/// - Parameter separator: A string to insert between each of the elements
/// in this sequence. The default separator is an empty string.
/// - Returns: A single, concatenated string.
@inlinable // FIXME(sil-serialize-all)
@_specialize(where Self == Array<Substring>)
@_specialize(where Self == Array<String>)
public func joined(separator: String = "") -> String {
return _joined(separator: separator)
}

@inlinable // FIXME(sil-serialize-all)
internal func _joined(separator: String = "") -> String {
let separatorSize = separator._guts.count
var width = separator._guts.byteWidth
Expand Down Expand Up @@ -1030,7 +1035,7 @@ extension BidirectionalCollection where Iterator.Element == String {
/// - Parameter separator: A string to insert between each of the elements
/// in this sequence. The default separator is an empty string.
/// - Returns: A single, concatenated string.
@inlinable // FIXME(sil-serialize-all)
@_specialize(where Self == Array<String>)
public func joined(separator: String = "") -> String {
return _joined(separator: separator)
}
Expand All @@ -1045,7 +1050,6 @@ internal func _stdlib_NSStringLowercaseString(_ str: AnyObject) -> _CocoaString
@_silgen_name("swift_stdlib_NSStringUppercaseString")
internal func _stdlib_NSStringUppercaseString(_ str: AnyObject) -> _CocoaString
#else
@inlinable // FIXME(sil-serialize-all)
internal func _nativeUnicodeLowercaseString(_ str: String) -> String {

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

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

Expand Down Expand Up @@ -1146,7 +1149,6 @@ extension String {
/// - Returns: A lowercase copy of the string.
///
/// - Complexity: O(*n*)
@inlinable // FIXME(sil-serialize-all)
public func lowercased() -> String {
if _guts.isASCII {
var guts = _guts
Expand Down Expand Up @@ -1194,7 +1196,6 @@ extension String {
/// - Returns: An uppercase copy of the string.
///
/// - Complexity: O(*n*)
@inlinable // FIXME(sil-serialize-all)
public func uppercased() -> String {
if _guts.isASCII {
var guts = _guts
Expand Down
3 changes: 0 additions & 3 deletions stdlib/public/core/StringCharacterView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,13 @@ extension String._CharacterView : RangeReplaceableCollection {
/// to allocate.
///
/// - Complexity: O(*n*), where *n* is the capacity being reserved.
@inlinable // FIXME(sil-serialize-all)
public mutating func reserveCapacity(_ n: Int) {
_base.reserveCapacity(n)
}

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