Skip to content

Commit

Permalink
Improve hexDump and ensure TextKit 2 is used
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanzhong committed Dec 11, 2024
1 parent d7b5d01 commit 5751287
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
import AppKit

public extension NSScrollView {
var allowsNonContiguousLayout: Bool {
get {
textView?.layoutManager?.allowsNonContiguousLayout ?? false
}
set {
textView?.layoutManager?.allowsNonContiguousLayout = newValue
}
}

var monospacedText: String {
get {
attributedText.string
Expand Down
23 changes: 9 additions & 14 deletions WhatCopiedMac/Sources/Tools/HexDump.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,36 @@
NSString *hexDump(NSData *data, NSUInteger bytesPerRow) {
const uint8_t *bytes = (const uint8_t *)data.bytes;
NSUInteger length = data.length;
NSMutableString *result = [NSMutableString stringWithCapacity:(length / bytesPerRow + 1) * (10 + bytesPerRow * 4)];

NSMutableString *result = [NSMutableString stringWithCapacity:length * 4];
NSMutableData *lineBuffer = [NSMutableData dataWithLength:(bytesPerRow * 3 - 1) + bytesPerRow + 20];
char *line = (char *)lineBuffer.mutableBytes;
char lineBuffer[1024]; // Enough space for typical rows

for (NSUInteger i = 0; i < length; i += bytesPerRow) {
// Offset column
snprintf(line, 10, "%08ld:", (unsigned long)i);
char *cursor = line + 9;
char *cursor = lineBuffer;
cursor += snprintf(cursor, sizeof(lineBuffer), "%08ld: ", (unsigned long)i);

// Hex column
NSUInteger rowLength = MIN(bytesPerRow, length - i);
for (NSUInteger j = 0; j < rowLength; j++) {
snprintf(cursor, 4, " %02X", bytes[i + j]);
cursor += 3;
cursor += snprintf(cursor, 4, "%02X ", bytes[i + j]);
}

if (rowLength < bytesPerRow) {
NSUInteger padding = (bytesPerRow - rowLength) * 3;
memset(cursor, ' ', padding);
cursor += padding;
memset(cursor, ' ', (bytesPerRow - rowLength) * 3);
cursor += (bytesPerRow - rowLength) * 3;
}

// ASCII column
*cursor++ = ' ';
*cursor++ = ' ';
for (NSUInteger j = 0; j < rowLength; j++) {
uint8_t byte = bytes[i + j];
BOOL useByte = (byte >= 32 && byte < 127) || (byte >= 160 && byte < 255);
*cursor++ = useByte ? (char)byte : '.';
*cursor++ = (byte >= 32 && byte < 127) ? (char)byte : '.';
}

*cursor++ = '\n';
*cursor = '\0';
[result appendFormat:@"%s", line];
[result appendString:[NSString stringWithUTF8String:lineBuffer]];
}

return result;
Expand Down
1 change: 0 additions & 1 deletion WhatCopiedMac/Sources/Views/DataViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ final class DataViewer: NSView {

textView.isHidden = !mode.usesTextView
textView.contentView.scroll(to: .zero)
textView.allowsNonContiguousLayout = mode == .hex

htmlView.isHidden = mode != .html
codeView.isHidden = mode != .sourceCode
Expand Down

0 comments on commit 5751287

Please sign in to comment.