diff --git a/Sources/SwiftRadix/Radix/Radix Collection Methods.swift b/Sources/SwiftRadix/Radix/Radix Collection Methods.swift index d15fa1a..15c16f1 100644 --- a/Sources/SwiftRadix/Radix/Radix Collection Methods.swift +++ b/Sources/SwiftRadix/Radix/Radix Collection Methods.swift @@ -17,10 +17,21 @@ extension Collection where Element : RadixProtocol { /// Convert an array of `Radix` to a concatenated String of radix string values. public var stringValue: String { - self.stringValue(padTo: 0) + self + .map { $0.stringValue } + .joined(separator: " ") } + + /// Convert an array of `Radix` to a concatenated String of radix string values, each value padded to the number of characters specified. + public func stringValue(prefix: Bool) -> String { + + self + .map { $0.stringValue(prefix: prefix) } + .joined(separator: " ") + } + /// Convert an array of `Radix` to a concatenated String of radix string values, each value padded to the number of characters specified. public func stringValue(padTo: Int, prefix: Bool = false) -> String { diff --git a/Sources/SwiftRadix/Radix/Radix Strings.swift b/Sources/SwiftRadix/Radix/Radix Strings.swift index 5fd7ac7..d4793ba 100644 --- a/Sources/SwiftRadix/Radix/Radix Strings.swift +++ b/Sources/SwiftRadix/Radix/Radix Strings.swift @@ -33,6 +33,16 @@ extension Radix { } + /// Computed property: + /// Returns radix String representation of `value`, optionally including `prefix`. + public func stringValue(prefix: Bool) -> String { + + prefix + ? stringPrefix + stringValue + : stringValue + + } + /// Computed property: /// Returns radix String representation of `value`, padding zeros to number of places passed. /// @@ -56,7 +66,9 @@ extension Radix { .joined(separator: splitter) } - return (prefix ? stringPrefix : "") + radixString + return prefix + ? stringPrefix + radixString + : radixString } @@ -84,7 +96,9 @@ extension Radix { .joined(separator: splitter) } - return (prefix ? stringPrefix : "") + radixString + return prefix + ? stringPrefix + radixString + : radixString } diff --git a/Sources/SwiftRadix/Radix/RadixProtocol.swift b/Sources/SwiftRadix/Radix/RadixProtocol.swift index 037f23f..97a872c 100644 --- a/Sources/SwiftRadix/Radix/RadixProtocol.swift +++ b/Sources/SwiftRadix/Radix/RadixProtocol.swift @@ -26,6 +26,7 @@ public protocol RadixProtocol { init?(_ string: String, base: Int) var stringValue: String { get set } + func stringValue(prefix: Bool) -> String func stringValue(padTo: Int, splitEvery: Int, prefix: Bool) -> String func stringValue(padToEvery: Int, splitEvery: Int, prefix: Bool) -> String diff --git a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Collection Methods Tests.swift b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Collection Methods Tests.swift index bbb373b..cf64741 100644 --- a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Collection Methods Tests.swift +++ b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Collection Methods Tests.swift @@ -12,7 +12,7 @@ import XCTest extension SwiftRadixTests { - func testRadix_CollectionExtension_stringValue_Get() { + func testRadix_CollectionExtension_stringValue() { // binary @@ -37,6 +37,40 @@ extension SwiftRadixTests { } + func testRadix_CollectionExtension_stringValue_Prefix() { + + // binary + + let source1 = [0x00.binary, 0xFF.binary] + + XCTAssertEqual(source1.stringValue(prefix: true), + "0b0 0b11111111") + + XCTAssertEqual(source1.stringValue(prefix: false), + "0 11111111") + + // hex + + let source2 = [0x00.hex, 0xFF.hex] + + XCTAssertEqual(source2.stringValue(prefix: true), + "0x0 0xFF") + + XCTAssertEqual(source2.stringValue(prefix: false), + "0 FF") + + // octal + + let source3 = [0x000.octal, 0o123.octal] + + XCTAssertEqual(source3.stringValue(prefix: true), + "0o0 0o123") + + XCTAssertEqual(source3.stringValue(prefix: false), + "0 123") + + } + func testRadix_CollectionExtension_stringValue_PadTo() { // binary diff --git a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Strings Tests.swift b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Strings Tests.swift index 68502c5..fa80b72 100644 --- a/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Strings Tests.swift +++ b/Tests/SwiftRadixTests/Unit Tests/Radix/Radix Strings Tests.swift @@ -140,6 +140,25 @@ extension SwiftRadixTests { } + func testRadix_stringValue_Prefix() { + + // binary + + XCTAssertEqual(0b0.binary.stringValue(prefix: true), "0b0") + XCTAssertEqual(0b0.binary.stringValue(prefix: false), "0") + + // hex + + XCTAssertEqual(0b0.hex.stringValue(prefix: true), "0x0") + XCTAssertEqual(0b0.hex.stringValue(prefix: false), "0") + + // octal + + XCTAssertEqual(0b0.octal.stringValue(prefix: true), "0o0") + XCTAssertEqual(0b0.octal.stringValue(prefix: false), "0") + + } + func testRadix_stringValue_PadTo() { // binary