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 9214ab3
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions src/node_i18n.cc
Original file line number Diff line number Diff line change
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 @@ -446,7 +445,6 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {

UErrorCode status = U_ZERO_ERROR;
MaybeStackBuffer<UChar> result;
MaybeLocal<Object> ret;

UBool flush = (flags & CONVERTER_FLAGS_FLUSH) == CONVERTER_FLAGS_FLUSH;

Expand Down Expand Up @@ -502,31 +500,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 9214ab3

Please sign in to comment.