Skip to content

Commit

Permalink
src: fix to set substitute character
Browse files Browse the repository at this point in the history
  • Loading branch information
cola119 committed Jul 26, 2022
1 parent 8f0dee1 commit aae38ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/node_i18n.cc
Expand Up @@ -425,7 +425,11 @@ void ConverterObject::Create(const FunctionCallbackInfo<Value>& args) {
nullptr, nullptr, nullptr, &status);
}

new ConverterObject(env, obj, conv, flags);
auto converter = new ConverterObject(env, obj, conv, flags);
size_t sublen = ucnv_getMinCharSize(conv);
std::string sub(sublen, '?');
converter->set_subst_chars(sub.c_str());

args.GetReturnValue().Set(obj);
}

Expand Down Expand Up @@ -473,6 +477,11 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
size_t source_length = input.length();

UChar* target = *result;
// printf("source c: %c\n", *source);
// printf("source d: %d\n", *source);
// printf("source s: %s\n", source);
// printf("source_length: %zu\n", source_length);
// printf("limit: %zu\n", limit);
ucnv_toUnicode(converter->conv(),
&target,
target + limit,
Expand All @@ -485,6 +494,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
if (U_SUCCESS(status)) {
bool omit_initial_bom = false;
if (limit > 0) {
// printf("result[0]: %d\n", result[0]);
result.SetLength(target - &result[0]);
if (result.length() > 0 &&
converter->unicode() &&
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-encoding-custom-textdecoder.js
Expand Up @@ -199,3 +199,10 @@ if (common.hasIntl) {
const str = decoder.decode(chunk);
assert.strictEqual(str, 'foo\ufffd');
}

{
const decoder = new TextDecoder('Shift_JIS');
const chunk = new Uint8Array([-1]);
const str = decoder.decode(chunk);
assert.strictEqual(str, '\ufffd');
}

0 comments on commit aae38ee

Please sign in to comment.