Skip to content

BitFoundation module to centralize shared components#1089

Merged
jackjackbits merged 2 commits intomainfrom
refactor/bit-foundation
Apr 14, 2026
Merged

BitFoundation module to centralize shared components#1089
jackjackbits merged 2 commits intomainfrom
refactor/bit-foundation

Conversation

@qalandarov
Copy link
Copy Markdown
Collaborator

No description provided.

@qalandarov qalandarov force-pushed the refactor/bit-foundation branch from 9f4fff8 to a3a3cfb Compare April 4, 2026 06:17
@qalandarov qalandarov marked this pull request as ready for review April 4, 2026 06:44
@qalandarov qalandarov requested a review from jackjackbits April 4, 2026 06:44
@qalandarov qalandarov self-assigned this Apr 4, 2026
chatgpt-codex-connector[bot]

This comment was marked as outdated.

@qalandarov qalandarov force-pushed the refactor/bit-foundation branch from a3a3cfb to f627be8 Compare April 4, 2026 09:29
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f627be84eb

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines 8 to +9
import Foundation
import CryptoKit

// MARK: - Hex Encoding/Decoding

extension Data {
func hexEncodedString() -> String {
if self.isEmpty {
return ""
}
return self.map { String(format: "%02x", $0) }.joined()
}

func sha256Hex() -> String {
let digest = SHA256.hash(data: self)
return digest.map { String(format: "%02x", $0) }.joined()
}

/// Initialize Data from a hex string.
/// - Parameter hexString: A hex string, optionally prefixed with "0x" or "0X".
/// Whitespace is trimmed. Must have even length after prefix removal.
/// - Returns: nil if the string has odd length or contains invalid hex characters.
init?(hexString: String) {
var hex = hexString.trimmingCharacters(in: .whitespaces)

// Remove optional 0x prefix
if hex.hasPrefix("0x") || hex.hasPrefix("0X") {
hex = String(hex.dropFirst(2))
}

// Reject odd-length strings
guard hex.count % 2 == 0 else {
return nil
}

// Reject empty strings
guard !hex.isEmpty else {
self = Data()
return
}

let len = hex.count / 2
var data = Data(capacity: len)
var index = hex.startIndex

for _ in 0..<len {
let nextIndex = hex.index(index, offsetBy: 2)
guard let byte = UInt8(String(hex[index..<nextIndex]), radix: 16) else {
return nil
}
data.append(byte)
index = nextIndex
}

self = data
}
}
import BitFoundation

This comment was marked as resolved.

This comment was marked as resolved.

@qalandarov qalandarov force-pushed the refactor/bit-foundation branch 4 times, most recently from 4eb4ee3 to 5b690de Compare April 10, 2026 20:41
@jackjackbits
Copy link
Copy Markdown
Collaborator

P3
Preserve newline trimming in Data(hexString:)
Dismiss
The extracted helper now trims only .whitespaces, while the original implementation trimmed .whitespacesAndNewlines via StringProtocol.trimmed. That means newline-terminated hex strings that used to parse will now return nil, which is an observable behavior change for clipboard/file input and no longer matches the doc comment that says whitespace is trimmed. Restoring .whitespacesAndNewlines and adding a newline case to HexStringTests would keep the refactor behavior-preserving.

@qalandarov qalandarov force-pushed the refactor/bit-foundation branch from aa26022 to 0c1c4fc Compare April 14, 2026 18:49
@qalandarov qalandarov force-pushed the refactor/bit-foundation branch from 0c1c4fc to dc783cb Compare April 14, 2026 18:54
@qalandarov
Copy link
Copy Markdown
Collaborator Author

Depends on #1100

@qalandarov qalandarov force-pushed the refactor/bit-foundation branch from dc783cb to 1aa80c5 Compare April 14, 2026 19:03
@qalandarov
Copy link
Copy Markdown
Collaborator Author

P3 Preserve newline trimming in Data(hexString:) Dismiss The extracted helper now trims only .whitespaces, while the original implementation trimmed .whitespacesAndNewlines via StringProtocol.trimmed. That means newline-terminated hex strings that used to parse will now return nil, which is an observable behavior change for clipboard/file input and no longer matches the doc comment that says whitespace is trimmed. Restoring .whitespacesAndNewlines and adding a newline case to HexStringTests would keep the refactor behavior-preserving.

fixed

@jackjackbits jackjackbits merged commit 4cfcefc into main Apr 14, 2026
3 checks passed
@jackjackbits jackjackbits deleted the refactor/bit-foundation branch April 14, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants