@@ -156,6 +156,10 @@ internal struct _Buffer72 {
156
156
#if !(os(Windows) || os(Android)) && (arch(i386) || arch(x86_64))
157
157
% end
158
158
159
+ // Returns a UInt64, but that value is the length of the string, so it's
160
+ // guaranteed to fit into an Int. This is part of the ABI, so we can't
161
+ // trivially change it to Int. Callers can safely convert the result
162
+ // to any integer type without checks, however.
159
163
@_silgen_name ( " swift_float${bits}ToString " )
160
164
internal func _float${ bits} ToStringImpl(
161
165
_ buffer: UnsafeMutablePointer < UTF8 . CodeUnit > ,
@@ -168,9 +172,9 @@ internal func _float${bits}ToString(
168
172
) -> ( buffer: _Buffer32, length: Int) {
169
173
_internalInvariant ( MemoryLayout< _Buffer32> . size == 32 )
170
174
var buffer = _Buffer32 ( )
171
- let length = buffer. withBytes { ( bufferPtr) in
172
- Int ( _float ${ bits} ToStringImpl( bufferPtr, 32 , value, debug) )
173
- }
175
+ let length = buffer. withBytes { ( bufferPtr) in Int (
176
+ truncatingIfNeeded : _float ${ bits} ToStringImpl( bufferPtr, 32 , value, debug)
177
+ ) }
174
178
return ( buffer, length)
175
179
}
176
180
@@ -180,6 +184,10 @@ internal func _float${bits}ToString(
180
184
181
185
% end
182
186
187
+ // Returns a UInt64, but that value is the length of the string, so it's
188
+ // guaranteed to fit into an Int. This is part of the ABI, so we can't
189
+ // trivially change it to Int. Callers can safely convert the result
190
+ // to any integer type without checks, however.
183
191
@_silgen_name ( " swift_int64ToString " )
184
192
internal func _int64ToStringImpl(
185
193
_ buffer: UnsafeMutablePointer < UTF8 . CodeUnit > ,
@@ -193,22 +201,26 @@ internal func _int64ToString(
193
201
if radix >= 10 {
194
202
var buffer = _Buffer32 ( )
195
203
return buffer. withBytes { ( bufferPtr) in
196
- let actualLength
197
- = _int64ToStringImpl ( bufferPtr , 32 , value , radix , uppercase )
198
- return String . _fromASCII (
199
- UnsafeBufferPointer ( start : bufferPtr , count : Int ( actualLength ) ) )
204
+ let actualLength = _int64ToStringImpl ( bufferPtr , 32 , value , radix , uppercase )
205
+ return String . _fromASCII ( UnsafeBufferPointer (
206
+ start : bufferPtr , count : Int ( truncatingIfNeeded : actualLength )
207
+ ) )
200
208
}
201
209
} else {
202
210
var buffer = _Buffer72 ( )
203
211
return buffer. withBytes { ( bufferPtr) in
204
- let actualLength
205
- = _int64ToStringImpl ( bufferPtr , 72 , value , radix , uppercase )
206
- return String . _fromASCII (
207
- UnsafeBufferPointer ( start : bufferPtr , count : Int ( actualLength ) ) )
212
+ let actualLength = _int64ToStringImpl ( bufferPtr , 72 , value , radix , uppercase )
213
+ return String . _fromASCII ( UnsafeBufferPointer (
214
+ start : bufferPtr , count : Int ( truncatingIfNeeded : actualLength )
215
+ ) )
208
216
}
209
217
}
210
218
}
211
219
220
+ // Returns a UInt64, but that value is the length of the string, so it's
221
+ // guaranteed to fit into an Int. This is part of the ABI, so we can't
222
+ // trivially change it to Int. Callers can safely convert the result
223
+ // to any integer type without checks, however.
212
224
@_silgen_name ( " swift_uint64ToString " )
213
225
internal func _uint64ToStringImpl(
214
226
_ buffer: UnsafeMutablePointer < UTF8 . CodeUnit > ,
@@ -222,18 +234,18 @@ func _uint64ToString(
222
234
if radix >= 10 {
223
235
var buffer = _Buffer32 ( )
224
236
return buffer. withBytes { ( bufferPtr) in
225
- let actualLength
226
- = _uint64ToStringImpl ( bufferPtr , 32 , value , radix , uppercase )
227
- return String . _fromASCII (
228
- UnsafeBufferPointer ( start : bufferPtr , count : Int ( actualLength ) ) )
237
+ let actualLength = _uint64ToStringImpl ( bufferPtr , 32 , value , radix , uppercase )
238
+ return String . _fromASCII ( UnsafeBufferPointer (
239
+ start : bufferPtr , count : Int ( truncatingIfNeeded : actualLength )
240
+ ) )
229
241
}
230
242
} else {
231
243
var buffer = _Buffer72 ( )
232
244
return buffer. withBytes { ( bufferPtr) in
233
- let actualLength
234
- = _uint64ToStringImpl ( bufferPtr , 72 , value , radix , uppercase )
235
- return String . _fromASCII (
236
- UnsafeBufferPointer ( start : bufferPtr , count : Int ( actualLength ) ) )
245
+ let actualLength = _uint64ToStringImpl ( bufferPtr , 72 , value , radix , uppercase )
246
+ return String . _fromASCII ( UnsafeBufferPointer (
247
+ start : bufferPtr , count : Int ( truncatingIfNeeded : actualLength )
248
+ ) )
237
249
}
238
250
}
239
251
}
0 commit comments