Skip to content

Commit

Permalink
Fix handling of surrogate pairs, kriszyp/lmdb-js#249
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Jul 3, 2023
1 parent 8e46ed4 commit 2c91751
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
15 changes: 7 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,19 @@ function finishUtf8(byte1, src) {
} else if ((byte1 & 0xf8) === 0xf0) {
// 4 bytes
if (pendingSurrogate) {
byte1 = pendingSurrogate
pendingSurrogate = null
position += 3
return byte1
byte1 = pendingSurrogate
pendingSurrogate = null
position += 3
return byte1
}
const byte2 = src[position++] & 0x3f
const byte3 = src[position++] & 0x3f
const byte4 = src[position++] & 0x3f
let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4
if (unit > 0xffff) {
unit -= 0x10000
unit = 0xdc00 | (unit & 0x3ff)
pendingSurrogate = ((unit >>> 10) & 0x3ff) | 0xd800
position -= 4 // reset so we can return the next part of the surrogate pair
pendingSurrogate = 0xdc00 | (unit & 0x3ff)
unit = (((unit - 0x10000) >>> 10) & 0x3ff) | 0xd800
position -= 4 // reset so we can return the next part of the surrogate pair
}
return unit
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ordered-binary",
"author": "Kris Zyp",
"version": "1.4.0",
"version": "1.4.1",
"description": "Conversion of JavaScript primitives to and from Buffer with binary order matching natural primitive order",
"license": "MIT",
"repository": {
Expand Down
1 change: 1 addition & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ suite('key buffers', () => {
assert.strictEqual(fromBufferKey(toBufferKey('')), '')
assert.strictEqual(fromBufferKey(toBufferKey('\x00')), '\x00')
assert.strictEqual(fromBufferKey(toBufferKey('\x03test\x01\x00')), '\x03test\x01\x00')
assert.strictEqual(fromBufferKey(toBufferKey('prance 🧚🏻‍♀️🩷')), 'prance 🧚🏻‍♀️🩷')
})
test('string comparison', () => {
assertBufferComparison(toBufferKey('4'), toBufferKey('5'))
Expand Down

0 comments on commit 2c91751

Please sign in to comment.