Skip to content

Commit

Permalink
fixing issue a2800276#24 -- misaligned last line when incomplete grou…
Browse files Browse the repository at this point in the history
…p is displayed
  • Loading branch information
rom-p committed Feb 1, 2024
1 parent d385237 commit 4d5bb41
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions hexy.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ var Hexy = function(buffer, config) {
var hex = function(buffer, bytes_per_line, bytes_per_group, radix, littleEndian) {
var str = ""
const delimiter = bytes_per_group == 0 ? "" : " "
const group_len = maxnumberlen(bytes_per_group, radix)
var group_len = maxnumberlen(bytes_per_group, radix) // this changes for the very last group in the file
const padlen = (bytes_per_line - buffer.length) * (bytes_per_group == 0 ? group_len: (group_len + 1) / bytes_per_group)
if (bytes_per_group == 0) {
bytes_per_group = 1
Expand All @@ -381,8 +381,11 @@ var Hexy = function(buffer, config) {
var val = bytes_per_group < 4 ? 0 : BigInt(0)
for (var ii = start; ii != end; ii += step) {
const i = group * bytes_per_group + ii
if (i >= buffer.length) { // not rendering dangling bytes. TODO: render them as smaller grouping
break
if (i >= buffer.length) { // dangling bytes
if (radix == 2 || radix == 16) {
group_len -= maxnumberlen(1, radix)
} // for decimal and octal representation, the length of binary column is difficult to predict
continue
}
if (bytes_per_group < 4) {
val = val * 256 + ((buffer.constructor == String ? buffer.codePointAt(i) : buffer[i]) & 0xff)
Expand Down
2 changes: 2 additions & 0 deletions test/testcases.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ const testcases = [
// #30
{ input: [ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xd2, 0x77, 0x6f, 0x72, 0x6c, 0x64 ], params: {format: "twos"}, result: "00000000: 68 65 6c 6c 6f d2 77 6f 72 6c 64 hello.world\n" },
{ input: [ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xd2, 0x77, 0x6f, 0x72, 0x6c, 0x64 ], params: {format: "twos", extendedChs:true}, result: "00000000: 68 65 6c 6c 6f d2 77 6f 72 6c 64 hello" + "\u00d2" + "world\n" },
{ input: "abc", params: {littleEndian: true}, result: "00000000: 6261 63 abc\n" },
{ input: "abc", params: {littleEndian: true, format: "eights"}, result: "00000000: 636261 abc\n" },
];
if (typeof window === 'undefined') {
module.exports = testcases;
Expand Down

0 comments on commit 4d5bb41

Please sign in to comment.