Skip to content

Commit

Permalink
Added prefix-only .stringValue overloads; minor performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Mar 4, 2021
1 parent 0d998d0 commit 51d9d62
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
13 changes: 12 additions & 1 deletion Sources/SwiftRadix/Radix/Radix Collection Methods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 16 additions & 2 deletions Sources/SwiftRadix/Radix/Radix Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -56,7 +66,9 @@ extension Radix {
.joined(separator: splitter)
}

return (prefix ? stringPrefix : "") + radixString
return prefix
? stringPrefix + radixString
: radixString

}

Expand Down Expand Up @@ -84,7 +96,9 @@ extension Radix {
.joined(separator: splitter)
}

return (prefix ? stringPrefix : "") + radixString
return prefix
? stringPrefix + radixString
: radixString

}

Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftRadix/Radix/RadixProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import XCTest

extension SwiftRadixTests {

func testRadix_CollectionExtension_stringValue_Get() {
func testRadix_CollectionExtension_stringValue() {

// binary

Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions Tests/SwiftRadixTests/Unit Tests/Radix/Radix Strings Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 51d9d62

Please sign in to comment.