src: refactor WriteUCS2 and remove flags argument
This change refactors StringBytes::WriteUCS2() in multiple ways.
The flags argument being passed to WriteUCS2() is not useful: the only really relevant flag is NO_NULL_TERMINATION since V8 ignores REPLACE_INVALID_UTF8, HINT_MANY_WRITES_EXPECTED, and PRESERVE_ONE_BYTE_NULL for UTF-16 strings. However, WriteUCS2() might not null-terminate the result correctly regardless of whether NO_NULL_TERMINATION is set because it makes multiple calls to String::Write() internally. For these reasons, this patch removes the flags argument entirely and always assumes NO_NULL_TERMINATION.
Next, this patch replaces the calls to the deprecated function String::Write() with calls to the new function String::WriteV2(), which always succeeds and always writes a predictable number of characters, removing the need to deal with a return value here.
Lastly, this patch simplifies the implementation of WriteUCS2() and computes the exact number of characters nchars from the beginning, removing the need to later check again if the number of characters is zero.
Codecov Report
Attention: Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.
Project coverage is 90.17%. Comparing base (
92102c0) to head (ea0a7c6). Report is 316 commits behind head on main.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/string_bytes.cc | 92.30% | 0 Missing and 1 partial :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## main #58163 +/- ##
==========================================
- Coverage 90.18% 90.17% -0.02%
==========================================
Files 630 630
Lines 186473 186499 +26
Branches 36612 36614 +2
==========================================
Hits 168169 168169
- Misses 11116 11120 +4
- Partials 7188 7210 +22
| Files with missing lines | Coverage Ξ | |
|---|---|---|
| src/string_bytes.h | 80.00% <ΓΈ> (ΓΈ) |
|
| src/string_bytes.cc | 70.41% <92.30%> (+0.89%) |
:arrow_up: |
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
CI: https://ci.nodejs.org/job/node-test-pull-request/66639/
CI: https://ci.nodejs.org/job/node-test-pull-request/66655/
Landed in e38ce27f3ca0a65f68a31cedd984cddb927d4002
Adding https://github.com/nodejs/node/labels/dont-land-on-v22%2Ex as it looks like WriteV2 is not available on 22.x.