-
Notifications
You must be signed in to change notification settings - Fork 30.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
buffer: use simdutf convert_latin1_to_utf8_safe
simdutf 5.5 includes convert_latin1_to_utf8_safe
- Loading branch information
Showing
2 changed files
with
46 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const assert = require('node:assert') | ||
const crypto = require('node:crypto') | ||
|
||
for (let i = 0; i < 1e6; i++) { | ||
const len = Math.floor(Math.random() * 128) | ||
const buf = Buffer.allocUnsafe(len) | ||
crypto.getRandomValues(buf) | ||
|
||
for (let n = 0; n < 16; n++) { | ||
const start = Math.floor(Math.random() * len) | ||
const end = Math.min(len, start + Math.floor(Math.random() * (len - start))) | ||
const src = buf.subarray(start, end) | ||
const str = src.toString('utf8') | ||
|
||
let bufNew = Buffer.alloc(buf.length); | ||
let bufOld = Buffer.alloc(buf.length); | ||
|
||
bufNew = bufNew.subarray(0, bufNew.utf8Write(str, start)) | ||
bufOld = bufOld.subarray(0, bufOld.utf8WriteLegacy(str, start)) | ||
|
||
try { | ||
assert.deepStrictEqual(bufNew, bufOld) | ||
} catch (err) { | ||
console.error({ | ||
start, | ||
src: new Uint8Array(src), | ||
bufNew: { | ||
byteOffset: bufNew.byteOffset, | ||
byteLength: bufNew.byteLength, | ||
buffer: { | ||
byteLength: bufNew.buffer.byteLength | ||
} | ||
}, | ||
bufOld: { | ||
byteOffset: bufOld.byteOffset, | ||
byteLength: bufOld.byteLength, | ||
buffer: { | ||
byteLength: bufOld.buffer.byteLength | ||
} | ||
} | ||
}) | ||
throw err | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters