-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
3,385 additions
and
3,533 deletions.
There are no files selected for viewing
This file contains 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
136 changes: 65 additions & 71 deletions
136
Sources/SwiftRadix/Radix/Binary/Binary Type Extensions.swift
This file contains 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 |
---|---|---|
@@ -1,93 +1,87 @@ | ||
// | ||
// Binary Type Extensions.swift | ||
// SwiftRadix | ||
// | ||
// Created by Steffan Andrews on 2020-09-13. | ||
// MIT License | ||
// https://github.com/orchetect/SwiftRadix | ||
// SwiftRadix • https://github.com/orchetect/SwiftRadix | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - Constructors | ||
|
||
extension BinaryInteger { | ||
/// Returns a new `Radix<T>` base-2 struct from an integer, preserving the integer type. | ||
@inline(__always) public var binary: Radix<Self> { | ||
Radix(self, base: 2) | ||
} | ||
/// Returns a new `Radix<T>` base-2 struct from an integer, preserving the integer type. | ||
@inline(__always) public var binary: Radix<Self> { | ||
Radix(self, base: 2) | ||
} | ||
} | ||
|
||
extension String { | ||
|
||
/// Returns a new `Radix<Int>?` base-2 struct from a binary string. | ||
@inlinable public var binary: Radix<Int>? { | ||
|
||
Radix(self, base: 2) | ||
|
||
} | ||
|
||
/// Returns a new `Radix<T>?` base-2 struct from a binary string. | ||
/// | ||
/// Example usage: | ||
/// | ||
/// ``` | ||
/// "1010".binary(as: Int16.self) | ||
/// ``` | ||
@inlinable public func binary<T: BinaryInteger>(as type: T.Type) -> Radix<T>? { | ||
|
||
Radix<T>(self, base: 2) | ||
|
||
} | ||
|
||
|
||
/// Returns a new `Radix<Int>?` base-2 struct from a binary string. | ||
@inlinable public var binary: Radix<Int>? { | ||
|
||
Radix(self, base: 2) | ||
|
||
} | ||
|
||
/// Returns a new `Radix<T>?` base-2 struct from a binary string. | ||
/// | ||
/// Example usage: | ||
/// | ||
/// "1010".binary(as: Int16.self) | ||
/// | ||
@inlinable public func binary<T: BinaryInteger>(as type: T.Type) -> Radix<T>? { | ||
|
||
Radix<T>(self, base: 2) | ||
|
||
} | ||
|
||
} | ||
|
||
extension Array where Element == String { | ||
|
||
/// Returns an array of `Radix<Int>?` base-2 structs constructed from an array of binary strings. | ||
public var binary: [Radix<Int>?] { | ||
|
||
self.map { Radix<Int>($0, base: 2) } | ||
|
||
} | ||
|
||
/// Returns an array of `Radix<T>?` base-2 structs constructed from an array of binary strings. | ||
/// | ||
/// Example usage: | ||
/// | ||
/// ``` | ||
/// ["1010", "1111"].binary(as: Int16.self) | ||
/// ``` | ||
public func binary<T: BinaryInteger>(as type: T.Type) -> [Radix<T>?] { | ||
|
||
self.map { Radix<T>($0, base: 2) } | ||
|
||
} | ||
|
||
|
||
/// Returns an array of `Radix<Int>?` base-2 structs constructed from an array of binary strings. | ||
public var binary: [Radix<Int>?] { | ||
|
||
self.map { Radix<Int>($0, base: 2) } | ||
|
||
} | ||
|
||
/// Returns an array of `Radix<T>?` base-2 structs constructed from an array of binary strings. | ||
/// | ||
/// Example usage: | ||
/// | ||
/// ["1010", "1111"].binary(as: Int16.self) | ||
/// | ||
public func binary<T: BinaryInteger>(as type: T.Type) -> [Radix<T>?] { | ||
|
||
self.map { Radix<T>($0, base: 2) } | ||
|
||
} | ||
|
||
} | ||
|
||
extension Collection where Element: BinaryInteger { | ||
/// Returns an array of `Radix<T>` base-2 structs built from an integer array, preserving the integer type. | ||
@inlinable public var binary: [Radix<Element>] { | ||
self.map { Radix($0, base: 2) } | ||
} | ||
/// Returns an array of `Radix<T>` base-2 structs built from an integer array, preserving the integer type. | ||
@inlinable public var binary: [Radix<Element>] { | ||
self.map { Radix($0, base: 2) } | ||
} | ||
} | ||
|
||
extension Data { | ||
/// Returns an array of `Radix<UInt8>` base-2 structs built from Data bytes. | ||
public var binary: [Radix<UInt8>] { | ||
self.map { Radix($0, base: 2) } | ||
} | ||
/// Returns an array of `Radix<UInt8>` base-2 structs built from `Data` bytes. | ||
public var binary: [Radix<UInt8>] { | ||
self.map { Radix($0, base: 2) } | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,37 +1,32 @@ | ||
// | ||
// Binary.swift | ||
// SwiftRadix | ||
// | ||
// Created by Steffan Andrews on 2020-09-13. | ||
// MIT License | ||
// https://github.com/orchetect/SwiftRadix | ||
// SwiftRadix • https://github.com/orchetect/SwiftRadix | ||
// | ||
|
||
import Foundation | ||
|
||
/// Convenience constructor for `Radix<T>` with a radix of 2 (binary). | ||
@inlinable public func Binary<T: BinaryInteger>(_ number: T) -> Radix<T> { | ||
Radix<T>(number, base: 2) | ||
Radix<T>(number, base: 2) | ||
} | ||
|
||
/// Convenience constructor for `Radix<Int>` with a radix of 2 (binary). | ||
@inlinable public func Binary(_ string: String) -> Radix<Int>? { | ||
|
||
Radix<Int>(string, base: 2) | ||
|
||
Radix<Int>(string, base: 2) | ||
} | ||
|
||
/// Convenience constructor for `Radix<T>` with a radix of 2 (binary). | ||
/// | ||
/// Example usage: | ||
/// | ||
/// ``` | ||
/// Binary("1010", as: Int16.self) | ||
/// ``` | ||
/// Binary("1010", as: Int16.self) | ||
/// | ||
@inlinable public func Binary<T: BinaryInteger>(_ string: String, as type: T.Type) -> Radix<T>? { | ||
Radix<T>(string, base: 2) | ||
Radix<T>(string, base: 2) | ||
} |
Oops, something went wrong.