Skip to content

Use the truncatingIfNeeded init on the result of these runtime funcs. #28426

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
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
50 changes: 31 additions & 19 deletions stdlib/public/core/Runtime.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ internal struct _Buffer72 {
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
% end

// Returns a UInt64, but that value is the length of the string, so it's
// guaranteed to fit into an Int. This is part of the ABI, so we can't
// trivially change it to Int. Callers can safely convert the result
// to any integer type without checks, however.
@_silgen_name("swift_float${bits}ToString")
internal func _float${bits}ToStringImpl(
_ buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
Expand All @@ -168,9 +172,9 @@ internal func _float${bits}ToString(
) -> (buffer: _Buffer32, length: Int) {
_internalInvariant(MemoryLayout<_Buffer32>.size == 32)
var buffer = _Buffer32()
let length = buffer.withBytes { (bufferPtr) in
Int(_float${bits}ToStringImpl(bufferPtr, 32, value, debug))
}
let length = buffer.withBytes { (bufferPtr) in Int(
truncatingIfNeeded: _float${bits}ToStringImpl(bufferPtr, 32, value, debug)
)}
return (buffer, length)
}

Expand All @@ -180,6 +184,10 @@ internal func _float${bits}ToString(

% end

// Returns a UInt64, but that value is the length of the string, so it's
// guaranteed to fit into an Int. This is part of the ABI, so we can't
// trivially change it to Int. Callers can safely convert the result
// to any integer type without checks, however.
@_silgen_name("swift_int64ToString")
internal func _int64ToStringImpl(
_ buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
Expand All @@ -193,22 +201,26 @@ internal func _int64ToString(
if radix >= 10 {
var buffer = _Buffer32()
return buffer.withBytes { (bufferPtr) in
let actualLength
= _int64ToStringImpl(bufferPtr, 32, value, radix, uppercase)
return String._fromASCII(
UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
let actualLength = _int64ToStringImpl(bufferPtr, 32, value, radix, uppercase)
return String._fromASCII(UnsafeBufferPointer(
start: bufferPtr, count: Int(truncatingIfNeeded: actualLength)
))
}
} else {
var buffer = _Buffer72()
return buffer.withBytes { (bufferPtr) in
let actualLength
= _int64ToStringImpl(bufferPtr, 72, value, radix, uppercase)
return String._fromASCII(
UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
let actualLength = _int64ToStringImpl(bufferPtr, 72, value, radix, uppercase)
return String._fromASCII(UnsafeBufferPointer(
start: bufferPtr, count: Int(truncatingIfNeeded: actualLength)
))
}
}
}

// Returns a UInt64, but that value is the length of the string, so it's
// guaranteed to fit into an Int. This is part of the ABI, so we can't
// trivially change it to Int. Callers can safely convert the result
// to any integer type without checks, however.
@_silgen_name("swift_uint64ToString")
internal func _uint64ToStringImpl(
_ buffer: UnsafeMutablePointer<UTF8.CodeUnit>,
Expand All @@ -222,18 +234,18 @@ func _uint64ToString(
if radix >= 10 {
var buffer = _Buffer32()
return buffer.withBytes { (bufferPtr) in
let actualLength
= _uint64ToStringImpl(bufferPtr, 32, value, radix, uppercase)
return String._fromASCII(
UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
let actualLength = _uint64ToStringImpl(bufferPtr, 32, value, radix, uppercase)
return String._fromASCII(UnsafeBufferPointer(
start: bufferPtr, count: Int(truncatingIfNeeded: actualLength)
))
}
} else {
var buffer = _Buffer72()
return buffer.withBytes { (bufferPtr) in
let actualLength
= _uint64ToStringImpl(bufferPtr, 72, value, radix, uppercase)
return String._fromASCII(
UnsafeBufferPointer(start: bufferPtr, count: Int(actualLength)))
let actualLength = _uint64ToStringImpl(bufferPtr, 72, value, radix, uppercase)
return String._fromASCII(UnsafeBufferPointer(
start: bufferPtr, count: Int(truncatingIfNeeded: actualLength)
))
}
}
}
Expand Down