Skip to content
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

buffer: port byteLengthUtf8 to JavaScript #18356

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
'use strict';

const {
byteLengthUtf8,
copy: _copy,
compare: _compare,
compareOffset,
Expand Down Expand Up @@ -320,6 +319,42 @@ function allocate(size) {
}


// Ported from deps/v8/src/unicode-inl.h
function isTrailSurrogate(code) {
return (code & 0xfc00) === 0xdc00;
}


function isLeadSurrogate(code) {
// No previous character
if (code === -1) return false;
return (code & 0xfc00) === 0xd800;
}


function byteLengthUtf8(string) {
var len = 0;
var previous = -1;
for (var i = 0; i < string.length; i++) {
// NOTE: 0 <= code <= 0xffff
var code = string.charCodeAt(i);
if (code <= 0x7f) {
len++;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

len += 1; for consistency with line 348.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking 😉

} else if (code <= 0x7ff) {
len += 2;
} else {
// kSizeOfUnmatchedSurrogate - kBytesSavedByCombiningSurrogates
if (isTrailSurrogate(code) && isLeadSurrogate(previous))
len += 1;
else
len += 3;
}
previous = code;
}
return len;
}


function fromString(string, encoding) {
var length;
if (typeof encoding !== 'string' || encoding.length === 0) {
Expand Down
8 changes: 0 additions & 8 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,6 @@ void WriteDoubleBE(const FunctionCallbackInfo<Value>& args) {
}


void ByteLengthUtf8(const FunctionCallbackInfo<Value> &args) {
CHECK(args[0]->IsString());

// Fast case: avoid StringBytes on UTF8 string. Jump to v8.
args.GetReturnValue().Set(args[0].As<String>()->Utf8Length());
}

// Normalize val to be an integer in the range of [1, -1] since
// implementations of memcmp() can vary by platform.
static int normalizeCompareVal(int val, size_t a_length, size_t b_length) {
Expand Down Expand Up @@ -1214,7 +1207,6 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "setupBufferJS", SetupBufferJS);
env->SetMethod(target, "createFromString", CreateFromString);

env->SetMethod(target, "byteLengthUtf8", ByteLengthUtf8);
env->SetMethod(target, "copy", Copy);
env->SetMethod(target, "compare", Compare);
env->SetMethod(target, "compareOffset", CompareOffset);
Expand Down