Skip to content

src: refactor WriteUCS2 and remove flags argument #58163

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tniessen
Copy link
Member

@tniessen tniessen commented May 4, 2025

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.

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.
@tniessen tniessen added the c++ Issues and PRs that require attention from people who are familiar with C++. label May 4, 2025
@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. needs-ci PRs that need a full CI run. labels May 4, 2025
Copy link

codecov bot commented May 4, 2025

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 7 commits behind head on main.

Files with missing lines Patch % Lines
src/string_bytes.cc 92.30% 0 Missing and 1 partial ⚠️
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%) ⬆️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants