Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed missing cast in case of startAddress == 0 AND added hexdump like ASCII print #2

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void main() {

```dart
void main() {
print(hexView(5, List<int>.generate(125, (i) => i + 5)));
print(hexView(5, List<int>.generate(125, (i) => i + 5), printAscii: true));
}
```

Expand All @@ -48,4 +48,4 @@ void main() {
final hexString = '00:B2:0C:EC:EA:F3:76:B2:F7:87:56:B6:97:57:4C:DD:A1:5C:A1:59:41:D6:35:0D:31:9E:11:D3:3E:18:ED:E6:08:07:99:2B:EB:53:1B:D4:A5:77:CA:58:23:FD:81:9A:39:F5:0E:DD:C7:4C:9D:8E:A0:6C:76:8A:17:0A:89:95:3F:89:06:0E:3B:A5:BC:E1:D6:82:F7:15:9B:04:EC:2F:46:C0:32:A9:AC:0B:2C:89:28:CE:89:5A:A8:BC:13:E0:8F:4C:EA:4C:05:67:5A:A5:4C:6F:C1:A4:A6:A2:9F:20:7A:24:ED:9D:7D:1A:57:8C:C6:5F:0B:33:DA:6C:1D:A2:06:E8:BA:74:23:30:1F:C4:C3:BB:EF:F1:7B:6A:C1:74:A9:9D:1E:E4:33:91:CF:7C:CE:F0:6F:D1:7C:32:39:51:BA:76:BA:36:DD:95:EE:2D:28:52:BF:84:12:DE:9F:21:81:15:64:58:87:4D:25:33:0C:B6:B3:D9:E0:A5:25:E0:8D:76:FC:AA:69:E1:82:0A:AB:3E:2E:E9:CC:AB:79:19:8C:EF:5A:86:D4:B6:FB:5A:99:C5:F0:73:69:CF:F4:29:74:58:9C:B6:B4:90:64:07:65:BC:34:79:C8:1D:64:2F:58:A6:AA:8C:79:37:92:9C:93:CF:5F:8F:D3:42:DE:78:A9:52:D0:5C:CD:FE:87:CC:6F:C2:DD:E5:22:D7:09:3D:94:74:BF:ED:32:41:EE:50:13:03:CB:E1:CE:54:17:CD:63:7C:6A:D8:7C:4C:E6:1A:D8:7A:29:12:26:44:C7:45:2C:5F:7E:17:BA:33:02:01:AA:08:B4:2D:DD:E5:76:60:C5:C8:05:87:D9:4B:D2:FF:94:EF:D0:DE:6E:0C:18:04:59:5D:41:37:8E:59:1C:7C:59:5C:6E:70:34:BB:03:01:4A:37:4D:52:CC:38:CC:78:D5:C2:4E:9A:1B:90:B0:60:56:15:1C:6F:67:E6:FE:2A:0B:12:95:10:D4:62:9F:38:06:46:7C:24:37:20:03:79:CB:DB:71:0D:F4:FD:43:AE:85:4E:58:06:77:C5:49:8A:EB:86:AF:DF:C8:26:E9:9D:EB:B1:93:61:28:B8:BD:E9:93:F1:80:EE:61:B3:AA:18:CF:63:36:9A:34:65:59:F2:7F:98:CA:CD:24:C6:E3:FD:5C:33:94:10:39:23:5E:96:BF:4B:37:99:12:4F:45:5C:C9:4B:73:B5:B2:76:3F:FB:11:2E:2B:E9:26:02:B3:82:8C:AF:EA:7B:0A:31:60:FD:AE:C8:75:EA:4B:43:7B:71:AD:97:9C:AC:1C:68:1B:99:81:0D:88:02:24:27:CB:06:ED';
print(bigIntFromHex(hexString));
}
```
```
2 changes: 1 addition & 1 deletion example/hexview_example.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:ninja_hex/ninja_hex.dart';

void main() {
print(hexView(5, List<int>.generate(125, (i) => i + 5)));
print(hexView(5, List<int>.generate(125, (i) => i + 5), printAscii: true));
}
53 changes: 50 additions & 3 deletions lib/src/hexview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Record {
data = <int?>[...List<int?>.filled(size - data.length, null), ...data];

startAddress -= data.length - size;
} else {
data = data.toList();
}

return Record(startAddress, data.toList());
Expand All @@ -27,6 +29,8 @@ class Record {
data = <int?>[...data, ...List<int?>.filled(size - data.length, null)];

startAddress -= data.length - size;
} else {
data = data.toList();
}

return Record(startAddress, data.toList());
Expand Down Expand Up @@ -66,13 +70,14 @@ class Record {
}
}

abstract class HexRecordFormatter {
// Experimental : print ASCII repr
abstract class AbsRecordFormatter {
int get recordLength;

String format(Record record, {int? fullDataEndAddress});
}

class DefaultRecordFormatter implements HexRecordFormatter {
class DefaultRecordFormatter implements AbsRecordFormatter {
const DefaultRecordFormatter({this.recordLength = 16});

@override
Expand All @@ -99,8 +104,38 @@ class DefaultRecordFormatter implements HexRecordFormatter {
}
}

// Experimental : print ASCII repr
class AsciiRecordFormatter implements AbsRecordFormatter {
const AsciiRecordFormatter({this.recordLength = 16});

@override
final int recordLength;

String format(Record record, {int? fullDataEndAddress}) {
if (record.length != recordLength) {
throw Exception('Record should be of length $recordLength');
}

final sb = StringBuffer();
sb.write('\t\t');
for (int? datum in record.data) {
if (datum == null) {
sb.write(' ');
} else if (datum > 32 && datum < 127) {
sb.write(String.fromCharCode(datum));
} else {
sb.write('.');
}
sb.write(' ');
}
return sb.toString();
}
}

String hexView(int startAddress, Iterable<int> data,
{HexRecordFormatter formatter = const DefaultRecordFormatter(),
{AbsRecordFormatter formatter = const DefaultRecordFormatter(),
bool printAscii = false,
AbsRecordFormatter asciiFormatter = const AsciiRecordFormatter(),
String recordSeparator = '\n'}) {
final recordLength = formatter.recordLength;
final sb = StringBuffer();
Expand All @@ -113,6 +148,10 @@ String hexView(int startAddress, Iterable<int> data,
data = data.skip(firstTake);
final record = Record.prefix(startAddress, first, recordLength);
sb.write(formatter.format(record));
// Experimental : print ASCII repr
if (printAscii) {
sb.write(asciiFormatter.format(record));
}
address = record.startAddress + recordLength;
}

Expand All @@ -121,6 +160,10 @@ String hexView(int startAddress, Iterable<int> data,
final record = Record(address, recordData.toList());
sb.write(recordSeparator);
sb.write(formatter.format(record));
// Experimental : print ASCII repr
if (printAscii) {
sb.write(asciiFormatter.format(record));
}
address = record.startAddress + recordLength;
data = data.skip(recordLength);
}
Expand All @@ -129,6 +172,10 @@ String hexView(int startAddress, Iterable<int> data,
final record = Record.suffix(address, data, recordLength);
sb.write(recordSeparator);
sb.write(formatter.format(record));
// Experimental : print ASCII repr
if (printAscii) {
sb.write(asciiFormatter.format(record));
}
}

return sb.toString();
Expand Down