From ff3020ed819b63943803d04bd55cdcbbe42e1dbf Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Sun, 27 Nov 2022 20:15:42 +0900 Subject: [PATCH 1/4] src: use `enum class` instead of `enum` in node_i18n "enum class" has more advantages than "enum" because it's strongly typed and scoped. Refs: https://isocpp.org/wiki/faq/cpp11-language-types#enum-class --- src/node_i18n.cc | 11 ++++++----- src/node_i18n.h | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/node_i18n.cc b/src/node_i18n.cc index 441c2b32763a96..30436a6f7a4593 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -642,13 +642,13 @@ int32_t ToUnicode(MaybeStackBuffer* buf, int32_t ToASCII(MaybeStackBuffer* buf, const char* input, size_t length, - enum idna_mode mode) { + idna_mode mode) { UErrorCode status = U_ZERO_ERROR; uint32_t options = // CheckHyphens = false; handled later UIDNA_CHECK_BIDI | // CheckBidi = true UIDNA_CHECK_CONTEXTJ | // CheckJoiners = true UIDNA_NONTRANSITIONAL_TO_ASCII; // Nontransitional_Processing - if (mode == IDNA_STRICT) { + if (mode == idna_mode::IDNA_STRICT) { options |= UIDNA_USE_STD3_RULES; // UseSTD3ASCIIRules = beStrict // VerifyDnsLength = beStrict; // handled later @@ -696,14 +696,15 @@ int32_t ToASCII(MaybeStackBuffer* buf, info.errors &= ~UIDNA_ERROR_LEADING_HYPHEN; info.errors &= ~UIDNA_ERROR_TRAILING_HYPHEN; - if (mode != IDNA_STRICT) { + if (mode != idna_mode::IDNA_STRICT) { // VerifyDnsLength = beStrict info.errors &= ~UIDNA_ERROR_EMPTY_LABEL; info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG; info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG; } - if (U_FAILURE(status) || (mode != IDNA_LENIENT && info.errors != 0)) { + if (U_FAILURE(status) || + (mode != idna_mode::IDNA_LENIENT && info.errors != 0)) { len = -1; buf->SetLength(0); } else { @@ -741,7 +742,7 @@ static void ToASCII(const FunctionCallbackInfo& args) { Utf8Value val(env->isolate(), args[0]); // optional arg bool lenient = args[1]->BooleanValue(env->isolate()); - enum idna_mode mode = lenient ? IDNA_LENIENT : IDNA_DEFAULT; + idna_mode mode = lenient ? idna_mode::IDNA_LENIENT : idna_mode::IDNA_DEFAULT; MaybeStackBuffer buf; int32_t len = ToASCII(&buf, *val, val.length(), mode); diff --git a/src/node_i18n.h b/src/node_i18n.h index 1e9f13ebc93f02..33001383d5402c 100644 --- a/src/node_i18n.h +++ b/src/node_i18n.h @@ -42,7 +42,7 @@ bool InitializeICUDirectory(const std::string& path); void SetDefaultTimeZone(const char* tzid); -enum idna_mode { +enum class idna_mode { // Default mode for maximum compatibility. IDNA_DEFAULT, // Ignore all errors in IDNA conversion, if possible. @@ -58,7 +58,7 @@ enum idna_mode { int32_t ToASCII(MaybeStackBuffer* buf, const char* input, size_t length, - enum idna_mode mode = IDNA_DEFAULT); + idna_mode mode = idna_mode::IDNA_DEFAULT); // Implements the WHATWG URL Standard "domain to Unicode" algorithm. // https://url.spec.whatwg.org/#concept-domain-to-unicode From 5d4cb6f384906a6a135fec3938cad0b331279b96 Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Mon, 28 Nov 2022 10:47:22 +0900 Subject: [PATCH 2/4] remove `IDNA_` prefix from `enum idna_mode` --- src/node_i18n.cc | 9 ++++----- src/node_i18n.h | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/node_i18n.cc b/src/node_i18n.cc index 30436a6f7a4593..55088cb282c34f 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -648,7 +648,7 @@ int32_t ToASCII(MaybeStackBuffer* buf, UIDNA_CHECK_BIDI | // CheckBidi = true UIDNA_CHECK_CONTEXTJ | // CheckJoiners = true UIDNA_NONTRANSITIONAL_TO_ASCII; // Nontransitional_Processing - if (mode == idna_mode::IDNA_STRICT) { + if (mode == idna_mode::STRICT) { options |= UIDNA_USE_STD3_RULES; // UseSTD3ASCIIRules = beStrict // VerifyDnsLength = beStrict; // handled later @@ -696,15 +696,14 @@ int32_t ToASCII(MaybeStackBuffer* buf, info.errors &= ~UIDNA_ERROR_LEADING_HYPHEN; info.errors &= ~UIDNA_ERROR_TRAILING_HYPHEN; - if (mode != idna_mode::IDNA_STRICT) { + if (mode != idna_mode::STRICT) { // VerifyDnsLength = beStrict info.errors &= ~UIDNA_ERROR_EMPTY_LABEL; info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG; info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG; } - if (U_FAILURE(status) || - (mode != idna_mode::IDNA_LENIENT && info.errors != 0)) { + if (U_FAILURE(status) || (mode != idna_mode::LENIENT && info.errors != 0)) { len = -1; buf->SetLength(0); } else { @@ -742,7 +741,7 @@ static void ToASCII(const FunctionCallbackInfo& args) { Utf8Value val(env->isolate(), args[0]); // optional arg bool lenient = args[1]->BooleanValue(env->isolate()); - idna_mode mode = lenient ? idna_mode::IDNA_LENIENT : idna_mode::IDNA_DEFAULT; + idna_mode mode = lenient ? idna_mode::LENIENT : idna_mode::DEFAULT; MaybeStackBuffer buf; int32_t len = ToASCII(&buf, *val, val.length(), mode); diff --git a/src/node_i18n.h b/src/node_i18n.h index 33001383d5402c..7a4e81e099069d 100644 --- a/src/node_i18n.h +++ b/src/node_i18n.h @@ -44,13 +44,13 @@ void SetDefaultTimeZone(const char* tzid); enum class idna_mode { // Default mode for maximum compatibility. - IDNA_DEFAULT, + DEFAULT, // Ignore all errors in IDNA conversion, if possible. - IDNA_LENIENT, + LENIENT, // Enforce STD3 rules (UseSTD3ASCIIRules) and DNS length restrictions // (VerifyDnsLength). Corresponds to `beStrict` flag in the "domain to ASCII" // algorithm. - IDNA_STRICT + STRICT }; // Implements the WHATWG URL Standard "domain to ASCII" algorithm. @@ -58,7 +58,7 @@ enum class idna_mode { int32_t ToASCII(MaybeStackBuffer* buf, const char* input, size_t length, - idna_mode mode = idna_mode::IDNA_DEFAULT); + idna_mode mode = idna_mode::DEFAULT); // Implements the WHATWG URL Standard "domain to Unicode" algorithm. // https://url.spec.whatwg.org/#concept-domain-to-unicode From b197bf1fe875fc7e8311a4eaecdd47d8d843abbb Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Mon, 28 Nov 2022 19:02:59 +0900 Subject: [PATCH 3/4] Add `k` prefix to each element of `enum` This change is needed to fix error of window build. --- src/node_i18n.cc | 8 ++++---- src/node_i18n.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/node_i18n.cc b/src/node_i18n.cc index 55088cb282c34f..fcf3106995261b 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -648,7 +648,7 @@ int32_t ToASCII(MaybeStackBuffer* buf, UIDNA_CHECK_BIDI | // CheckBidi = true UIDNA_CHECK_CONTEXTJ | // CheckJoiners = true UIDNA_NONTRANSITIONAL_TO_ASCII; // Nontransitional_Processing - if (mode == idna_mode::STRICT) { + if (mode == idna_mode::kSTRICT) { options |= UIDNA_USE_STD3_RULES; // UseSTD3ASCIIRules = beStrict // VerifyDnsLength = beStrict; // handled later @@ -696,14 +696,14 @@ int32_t ToASCII(MaybeStackBuffer* buf, info.errors &= ~UIDNA_ERROR_LEADING_HYPHEN; info.errors &= ~UIDNA_ERROR_TRAILING_HYPHEN; - if (mode != idna_mode::STRICT) { + if (mode != idna_mode::kSTRICT) { // VerifyDnsLength = beStrict info.errors &= ~UIDNA_ERROR_EMPTY_LABEL; info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG; info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG; } - if (U_FAILURE(status) || (mode != idna_mode::LENIENT && info.errors != 0)) { + if (U_FAILURE(status) || (mode != idna_mode::kLENIENT && info.errors != 0)) { len = -1; buf->SetLength(0); } else { @@ -741,7 +741,7 @@ static void ToASCII(const FunctionCallbackInfo& args) { Utf8Value val(env->isolate(), args[0]); // optional arg bool lenient = args[1]->BooleanValue(env->isolate()); - idna_mode mode = lenient ? idna_mode::LENIENT : idna_mode::DEFAULT; + idna_mode mode = lenient ? idna_mode::kLENIENT : idna_mode::kDEFAULT; MaybeStackBuffer buf; int32_t len = ToASCII(&buf, *val, val.length(), mode); diff --git a/src/node_i18n.h b/src/node_i18n.h index 7a4e81e099069d..12b613f843a814 100644 --- a/src/node_i18n.h +++ b/src/node_i18n.h @@ -44,13 +44,13 @@ void SetDefaultTimeZone(const char* tzid); enum class idna_mode { // Default mode for maximum compatibility. - DEFAULT, + kDEFAULT, // Ignore all errors in IDNA conversion, if possible. - LENIENT, + kLENIENT, // Enforce STD3 rules (UseSTD3ASCIIRules) and DNS length restrictions // (VerifyDnsLength). Corresponds to `beStrict` flag in the "domain to ASCII" // algorithm. - STRICT + kSTRICT }; // Implements the WHATWG URL Standard "domain to ASCII" algorithm. @@ -58,7 +58,7 @@ enum class idna_mode { int32_t ToASCII(MaybeStackBuffer* buf, const char* input, size_t length, - idna_mode mode = idna_mode::DEFAULT); + idna_mode mode = idna_mode::kDEFAULT); // Implements the WHATWG URL Standard "domain to Unicode" algorithm. // https://url.spec.whatwg.org/#concept-domain-to-unicode From 574af27be011b4c8c64c4bd0cd503788e9ec9edb Mon Sep 17 00:00:00 2001 From: Deokjin Kim Date: Mon, 28 Nov 2022 23:46:54 +0900 Subject: [PATCH 4/4] Apply `k` followed by the name in `PascalCase` --- src/node_i18n.cc | 8 ++++---- src/node_i18n.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/node_i18n.cc b/src/node_i18n.cc index fcf3106995261b..808f1fa8d77718 100644 --- a/src/node_i18n.cc +++ b/src/node_i18n.cc @@ -648,7 +648,7 @@ int32_t ToASCII(MaybeStackBuffer* buf, UIDNA_CHECK_BIDI | // CheckBidi = true UIDNA_CHECK_CONTEXTJ | // CheckJoiners = true UIDNA_NONTRANSITIONAL_TO_ASCII; // Nontransitional_Processing - if (mode == idna_mode::kSTRICT) { + if (mode == idna_mode::kStrict) { options |= UIDNA_USE_STD3_RULES; // UseSTD3ASCIIRules = beStrict // VerifyDnsLength = beStrict; // handled later @@ -696,14 +696,14 @@ int32_t ToASCII(MaybeStackBuffer* buf, info.errors &= ~UIDNA_ERROR_LEADING_HYPHEN; info.errors &= ~UIDNA_ERROR_TRAILING_HYPHEN; - if (mode != idna_mode::kSTRICT) { + if (mode != idna_mode::kStrict) { // VerifyDnsLength = beStrict info.errors &= ~UIDNA_ERROR_EMPTY_LABEL; info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG; info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG; } - if (U_FAILURE(status) || (mode != idna_mode::kLENIENT && info.errors != 0)) { + if (U_FAILURE(status) || (mode != idna_mode::kLenient && info.errors != 0)) { len = -1; buf->SetLength(0); } else { @@ -741,7 +741,7 @@ static void ToASCII(const FunctionCallbackInfo& args) { Utf8Value val(env->isolate(), args[0]); // optional arg bool lenient = args[1]->BooleanValue(env->isolate()); - idna_mode mode = lenient ? idna_mode::kLENIENT : idna_mode::kDEFAULT; + idna_mode mode = lenient ? idna_mode::kLenient : idna_mode::kDefault; MaybeStackBuffer buf; int32_t len = ToASCII(&buf, *val, val.length(), mode); diff --git a/src/node_i18n.h b/src/node_i18n.h index 12b613f843a814..f32ade831b17a0 100644 --- a/src/node_i18n.h +++ b/src/node_i18n.h @@ -44,13 +44,13 @@ void SetDefaultTimeZone(const char* tzid); enum class idna_mode { // Default mode for maximum compatibility. - kDEFAULT, + kDefault, // Ignore all errors in IDNA conversion, if possible. - kLENIENT, + kLenient, // Enforce STD3 rules (UseSTD3ASCIIRules) and DNS length restrictions // (VerifyDnsLength). Corresponds to `beStrict` flag in the "domain to ASCII" // algorithm. - kSTRICT + kStrict }; // Implements the WHATWG URL Standard "domain to ASCII" algorithm. @@ -58,7 +58,7 @@ enum class idna_mode { int32_t ToASCII(MaybeStackBuffer* buf, const char* input, size_t length, - idna_mode mode = idna_mode::kDEFAULT); + idna_mode mode = idna_mode::kDefault); // Implements the WHATWG URL Standard "domain to Unicode" algorithm. // https://url.spec.whatwg.org/#concept-domain-to-unicode