Skip to content

Commit

Permalink
Add Base16.isValidCount(_:) method
Browse files Browse the repository at this point in the history
  • Loading branch information
yaslab committed Jul 21, 2024
1 parent 2db7d3e commit 0ffa490
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Sources/Hex/Base16.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import Foundation

/// A Base-16 codec that translates between Byte sequence and Hexadecimal (also known as Base-16) strings.
public enum Base16 {
/// Validates the length of hex characters.
@inlinable
public static func isValidCount(_ count: Int) -> Bool {
(0 <= count) && count.isMultiple(of: 2)
}

/// The result of one Base-16 decoding step.
public enum DecodingResult: Sendable {
public enum DecodingResult: Equatable, Sendable {
/// An indication that no more hex characters are available in the input.
case emptyInput
/// An indication of a decoding error.
Expand Down
6 changes: 6 additions & 0 deletions Sources/Hex/Data+Hex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ extension Data {
///
/// - Parameter hexData: The Hexadecimal encoded data.
public init?(hexEncoded hexData: Data) {
guard Base16.isValidCount(hexData.count) else {
return nil
}
guard let bytes = Data.decode(hexData) else {
return nil
}
Expand All @@ -15,6 +18,9 @@ extension Data {
///
/// - Parameter hexString: The Hexadecimal encoded string.
public init?(hexEncoded hexString: String) {
guard Base16.isValidCount(hexString.count) else {
return nil
}
var hexString = hexString
guard let bytes = hexString.withUTF8(Data.decode(_:)) else {
return nil
Expand Down
38 changes: 38 additions & 0 deletions Tests/HexTests/Base16Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#if canImport(Testing)

import Foundation
import Hex
import Testing

struct Base16Tests {
@Test(
// Arrange
arguments: [
(-2, false),
(-1, false),
(0, true),
(1, false),
(2, true),
]
)
func isValidCount(argument: (count: Int, expected: Bool)) {
// Act
let isValid = Base16.isValidCount(argument.count)

// Assert
#expect(isValid == argument.expected)
}

@Test func decodeOneDigitString() {
// Arrange
var it = "0".utf8.makeIterator()

// Act
let result = Base16.decode(&it)

// Assert
#expect(result == .error)
}
}

#endif
24 changes: 23 additions & 1 deletion Tests/HexTests/Data+HexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ struct DataHexTests {
#expect(data.isEmpty)
}

@Test func decodeOneDigitData() {
// Arrange
let hexData = Data("4".utf8)

// Act
let data = Data(hexEncoded: hexData)

// Assert
#expect(data == nil)
}

@Test func decodeOneDigitString() {
// Arrange
let hexString = "4"
Expand Down Expand Up @@ -93,9 +104,20 @@ struct DataHexTests {
#expect(data == nil)
}

@Test func decodeNonHexData() {
// Arrange
let hexData = Data("G0".utf8)

// Act
let data = Data(hexEncoded: hexData)

// Assert
#expect(data == nil)
}

@Test func decodeNonHexString() {
// Arrange
let hexString = "4G"
let hexString = "0G"

// Act
let data = Data(hexEncoded: hexString)
Expand Down

0 comments on commit 0ffa490

Please sign in to comment.