|
| 1 | +// |
| 2 | +// String+HexDump.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | +// Hex dump formatting utilities for binary data display. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +extension String { |
| 11 | + /// Returns a classic hex dump representation of this string's bytes, or nil if empty. |
| 12 | + /// |
| 13 | + /// Format per line: `OFFSET HH HH HH HH HH HH HH HH HH HH HH HH HH HH HH HH |ASCII...........|` |
| 14 | + /// - Parameter maxBytes: Maximum bytes to display before truncating (default 10KB). |
| 15 | + func formattedAsHexDump(maxBytes: Int = 10_240) -> String? { |
| 16 | + // Convert to bytes: try isoLatin1 first (matches plugin fallback encoding for non-UTF-8 data), |
| 17 | + // then utf8 |
| 18 | + guard let bytes = data(using: .isoLatin1) ?? data(using: .utf8) else { |
| 19 | + return nil |
| 20 | + } |
| 21 | + |
| 22 | + let totalCount = bytes.count |
| 23 | + guard totalCount > 0 else { return nil } |
| 24 | + |
| 25 | + let displayCount = min(totalCount, maxBytes) |
| 26 | + let bytesArray = [UInt8](bytes.prefix(displayCount)) |
| 27 | + |
| 28 | + var lines: [String] = [] |
| 29 | + lines.reserveCapacity(displayCount / 16 + 2) |
| 30 | + |
| 31 | + let bytesPerLine = 16 |
| 32 | + var offset = 0 |
| 33 | + |
| 34 | + while offset < displayCount { |
| 35 | + let lineEnd = min(offset + bytesPerLine, displayCount) |
| 36 | + let lineBytes = bytesArray[offset..<lineEnd] |
| 37 | + |
| 38 | + // Offset column (8-digit hex) |
| 39 | + var line = String(format: "%08X ", offset) |
| 40 | + |
| 41 | + // Hex columns: two groups of 8 bytes |
| 42 | + for i in 0..<bytesPerLine { |
| 43 | + if i == 8 { line += " " } |
| 44 | + if offset + i < lineEnd { |
| 45 | + line += String(format: "%02X ", lineBytes[offset + i]) |
| 46 | + } else { |
| 47 | + line += " " |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // ASCII column |
| 52 | + line += " |" |
| 53 | + for byte in lineBytes { |
| 54 | + if byte >= 0x20, byte <= 0x7E { |
| 55 | + line += String(UnicodeScalar(byte)) |
| 56 | + } else { |
| 57 | + line += "." |
| 58 | + } |
| 59 | + } |
| 60 | + line += "|" |
| 61 | + |
| 62 | + lines.append(line) |
| 63 | + offset += bytesPerLine |
| 64 | + } |
| 65 | + |
| 66 | + if totalCount > maxBytes { |
| 67 | + let formatter = NumberFormatter() |
| 68 | + formatter.numberStyle = .decimal |
| 69 | + let formattedTotal = formatter.string(from: NSNumber(value: totalCount)) ?? "\(totalCount)" |
| 70 | + lines.append("... (truncated, \(formattedTotal) bytes total)") |
| 71 | + } |
| 72 | + |
| 73 | + return lines.joined(separator: "\n") |
| 74 | + } |
| 75 | + |
| 76 | + /// Returns a space-separated hex representation suitable for editing. |
| 77 | + /// |
| 78 | + /// Format: `48 65 6C 6C 6F` — one hex byte pair separated by spaces, no offset or ASCII columns. |
| 79 | + /// - Parameter maxBytes: Maximum bytes to display before truncating (default 10KB). |
| 80 | + func formattedAsEditableHex(maxBytes: Int = 10_240) -> String? { |
| 81 | + guard let bytes = data(using: .isoLatin1) ?? data(using: .utf8) else { |
| 82 | + return nil |
| 83 | + } |
| 84 | + |
| 85 | + let totalCount = bytes.count |
| 86 | + guard totalCount > 0 else { return nil } |
| 87 | + |
| 88 | + let displayCount = min(totalCount, maxBytes) |
| 89 | + let bytesArray = [UInt8](bytes.prefix(displayCount)) |
| 90 | + |
| 91 | + var hex = bytesArray.map { String(format: "%02X", $0) }.joined(separator: " ") |
| 92 | + |
| 93 | + if totalCount > maxBytes { |
| 94 | + hex += " …" |
| 95 | + } |
| 96 | + |
| 97 | + return hex |
| 98 | + } |
| 99 | + |
| 100 | + /// Returns a compact single-line hex representation for data grid cells. |
| 101 | + /// |
| 102 | + /// Format: `0x48656C6C6F` for short values, truncated with `…` for longer ones. |
| 103 | + /// - Parameter maxBytes: Maximum bytes to show before truncating (default 64). |
| 104 | + func formattedAsCompactHex(maxBytes: Int = 64) -> String? { |
| 105 | + guard let bytes = data(using: .isoLatin1) ?? data(using: .utf8) else { |
| 106 | + return nil |
| 107 | + } |
| 108 | + |
| 109 | + let totalCount = bytes.count |
| 110 | + guard totalCount > 0 else { return nil } |
| 111 | + |
| 112 | + let displayCount = min(totalCount, maxBytes) |
| 113 | + let bytesArray = [UInt8](bytes.prefix(displayCount)) |
| 114 | + |
| 115 | + var hex = "0x" |
| 116 | + for byte in bytesArray { |
| 117 | + hex += String(format: "%02X", byte) |
| 118 | + } |
| 119 | + |
| 120 | + if totalCount > maxBytes { |
| 121 | + hex += "…" |
| 122 | + } |
| 123 | + |
| 124 | + return hex |
| 125 | + } |
| 126 | +} |
0 commit comments