Skip to content

Commit

Permalink
util: change implementation to not use buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 3, 2022
1 parent 472ec5a commit 9784e78
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/node_i18n.cc
Expand Up @@ -97,7 +97,6 @@ using v8::NewStringType;
using v8::Object;
using v8::ObjectTemplate;
using v8::String;
using v8::Uint8Array;
using v8::Value;

namespace i18n {
Expand Down Expand Up @@ -502,31 +501,28 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
converter->set_bom_seen(true);
}
}
ret = ToBufferEndian(env, &result);

if (!ret.IsEmpty()) {
CHECK(ret.ToLocalChecked()->IsUint8Array());

if (omit_initial_bom) {
// Perform `ret = ret.slice(2)`.
Local<Uint8Array> orig_ret = ret.ToLocalChecked().As<Uint8Array>();
ret = Buffer::New(env,
orig_ret->Buffer(),
orig_ret->ByteOffset() + 2,
orig_ret->ByteLength() - 2)
.FromMaybe(Local<Uint8Array>());
}
Local<Value> error;
char16_t* output = result.out();
size_t beginning = 0;
size_t length = 0;

Local<Value> error;
ArrayBufferViewContents<char> buf(ret.ToLocalChecked());
MaybeLocal<Value> encoded = StringBytes::Encode(
env->isolate(), buf.data(), buf.length(), encoding::UCS2, &error);
for (size_t i = 0; i < result.length(); i++) length += sizeof(output[i]);

if (!encoded.IsEmpty()) {
args.GetReturnValue().Set(encoded.ToLocalChecked());
} else {
args.GetReturnValue().Set(error);
}
if (omit_initial_bom) {
// Perform `ret = ret.slice(2)`.
beginning += 2;
length -= 2;
}

const char* value = reinterpret_cast<const char*>(output) + beginning;
MaybeLocal<Value> encoded =
StringBytes::Encode(env->isolate(), value, length, UCS2, &error);

if (!encoded.IsEmpty()) {
args.GetReturnValue().Set(encoded.ToLocalChecked());
} else {
args.GetReturnValue().Set(error);
}

return;
Expand Down

0 comments on commit 9784e78

Please sign in to comment.