Skip to content

[stdlib] Conform fixed-width integer types to LosslessStringConvertible #11193

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
Jul 29, 2017
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
28 changes: 26 additions & 2 deletions stdlib/public/core/IntegerParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ extension FixedWidthInteger {
///
/// The string passed as `text` may begin with a plus or minus sign character
/// (`+` or `-`), followed by one or more numeric digits (`0-9`) or letters
/// (`a-z` or `A-Z`). The string is case insensitive.
/// (`a-z` or `A-Z`). Parsing of the string is case insensitive.
///
/// let x = Int("123")
/// // x == 123
Expand All @@ -116,7 +116,7 @@ extension FixedWidthInteger {
/// // z == 123
///
/// If `text` is in an invalid format or contains characters that are out of
/// range for the given `radix`, or if the value it denotes in the given
/// bounds for the given `radix`, or if the value it denotes in the given
/// `radix` is not representable, the result is `nil`. For example, the
/// following conversions result in `nil`:
///
Expand Down Expand Up @@ -155,4 +155,28 @@ extension FixedWidthInteger {
guard _fastPath(result != nil) else { return nil }
self = result!
}

/// Creates a new integer value from the given string.
///
/// The string passed as `description` may begin with a plus or minus sign
/// character (`+` or `-`), followed by one or more numeric digits (`0-9`).
///
/// let x = Int("123")
/// // x == 123
///
/// If `description` is in an invalid format, or if the value it denotes in
/// base 10 is not representable, the result is `nil`. For example, the
/// following conversions result in `nil`:
///
/// Int(" 100") // Includes whitespace
/// Int("21-50") // Invalid format
/// Int("ff6600") // Characters out of bounds
/// Int("10000000000000000000000000") // Out of range
///
/// - Parameter description: The ASCII representation of a number.
@_semantics("optimize.sil.specialize.generic.partial.never")
@inline(__always)
public init?(_ description: String) {
self.init(description, radix: 10)
}
}
3 changes: 2 additions & 1 deletion stdlib/public/core/Integers.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1969,7 +1969,8 @@ public enum ArithmeticOverflow {
/// customization points for arithmetic operations. When you provide just those
/// methods, the standard library provides default implementations for all
/// other arithmetic methods and operators.
public protocol FixedWidthInteger : BinaryInteger, _BitwiseOperations
public protocol FixedWidthInteger :
BinaryInteger, LosslessStringConvertible, _BitwiseOperations
where Magnitude : FixedWidthInteger
{
/// The number of bits used for the underlying binary representation of
Expand Down
10 changes: 10 additions & 0 deletions test/stdlib/Integers.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,16 @@ tests.test("extendingOrTruncating") {
expectEqual(129, UInt8(extendingOrTruncating: 129 + 1024 as UDWord))
}

tests.test("Parsing/LosslessStringConvertible") {
func _toArray<T: LosslessStringConvertible>(_ text: String) -> [T] {
return text.split(separator: " ").map { T(String($0)) }.flatMap { $0 }
}

expectEqualSequence([1, 2, 3], _toArray("1 2 3") as [Int])
expectEqualSequence(
[Int](), _toArray("21-50 ff6600 10000000000000000000000000") as [Int])
}

tests.test("HeterogeneousEquality") {
expectTrue(-1 as DWord != UDWord.max)
expectTrue(DWord.max == UDWord.max / 2)
Expand Down