-
-
Notifications
You must be signed in to change notification settings - Fork 8
Double roundtripping #98
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
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f742668
make floating point types roundtrippable
tayloraswift 53b0ab6
Merge remote-tracking branch 'origin/master' into double-roundtripping
tayloraswift 593ec16
lint
tayloraswift ce79bf9
fix doc validation
tayloraswift 88387f3
strip useless markdown links
tayloraswift File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| extension JSON.Number { | ||
| /// This type is memory-efficient, and can store fixed-point numbers with | ||
| /// up to 64 bits of precision. It uses all 64 bits to encode its magnitude, | ||
| /// which enables it to round-trip integers of width up to ``UInt64``. | ||
| @frozen public struct Inline: Hashable, Equatable, Sendable { | ||
| // this layout should allow instances of `Number` to fit in 2 words. | ||
| // this is backed by an `Int`, but the swift compiler can optimize it | ||
| // into a `UInt8`-sized field | ||
|
|
||
| /// The sign of this numeric literal. | ||
| public var sign: FloatingPointSign | ||
| // cannot have an inlinable property wrapper | ||
| @usableFromInline internal var _places: UInt32 | ||
| /// The number of decimal places this numeric literal has. | ||
| /// | ||
| /// > Note: | ||
| /// > This property has type ``UInt64`` to facilitate computations with | ||
| /// ``units``. It is backed by a ``UInt32`` and can therefore only store | ||
| /// 32 bits of precision. | ||
| @inlinable public var places: UInt64 { | ||
| .init(self._places) | ||
| } | ||
| /// The magnitude of this numeric literal, expressed in units of ``places``. | ||
| /// | ||
| /// If ``units`` is `123`, and ``places`` is `2`, that would represent | ||
| /// a magnitude of `1.23`. | ||
| public var units: UInt64 | ||
| /// Creates a numeric literal. | ||
| /// - Parameters: | ||
| /// - sign: The sign, positive or negative. | ||
| /// - units: The magnitude, in units of `places`. | ||
| /// - places: The number of decimal places. | ||
| @inlinable public init(sign: FloatingPointSign, units: UInt64, places: UInt32 = 0) { | ||
| self.sign = sign | ||
| self.units = units | ||
| self._places = places | ||
| } | ||
| } | ||
| } | ||
| extension JSON.Number.Inline { | ||
| @inlinable public init<T>(_ signed: T) where T: SignedInteger { | ||
| self.init(sign: signed < 0 ? .minus : .plus, units: UInt64.init(signed.magnitude)) | ||
| } | ||
| @inlinable public init<T>(_ unsigned: T) where T: UnsignedInteger { | ||
| self.init(sign: .plus, units: UInt64.init(unsigned)) | ||
| } | ||
| } | ||
| extension JSON.Number.Inline { | ||
| @inlinable public func `as`<T>( | ||
| _: T.Type | ||
| ) -> T? where T: FixedWidthInteger & UnsignedInteger { | ||
| guard self.places == 0 else { | ||
| return nil | ||
| } | ||
| switch self.sign { | ||
| case .minus: | ||
| return self.units == 0 ? 0 : nil | ||
| case .plus: | ||
| return T.init(exactly: self.units) | ||
| } | ||
| } | ||
|
|
||
| @inlinable public func `as`<T>( | ||
| _: T.Type | ||
| ) -> T? where T: FixedWidthInteger & SignedInteger { | ||
| guard self.places == 0 else { | ||
| return nil | ||
| } | ||
| switch self.sign { | ||
| case .minus: | ||
| let negated: Int64 = .init(bitPattern: 0 &- self.units) | ||
| return negated <= 0 ? T.init(exactly: negated) : nil | ||
| case .plus: | ||
| return T.init(exactly: self.units) | ||
| } | ||
| } | ||
|
|
||
| @inlinable public func `as`<T>(_: (units: T, places: T).Type) -> (units: T, places: T)? | ||
| where T: FixedWidthInteger & SignedInteger { | ||
| guard let places: T = T.init(exactly: self.places) else { | ||
| return nil | ||
| } | ||
| switch self.sign { | ||
| case .minus: | ||
| let negated: Int64 = Int64.init(bitPattern: 0 &- self.units) | ||
| guard negated <= 0, | ||
| let units: T = T.init(exactly: negated) else { | ||
| return nil | ||
| } | ||
| return (units: units, places: places) | ||
| case .plus: | ||
| guard let units: T = T.init(exactly: self.units) else { | ||
| return nil | ||
| } | ||
| return (units: units, places: places) | ||
| } | ||
| } | ||
| } | ||
| extension JSON.Number.Inline: CustomStringConvertible { | ||
| /// Returns a zero-padded string representation of this numeric literal. | ||
| /// | ||
| /// This property always formats the number with full precision. | ||
| /// If ``units`` is `100` and ``places`` is `2`, this will return | ||
| /// `"1.00"`. | ||
| /// | ||
| /// This string is guaranteed to be round-trippable; reparsing it | ||
| /// will always return the same value. | ||
| /// | ||
| /// > Warning: | ||
| /// > This string is not necessarily identical to how this literal was | ||
| /// written in its original source file. In particular, if it was | ||
| /// written with an exponent, the exponent would have been normalized | ||
| /// into ``units`` and ``places``. | ||
| public var description: String { | ||
| guard self.places > 0 else { | ||
| switch self.sign { | ||
| case .plus: return "\(self.units)" | ||
| case .minus: return "-\(self.units)" | ||
| } | ||
| } | ||
| let places: Int = .init(self.places) | ||
| let unpadded: String = .init(self.units) | ||
| let string: String = """ | ||
| \(String.init(repeating: "0", count: Swift.max(0, 1 + places - unpadded.count)))\ | ||
| \(unpadded) | ||
| """ | ||
| switch self.sign { | ||
| case .plus: return "\(string.dropLast(places)).\(string.suffix(places))" | ||
| case .minus: return "-\(string.dropLast(places)).\(string.suffix(places))" | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of code for adding Swift settings is overly complex and uses an obscure pattern with an immediately-invoked closure and an
inoutparameter. This makes the code hard to read and maintain. A more straightforward approach using aguardstatement and direct property assignment would be much clearer.