Skip to content

Commit 02a8d10

Browse files
Add Swift 3 compatibility versions of String.init for Signed/UnsignedInteger (#8926) (#8934)
1 parent 9be0234 commit 02a8d10

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

stdlib/public/core/StringLegacy.swift

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,45 @@ extension String {
273273
Int64(value), radix: Int64(radix), uppercase: uppercase)
274274
}
275275

276+
/// Creates a string representing the given value in base 10, or some other
277+
/// specified base.
278+
///
279+
/// The following example converts the maximal `Int` value to a string and
280+
/// prints its length:
281+
///
282+
/// let max = String(Int.max)
283+
/// print("\(max) has \(max.utf16.count) digits.")
284+
/// // Prints "9223372036854775807 has 19 digits."
285+
///
286+
/// Numerals greater than 9 are represented as Roman letters. These letters
287+
/// start with `"A"` if `uppercase` is `true`; otherwise, with `"a"`.
288+
///
289+
/// let v = 999_999
290+
/// print(String(v, radix: 2))
291+
/// // Prints "11110100001000111111"
292+
///
293+
/// print(String(v, radix: 16))
294+
/// // Prints "f423f"
295+
/// print(String(v, radix: 16, uppercase: true))
296+
/// // Prints "F423F"
297+
///
298+
/// - Parameters:
299+
/// - value: The value to convert to a string.
300+
/// - radix: The base to use for the string representation. `radix` must be
301+
/// at least 2 and at most 36. The default is 10.
302+
/// - uppercase: Pass `true` to use uppercase letters to represent numerals
303+
/// greater than 9, or `false` to use lowercase letters. The default is
304+
/// `false`.
305+
// FIXME(integers): tiebreaker between T : FixedWidthInteger and other obsoleted
306+
@available(swift, obsoleted: 4)
307+
public init<T : FixedWidthInteger>(
308+
_ value: T, radix: Int = 10, uppercase: Bool = false
309+
) where T : SignedInteger {
310+
_precondition(radix > 1, "Radix must be greater than 1")
311+
self = _int64ToString(
312+
Int64(value), radix: Int64(radix), uppercase: uppercase)
313+
}
314+
276315
/// Creates a string representing the given value in base 10, or some other
277316
/// specified base.
278317
///
@@ -310,6 +349,82 @@ extension String {
310349
self = _uint64ToString(
311350
UInt64(value), radix: Int64(radix), uppercase: uppercase)
312351
}
352+
353+
/// Creates a string representing the given value in base 10, or some other
354+
/// specified base.
355+
///
356+
/// The following example converts the maximal `Int` value to a string and
357+
/// prints its length:
358+
///
359+
/// let max = String(Int.max)
360+
/// print("\(max) has \(max.utf16.count) digits.")
361+
/// // Prints "9223372036854775807 has 19 digits."
362+
///
363+
/// Numerals greater than 9 are represented as Roman letters. These letters
364+
/// start with `"A"` if `uppercase` is `true`; otherwise, with `"a"`.
365+
///
366+
/// let v = 999_999
367+
/// print(String(v, radix: 2))
368+
/// // Prints "11110100001000111111"
369+
///
370+
/// print(String(v, radix: 16))
371+
/// // Prints "f423f"
372+
/// print(String(v, radix: 16, uppercase: true))
373+
/// // Prints "F423F"
374+
///
375+
/// - Parameters:
376+
/// - value: The value to convert to a string.
377+
/// - radix: The base to use for the string representation. `radix` must be
378+
/// at least 2 and at most 36. The default is 10.
379+
/// - uppercase: Pass `true` to use uppercase letters to represent numerals
380+
/// greater than 9, or `false` to use lowercase letters. The default is
381+
/// `false`.
382+
@available(swift, obsoleted: 4, message: "Please use the version for FixedWidthInteger instead.")
383+
public init<T : _SignedInteger>(
384+
_ value: T, radix: Int = 10, uppercase: Bool = false
385+
) {
386+
_precondition(radix > 1, "Radix must be greater than 1")
387+
self = _int64ToString(
388+
value.toIntMax(), radix: Int64(radix), uppercase: uppercase)
389+
}
390+
391+
/// Creates a string representing the given value in base 10, or some other
392+
/// specified base.
393+
///
394+
/// The following example converts the maximal `Int` value to a string and
395+
/// prints its length:
396+
///
397+
/// let max = String(Int.max)
398+
/// print("\(max) has \(max.utf16.count) digits.")
399+
/// // Prints "9223372036854775807 has 19 digits."
400+
///
401+
/// Numerals greater than 9 are represented as Roman letters. These letters
402+
/// start with `"A"` if `uppercase` is `true`; otherwise, with `"a"`.
403+
///
404+
/// let v: UInt = 999_999
405+
/// print(String(v, radix: 2))
406+
/// // Prints "11110100001000111111"
407+
///
408+
/// print(String(v, radix: 16))
409+
/// // Prints "f423f"
410+
/// print(String(v, radix: 16, uppercase: true))
411+
/// // Prints "F423F"
412+
///
413+
/// - Parameters:
414+
/// - value: The value to convert to a string.
415+
/// - radix: The base to use for the string representation. `radix` must be
416+
/// at least 2 and at most 36. The default is 10.
417+
/// - uppercase: Pass `true` to use uppercase letters to represent numerals
418+
/// greater than 9, or `false` to use lowercase letters. The default is
419+
/// `false`.
420+
@available(swift, obsoleted: 4, message: "Please use the version for FixedWidthInteger instead.")
421+
public init<T : UnsignedInteger>(
422+
_ value: T, radix: Int = 10, uppercase: Bool = false
423+
) {
424+
_precondition(radix > 1, "Radix must be greater than 1")
425+
self = _uint64ToString(
426+
value.toUIntMax(), radix: Int64(radix), uppercase: uppercase)
427+
}
313428
}
314429

315430
extension String {

0 commit comments

Comments
 (0)