@@ -410,7 +410,8 @@ bool InitializeICUDirectory(const std::string& path) {
410
410
411
411
int32_t ToUnicode (MaybeStackBuffer<char >* buf,
412
412
const char * input,
413
- size_t length) {
413
+ size_t length,
414
+ bool lenient) {
414
415
UErrorCode status = U_ZERO_ERROR;
415
416
uint32_t options = UIDNA_DEFAULT;
416
417
options |= UIDNA_NONTRANSITIONAL_TO_UNICODE;
@@ -435,7 +436,7 @@ int32_t ToUnicode(MaybeStackBuffer<char>* buf,
435
436
&status);
436
437
}
437
438
438
- if (U_FAILURE (status)) {
439
+ if (U_FAILURE (status) || (!lenient && info. errors != 0 ) ) {
439
440
len = -1 ;
440
441
buf->SetLength (0 );
441
442
} else {
@@ -448,7 +449,8 @@ int32_t ToUnicode(MaybeStackBuffer<char>* buf,
448
449
449
450
int32_t ToASCII (MaybeStackBuffer<char >* buf,
450
451
const char * input,
451
- size_t length) {
452
+ size_t length,
453
+ bool lenient) {
452
454
UErrorCode status = U_ZERO_ERROR;
453
455
uint32_t options = UIDNA_DEFAULT;
454
456
options |= UIDNA_NONTRANSITIONAL_TO_ASCII;
@@ -473,7 +475,7 @@ int32_t ToASCII(MaybeStackBuffer<char>* buf,
473
475
&status);
474
476
}
475
477
476
- if (U_FAILURE (status)) {
478
+ if (U_FAILURE (status) || (!lenient && info. errors != 0 ) ) {
477
479
len = -1 ;
478
480
buf->SetLength (0 );
479
481
} else {
@@ -489,8 +491,11 @@ static void ToUnicode(const FunctionCallbackInfo<Value>& args) {
489
491
CHECK_GE (args.Length (), 1 );
490
492
CHECK (args[0 ]->IsString ());
491
493
Utf8Value val (env->isolate (), args[0 ]);
494
+ // optional arg
495
+ bool lenient = args[1 ]->BooleanValue (env->context ()).FromJust ();
496
+
492
497
MaybeStackBuffer<char > buf;
493
- int32_t len = ToUnicode (&buf, *val, val.length ());
498
+ int32_t len = ToUnicode (&buf, *val, val.length (), lenient );
494
499
495
500
if (len < 0 ) {
496
501
return env->ThrowError (" Cannot convert name to Unicode" );
@@ -508,8 +513,11 @@ static void ToASCII(const FunctionCallbackInfo<Value>& args) {
508
513
CHECK_GE (args.Length (), 1 );
509
514
CHECK (args[0 ]->IsString ());
510
515
Utf8Value val (env->isolate (), args[0 ]);
516
+ // optional arg
517
+ bool lenient = args[1 ]->BooleanValue (env->context ()).FromJust ();
518
+
511
519
MaybeStackBuffer<char > buf;
512
- int32_t len = ToASCII (&buf, *val, val.length ());
520
+ int32_t len = ToASCII (&buf, *val, val.length (), lenient );
513
521
514
522
if (len < 0 ) {
515
523
return env->ThrowError (" Cannot convert name to ASCII" );
0 commit comments