-
Notifications
You must be signed in to change notification settings - Fork 30.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: add a method to get string encoding info
Add a util method to check the encoding information of a string. The encoing information is from V8 Refs: #56090
- Loading branch information
1 parent
c4aa34a
commit a4a445a
Showing
5 changed files
with
112 additions
and
0 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
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
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
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
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,41 @@ | ||
// Flags: --expose-internals | ||
'use strict'; | ||
require('../common'); | ||
const assert = require('assert'); | ||
const { getStringEncodingInfo } = require('util'); | ||
|
||
[ | ||
undefined, | ||
null, | ||
false, | ||
5n, | ||
5, | ||
Symbol(), | ||
() => {}, | ||
{}, | ||
].forEach((value) => { | ||
assert.throws( | ||
() => { getStringEncodingInfo(value); }, | ||
/The "content" argument must be of type string/ | ||
); | ||
}); | ||
|
||
{ | ||
const latin1String = 'hello world!'; | ||
// Run this inside a for loop to trigger the fast API | ||
for (let i = 0; i < 10_000; i++) { | ||
const { encoding, byteLength } = getStringEncodingInfo(latin1String); | ||
assert.strictEqual(encoding, 'latin1'); | ||
assert.strictEqual(byteLength, latin1String.length); | ||
} | ||
} | ||
|
||
{ | ||
const utf16String = '你好😀😃'; | ||
// Run this inside a for loop to trigger the fast API | ||
for (let i = 0; i < 10_000; i++) { | ||
const { encoding, byteLength } = getStringEncodingInfo(utf16String); | ||
assert.strictEqual(encoding, 'utf16le'); | ||
assert.strictEqual(byteLength, utf16String.length * 2); | ||
} | ||
} |