Skip to content

Commit

Permalink
Update binary integer description documentation (#131).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Nov 15, 2024
1 parent b2144f0 commit 45cc68e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
32 changes: 32 additions & 0 deletions Sources/CoreKit/BinaryInteger+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
// MARK: Initializers
//=------------------------------------------------------------------------=

/// Decodes the `description`, if possible.
///
/// ### Binary Integer Description
///
/// - Note: The default format is `TextInt.decimal`.
///
/// - Note: Decoding failures throw `TextInt.Error`.
///
@inlinable public init?(_ description: String) {
always: do {
self = try TextInt.decimal.decode(description)
Expand All @@ -28,6 +36,14 @@
}
}

/// Decodes the `description` using the given `format`, if possible.
///
/// ### Binary Integer Description
///
/// - Note: The default format is `TextInt.decimal`.
///
/// - Note: Decoding failures throw `TextInt.Error`.
///
@inlinable public init(_ description: some StringProtocol, using format: TextInt) throws {
self = try format.decode(description)
}
Expand All @@ -36,10 +52,26 @@
// MARK: Utilities
//=------------------------------------------------------------------------=
/// Returns the `description` of `self`.
///
/// ### Binary Integer Description
///
/// - Note: The default format is `TextInt.decimal`.
///
/// - Note: Decoding failures throw `TextInt.Error`.
///
@inlinable public var description: String {
self.description(using: TextInt.decimal)
}

/// Returns the `description` of `self` using the given `format`.
///
/// ### Binary Integer Description
///
/// - Note: The default format is `TextInt.decimal`.
///
/// - Note: Decoding failures throw `TextInt.Error`.
///
@inlinable public func description(using format: TextInt) -> String {
format.encode(self)
}
Expand Down
44 changes: 37 additions & 7 deletions Sources/StdlibIntKit/StdlibInt+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ extension StdlibInt {

/// Decodes the decimal `description`, if possible.
///
/// - Note: This method uses `TextInt`.
/// ### Binary Integer Description
///
/// - Note: The default format is `TextInt.decimal`.
///
/// - Note: Decoding failures throw `TextInt.Error`.
///
/// ### Binary Integer Description (StdlibInt)
///
/// - Note: `String.init(_:radix:)` does not use `TextInt`.
///
@inlinable public init?(_ description: String) {
if let base = Base(description) {
Expand All @@ -32,9 +40,17 @@ extension StdlibInt {
}
}

/// Decodes the `format` `description`, if possible.
/// Decodes the `description` using the given `format`, if possible.
///
/// ### Binary Integer Description
///
/// - Note: The default format is `TextInt.decimal`.
///
/// - Note: Decoding failures throw `TextInt.Error`.
///
/// ### Binary Integer Description (StdlibInt)
///
/// - Note: This method uses `TextInt`.
/// - Note: `String.init(_:radix:)` does not use `TextInt`.
///
@inlinable public init(_ description: some StringProtocol, as format: TextInt) throws {
try self.init(Base(description, using: format))
Expand All @@ -44,17 +60,31 @@ extension StdlibInt {
// MARK: Utilities
//=------------------------------------------------------------------------=

/// Returns the decimal `description` of `self`.
/// Returns the `description` of `self`.
///
/// ### Binary Integer Description
///
/// - Note: The default format is `TextInt.decimal`.
///
/// - Note: This method uses `TextInt`.
/// - Note: Decoding failures throw `TextInt.Error`.
///
/// ### Binary Integer Description (StdlibInt)
///
/// - Note: `String.init(_:radix:)` does not use `TextInt`.
///
@inlinable public var description: String {
self.base.description
}

/// Returns the `format` `description` of `self`.
/// Returns the `description` of `self` using the given `format`.
///
/// ### Binary Integer Description
///
/// - Note: The default format is `TextInt.decimal`.
///
/// - Note: Decoding failures throw `TextInt.Error`.
///
/// - Note: This method uses `TextInt`.
/// ### Binary Integer Description (StdlibInt)
///
/// - Note: `String.init(_:radix:)` does not use `TextInt`.
///
Expand Down

0 comments on commit 45cc68e

Please sign in to comment.