-
Notifications
You must be signed in to change notification settings - Fork 30k
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
n-api: add string api for latin1 encoding and new test cases #12368
Conversation
src/node_api.cc
Outdated
@@ -1714,6 +1733,39 @@ napi_status napi_get_value_string_length(napi_env env, | |||
return GET_RETURN_STATUS(env); | |||
} | |||
|
|||
// Copies a JavaScript string into a LATIN-1 string buffer. The result is the | |||
// number of bytes copied into buf, including the null terminator. If bufsize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the null terminator is not counted in the result
. I understand that is done for consistency with the usage that passes a null buffer just to get the string length. But then the comments need to be updated on all 3 functions.
test/addons-napi/test_string/test.js
Outdated
assert.strictEqual(test_string.Copy(str4), str4); | ||
assert.strictEqual(test_string.Length(str4), 3); | ||
assert.strictEqual(test_string.Utf8Length(str4), 9); | ||
const str4 = '\u{2003}\u{2101}\u{2001}\u{202}\u{2011}'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Latin1 is not tested here, I assume because these characters are not supported by Latin1 encoding. So then can there be another test case that tests a string with non-ASCII Latin1 characters? (That would also work with UTF8 and UTF16 encodings.)
208bcd4
to
f506579
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM based on earlier discussion on null termination and with a few requested changes to the comments and subject to adding the test as suggested by Jason. We will need to use our documentation to make it very clear what the returned length means, as otherwise I can see developers using that length to create the buffers they pass in and then having it be truncated to add the terminator.
src/node_api.cc
Outdated
// number of bytes (excluding the null terminator) copied into buf. | ||
// If bufsize is insufficient, the string will be truncated and null terminated. | ||
// If buf is NULL, this method returns the length of the string (in bytes) | ||
// via the result parameter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we clarify here that the length of the string does not include space for the terminator and that you should pass in a buffer of length+1 to avoid truncation ?
src/node_api.cc
Outdated
// (in 2-byte code units) via the result parameter. | ||
// number of 2-byte code units (excluding the null terminator) copied into buf. | ||
// If bufsize is insufficient, the string will be truncated and null terminated. | ||
// If buf is NULL, this method returns the length of the string (in 2-byte |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment here as above to explain a buffer of length+1 should be passed in
Landed in ad5f987 |
PR-URL: #12368 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
PR-URL: nodejs#12368 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jason Ginchereau <jasongin@microsoft.com>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
n-api