Skip to content

Commit aae38ee

Browse files
committed
src: fix to set substitute character
1 parent 8f0dee1 commit aae38ee

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/node_i18n.cc

+11-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,11 @@ void ConverterObject::Create(const FunctionCallbackInfo<Value>& args) {
425425
nullptr, nullptr, nullptr, &status);
426426
}
427427

428-
new ConverterObject(env, obj, conv, flags);
428+
auto converter = new ConverterObject(env, obj, conv, flags);
429+
size_t sublen = ucnv_getMinCharSize(conv);
430+
std::string sub(sublen, '?');
431+
converter->set_subst_chars(sub.c_str());
432+
429433
args.GetReturnValue().Set(obj);
430434
}
431435

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

475479
UChar* target = *result;
480+
// printf("source c: %c\n", *source);
481+
// printf("source d: %d\n", *source);
482+
// printf("source s: %s\n", source);
483+
// printf("source_length: %zu\n", source_length);
484+
// printf("limit: %zu\n", limit);
476485
ucnv_toUnicode(converter->conv(),
477486
&target,
478487
target + limit,
@@ -485,6 +494,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
485494
if (U_SUCCESS(status)) {
486495
bool omit_initial_bom = false;
487496
if (limit > 0) {
497+
// printf("result[0]: %d\n", result[0]);
488498
result.SetLength(target - &result[0]);
489499
if (result.length() > 0 &&
490500
converter->unicode() &&

test/parallel/test-whatwg-encoding-custom-textdecoder.js

+7
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,10 @@ if (common.hasIntl) {
199199
const str = decoder.decode(chunk);
200200
assert.strictEqual(str, 'foo\ufffd');
201201
}
202+
203+
{
204+
const decoder = new TextDecoder('Shift_JIS');
205+
const chunk = new Uint8Array([-1]);
206+
const str = decoder.decode(chunk);
207+
assert.strictEqual(str, '\ufffd');
208+
}

0 commit comments

Comments
 (0)