-
Notifications
You must be signed in to change notification settings - Fork 258
feat(NODE-5861): optimize parsing basic latin strings #642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6c0ec8d
feat(NODE-5861): optimize parsing basic latin strings
nbbeeken e353a30
feat(NODE-5861): move UTF8 validation below basic latin parsing
nbbeeken 7e3bcd0
fix: error ctor and assertion for error
nbbeeken d956aea
fix: lint
nbbeeken 9ef267c
feat: lower stringByteLength cap to 20
nbbeeken 5837166
test: release new file
nbbeeken 49d96af
test: add unit tests
nbbeeken 3e609bf
fix: remove apply
nbbeeken 267cc51
fix: move len 20 check up and remove basicLatin boolean
nbbeeken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,61 @@ | ||
/** | ||
* This function is an optimization for small basic latin strings. | ||
* @internal | ||
* @remarks | ||
* ### Important characteristics: | ||
* - If the uint8array or distance between start and end is 0 this function returns an empty string | ||
* - If the byteLength of the string is 1, 2, or 3 we invoke String.fromCharCode and manually offset into the buffer | ||
* - If the byteLength of the string is less than or equal to 20 an array of bytes is built and `String.fromCharCode.apply` is called with the result | ||
* - If any byte exceeds 128 this function returns null | ||
* | ||
* @param uint8array - A sequence of bytes that may contain basic latin characters | ||
* @param start - The start index from which to search the uint8array | ||
* @param end - The index to stop searching the uint8array | ||
* @returns string if all bytes are within the basic latin range, otherwise null | ||
*/ | ||
export function tryLatin(uint8array: Uint8Array, start: number, end: number): string | null { | ||
if (uint8array.length === 0) { | ||
return ''; | ||
} | ||
|
||
const stringByteLength = end - start; | ||
if (stringByteLength === 0) { | ||
return ''; | ||
} | ||
|
||
if (stringByteLength > 20) { | ||
return null; | ||
} | ||
|
||
if (stringByteLength === 1 && uint8array[start] < 128) { | ||
return String.fromCharCode(uint8array[start]); | ||
addaleax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (stringByteLength === 2 && uint8array[start] < 128 && uint8array[start + 1] < 128) { | ||
return String.fromCharCode(uint8array[start]) + String.fromCharCode(uint8array[start + 1]); | ||
} | ||
|
||
if ( | ||
stringByteLength === 3 && | ||
uint8array[start] < 128 && | ||
uint8array[start + 1] < 128 && | ||
uint8array[start + 2] < 128 | ||
) { | ||
return ( | ||
String.fromCharCode(uint8array[start]) + | ||
String.fromCharCode(uint8array[start + 1]) + | ||
String.fromCharCode(uint8array[start + 2]) | ||
); | ||
} | ||
|
||
const latinBytes = []; | ||
for (let i = start; i < end; i++) { | ||
const byte = uint8array[i]; | ||
if (byte > 127) { | ||
return null; | ||
} | ||
latinBytes.push(byte); | ||
} | ||
|
||
return String.fromCharCode(...latinBytes); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.