Skip to content

[4.2] Small Strings cherry-picked into 4.2 #15553

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 6 commits into from
Mar 29, 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
4 changes: 2 additions & 2 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -588,10 +588,10 @@ func _stdlib_getline() -> String? {
if result.isEmpty {
return nil
}
return String._fromWellFormedCodeUnitSequence(UTF8.self, input: result)
return String(decoding: result, as: UTF8.self)
}
if c == CInt(Unicode.Scalar("\n").value) {
return String._fromWellFormedCodeUnitSequence(UTF8.self, input: result)
return String(decoding: result, as: UTF8.self)
}
result.append(UInt8(c))
}
Expand Down
8 changes: 4 additions & 4 deletions stdlib/private/SwiftPrivate/IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public struct _FDInputStream {
public mutating func getline() -> String? {
if let newlineIndex =
_buffer[0..<_bufferUsed].index(of: UInt8(Unicode.Scalar("\n").value)) {
let result = String._fromWellFormedCodeUnitSequence(
UTF8.self, input: _buffer[0..<newlineIndex])
let result = String._fromWellFormedUTF8CodeUnitSequence(
_buffer[0..<newlineIndex])
_buffer.removeSubrange(0...newlineIndex)
_bufferUsed -= newlineIndex + 1
return result
}
if isEOF && _bufferUsed > 0 {
let result = String._fromWellFormedCodeUnitSequence(
UTF8.self, input: _buffer[0..<_bufferUsed])
let result = String._fromWellFormedUTF8CodeUnitSequence(
_buffer[0..<_bufferUsed])
_buffer.removeAll()
_bufferUsed = 0
return result
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ set(SWIFTLIB_ESSENTIAL
ShadowProtocols.swift
Shims.swift
Slice.swift
SmallString.swift
Sort.swift.gyb
StaticString.swift
Stride.swift.gyb
Expand Down
66 changes: 50 additions & 16 deletions stdlib/public/core/CString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ extension String {
/// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
@_inlineable // FIXME(sil-serialize-all)
public init(cString: UnsafePointer<CChar>) {
let len = UTF8._nullCodeUnitOffset(in: cString)
let (result, _) = cString.withMemoryRebound(to: UInt8.self, capacity: len) {
_decodeCString(
$0, as: UTF8.self, length: len, repairingInvalidCodeUnits: true)!
}
self = result
self = _decodeValidCString(cString, repair: true)
}

/// Creates a new string by copying the null-terminated UTF-8 data referenced
Expand All @@ -60,8 +55,7 @@ extension String {
/// unsigned sequence of bytes.
@_inlineable // FIXME(sil-serialize-all)
public init(cString: UnsafePointer<UInt8>) {
self = String.decodeCString(
cString, as: UTF8.self, repairingInvalidCodeUnits: true)!.result
self = _decodeValidCString(cString, repair: true)
}

/// Creates a new string by copying and validating the null-terminated UTF-8
Expand Down Expand Up @@ -92,16 +86,10 @@ extension String {
/// - Parameter cString: A pointer to a null-terminated UTF-8 code sequence.
@_inlineable // FIXME(sil-serialize-all)
public init?(validatingUTF8 cString: UnsafePointer<CChar>) {
let len = UTF8._nullCodeUnitOffset(in: cString)
guard let (result, _) =
cString.withMemoryRebound(to: UInt8.self, capacity: len, {
_decodeCString($0, as: UTF8.self, length: len,
repairingInvalidCodeUnits: false)
})
else {
guard let str = _decodeCString(cString, repair: false) else {
return nil
}
self = result
self = str
}

/// Creates a new string by copying the null-terminated data referenced by
Expand Down Expand Up @@ -182,6 +170,52 @@ public func _persistCString(_ p: UnsafePointer<CChar>?) -> [CChar]? {
return result
}

@_inlineable
@_versioned
internal func _decodeValidCString(
_ cString: UnsafePointer<Int8>, repair: Bool
) -> String {
let len = UTF8._nullCodeUnitOffset(in: cString)
return cString.withMemoryRebound(to: UInt8.self, capacity: len) {
(ptr: UnsafePointer<UInt8>) -> String in
let bufPtr = UnsafeBufferPointer(start: ptr, count: len)
return String._fromWellFormedUTF8CodeUnitSequence(bufPtr, repair: repair)
}
}

@_inlineable
@_versioned
internal func _decodeValidCString(
_ cString: UnsafePointer<UInt8>, repair: Bool
) -> String {
let len = UTF8._nullCodeUnitOffset(in: cString)
let bufPtr = UnsafeBufferPointer(start: cString, count: len)
return String._fromWellFormedUTF8CodeUnitSequence(bufPtr, repair: repair)
}

@_inlineable
@_versioned
internal func _decodeCString(
_ cString: UnsafePointer<Int8>, repair: Bool
) -> String? {
let len = UTF8._nullCodeUnitOffset(in: cString)
return cString.withMemoryRebound(to: UInt8.self, capacity: len) {
(ptr: UnsafePointer<UInt8>) -> String? in
let bufPtr = UnsafeBufferPointer(start: ptr, count: len)
return String._fromUTF8CodeUnitSequence(bufPtr, repair: repair)
}
}

@_inlineable
@_versioned
internal func _decodeCString(
_ cString: UnsafePointer<UInt8>, repair: Bool
) -> String? {
let len = UTF8._nullCodeUnitOffset(in: cString)
let bufPtr = UnsafeBufferPointer(start: cString, count: len)
return String._fromUTF8CodeUnitSequence(bufPtr, repair: repair)
}

/// Creates a new string by copying the null-terminated data referenced by
/// the given pointer using the specified encoding.
///
Expand Down
9 changes: 7 additions & 2 deletions stdlib/public/core/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,15 @@ extension String {
@_inlineable // FIXME(sil-serialize-all)
public init(_ c: Character) {
if let utf16 = c._smallUTF16 {
self = String(decoding: utf16, as: Unicode.UTF16.self)
if let small = _SmallUTF8String(utf16) {
self = String(_StringGuts(small))
} else {
// FIXME: Remove when we support UTF-8 in small string
self = String(decoding: utf16, as: Unicode.UTF16.self)
}
}
else {
// TODO(SSO): small check
// TODO(SSO): small check. For now, since we only do ASCII, this won't hit
self = String(_StringGuts(_large: c._largeUTF16!))
}
}
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"CharacterUnicodeScalars.swift",
"ICU.swift",
"NormalizedCodeUnitIterator.swift",
"SmallString.swift",
"StaticString.swift",
"String.swift",
"StringBridge.swift",
Expand Down
7 changes: 3 additions & 4 deletions stdlib/public/core/InputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,9 @@ public func readLine(strippingNewline: Bool = true) -> String? {
}
}
}
let result = String._fromCodeUnitSequenceWithRepair(UTF8.self,
input: UnsafeMutableBufferPointer(
start: linePtr,
count: readBytes)).0
let result = String._fromUTF8CodeUnitSequence(
UnsafeMutableBufferPointer(start: linePtr, count: readBytes),
repair: true)!
_stdlib_free(linePtr)
return result
}
3 changes: 1 addition & 2 deletions stdlib/public/core/Integers.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1802,8 +1802,7 @@ extension BinaryInteger {
if isNegative {
result.append(UInt8(("-" as Unicode.Scalar).value))
}
return String._fromWellFormedCodeUnitSequence(
UTF8.self, input: result.reversed())
return String._fromASCII(result.reversed())
}

/// A textual representation of this value.
Expand Down
3 changes: 1 addition & 2 deletions stdlib/public/core/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public func _getTypeName(_ type: Any.Type, qualified: Bool)
public // @testable
func _typeName(_ type: Any.Type, qualified: Bool = true) -> String {
let (stringPtr, count) = _getTypeName(type, qualified: qualified)
return ._fromWellFormedCodeUnitSequence(UTF8.self,
input: UnsafeBufferPointer(start: stringPtr, count: count))
return ._fromASCII(UnsafeBufferPointer(start: stringPtr, count: count))
}

/// Lookup a class given a name. Until the demangled encoding of type
Expand Down
25 changes: 10 additions & 15 deletions stdlib/public/core/Runtime.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,8 @@ internal func _float${bits}ToString(
var buffer = _Buffer32()
return buffer.withBytes { (bufferPtr) in
let actualLength = _float${bits}ToStringImpl(bufferPtr, 32, value, debug)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
return String._fromASCII(
UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
}
}

Expand Down Expand Up @@ -467,18 +466,16 @@ internal func _int64ToString(
return buffer.withBytes { (bufferPtr) in
let actualLength
= _int64ToStringImpl(bufferPtr, 32, value, radix, uppercase)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
return String._fromASCII(
UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
}
} else {
var buffer = _Buffer72()
return buffer.withBytes { (bufferPtr) in
let actualLength
= _int64ToStringImpl(bufferPtr, 72, value, radix, uppercase)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
return String._fromASCII(
UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
}
}
}
Expand All @@ -501,18 +498,16 @@ func _uint64ToString(
return buffer.withBytes { (bufferPtr) in
let actualLength
= _uint64ToStringImpl(bufferPtr, 32, value, radix, uppercase)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
return String._fromASCII(
UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
}
} else {
var buffer = _Buffer72()
return buffer.withBytes { (bufferPtr) in
let actualLength
= _uint64ToStringImpl(bufferPtr, 72, value, radix, uppercase)
return String._fromWellFormedCodeUnitSequence(
UTF8.self,
input: UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
return String._fromASCII(
UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
}
}
}
Expand Down
Loading