Skip to content

Commit

Permalink
fix: StringT: assume unknown encodings are 1-byte
Browse files Browse the repository at this point in the history
- add a test for 'x-mac-roman'
- add two utf-16 alias names
- if an encoding is otherwise unknown, assume 1-byte length
(this matches prior behavior)

Fixes: #60
  • Loading branch information
srl295 committed Apr 6, 2024
1 parent 6680114 commit fcf7d64
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,16 @@ function encodingWidth(encoding) {
return 1;
case 'utf16le':
case 'utf16-le':
case 'utf-16be':
case 'utf-16le':
case 'utf16be':
case 'utf16-be':
case 'ucs2':
return 2;
default:
throw new Error('Unknown encoding ' + encoding);
//TODO: assume all other encodings are 1-byters
//throw new Error('Unknown encoding ' + encoding);
return 1;
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/String.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ describe('String', function() {
const string = new StringT(null, 'utf16le');
assert.equal(string.fromBuffer(Buffer.from('🍻', 'utf16le')), '🍻');
});

it('should decode x-mac-roman', function() {
const string = new StringT(null, 'x-mac-roman');
const buf = new Uint8Array([0x8a, 0x63, 0x63, 0x65, 0x6e, 0x74, 0x65, 0x64, 0x20, 0x63, 0x68, 0x87, 0x72, 0x61, 0x63, 0x74, 0x65, 0x72, 0x73]);
assert.equal(string.fromBuffer(buf), 'äccented cháracters');
})
});

describe('size', function() {
Expand Down

0 comments on commit fcf7d64

Please sign in to comment.